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

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