home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libcnews / hostname.c < prev    next >
C/C++ Source or Header  |  1993-11-20  |  878b  |  40 lines

  1. /*
  2.  * hostname - return the Usenet name of this machine
  3.  *
  4.  * One interesting possibility would be to assume that the first
  5.  * name in the sys file is our Usenet name, unless it is "ME",
  6.  * which would require our current strategy anyway.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11.  
  12. #include "libc.h"
  13. #include "news.h"
  14. #include "config.h"
  15.  
  16. #ifndef NAMEFILE
  17. #define NAMEFILE ctlfile("whoami")
  18. #endif
  19.  
  20. char *
  21. hostname()            /* return this Usenet machine's name */
  22. {
  23.     static char name[MAXHOST];
  24.  
  25.     if (name[0] == '\0') {    /* try to get the "news hostname" */
  26.         register FILE *fp;
  27.  
  28.         fp = fopenclex(NAMEFILE, "r");
  29.         if (fp != NULL) {
  30.             (void) fgets(name, sizeof name, fp);
  31.             (void) nfclose(fp);
  32.             if (name[0] != '\0' && name[strlen(name) - 1] == '\n')
  33.                 name[strlen(name) - 1] = '\0';
  34.         }
  35.     }
  36.     if (name[0] == '\0')
  37.         (void) strcpy(name, "whoami-file-missing");
  38.     return name;
  39. }
  40.