From 73f9aaa62ddd095db672645a5876fa9934abfba2 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sat, 28 Jul 2007 12:32:22 +1000 Subject: [PATCH 1/1] Initial revision --- proxyconf.sh | 17 +++++++ sysconfig.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100755 proxyconf.sh create mode 100644 sysconfig.c diff --git a/proxyconf.sh b/proxyconf.sh new file mode 100755 index 0000000..61b0f50 --- /dev/null +++ b/proxyconf.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ "$(sysconfig -n "HTTPEnable")" -eq 1 ]; then + host="$(sysconfig -s "HTTPProxy")" + port="$(sysconfig -n "HTTPPort")" + echo "export http_proxy=\"http://${host}:${port}\"" +fi +if [ "$(sysconfig -n "HTTPSEnable")" -eq 1 ]; then + host="$(sysconfig -s "HTTPSProxy")" + port="$(sysconfig -n "HTTPSPort")" + echo "export https_proxy=\"http://${host}:${port}\"" +fi +if [ "$(sysconfig -n "FTPEnable")" -eq 1 ]; then + host="$(sysconfig -s "FTPProxy")" + port="$(sysconfig -n "FTPPort")" + echo "export ftp_proxy=\"http://${host}:${port}\"" +fi diff --git a/sysconfig.c b/sysconfig.c new file mode 100644 index 0000000..d1028b2 --- /dev/null +++ b/sysconfig.c @@ -0,0 +1,123 @@ +#include +#include + +const char* KEYFILE = "/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCSchemaDefinitions.h"; + + +Boolean +getNumberValue(int* numberVal, const void* numberKey) +{ + assert(numberVal != NULL); + assert(numberKey != NULL); + + Boolean result; + CFDictionaryRef dictRef; + CFNumberRef numberRef; + + + dictRef = SCDynamicStoreCopyProxies((SCDynamicStoreRef)NULL); + result = (dictRef != NULL); + if(result) { + numberRef = (CFNumberRef)CFDictionaryGetValue(dictRef, numberKey); + result = (numberRef != NULL && + CFGetTypeID(numberRef) == CFNumberGetTypeID()); + } + if(result) { + result = CFNumberGetValue(numberRef, kCFNumberIntType, numberVal); + } + + + if(dictRef != NULL) { + CFRelease(dictRef); + } + if(!result) { + *numberVal = 0; + } + return result; +} + +Boolean +getStringValue(char* strVal, size_t strSize, const void* strKey) +{ + assert(strVal != NULL); + assert(strKey != NULL); + + Boolean result; + CFDictionaryRef dictRef; + CFStringRef strRef; + + + dictRef = SCDynamicStoreCopyProxies((SCDynamicStoreRef)NULL); + result = (dictRef != NULL); + + if(result) { + strRef = (CFStringRef)CFDictionaryGetValue(dictRef, strKey); + result = (strRef != NULL) && + (CFGetTypeID(strRef) == CFStringGetTypeID()); + } + if(result) { + result = CFStringGetCString(strRef, strVal, (CFIndex)strSize, + kCFStringEncodingASCII); + } + + + if(dictRef != NULL) { + CFRelease(dictRef); + } + if(!result) { + *strVal = 0; + } + return result; +} + +CFStringRef +createCFString(const char* str) +{ + return CFStringCreateWithCStringNoCopy(NULL, str, + kCFStringEncodingASCII, + kCFAllocatorNull); +} + +void +usage(const char* program) +{ + fprintf(stderr, "Usage: %s (-n NumberKey) | (-s StringKey)\n", program); + fprintf(stderr, "Look in %s for keys. Eg, HTTPProxy\n\n", KEYFILE); +} + +int +main(int argc, char** argv) +{ + if(argc != 3) { + usage(argv[0]); + return 1; + } + + CFStringRef keyRef = createCFString(argv[2]); + if(keyRef == NULL) { + fprintf(stderr, "Fatal error: Couldn't create CFStringRef from arg2\n"); + return 1; + } + + Boolean result; + if(strcmp("-n", argv[1]) == 0) { + int var = 0; + result = getNumberValue(&var, keyRef); + if(result) { + printf("%d\n", var); + } + } else if(strcmp("-s", argv[1]) == 0) { + char str[1024]; + result = getStringValue(str, 1024, keyRef); + if(result) { + printf("%s\n", str); + } + } + else { + usage(argv[0]); + return 1; + } + + return 0; +} + -- 2.39.2