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

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