-#define HOSTCONFIG "/etc/hostconfig"
-#define HOSTNAME_KEY "HOSTNAME="
-#define AUTOMATIC "-AUTOMATIC-"
-
-#define HOSTNAME_KEY_LEN (sizeof(HOSTNAME_KEY) - 1)
-
-static CFStringRef
-copy_static_name()
-{
- FILE * f;
- char buf[256];
- CFStringRef name = NULL;
-
- f = fopen(HOSTCONFIG, "r");
- if (f == NULL) {
- return NULL;
- }
-
- while (fgets(buf, sizeof(buf), f) != NULL) {
- char * bp;
- int n;
- char * np;
- Boolean str_escape;
- Boolean str_quote;
-
- n = strlen(buf);
- if (buf[n-1] == '\n') {
- /* the entire line fit in the buffer, remove the newline */
- buf[n-1] = '\0';
- } else {
- /* eat the remainder of the line */
- do {
- n = fgetc(f);
- } while ((n != '\n') && (n != EOF));
- }
-
- // skip leading white space
- bp = &buf[0];
- while (isspace(*bp)) {
- bp++;
- }
-
- // find "HOSTNAME=" key
- if (strncmp(bp, HOSTNAME_KEY, HOSTNAME_KEY_LEN) != 0) {
- continue; // if not
- }
-
- // get the hostname string
- bp += HOSTNAME_KEY_LEN;
- str_escape = FALSE;
- str_quote = FALSE;
-
- np = &buf[0];
- while (*bp != '\0') {
- char ch = *bp;
-
- switch (ch) {
- case '\\' :
- if (!str_escape) {
- str_escape = TRUE;
- bp++;
- continue;
- }
- break;
- case '"' :
- if (!str_escape) {
- str_quote = !str_quote;
- bp++;
- continue;
- }
- break;
- default :
- break;
- }
-
- if (str_escape) {
- str_escape = FALSE;
- } else if (!str_quote && (isspace(ch) || (ch == '#'))) {
- break;
- }
-
- *np++ = ch;
- bp++;
- }
-
- *np = '\0';
-
- if (name != NULL) {
- CFRelease(name);
- name = NULL;
- }
-
- if (str_quote) {
- // the shell won't parse this file so neither will we
- break;
- }
-
- if (strcmp(buf, AUTOMATIC) == 0) {
- // skip "-AUTOMATIC-"
- continue;
- }
-
- name = CFStringCreateWithCString(NULL, buf, kCFStringEncodingUTF8);
- }
-
- (void) fclose(f);
- return name;
-}
-
-