]> code.delx.au - osx-proxyconf/blob - sysconfig.m
Consistent style.
[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", @"sysconfig [-q] SystemConfigurationKeyName\n");
55 exit(1);
56 }
57
58 int
59 main(int argc, const char* argv[])
60 {
61 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
62 NSDictionary* proxies = (NSDictionary*)SCDynamicStoreCopyProxies(nil);
63 bool quiet = FALSE;
64 NSString* keyName;
65
66 if(2 == argc) {
67 keyName = [NSString stringWithCString:argv[1]];
68 } else if(3 == argc) {
69 if(![[NSString stringWithCString:argv[1]] isEqualTo:@"-q"]) {
70 printUsage();
71 } else {
72 quiet = TRUE;
73 }
74
75 keyName = [NSString stringWithCString:argv[2]];
76 } else {
77 printUsage();
78 }
79
80 if(!printSystemConfiguration(quiet, keyName, proxies)) {
81 return 2;
82 }
83
84 [pool release];
85 return 0;
86 }
87