home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / ANSI / c-client / os_a32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-06  |  18.1 KB  |  654 lines

  1. /*
  2.  * Program:    Operating-system dependent routines -- AIX 3.2 on RS 6000
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    1 August 1988
  13.  * Last Edited:    6 July 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington.
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made available
  24.  * "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. /* TCP input buffer */
  37.  
  38. #define BUFLEN 8192
  39.  
  40.  
  41. /* TCP I/O stream (must be before osdep.h is included) */
  42.  
  43. #define TCPSTREAM struct tcp_stream
  44. TCPSTREAM {
  45.   char *host;            /* host name */
  46.   char *localhost;        /* local host name */
  47.   int tcpsi;            /* input socket */
  48.   int tcpso;            /* output socket */
  49.   int ictr;            /* input counter */
  50.   char *iptr;            /* input pointer */
  51.   char ibuf[BUFLEN];        /* input buffer */
  52. };
  53.  
  54.  
  55. #include "mail.h"
  56. #include "osdep.h"
  57. #include <sys/socket.h>
  58. #include <netinet/in.h>
  59. #include <netdb.h>
  60. #include <ctype.h>
  61. #include <errno.h>
  62. extern int errno;        /* just in case */
  63. #include <pwd.h>
  64. #include <syslog.h>
  65. #include "misc.h"
  66. extern char *crypt();
  67. #include <sys/poll.h>
  68. #include <sys/select.h>
  69. #include <sys/lockf.h>
  70.  
  71. extern int sys_nerr;
  72. extern char *sys_errlist[];
  73.  
  74. /* Write current time in RFC 822 format
  75.  * Accepts: destination string
  76.  */
  77.  
  78. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  79.  
  80. void rfc822_date (char *date)
  81. {
  82.   int zone,dstflag;
  83.   struct tm *t;
  84.   struct timeval tv;
  85.   struct timezone tz;
  86.   gettimeofday (&tv,&tz);    /* get time and timezone poop */
  87.   t = localtime (&tv.tv_sec);    /* convert to individual items */
  88.   tzset ();            /* get timezone from TZ environment stuff */
  89.   dstflag = daylight ? t->tm_isdst : 0;
  90. #if 0                /* doesn't work for some reason */
  91.   zone = (dstflag * 60) - timezone/60;
  92. #else
  93.   zone = (dstflag * 60) - tz.tz_minuteswest;
  94. #endif
  95.                 /* and output it */
  96.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %+03d%02d (%s)",
  97.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  98.        t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60,
  99.        tzname[dstflag]);
  100. }
  101.  
  102. /* Get a block of free storage
  103.  * Accepts: size of desired block
  104.  * Returns: free storage block
  105.  */
  106.  
  107. void *fs_get (size_t size)
  108. {
  109.   void *block = malloc (size);
  110.   if (!block) fatal ("Out of free storage");
  111.   return (block);
  112. }
  113.  
  114.  
  115. /* Resize a block of free storage
  116.  * Accepts: ** pointer to current block
  117.  *        new size
  118.  */
  119.  
  120. void fs_resize (void **block,size_t size)
  121. {
  122.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  123. }
  124.  
  125.  
  126. /* Return a block of free storage
  127.  * Accepts: ** pointer to free storage block
  128.  */
  129.  
  130. void fs_give (void **block)
  131. {
  132.   free (*block);
  133.   *block = NIL;
  134. }
  135.  
  136.  
  137. /* Report a fatal error
  138.  * Accepts: string to output
  139.  */
  140.  
  141. void fatal (char *string)
  142. {
  143.   mm_fatal (string);        /* pass up the string */
  144.   syslog (LOG_ALERT,"IMAP C-Client crash: %s",string);
  145.   abort ();            /* die horribly */
  146. }
  147.  
  148. /* Copy string with CRLF newlines
  149.  * Accepts: destination string
  150.  *        pointer to size of destination string
  151.  *        source string
  152.  *        length of source string
  153.  */
  154.  
  155. char *strcrlfcpy (char **dst,unsigned long *dstl,char *src,unsigned long srcl)
  156. {
  157.   long i,j;
  158.   char *d = src;
  159.                 /* count number of LF's in source string(s) */
  160.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  161.   if (i > *dstl) {        /* resize if not enough space */
  162.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  163.     *dst = (char *) fs_get ((*dstl = i) + 1);
  164.   }
  165.   d = *dst;            /* destination string */
  166.                 /* copy strings, inserting CR's before LF's */
  167.   while (srcl--) switch (*src) {
  168.   case '\015':            /* unlikely carriage return */
  169.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  170.     if (srcl && *src == '\012') {
  171.       *d++ = *src++;
  172.       srcl--;
  173.     }
  174.     break;
  175.   case '\012':            /* line feed? */
  176.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  177.   default:            /* ordinary chararacter */
  178.     *d++ = *src++;        /* just copy character */
  179.     break;
  180.   }
  181.   *d = '\0';            /* tie off destination */
  182.   return *dst;            /* return destination */
  183. }
  184.  
  185.  
  186. /* Length of string after strcrlfcpy applied
  187.  * Accepts: source string
  188.  *        length of source string
  189.  */
  190.  
  191. unsigned long strcrlflen (STRING *s)
  192. {
  193.   unsigned long pos = GETPOS (s);
  194.   unsigned long i = SIZE (s);
  195.   unsigned long j = i;
  196.   while (j--) switch (SNX (s)) {/* search for newlines */
  197.   case '\015':            /* unlikely carriage return */
  198.     if (j && (CHR (s) == '\012')) {
  199.       SNX (s);            /* eat the line feed */
  200.       j--;
  201.     }
  202.     break;
  203.   case '\012':            /* line feed? */
  204.     i++;
  205.   default:            /* ordinary chararacter */
  206.     break;
  207.   }
  208.   SETPOS (s,pos);        /* restore old position */
  209.   return i;
  210. }
  211.  
  212. /* Server log in
  213.  * Accepts: user name string
  214.  *        password string
  215.  *        optional place to return home directory
  216.  * Returns: T if password validated, NIL otherwise
  217.  */
  218.  
  219. long server_login (char *user,char *pass,char **home,int argc,char *argv[])
  220. {
  221.   struct passwd *pw = getpwnam (lcase (user));
  222.                 /* no entry for this user or root */
  223.   if (!(pw && pw->pw_uid)) return NIL;
  224.                 /* validate password */
  225.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  226.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  227.   initgroups (user,pw->pw_gid);    /* initialize groups */
  228.   setuid (pw->pw_uid);
  229.                 /* note home directory */
  230.   if (home) *home = cpystr (pw->pw_dir);
  231.   return T;
  232. }
  233.  
  234. /* Return my user name
  235.  * Returns: my user name
  236.  */
  237.  
  238. char *uname = NIL;
  239.  
  240. char *myusername ()
  241. {
  242.   return uname ? uname : (uname = cpystr (getpwuid (geteuid ())->pw_name));
  243. }
  244.  
  245.  
  246. /* Return my home directory name
  247.  * Returns: my home directory name
  248.  */
  249.  
  250. char *hdname = NIL;
  251.  
  252. char *myhomedir ()
  253. {
  254.   return hdname ? hdname : (hdname = cpystr (getpwuid (geteuid ())->pw_dir));
  255. }
  256.  
  257.  
  258. /* Build status lock file name
  259.  * Accepts: scratch buffer
  260.  *        file name
  261.  * Returns: name of file to lock
  262.  */
  263.  
  264. char *lockname (char *tmp,char *fname)
  265. {
  266.   int i;
  267.   sprintf (tmp,"/tmp/.%s",fname);
  268.   for (i = 6; i < strlen (tmp); ++i) if (tmp[i] == '/') tmp[i] = '\\';
  269.   return tmp;            /* note name for later */
  270. }
  271.   
  272. /* TCP/IP open
  273.  * Accepts: host name
  274.  *        contact port number
  275.  * Returns: TCP/IP stream if success else NIL
  276.  */
  277.  
  278. TCPSTREAM *tcp_open (char *host,int port)
  279. {
  280.   TCPSTREAM *stream = NIL;
  281.   int sock;
  282.   char *s;
  283.   struct sockaddr_in sin;
  284.   struct hostent *host_name;
  285.   char hostname[MAILTMPLEN];
  286.   char tmp[MAILTMPLEN];
  287.   /* The domain literal form is used (rather than simply the dotted decimal
  288.      as with other Unix programs) because it has to be a valid "host name"
  289.      in mailsystem terminology. */
  290.                 /* look like domain literal? */
  291.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  292.     strcpy (hostname,host+1);    /* yes, copy number part */
  293.     hostname[(strlen (hostname))-1] = '\0';
  294.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  295.       sin.sin_family = AF_INET;    /* family is always Internet */
  296.       strcpy (hostname,host);    /* hostname is user's argument */
  297.     }
  298.     else {
  299.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  300.       mm_log (tmp,ERROR);
  301.       return NIL;
  302.     }
  303.   }
  304.  
  305.   else {            /* lookup host name, note that brain-dead Unix
  306.                    requires lowercase! */
  307.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  308.     if ((host_name = gethostbyname (lcase (hostname)))) {
  309.                 /* copy address type */
  310.       sin.sin_family = host_name->h_addrtype;
  311.                 /* copy host name */
  312.       strcpy (hostname,host_name->h_name);
  313.                 /* copy host addresses */
  314.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  315.     }
  316.     else {
  317.       switch (h_errno) {
  318.     case HOST_NOT_FOUND:    /* authoritative error */
  319.       s = "No such host as %.80s";
  320.       break;
  321.     case TRY_AGAIN:        /* non-authoritative error */
  322.       s = "Transient error for host %.80s, try again";
  323.       break;
  324.     case NO_RECOVERY:    /* non-recoverable errors */
  325.       s = "Non-recoverable error looking up host %.80s";
  326.       break;
  327.     case NO_DATA:        /* no data record of requested type */
  328.       s = "No IP address known for host %.80s";
  329.       break;
  330.     default:
  331.       s = "Unknown error for host %.80s";
  332.       break;
  333.       }
  334.       sprintf (tmp,s,host);
  335.       mm_log (tmp,ERROR);
  336.       return NIL;
  337.     }
  338.   }
  339.  
  340.                 /* copy port number in network format */
  341.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  342.                 /* get a TCP stream */
  343.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  344.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  345.     mm_log (tmp,ERROR);
  346.     return NIL;
  347.   }
  348.                 /* open connection */
  349.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  350.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  351.          strerror (errno));
  352.     mm_log (tmp,ERROR);
  353.     return NIL;
  354.   }
  355.                 /* create TCP/IP stream */
  356.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  357.                 /* copy official host name */
  358.   stream->host = cpystr (hostname);
  359.                 /* get local name */
  360.   gethostname (tmp,MAILTMPLEN-1);
  361.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  362.                   host_name->h_name : tmp);
  363.                 /* init sockets */
  364.   stream->tcpsi = stream->tcpso = sock;
  365.   stream->ictr = 0;        /* init input counter */
  366.   return stream;        /* return success */
  367. }
  368.   
  369. /* TCP/IP authenticated open
  370.  * Accepts: host name
  371.  *        service name
  372.  * Returns: TCP/IP stream if success else NIL
  373.  */
  374.  
  375. TCPSTREAM *tcp_aopen (char *host,char *service)
  376. {
  377.   TCPSTREAM *stream = NIL;
  378.   struct hostent *host_name;
  379.   char hostname[MAILTMPLEN];
  380.   int i;
  381.   int pipei[2],pipeo[2];
  382.   /* The domain literal form is used (rather than simply the dotted decimal
  383.      as with other Unix programs) because it has to be a valid "host name"
  384.      in mailsystem terminology. */
  385.                 /* look like domain literal? */
  386.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  387.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  388.     hostname[i-1] = '\0';
  389.   }
  390.                 /* note that Unix requires lowercase! */
  391.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  392.     strcpy (hostname,host_name->h_name);
  393.                 /* make command pipes */
  394.   if (pipe (pipei) < 0) return NIL;
  395.   if (pipe (pipeo) < 0) {
  396.     close (pipei[0]); close (pipei[1]);
  397.     return NIL;
  398.   }
  399.   if ((i = fork ()) < 0) {    /* make inferior process */
  400.     close (pipei[0]); close (pipei[1]);
  401.     close (pipeo[0]); close (pipeo[1]);
  402.     return NIL;
  403.   }
  404.   if (i) {            /* parent? */
  405.     close (pipei[1]);        /* close child's side of the pipes */
  406.     close (pipeo[0]);
  407.   }
  408.   else {            /* child */
  409.     dup2 (pipei[1],1);        /* parent's input is my output */
  410.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  411.     close (pipei[0]); close (pipei[1]);
  412.     dup2 (pipeo[0],0);        /* parent's output is my input */
  413.     close (pipeo[0]); close (pipeo[1]);
  414.                 /* now run it */
  415.     execl ("/usr/ucb/rsh","rsh",hostname,"exec",service,0);
  416.     _exit (1);            /* spazzed */
  417.   }
  418.  
  419.                 /* create TCP/IP stream */
  420.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  421.                 /* copy official host name */
  422.   stream->host = cpystr (hostname);
  423.                 /* get local name */
  424.   gethostname (hostname,MAILTMPLEN-1);
  425.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  426.                   host_name->h_name : hostname);
  427.   stream->tcpsi = pipei[0];    /* init sockets */
  428.   stream->tcpso = pipeo[1];
  429.   stream->ictr = 0;        /* init input counter */
  430.   return stream;        /* return success */
  431. }
  432.  
  433. /* TCP/IP receive line
  434.  * Accepts: TCP/IP stream
  435.  * Returns: text line string or NIL if failure
  436.  */
  437.  
  438. char *tcp_getline (TCPSTREAM *stream)
  439. {
  440.   int n,m;
  441.   char *st,*ret,*stp;
  442.   char c = '\0';
  443.   char d;
  444.                 /* make sure have data */
  445.   if (!tcp_getdata (stream)) return NIL;
  446.   st = stream->iptr;        /* save start of string */
  447.   n = 0;            /* init string count */
  448.   while (stream->ictr--) {    /* look for end of line */
  449.     d = *stream->iptr++;    /* slurp another character */
  450.     if ((c == '\015') && (d == '\012')) {
  451.       ret = (char *) fs_get (n--);
  452.       memcpy (ret,st,n);    /* copy into a free storage string */
  453.       ret[n] = '\0';        /* tie off string with null */
  454.       return ret;
  455.     }
  456.     n++;            /* count another character searched */
  457.     c = d;            /* remember previous character */
  458.   }
  459.                 /* copy partial string from buffer */
  460.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  461.                 /* get more data from the net */
  462.   if (!tcp_getdata (stream)) return NIL;
  463.                 /* special case of newline broken by buffer */
  464.   if ((c == '\015') && (*stream->iptr == '\012')) {
  465.     stream->iptr++;        /* eat the line feed */
  466.     stream->ictr--;
  467.     ret[n - 1] = '\0';        /* tie off string with null */
  468.   }
  469.                 /* else recurse to get remainder */
  470.   else if (st = tcp_getline (stream)) {
  471.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  472.     memcpy (ret,stp,n);        /* copy first part */
  473.     memcpy (ret + n,st,m);    /* and second part */
  474.     fs_give ((void **) &stp);    /* flush first part */
  475.     fs_give ((void **) &st);    /* flush second part */
  476.     ret[n + m] = '\0';        /* tie off string with null */
  477.   }
  478.   return ret;
  479. }
  480.  
  481. /* TCP/IP receive buffer
  482.  * Accepts: TCP/IP stream
  483.  *        size in bytes
  484.  *        buffer to read into
  485.  * Returns: T if success, NIL otherwise
  486.  */
  487.  
  488. long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
  489. {
  490.   unsigned long n;
  491.   char *bufptr = buffer;
  492.   while (size > 0) {        /* until request satisfied */
  493.     if (!tcp_getdata (stream)) return NIL;
  494.     n = min (size,stream->ictr);/* number of bytes to transfer */
  495.                 /* do the copy */
  496.     memcpy (bufptr,stream->iptr,n);
  497.     bufptr += n;        /* update pointer */
  498.     stream->iptr +=n;
  499.     size -= n;            /* update # of bytes to do */
  500.     stream->ictr -=n;
  501.   }
  502.   bufptr[0] = '\0';        /* tie off string */
  503.   return T;
  504. }
  505.  
  506. /* TCP/IP receive data
  507.  * Accepts: TCP/IP stream
  508.  * Returns: T if success, NIL otherwise
  509.  */
  510.  
  511. long tcp_getdata (TCPSTREAM *stream)
  512. {
  513.   struct pollfd pollfd;
  514.   int pollstatus;
  515.   if (stream->tcpsi < 0) return NIL;
  516.   pollfd.fd = stream->tcpsi;    /* initialize selection vector */
  517.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  518.     pollfd.events = POLLIN;    /* block and read */
  519.     /* Note: am not sure here that it wouldn't also be a good idea to
  520.     restart poll() on EINTR, if SIGCHLD or whatever are expected.    DC */
  521.     while (((pollstatus = poll (&pollfd,1,-1)) < 0) && (errno == EAGAIN));
  522.     if (!pollstatus ||
  523.     ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  524.       close (stream->tcpsi);    /* nuke the socket */
  525.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  526.       stream->tcpsi = stream->tcpso = -1;
  527.       return NIL;
  528.     }
  529.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  530.   }
  531.   return T;
  532. }
  533.  
  534. /* TCP/IP send string as record
  535.  * Accepts: TCP/IP stream
  536.  *        string pointer
  537.  * Returns: T if success else NIL
  538.  */
  539.  
  540. long tcp_soutr (TCPSTREAM *stream,char *string)
  541. {
  542.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  543. }
  544.  
  545.  
  546. /* TCP/IP send string
  547.  * Accepts: TCP/IP stream
  548.  *        string pointer
  549.  *        byte count
  550.  * Returns: T if success else NIL
  551.  */
  552.  
  553. long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
  554. {
  555.   int i;
  556.   fd_set fds;
  557.   FD_ZERO (&fds);        /* initialize selection vector */
  558.   if (stream->tcpso < 0) return NIL;
  559.   while (size > 0) {        /* until request satisfied */
  560.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  561.     if ((select (stream->tcpso+1,0,&fds,0,0) < 0) ||
  562.     ((i = write (stream->tcpso,string,size)) < 0)) {
  563.       puts (strerror (errno));
  564.       close (stream->tcpsi);    /* nuke the socket */
  565.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  566.       stream->tcpsi = stream->tcpso = -1;
  567.       return NIL;
  568.     }
  569.     size -= i;            /* count this size */
  570.     string += i;
  571.   }
  572.   return T;            /* all done */
  573. }
  574.  
  575. /* TCP/IP close
  576.  * Accepts: TCP/IP stream
  577.  */
  578.  
  579. void tcp_close (TCPSTREAM *stream)
  580. {
  581.  
  582.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  583.     close (stream->tcpsi);    /* nuke the socket */
  584.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  585.     stream->tcpsi = stream->tcpso = -1;
  586.   }
  587.                 /* flush host names */
  588.   fs_give ((void **) &stream->host);
  589.   fs_give ((void **) &stream->localhost);
  590.   fs_give ((void **) &stream);    /* flush the stream */
  591. }
  592.  
  593.  
  594. /* TCP/IP get host name
  595.  * Accepts: TCP/IP stream
  596.  * Returns: host name for this stream
  597.  */
  598.  
  599. char *tcp_host (TCPSTREAM *stream)
  600. {
  601.   return stream->host;        /* return host name */
  602. }
  603.  
  604.  
  605. /* TCP/IP get local host name
  606.  * Accepts: TCP/IP stream
  607.  * Returns: local host name
  608.  */
  609.  
  610. char *tcp_localhost (TCPSTREAM *stream)
  611. {
  612.   return stream->localhost;    /* return local host name */
  613. }
  614.  
  615. /* Return implementation-defined string corresponding to error
  616.  * Accepts: error number
  617.  * Returns: string for that error
  618.  */
  619.  
  620. char *strerror (int n)
  621. {
  622.   return (n >= 0 && n < sys_nerr) ? sys_errlist[n] : NIL;
  623. }
  624.  
  625.  
  626. /* Emulator for BSD flock() call
  627.  * Accepts: file descriptor
  628.  *        operation bitmask
  629.  * Returns: 0 if successful, -1 if failure
  630.  * Note: this emulator does not handle shared locks
  631.  */
  632.  
  633. int flock (int fd,int operation)
  634. {
  635.   int func;
  636.   off_t offset = lseek (fd,0,SEEK_CUR);
  637.   switch (operation & ~LOCK_NB){/* translate to lockf() operation */
  638.   case LOCK_EX:            /* exclusive */
  639.   case LOCK_SH:            /* shared */
  640.     func = (operation & LOCK_NB) ? F_TLOCK : F_LOCK;
  641.     break;
  642.   case LOCK_UN:            /* unlock */
  643.     func = F_ULOCK;
  644.     break;
  645.   default:            /* default */
  646.     errno = EINVAL;
  647.     return -1;
  648.   }
  649.   lseek (fd,0,SEEK_SET);    /* position to start of the file */
  650.   func = lockf (fd,func,0);    /* do the lockf() */
  651.   lseek (fd,offset,SEEK_SET);    /* restore prior position */
  652.   return func;
  653. }
  654.