home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / compat / unistd / gethostn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-13  |  926 b   |  49 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <dpmi.h>
  8. #include <go32.h>
  9.  
  10. static char pc_n[]= "pc";
  11.  
  12. int
  13. gethostname (char *buf, int size)
  14. {
  15.   char *h, dosbuf[16];
  16.   int len;
  17.   __dpmi_regs r;
  18.  
  19.   /* Try asking [a lan extension of] dos for a name.  */
  20.   r.x.ax = 0x5e00;
  21.   r.h.ch = 0;  /* Try to detect overloading of 0x5e00  */
  22.   r.x.dx = __tb & 15;
  23.   r.x.ds = __tb / 16;
  24.   __dpmi_int (0x21, &r);
  25.   if ((r.x.flags & 1) || r.h.ch == 0)
  26.   {
  27.     /* Failed.  Try $HOSTNAME, then "pc".  */
  28.     h = getenv ("HOSTNAME");
  29.     if (h == 0)
  30.       h = pc_n;
  31.   }
  32.   else
  33.   {
  34.     dosmemget (__tb, sizeof (dosbuf), dosbuf);
  35.     h = dosbuf;
  36.   }
  37.  
  38.   len = strlen (h);
  39.   if (len + 1 > size)
  40.   {
  41.     errno = ERANGE;
  42.     return -1;
  43.   }
  44.   strcpy (buf, h);
  45.   return 0;
  46. }
  47.  
  48.  
  49.