Clean up of sysconfig.m
[osx-proxyconf] / sysconfig.m
1 #import <Foundation/Foundation.h>
2 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
3
4 void
5 NSPrintF(NSString* file, NSString* format, ...)
6 {
7 va_list args;
8 va_start(args, format);
9 NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
10 va_end(args);
11 [string writeToFile:file atomically:NO encoding:NSUTF8StringEncoding error:nil];
12 [string release];
13 }
14
15 void
16 NSPrint(NSString* format, ...)
17 {
18 va_list args;
19 va_start(args, format);
20 NSString* string = [[NSString alloc] initWithFormat:format arguments:args];
21 va_end(args);
22 [string writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:nil];
23 [string release];
24 }
25
26
27 bool
28 printSystemConfiguration(bool quiet, NSString* keyName, NSDictionary* proxies)
29 {
30 id value = [proxies objectForKey:keyName];
31 if(nil != value) {
32 NSArray* items;
33 if([value isKindOfClass:[NSArray class]]) {
34 items = value;
35 } else {
36 items = [NSArray arrayWithObjects:value, nil];
37 }
38
39 NSPrint([items componentsJoinedByString:@","]);
40 NSPrint(@"\n");
41
42 } else {
43 if(!quiet) {
44 NSPrintF(@"/dev/stderr", @"Value does not exist\n");
45 }
46 return FALSE; // Signal a fail condition
47 }
48 return TRUE;
49 }
50
51 void
52 printUsage(void)
53 {
54 NSPrintF(@"/dev/stderr", @"proxyconf [-q] SystemConfigurationKeyName\n");
55 exit(1);
56 }
57
58 int main(int argc, const char* argv[])
59 {
60 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
61 NSDictionary* proxies = (NSDictionary*)SCDynamicStoreCopyProxies(nil);
62 bool quiet = FALSE;
63 NSString* keyName;
64
65 if(2 == argc) {
66 keyName = [NSString stringWithCString:argv[1]];
67 } else if(3 == argc) {
68 if(![[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) {
69 printUsage();
70 } else {
71 quiet = TRUE;
72 }
73
74 keyName = [NSString stringWithCString:argv[2]];
75 } else {
76 printUsage();
77 }
78
79 if(!printSystemConfiguration(quiet, keyName, proxies)) {
80 return 2;
81 }
82
83 [pool release];
84 return 0;
85 }
86