home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / pine3.07 / c-client / os_sgi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-14  |  21.5 KB  |  794 lines

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