]> code.delx.au - osx-proxyconf/blob - sysconfig.c
Simple Makefile
[osx-proxyconf] / sysconfig.c
1 #include <CoreFoundation/CoreFoundation.h>
2 #include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
3
4 const char* KEYFILE = "/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCSchemaDefinitions.h";
5
6
7 Boolean
8 getNumberValue(int* numberVal, const void* numberKey)
9 {
10 assert(numberVal != NULL);
11 assert(numberKey != NULL);
12
13 Boolean result;
14 CFDictionaryRef dictRef;
15 CFNumberRef numberRef;
16
17
18 dictRef = SCDynamicStoreCopyProxies((SCDynamicStoreRef)NULL);
19 result = (dictRef != NULL);
20 if(result) {
21 numberRef = (CFNumberRef)CFDictionaryGetValue(dictRef, numberKey);
22 result = (numberRef != NULL &&
23 CFGetTypeID(numberRef) == CFNumberGetTypeID());
24 }
25 if(result) {
26 result = CFNumberGetValue(numberRef, kCFNumberIntType, numberVal);
27 }
28
29
30 if(dictRef != NULL) {
31 CFRelease(dictRef);
32 }
33 if(!result) {
34 *numberVal = 0;
35 }
36 return result;
37 }
38
39 Boolean
40 getStringValue(char* strVal, size_t strSize, const void* strKey)
41 {
42 assert(strVal != NULL);
43 assert(strKey != NULL);
44
45 Boolean result;
46 CFDictionaryRef dictRef;
47 CFStringRef strRef;
48
49
50 dictRef = SCDynamicStoreCopyProxies((SCDynamicStoreRef)NULL);
51 result = (dictRef != NULL);
52
53 if(result) {
54 strRef = (CFStringRef)CFDictionaryGetValue(dictRef, strKey);
55 result = (strRef != NULL) &&
56 (CFGetTypeID(strRef) == CFStringGetTypeID());
57 }
58 if(result) {
59 result = CFStringGetCString(strRef, strVal, (CFIndex)strSize,
60 kCFStringEncodingASCII);
61 }
62
63
64 if(dictRef != NULL) {
65 CFRelease(dictRef);
66 }
67 if(!result) {
68 *strVal = 0;
69 }
70 return result;
71 }
72
73 CFStringRef
74 createCFString(const char* str)
75 {
76 return CFStringCreateWithCStringNoCopy(NULL, str,
77 kCFStringEncodingASCII,
78 kCFAllocatorNull);
79 }
80
81 void
82 usage(const char* program)
83 {
84 fprintf(stderr, "Usage: %s (-n NumberKey) | (-s StringKey)\n", program);
85 fprintf(stderr, "Look in %s for keys. Eg, HTTPProxy\n\n", KEYFILE);
86 }
87
88 int
89 main(int argc, char** argv)
90 {
91 if(argc != 3) {
92 usage(argv[0]);
93 return 1;
94 }
95
96 CFStringRef keyRef = createCFString(argv[2]);
97 if(keyRef == NULL) {
98 fprintf(stderr, "Fatal error: Couldn't create CFStringRef from arg2\n");
99 return 1;
100 }
101
102 Boolean result;
103 if(strcmp("-n", argv[1]) == 0) {
104 int var = 0;
105 result = getNumberValue(&var, keyRef);
106 if(result) {
107 printf("%d\n", var);
108 }
109 } else if(strcmp("-s", argv[1]) == 0) {
110 char str[1024];
111 result = getStringValue(str, 1024, keyRef);
112 if(result) {
113 printf("%s\n", str);
114 }
115 }
116 else {
117 usage(argv[0]);
118 return 1;
119 }
120
121 return 0;
122 }
123