home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / gethostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  738 b   |  43 lines

  1. /*
  2.  *    gethostname - get current name of machine
  3.  *
  4.  */
  5. #include     "lib.h"
  6. #include    <sys/file.h>
  7. #include    <sys/param.h>
  8. #include    <errno.h>
  9.  
  10. #ifndef    MAXHOSTNAMELEN
  11. #  define    MAXHOSTNAMELEN    64        /* just like BSD */
  12. #endif
  13.  
  14. extern int    errno;
  15. static char    namebuf[ MAXHOSTNAMELEN ];
  16.  
  17. int gethostname( name, namelen )
  18.   char    *name;
  19.   int    namelen;
  20. {
  21.   int        fd;
  22.   char        *sp;
  23.  
  24.   if ((fd = open( HOSTNAME, O_RDONLY )) < 0) {
  25.     errno = EIO;                    /* cannot open */
  26.     return( -1 );
  27.   }
  28.   if ((read( fd, namebuf, MAXHOSTNAMELEN )) < 0) {
  29.     errno = EIO;                    /* cannot read */
  30.     return( -1 );
  31.   } else {
  32.     sp = namebuf;
  33.     while (( *sp ) && (--namelen))
  34.         if (*sp == '\n') {
  35.             *sp = '\0';
  36.             break;
  37.         } else
  38.             *name++ = *sp++;
  39.   }
  40.   close( fd );
  41.   return( 0 );
  42. }
  43.