]> code.delx.au - osx-proxyconf/blob - sysconfig.m
Allow C99 features
[osx-proxyconf] / sysconfig.m
1 #import <Foundation/Foundation.h>
2 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
3
4 // Begin the nasty hack so that I can easily print out NSStrings to stdout
5 void fNSPrint(NSString * outputFilename, NSString * str)
6 {
7 NSString * output = [NSString stringWithFormat:@"%@\n", str];
8 [output writeToFile:outputFilename atomically:NO
9 encoding:NSUTF8StringEncoding error:nil];
10 }
11
12 void NSPrint(NSString *str)
13 {
14 fNSPrint(@"/dev/stdout", str);
15 }
16
17 bool printSystemConfiguration(bool quiet, NSString * keyName, NSDictionary * proxies)
18 {
19 id value = [proxies objectForKey:keyName];
20 if (nil != value) {
21 NSArray * itemsArray;
22 if ([value isKindOfClass:[NSArray class]]) {
23 itemsArray = value;
24 } else {
25 itemsArray = [NSArray arrayWithObjects:value, nil];
26 }
27
28 NSEnumerator * enumerator = [itemsArray objectEnumerator];
29 id item;
30 while ((item = [enumerator nextObject]))
31 NSPrint([NSString stringWithFormat:@"%@", item]);
32
33 } else {
34 if (!quiet)
35 fNSPrint(@"/dev/stderr", @"Value does not exist");
36 return FALSE; // Signal a fail condition
37 }
38 return TRUE;
39 }
40
41 void printUsage(void)
42 {
43 fNSPrint(@"/dev/stderr", @"proxyconf [-q] SystemConfigurationKeyName");
44 }
45
46 int resolveSystemConfiguration(int argc, const char * argv[])
47 {
48 NSDictionary * proxies = (NSDictionary *)SCDynamicStoreCopyProxies(nil);
49
50 bool quiet = FALSE;
51 NSString * keyName;
52
53 if (2 == argc) {
54 keyName = [NSString stringWithCString:argv[1]];
55 } else if (3 == argc) {
56 if (! [[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) {
57 printUsage();
58 return 1;
59 } else {
60 quiet = TRUE;
61 }
62
63 keyName = [NSString stringWithCString:argv[2]];
64 } else {
65 printUsage();
66 return 1;
67 }
68
69 if (! printSystemConfiguration(quiet, keyName, proxies))
70 return 2;
71
72 return 0;
73 }
74
75 int main (int argc, const char * argv[])
76 {
77 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
78
79 int ret = resolveSystemConfiguration(argc, argv);
80
81 [pool drain];
82 return ret;
83 }