home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / pine / c-client / os_dyn.c < prev    next >
C/C++ Source or Header  |  1994-01-09  |  23KB  |  880 lines

  1. /*
  2.  * Program:    Operating-system dependent routines -- Dynix version
  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.  *
  11.  * Date:    11 May 1989
  12.  * Last Edited:    3 October 1993
  13.  *
  14.  * Copyright 1993 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. /* TCP input buffer */
  36.  
  37. #define BUFLEN 8192
  38.  
  39.  
  40. /* TCP I/O stream (must be before osdep.h is included) */
  41.  
  42. #define TCPSTREAM struct tcp_stream
  43. TCPSTREAM {
  44.   char *host;            /* host name */
  45.   char *localhost;        /* local host name */
  46.   int tcpsi;            /* input socket */
  47.   int tcpso;            /* output socket */
  48.   int ictr;            /* input counter */
  49.   char *iptr;            /* input pointer */
  50.   char ibuf[BUFLEN];        /* input buffer */
  51. };
  52.  
  53.  
  54. #include "osdep.h"
  55. #include <ctype.h>
  56. #include <sys/time.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 "mail.h"
  66. #include "misc.h"
  67.  
  68. extern char *timezone  ();
  69. extern int sys_nerr;
  70. extern char *sys_errlist[];
  71.  
  72. #define toint(c)    ((c)-'0')
  73. #define isodigit(c)    (((unsigned)(c)>=060)&((unsigned)(c)<=067))
  74.  
  75. /* Write current time in RFC 822 format
  76.  * Accepts: destination string
  77.  */
  78.  
  79. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  80.  
  81. void rfc822_date (date)
  82.     char *date;
  83. {
  84.   int zone;
  85.   char *zonename;
  86.   struct tm *t;
  87.   struct timeval tv;
  88.   struct timezone tz;
  89.   gettimeofday (&tv,&tz);    /* get time and timezone poop */
  90.   t = localtime (&tv.tv_sec);    /* convert to individual items */
  91.                 /* use this for older systems */
  92.   zone = (t->tm_isdst ? 60 : 0) -tz.tz_minuteswest;
  93.   zonename = timezone (tz.tz_minuteswest,t->tm_isdst);
  94.                 /* and output it */
  95.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %+03d%02d (%s)",
  96.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  97.        t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60,zonename);
  98. }
  99.  
  100. /* Get a block of free storage
  101.  * Accepts: size of desired block
  102.  * Returns: free storage block
  103.  */
  104.  
  105. void *fs_get (size)
  106.     size_t size;
  107. {
  108.   void *block = malloc (size);
  109.   if (!block) fatal ("Out of free storage");
  110.   return (block);
  111. }
  112.  
  113.  
  114. /* Resize a block of free storage
  115.  * Accepts: ** pointer to current block
  116.  *        new size
  117.  */
  118.  
  119. void fs_resize (block,size)
  120.     void **block;
  121.     size_t size;
  122. {
  123.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  124. }
  125.  
  126.  
  127. /* Return a block of free storage
  128.  * Accepts: ** pointer to free storage block
  129.  */
  130.  
  131. void fs_give (block)
  132.     void **block;
  133. {
  134.   free (*block);
  135.   *block = NIL;
  136. }
  137.  
  138.  
  139. /* Report a fatal error
  140.  * Accepts: string to output
  141.  */
  142.  
  143. void fatal (string)
  144.     char *string;
  145. {
  146.   mm_fatal (string);        /* output the string */
  147.   syslog (LOG_ALERT,"IMAP C-Client crash: %s",string);
  148.   abort ();            /* die horribly */
  149. }
  150.  
  151. /* Copy string with CRLF newlines
  152.  * Accepts: destination string
  153.  *        pointer to size of destination string
  154.  *        source string
  155.  *        length of source string
  156.  * Returns: length of copied string
  157.  */
  158.  
  159. unsigned long strcrlfcpy (dst,dstl,src,srcl)
  160.     char **dst;
  161.     unsigned long *dstl;
  162.     char *src;
  163.     unsigned long srcl;
  164. {
  165.   long i,j;
  166.   char *d = src;
  167.                 /* count number of LF's in source string(s) */
  168.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  169.   if (i > *dstl) {        /* resize if not enough space */
  170.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  171.     *dst = (char *) fs_get ((*dstl = i) + 1);
  172.   }
  173.   d = *dst;            /* destination string */
  174.                 /* copy strings, inserting CR's before LF's */
  175.   while (srcl--) switch (*src) {
  176.   case '\015':            /* unlikely carriage return */
  177.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  178.     if (srcl && *src == '\012') {
  179.       *d++ = *src++;
  180.       srcl--;
  181.     }
  182.     break;
  183.   case '\012':            /* line feed? */
  184.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  185.   default:            /* ordinary chararacter */
  186.     *d++ = *src++;        /* just copy character */
  187.     break;
  188.   }
  189.   *d = '\0';            /* tie off destination */
  190.   return d - *dst;        /* return length */
  191. }
  192.  
  193.  
  194. /* Length of string after strcrlfcpy applied
  195.  * Accepts: source string
  196.  * Returns: length of string
  197.  */
  198.  
  199. unsigned long strcrlflen (s)
  200.     STRING *s;
  201. {
  202.   unsigned long pos = GETPOS (s);
  203.   unsigned long i = SIZE (s);
  204.   unsigned long j = i;
  205.   while (j--) switch (SNX (s)) {/* search for newlines */
  206.   case '\015':            /* unlikely carriage return */
  207.     if (j && (CHR (s) == '\012')) {
  208.       SNX (s);            /* eat the line feed */
  209.       j--;
  210.     }
  211.     break;
  212.   case '\012':            /* line feed? */
  213.     i++;
  214.   default:            /* ordinary chararacter */
  215.     break;
  216.   }
  217.   SETPOS (s,pos);        /* restore old position */
  218.   return i;
  219. }
  220.  
  221. /* Server log in
  222.  * Accepts: user name string
  223.  *        password string
  224.  *        optional place to return home directory
  225.  * Returns: T if password validated, NIL otherwise
  226.  */
  227.  
  228. long server_login (user,pass,home,argc,argv)
  229.     char *user;
  230.     char *pass;
  231.     char **home;
  232.     int argc;
  233.     char *argv[];
  234. {
  235.   struct passwd *pw = getpwnam (lcase (user));
  236.                 /* no entry for this user or root */
  237.   if (!(pw && pw->pw_uid)) return NIL;
  238.                 /* validate password */
  239.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  240.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  241.   initgroups (user,pw->pw_gid);    /* initialize groups */
  242.   setuid (pw->pw_uid);
  243.                 /* note home directory */
  244.   if (home) *home = cpystr (pw->pw_dir);
  245.   return T;
  246. }
  247.  
  248. /* Return my user name
  249.  * Returns: my user name
  250.  */
  251.  
  252. char *uname = NIL;
  253.  
  254. char *myusername ()
  255. {
  256.   return uname ? uname : (uname = cpystr (getpwuid (geteuid ())->pw_name));
  257. }
  258.  
  259.  
  260. /* Return my home directory name
  261.  * Returns: my home directory name
  262.  */
  263.  
  264. char *hdname = NIL;
  265.  
  266. char *myhomedir ()
  267. {
  268.   return hdname ? hdname : (hdname = cpystr (getpwuid (geteuid ())->pw_dir));
  269. }
  270.  
  271.  
  272. /* Build status lock file name
  273.  * Accepts: scratch buffer
  274.  *        file name
  275.  * Returns: name of file to lock
  276.  */
  277.  
  278. char *lockname (tmp,fname)
  279.     char *tmp;
  280.     char *fname;
  281. {
  282.   int i;
  283.   sprintf (tmp,"/tmp/.%s",fname);
  284.   for (i = 6; i < strlen (tmp); ++i) if (tmp[i] == '/') tmp[i] = '\\';
  285.   return tmp;            /* note name for later */
  286. }
  287.  
  288. /* TCP/IP open
  289.  * Accepts: host name
  290.  *        contact port number
  291.  * Returns: TCP/IP stream if success else NIL
  292.  */
  293.  
  294. TCPSTREAM *tcp_open (host,port)
  295.     char *host;
  296.     long port;
  297. {
  298.   TCPSTREAM *stream = NIL;
  299.   int sock;
  300.   char *s;
  301.   struct sockaddr_in sin;
  302.   struct hostent *host_name;
  303.   char hostname[MAILTMPLEN];
  304.   char tmp[MAILTMPLEN];
  305.   /* The domain literal form is used (rather than simply the dotted decimal
  306.      as with other Unix programs) because it has to be a valid "host name"
  307.      in mailsystem terminology. */
  308.                 /* look like domain literal? */
  309.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  310.     strcpy (hostname,host+1);    /* yes, copy number part */
  311.     hostname[(strlen (hostname))-1] = '\0';
  312.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  313.       sin.sin_family = AF_INET;    /* family is always Internet */
  314.       strcpy (hostname,host);    /* hostname is user's argument */
  315.     }
  316.     else {
  317.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  318.       mm_log (tmp,ERROR);
  319.       return NIL;
  320.     }
  321.   }
  322.  
  323.   else {            /* lookup host name, note that brain-dead Unix
  324.                    requires lowercase! */
  325.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  326.     if ((host_name = gethostbyname (lcase (hostname)))) {
  327.                 /* copy address type */
  328.       sin.sin_family = host_name->h_addrtype;
  329.                 /* copy host name */
  330.       strcpy (hostname,host_name->h_name);
  331.                 /* copy host addresses */
  332.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  333.     }
  334.     else {
  335.       sprintf (tmp,"No such host as %.80s",host);
  336.       mm_log (tmp,ERROR);
  337.       return NIL;
  338.     }
  339.   }
  340.  
  341.                 /* copy port number in network format */
  342.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  343.                 /* get a TCP stream */
  344.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  345.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  346.     mm_log (tmp,ERROR);
  347.     return NIL;
  348.   }
  349.                 /* open connection */
  350.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  351.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  352.          strerror (errno));
  353.     mm_log (tmp,ERROR);
  354.     return NIL;
  355.   }
  356.                 /* create TCP/IP stream */
  357.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  358.                 /* copy official host name */
  359.   stream->host = cpystr (hostname);
  360.                 /* get local name */
  361.   gethostname (tmp,MAILTMPLEN-1);
  362.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  363.                   host_name->h_name : tmp);
  364.                 /* init sockets */
  365.   stream->tcpsi = stream->tcpso = sock;
  366.   stream->ictr = 0;        /* init input counter */
  367.   return stream;        /* return success */
  368. }
  369.  
  370. /* TCP/IP authenticated open
  371.  * Accepts: host name
  372.  *        service name
  373.  * Returns: TCP/IP stream if success else NIL
  374.  */
  375.  
  376. TCPSTREAM *tcp_aopen (host,service)
  377.     char *host;
  378.     char *service;
  379. {
  380.   TCPSTREAM *stream = NIL;
  381.   struct hostent *host_name;
  382.   char hostname[MAILTMPLEN];
  383.   int i;
  384.   int pipei[2],pipeo[2];
  385.   /* The domain literal form is used (rather than simply the dotted decimal
  386.      as with other Unix programs) because it has to be a valid "host name"
  387.      in mailsystem terminology. */
  388.                 /* look like domain literal? */
  389.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  390.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  391.     hostname[i-1] = '\0';
  392.   }
  393.                 /* note that Unix requires lowercase! */
  394.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  395.     strcpy (hostname,host_name->h_name);
  396.                 /* make command pipes */
  397.   if (pipe (pipei) < 0) return NIL;
  398.   if (pipe (pipeo) < 0) {
  399.     close (pipei[0]); close (pipei[1]);
  400.     return NIL;
  401.   }
  402.   if ((i = fork ()) < 0) {    /* make inferior process */
  403.     close (pipei[0]); close (pipei[1]);
  404.     close (pipeo[0]); close (pipeo[1]);
  405.     return NIL;
  406.   }
  407.   if (i) {            /* parent? */
  408.     close (pipei[1]);        /* close child's side of the pipes */
  409.     close (pipeo[0]);
  410.   }
  411.   else {            /* child */
  412.     dup2 (pipei[1],1);        /* parent's input is my output */
  413.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  414.     close (pipei[0]); close (pipei[1]);
  415.     dup2 (pipeo[0],0);        /* parent's output is my input */
  416.     close (pipeo[0]); close (pipeo[1]);
  417.                 /* now run it */
  418.     execl (RSHPATH,RSH,hostname,"exec",service,0);
  419.     _exit (1);            /* spazzed */
  420.   }
  421.  
  422.                 /* create TCP/IP stream */
  423.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  424.                 /* copy official host name */
  425.   stream->host = cpystr (hostname);
  426.                 /* get local name */
  427.   gethostname (hostname,MAILTMPLEN-1);
  428.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  429.                   host_name->h_name : hostname);
  430.   stream->tcpsi = pipei[0];    /* init sockets */
  431.   stream->tcpso = pipeo[1];
  432.   stream->ictr = 0;        /* init input counter */
  433.   return stream;        /* return success */
  434. }
  435.  
  436. /* TCP/IP receive line
  437.  * Accepts: TCP/IP stream
  438.  * Returns: text line string or NIL if failure
  439.  */
  440.  
  441. char *tcp_getline (stream)
  442.     TCPSTREAM *stream;
  443. {
  444.   int n,m;
  445.   char *st,*ret,*stp;
  446.   char c = '\0';
  447.   char d;
  448.                 /* make sure have data */
  449.   if (!tcp_getdata (stream)) return NIL;
  450.   st = stream->iptr;        /* save start of string */
  451.   n = 0;            /* init string count */
  452.   while (stream->ictr--) {    /* look for end of line */
  453.     d = *stream->iptr++;    /* slurp another character */
  454.     if ((c == '\015') && (d == '\012')) {
  455.       ret = (char *) fs_get (n--);
  456.       memcpy (ret,st,n);    /* copy into a free storage string */
  457.       ret[n] = '\0';        /* tie off string with null */
  458.       return ret;
  459.     }
  460.     n++;            /* count another character searched */
  461.     c = d;            /* remember previous character */
  462.   }
  463.                 /* copy partial string from buffer */
  464.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  465.                 /* get more data from the net */
  466.   if (!tcp_getdata (stream)) return NIL;
  467.                 /* special case of newline broken by buffer */
  468.   if ((c == '\015') && (*stream->iptr == '\012')) {
  469.     stream->iptr++;        /* eat the line feed */
  470.     stream->ictr--;
  471.     ret[n - 1] = '\0';        /* tie off string with null */
  472.   }
  473.                 /* else recurse to get remainder */
  474.   else if (st = tcp_getline (stream)) {
  475.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  476.     memcpy (ret,stp,n);        /* copy first part */
  477.     memcpy (ret + n,st,m);    /* and second part */
  478.     fs_give ((void **) &stp);    /* flush first part */
  479.     fs_give ((void **) &st);    /* flush second part */
  480.     ret[n + m] = '\0';        /* tie off string with null */
  481.   }
  482.   return ret;
  483. }
  484.  
  485. /* TCP/IP receive buffer
  486.  * Accepts: TCP/IP stream
  487.  *        size in bytes
  488.  *        buffer to read into
  489.  * Returns: T if success, NIL otherwise
  490.  */
  491.  
  492. long tcp_getbuffer (stream,size,buffer)
  493.     TCPSTREAM *stream;
  494.     unsigned long size;
  495.     char *buffer;
  496. {
  497.   unsigned long n;
  498.   char *bufptr = buffer;
  499.   while (size > 0) {        /* until request satisfied */
  500.     if (!tcp_getdata (stream)) return NIL;
  501.     n = min (size,stream->ictr);/* number of bytes to transfer */
  502.                 /* do the copy */
  503.     memcpy (bufptr,stream->iptr,n);
  504.     bufptr += n;        /* update pointer */
  505.     stream->iptr +=n;
  506.     size -= n;            /* update # of bytes to do */
  507.     stream->ictr -=n;
  508.   }
  509.   bufptr[0] = '\0';        /* tie off string */
  510.   return T;
  511. }
  512.  
  513.  
  514.  
  515. long tcp_abort ();
  516.  
  517. /* TCP/IP receive data
  518.  * Accepts: TCP/IP stream
  519.  * Returns: T if success, NIL otherwise
  520.  */
  521.  
  522. long tcp_getdata (stream)
  523.     TCPSTREAM *stream;
  524. {
  525.   int i;
  526.   fd_set fds;
  527.   FD_ZERO (&fds);        /* initialize selection vector */
  528.   if (stream->tcpsi < 0) return NIL;
  529.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  530.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  531.     errno = NIL;        /* block and read */
  532.     while (((i = select (stream->tcpsi+1,&fds,0,0,0)) < 0) &&
  533.        (errno == EINTR));
  534.     if (i < 0) return tcp_abort (stream);
  535.     while (((i = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1) &&
  536.        (errno == EINTR));
  537.     if (i < 1) return tcp_abort (stream);
  538.     stream->ictr = i;        /* set new byte count */
  539.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  540.   }
  541.   return T;
  542. }
  543.  
  544. /* TCP/IP send string as record
  545.  * Accepts: TCP/IP stream
  546.  *        string pointer
  547.  * Returns: T if success else NIL
  548.  */
  549.  
  550. long tcp_soutr (stream,string)
  551.     TCPSTREAM *stream;
  552.     char *string;
  553. {
  554.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  555. }
  556.  
  557.  
  558. /* TCP/IP send string
  559.  * Accepts: TCP/IP stream
  560.  *        string pointer
  561.  *        byte count
  562.  * Returns: T if success else NIL
  563.  */
  564.  
  565. long tcp_sout (stream,string,size)
  566.     TCPSTREAM *stream;
  567.     char *string;
  568.     unsigned long size;
  569. {
  570.   int i;
  571.   fd_set fds;
  572.   FD_ZERO (&fds);        /* initialize selection vector */
  573.   if (stream->tcpso < 0) return NIL;
  574.   while (size > 0) {        /* until request satisfied */
  575.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  576.     errno = NIL;        /* block and wrtie */
  577.     while (((i = select (stream->tcpso+1,0,&fds,0,0)) < 0) &&
  578.        (errno == EINTR));
  579.     if (i < 0) return tcp_abort (stream);
  580.     while (((i = write (stream->tcpso,string,size)) < 0) &&
  581.        (errno == EINTR));
  582.     if (i < 0) return tcp_abort (stream);
  583.     size -= i;            /* how much we sent */
  584.     string += i;
  585.   }
  586.   return T;            /* all done */
  587. }
  588.  
  589. /* TCP/IP close
  590.  * Accepts: TCP/IP stream
  591.  */
  592.  
  593. void tcp_close (stream)
  594.     TCPSTREAM *stream;
  595. {
  596.   tcp_abort (stream);        /* nuke the stream */
  597.                 /* flush host names */
  598.   fs_give ((void **) &stream->host);
  599.   fs_give ((void **) &stream->localhost);
  600.   fs_give ((void **) &stream);    /* flush the stream */
  601. }
  602.  
  603.  
  604. /* TCP/IP abort stream
  605.  * Accepts: TCP/IP stream
  606.  * Returns: NIL always
  607.  */
  608.  
  609. long tcp_abort (stream)
  610.      TCPSTREAM *stream;
  611. {
  612.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  613.     close (stream->tcpsi);    /* nuke the socket */
  614.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  615.     stream->tcpsi = stream->tcpso = -1;
  616.   }
  617.   return NIL;
  618. }
  619.  
  620. /* TCP/IP get host name
  621.  * Accepts: TCP/IP stream
  622.  * Returns: host name for this stream
  623.  */
  624.  
  625. char *tcp_host (stream)
  626.     TCPSTREAM *stream;
  627. {
  628.   return stream->host;        /* return host name */
  629. }
  630.  
  631.  
  632. /* TCP/IP get local host name
  633.  * Accepts: TCP/IP stream
  634.  * Returns: local host name
  635.  */
  636.  
  637. char *tcp_localhost (stream)
  638.     TCPSTREAM *stream;
  639. {
  640.   return stream->localhost;    /* return local host name */
  641. }
  642.  
  643. /* Find token in string
  644.  * Accepts: source pointer or NIL to use previous source
  645.  *        vector of token delimiters pointer
  646.  * Returns: pointer to next token
  647.  */
  648.  
  649. char *ts = NIL;            /* string to locate tokens */
  650.  
  651. char *strtok (s,ct)
  652.      char *s;
  653.      char *ct;
  654. {
  655.   char *t;
  656.   if (!s) s = ts;        /* use previous token if none specified */
  657.   if (!(s && *s)) return NIL;    /* no tokens */
  658.                 /* find any leading delimiters */
  659.   do for (t = ct, ts = NIL; *t; t++) if (*t == *s) {
  660.     if (*(ts = ++s)) break;    /* yes, restart seach if more in string */
  661.     return ts = NIL;        /* else no more tokens */
  662.   } while (ts);            /* continue until no more leading delimiters */
  663.                 /* can we find a new delimiter? */
  664.   for (ts = s; *ts; ts++) for (t = ct; *t; t++) if (*t == *ts) {
  665.     *ts++ = '\0';        /* yes, tie off token at that point */
  666.     return s;            /* return our token */
  667.   }
  668.   ts = NIL;            /* no more tokens */
  669.   return s;            /* return final token */
  670. }
  671.  
  672.  
  673. /* Find first occurance of character in string
  674.  * Accepts: source pointer
  675.  *        character to search
  676.  * Returns: pointer to character or NIL if none
  677.  */
  678.  
  679. char *strchr (cs,c)
  680.      char *cs;
  681.      char c;
  682. {
  683.   return index (cs,c);        /* they should have this one */
  684. }
  685.  
  686.  
  687. /* Find last occurance of character in string
  688.  * Accepts: source pointer
  689.  *        character to search
  690.  * Returns: pointer to character or NIL if none
  691.  */
  692.  
  693. char *strrchr (cs,c)
  694.      char *cs;
  695.      char c;
  696. {
  697.   return rindex (cs,c);        /* they should have this one */
  698. }
  699.  
  700. /* Return pointer to first occurance in string of a substring
  701.  * Accepts: source pointer
  702.  *        substring pointer
  703.  * Returns: pointer to substring in source or NIL if not found
  704.  */
  705.  
  706. char *strstr (cs,ct)
  707.      char *cs;
  708.      char *ct;
  709. {
  710.   char *s;
  711.   char *t;
  712.   while (cs = index (cs,*ct)) {    /* for each occurance of the first character */
  713.                 /* see if remainder of string matches */
  714.     for (s = cs + 1, t = ct + 1; *t && *s == *t; s++, t++);
  715.     if (!*t) return cs;        /* if ran out of substring then have match */
  716.     cs++;            /* try from next character */
  717.   }
  718.   return NIL;            /* not found */
  719. }
  720.  
  721.  
  722. /* Return pointer to first occurance in string of any delimiter
  723.  * Accepts: source pointer
  724.  *        vector of delimiters pointer
  725.  * Returns: pointer to delimiter or NIL if not found
  726.  */
  727.  
  728. char *strpbrk (cs,ct)
  729.      char *cs;
  730.      char *ct;
  731. {
  732.   char *s;
  733.                 /* search for delimiter until end of string */
  734.   for (; *cs; cs++) for (s = ct; *s; s++) if (*s == *cs) return cs;
  735.   return NIL;            /* not found */
  736. }
  737.  
  738.  
  739. /* Return implementation-defined string corresponding to error
  740.  * Accepts: error number
  741.  * Returns: string for that error
  742.  */
  743.  
  744. char *strerror (n)
  745.      int n;
  746. {
  747.   return (n >= 0 && n < sys_nerr) ? sys_errlist[n] : NIL;
  748. }
  749.  
  750. /* Copy memory block
  751.  * Accepts: destination pointer
  752.  *        source pointer
  753.  *        length
  754.  * Returns: destination pointer
  755.  */
  756.  
  757. char *memcpy (s,ct,n)
  758.      char *s;
  759.      char *ct;
  760.      int n;
  761. {
  762.   bcopy (ct,s,n);        /* they should have this one */
  763.   return ct;
  764. }
  765.  
  766.  
  767. /* Copy memory block
  768.  * Accepts: destination pointer
  769.  *        source pointer
  770.  *        length
  771.  * Returns: destination pointer
  772.  */
  773.  
  774. char *memmove (s,ct,n)
  775.      char *s;
  776.      char *ct;
  777.      int n;
  778. {
  779.   bcopy (ct,s,n);        /* they should have this one */
  780.   return ct;
  781. }
  782.  
  783.  
  784. /* Set a block of memory
  785.  * Accepts: destination pointer
  786.  *        value to set
  787.  *        length
  788.  * Returns: destination pointer
  789.  */
  790.  
  791. char *memset (s,c,n)
  792.      char *s;
  793.      char c;
  794.      int n;
  795. {
  796.   if (c) while (n) s[--n] = c;    /* this way if non-zero */
  797.   else bzero (s,n);        /* they should have this one */
  798.   return s;
  799. }
  800.  
  801. /*
  802.  * Turn a string long into the real thing
  803.  * Accepts: source string
  804.  *        pointer to place to return end pointer
  805.  *        base
  806.  * Returns: parsed long integer, end pointer is updated
  807.  */
  808.  
  809. long strtol (s,endp,base)
  810.      char *s;
  811.      char **endp;
  812.      int base;
  813. {
  814.   long value = 0;        /* the accumulated value */
  815.   int negative = 0;        /* this a negative number? */
  816.   int ok;            /* true while valid chars for number */
  817.   char c;
  818.                 /* insist upon valid base */
  819.   if (base && (base < 2 || base > 36)) return NIL;
  820.   while (isspace (*s)) s++;    /* skip leading whitespace */
  821.   if (!base) {            /* if base = 0, */
  822.     if (*s == '-') {        /* integer constants are allowed a */
  823.       negative = 1;        /* leading unary minus, but not */
  824.       s++;            /* unary plus. */
  825.     }
  826.     else negative = 0;
  827.     if (*s == '0') {        /* if it starts with 0, its either */
  828.                 /* 0X, which marks a hex constant, */
  829.       if (toupper (*++s) == 'X') {
  830.     s++;            /* skip the X */
  831.            base = 16;        /* base is hex 16 */
  832.       }
  833.       else base = 8;        /* leading 0 means octal number */
  834.     }
  835.     else base = 10;        /* otherwise, decimal. */
  836.     do {
  837.       switch (base) {        /* char has to be valid for base */
  838.       case 8:            /* this digit ok? */
  839.     ok = isodigit(*s);
  840.     break;
  841.       case 10:
  842.     ok = isdigit(*s);
  843.     break;
  844.       case 16:
  845.     ok = isxdigit(*s);
  846.     break;
  847.       default:            /* it's good form you know */
  848.     return NIL;
  849.       }
  850.                 /* if valid char, accumulate */
  851.       if (ok) value = value * base + toint(*s++);
  852.     } while (ok);
  853.     if (toupper(*s) == 'L') s++;/* ignore 'l' or 'L' marker */
  854.     if (endp) *endp = s;    /* set user pointer to after num */
  855.     return (negative) ? -value : value;
  856.   }
  857.  
  858.   switch (*s) {            /* check for leading sign char */
  859.   case '-':
  860.     negative = 1;        /* yes, negative #.  fall into '+' */
  861.   case '+':
  862.     s++;            /* skip the sign character */
  863.   }
  864.                 /* allow hex prefix "0x" */
  865.   if (base == 16 && *s == '0' && toupper (*(s + 1)) == 'X') s += 2;
  866.   do {
  867.                 /* convert to numeric form if digit*/
  868.     if (isdigit (*s)) c = toint (*s);
  869.                 /* alphabetic conversion */
  870.     else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
  871.     else break;            /* else no way it's valid */
  872.     if (c >= base) break;    /* digit out of range for base? */
  873.     value = value * base + c;    /* accumulate the digit */
  874.   } while (*++s);        /* loop until non-numeric character */
  875.   if (endp) *endp = s;        /* save users endp to after number */
  876.                 /* negate number if needed */
  877.   return (negative) ? -value : value;
  878. }
  879.  
  880.