home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / nn6.4 / part20 / hostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.3 KB  |  129 lines

  1. /*
  2.  *    (c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
  3.  *
  4.  *    "generic" gethostname() emulation:
  5.  * 
  6.  *    Possibilities are used in following order:
  7.  *
  8.  *    HAVE_GETHOSTNAME        -- use gethostname()
  9.  *    HAVE_UNAME            -- use sysV uname().nodename
  10.  *    HOSTNAME_FILE "/.../..."    -- read hostname from file
  11.  *    HOSTNAME_WHOAMI            -- use sysname from whoami.h
  12.  *    HOSTNAME "host"            -- hard-coded hostname
  13.  *    You lose!
  14.  */
  15.  
  16. #include "config.h"
  17.  
  18. #undef DONE
  19.  
  20. #ifdef HAVE_GETHOSTNAME
  21. /*
  22.  *    Easy -- we already got it
  23.  */
  24. #define DONE
  25. #endif
  26.  
  27. #ifndef DONE
  28. #ifdef HAVE_UNAME
  29. /*
  30.  *    System V uname() is available.    Use nodename field.
  31.  */
  32.  
  33. #include <sys/utsname.h>
  34.  
  35. gethostname(name, length)
  36. char *name;
  37. int length;
  38. {
  39.     struct utsname un;
  40.  
  41.     uname(&un);
  42.     strncpy(name, un.nodename, length);
  43. }
  44. #define DONE
  45. #endif
  46. #endif
  47.  
  48. #ifndef DONE
  49. #ifdef HOSTNAME_FILE
  50. /*
  51.  *    Hostname is available in a file.
  52.  *    The name of the file is defined in HOSTNAME_FILE.
  53.  *    This is not intended to fail (or exit would have been via nn_exit)
  54.  */
  55.  
  56. gethostname(name, length)
  57. char *name;
  58. int length;
  59. {
  60.     FILE *f;
  61.     register char *p;
  62.     
  63.     f = fopen(HOSTNAME_FILE, "r"); /* Generic code -- don't use open_file */
  64.     if (f == NULL) goto err;
  65.     if (fgets(name, length, f) == NULL) goto err;
  66.     if ((p = strchr(name, NL)) != NULL) *p = NUL;
  67.     fclose(f);
  68.     return;
  69.     
  70. err:    
  71.     fprintf(stderr, "HOSTNAME NOT FOUND IN %s\n", HOSTNAME_FILE);
  72.     exit(77);
  73. }
  74. #define DONE
  75. #endif
  76. #endif
  77.  
  78. #ifndef DONE
  79. #ifdef HOSTNAME_WHOAMI
  80. /*
  81.  *    Hostname is found in whoami.h
  82.  */
  83.  
  84. gethostname(name, length)
  85. char *name;
  86. int length;
  87. {
  88.     FILE *f;
  89.     char buf[512];
  90.     register char *p, *q;
  91.     
  92.     f = fopen("/usr/include/whoami.h", "r");
  93.     if (f == NULL) goto err;
  94.     
  95.     while (fgets(buf, 512, f) != NULL) {
  96.     if (buf[0] != '#') continue;
  97.     if ((p = strchr(buf, '"')) == NULL) continue;
  98.     *p++ = NUL;
  99.     if (strncmp(buf, "#define sysname", 15)) continue;
  100.     if ((q = strchr(p, '"')) == NULL) break;
  101.     *q = NUL;
  102.     strncpy(name, p, length);
  103.     return;
  104.     }
  105.     
  106.  err:
  107.     fprintf(stderr, "HOSTNAME (sysname) NOT DEFINED IN whoami.h\n");
  108.     exit(77);
  109. }
  110. #define DONE
  111. #endif
  112. #endif
  113.  
  114. #ifndef DONE
  115. #ifdef HOSTNAME
  116. gethostname(name, length)
  117. char *name;
  118. int length;
  119. {
  120.     strncpy(name, HOSTNAME, length);
  121. }
  122. #define DONE
  123. #endif
  124. #endif
  125.  
  126. #ifndef DONE
  127. YOU LOSE ON GETHOSTNAME -- DEFINE HOSTNAME IN CONFIG.H
  128. #endif
  129.