home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / hostname.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  2KB  |  141 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. void
  25. nn_gethostname(name, length)
  26. char *name;
  27. int length;
  28. {
  29.     gethostname(name, length);
  30. }
  31. #define DONE
  32. #endif
  33.  
  34. #ifndef DONE
  35. #ifdef HAVE_UNAME
  36. /*
  37.  *    System V uname() is available.    Use nodename field.
  38.  */
  39.  
  40. #include <sys/utsname.h>
  41.  
  42. void
  43. nn_gethostname(name, length)
  44. char *name;
  45. int length;
  46. {
  47.     struct utsname un;
  48.  
  49.     uname(&un);
  50.     strncpy(name, un.nodename, length);
  51. }
  52. #define DONE
  53. #endif
  54. #endif
  55.  
  56. #ifndef DONE
  57. #ifdef HOSTNAME_FILE
  58. /*
  59.  *    Hostname is available in a file.
  60.  *    The name of the file is defined in HOSTNAME_FILE.
  61.  *    This is not intended to fail (or exit would have been via nn_exit)
  62.  */
  63.  
  64. void
  65. nn_gethostname(name, length)
  66. char *name;
  67. int length;
  68. {
  69.     FILE *f;
  70.     register char *p;
  71.     
  72.     f = fopen(HOSTNAME_FILE, "r"); /* Generic code -- don't use open_file */
  73.     if (f == NULL) goto err;
  74.     if (fgets(name, length, f) == NULL) goto err;
  75.     if ((p = strchr(name, NL)) != NULL) *p = NUL;
  76.     fclose(f);
  77.     return;
  78.     
  79. err:    
  80.     fprintf(stderr, "HOSTNAME NOT FOUND IN %s\n", HOSTNAME_FILE);
  81.     exit(77);
  82. }
  83. #define DONE
  84. #endif
  85. #endif
  86.  
  87. #ifndef DONE
  88. #ifdef HOSTNAME_WHOAMI
  89. /*
  90.  *    Hostname is found in whoami.h
  91.  */
  92.  
  93. void
  94. nn_gethostname(name, length)
  95. char *name;
  96. int length;
  97. {
  98.     FILE *f;
  99.     char buf[512];
  100.     register char *p, *q;
  101.     
  102.     f = fopen("/usr/include/whoami.h", "r");
  103.     if (f == NULL) goto err;
  104.     
  105.     while (fgets(buf, 512, f) != NULL) {
  106.     if (buf[0] != '#') continue;
  107.     if ((p = strchr(buf, '"')) == NULL) continue;
  108.     *p++ = NUL;
  109.     if (strncmp(buf, "#define sysname", 15)) continue;
  110.     if ((q = strchr(p, '"')) == NULL) break;
  111.     *q = NUL;
  112.     strncpy(name, p, length);
  113.     return;
  114.     }
  115.     
  116.  err:
  117.     fprintf(stderr, "HOSTNAME (sysname) NOT DEFINED IN whoami.h\n");
  118.     exit(77);
  119. }
  120. #define DONE
  121. #endif
  122. #endif
  123.  
  124. #ifndef DONE
  125. #ifdef HOSTNAME
  126.  
  127. void
  128. nn_gethostname(name, length)
  129. char *name;
  130. int length;
  131. {
  132.     strncpy(name, HOSTNAME, length);
  133. }
  134. #define DONE
  135. #endif
  136. #endif
  137.  
  138. #ifndef DONE
  139. YOU LOSE ON GETHOSTNAME -- DEFINE HOSTNAME IN CONFIG.H
  140. #endif
  141.