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

  1. /*
  2.  * Program:    Operating-system dependent routines -- MS-DOS (B&W) version
  3.  *
  4.  * Author:    Mark Crispin/Ken Bobey
  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:    11 April 1989
  13.  * Last Edited:    15 June 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  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 -- must be large enough to prevent overflow */
  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 tcps;            /* tcp socket */
  48.   long ictr;            /* input counter */
  49.   char *iptr;            /* input pointer */
  50.   char ibuf[BUFLEN];        /* input buffer */
  51. };
  52.  
  53.  
  54. /* Private function prototypes */
  55.  
  56. #include "mail.h"
  57. #include "osdep.h"
  58. #include <time.h>
  59. #include <sys\timeb.h>
  60. #include "misc.h"
  61. #include "stdlib.h"
  62. #include "bwtcp.h"
  63.  
  64.  
  65. /* Global data */
  66.  
  67. unsigned long rndm = 0xfeed;    /* initial `random' number */
  68.  
  69. /* Write current time in RFC 822 format
  70.  * Accepts: destination string
  71.  */
  72.  
  73. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  74.  
  75. void rfc822_date (char *date)
  76. {
  77.   time_t ti = time (0);
  78.   struct tm *t;
  79.   tzset ();            /* initialize timezone stuff */
  80.   t = localtime (&ti);        /* output local time */
  81.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %s",
  82.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  83.        t->tm_hour,t->tm_min,t->tm_sec,tzname[t->tm_isdst]);
  84. }
  85.  
  86. /* Get a block of free storage
  87.  * Accepts: size of desired block
  88.  * Returns: free storage block
  89.  */
  90.  
  91. void *fs_get (size_t size)
  92. {
  93.   void *block = malloc (size);
  94.   if (!block) fatal ("Out of free storage");
  95.   return (block);
  96. }
  97.  
  98.  
  99. /* Resize a block of free storage
  100.  * Accepts: ** pointer to current block
  101.  *        new size
  102.  */
  103.  
  104. void fs_resize (void **block,size_t size)
  105. {
  106.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  107. }
  108.  
  109.  
  110. /* Return a block of free storage
  111.  * Accepts: ** pointer to free storage block
  112.  */
  113.  
  114. void fs_give (void **block)
  115. {
  116.   free (*block);
  117.   *block = NIL;
  118. }
  119.  
  120.  
  121. /* Report a fatal error
  122.  * Accepts: string to output
  123.  */
  124.  
  125. void fatal (char *string)
  126. {
  127.   mm_fatal (string);        /* pass the string */
  128.   abort ();            /* die horribly */
  129. }
  130.  
  131. /* Copy string with CRLF newlines
  132.  * Accepts: destination string
  133.  *        pointer to size of destination string
  134.  *        source string
  135.  *        length of source string
  136.  */
  137.  
  138. char *strcrlfcpy (char **dst,unsigned long *dstl,char *src,unsigned long srcl)
  139. {
  140.   if (srcl > *dstl) {        /* resize if not enough space */
  141.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  142.     *dst = (char *) fs_get ((*dstl = srcl) + 1);
  143.   }
  144.                 /* copy strings */
  145.   if (srcl) memcpy (*dst,src,srcl);
  146.   *(*dst + srcl) = '\0';    /* tie off destination */
  147.   return *dst;            /* return destination */
  148. }
  149.  
  150.  
  151. /* Length of string after strcrlfcpy applied
  152.  * Accepts: source string
  153.  *        length of source string
  154.  */
  155.  
  156. unsigned long strcrlflen (STRING *s)
  157. {
  158.   return SIZE (s);        /* no-brainer on DOS! */
  159. }
  160.  
  161.  
  162. /* Return my home directory name
  163.  * Returns: my home directory name
  164.  */
  165.  
  166. char *hdname = NIL;
  167.  
  168. char *myhomedir ()
  169. {
  170.   int i;
  171.   char *s;
  172.   if (!hdname) {        /* get home directory name if not yet known */
  173.     hdname = cpystr ((s = getenv ("HOME")) ? s : "");
  174.     if ((i = strlen (hdname)) && ((hdname[i-1] == '\\') || (hdname[i-1]=='/')))
  175.       hdname[i-1] = '\0';    /* tie off trailing directory delimiter */
  176.   }
  177.   return hdname;
  178. }
  179.  
  180. /* TCP/IP open
  181.  * Accepts: host name
  182.  *        contact port number
  183.  * Returns: TCP/IP stream if success else NIL
  184.  */
  185.  
  186. TCPSTREAM *tcp_open (char *host,long port)
  187. {
  188.   TCPSTREAM *stream = NIL;
  189.   struct sockaddr_in sin;
  190.   struct hostent *host_name;
  191.   int sock;
  192.   long adr,i,j,k,l;
  193.   char *s;
  194.   char tmp[MAILTMPLEN];
  195.   char *hostname = cpystr (host);
  196.                 /* set default gets routine */
  197.   if (!mailgets) mailgets = mm_gets;
  198.   /* The domain literal form is used (rather than simply the dotted decimal
  199.      as with other Unix programs) because it has to be a valid "host name"
  200.      in mailsystem terminology. */
  201.   sin.sin_family = AF_INET;    /* family is always Internet */
  202.                 /* look like domain literal? */
  203.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  204.     strcpy (tmp,host+1);    /* yes, copy number part */
  205.     tmp[strlen (tmp)-1] = '\0';
  206.     if ((sin.sin_addr.s_addr = inet_addr (tmp)) == -1) {
  207.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  208.       mm_log (tmp,ERROR);
  209.       fs_give ((void **) hostname);
  210.       return NIL;
  211.     }
  212.   }
  213.   else {            /* lookup host name */
  214.     if ((sin.sin_addr.s_addr = rhost (&hostname)) == -1) {
  215.       sprintf (tmp,"Host not found: %s",host);
  216.       mm_log (tmp,ERROR);
  217.       fs_give ((void **) hostname);
  218.       return NIL;
  219.     }
  220.   }
  221.  
  222.                 /* copy port number in network format */
  223.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  224.                 /* get a TCP stream */
  225.   
  226.   if ((sock = socket (SOCK_STREAM,NIL,&sin,0)) < 0) {
  227.     mm_log ("Unable to create TCP socket",ERROR);
  228.     return NIL;
  229.   }
  230.                 /* get local host name from DISPLAY env var */
  231.   if (!((s = getenv ("DISPLAY")) || (s = getenv ("display")))) {
  232.     mm_log ("Environment variable 'DISPLAY' is not set", ERROR);
  233.     return NIL;
  234.   }
  235.                 /* open connection */
  236.   if (connect (sock,(struct sockaddr *) &sin) < 0) {
  237.     switch (errno) {        /* analyze error */
  238.     case ECONNREFUSED: s = "Refused"; break;
  239.     case ENOBUFS: s = "Insufficient system resources"; break;
  240.     case ETIMEDOUT: s = "Timed out"; break;
  241.     default: s = "Unknown error"; break;
  242.     }
  243.     sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",hostname,port,s,errno);
  244.     mm_log (tmp,ERROR);
  245.     fs_give ((void **) hostname);
  246.     soclose (sock);
  247.     return NIL;
  248.   }
  249.                 /* create TCP/IP stream */
  250.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  251.   stream->host = hostname;    /* official host name */
  252.   stream->localhost = cpystr (s);
  253.   stream->tcps = sock;        /* init socket */
  254.   stream->ictr = 0;        /* init input counter */
  255.   return stream;        /* return success */
  256. }
  257.   
  258. /* TCP/IP authenticated open
  259.  * Accepts: host name
  260.  *        service name
  261.  * Returns: TCP/IP stream if success else NIL
  262.  */
  263.  
  264. TCPSTREAM *tcp_aopen (char *host,char *service)
  265. {
  266.   return NIL;            /* always NIL on DOS */
  267. }
  268.  
  269. /* TCP/IP receive line
  270.  * Accepts: TCP/IP stream
  271.  * Returns: text line string or NIL if failure
  272.  */
  273.  
  274. char *tcp_getline (TCPSTREAM *stream)
  275. {
  276.   int n,m;
  277.   char *st,*ret,*stp;
  278.   char c = '\0';
  279.   char d;
  280.                 /* make sure have data */
  281.   if (!tcp_getdata (stream)) return NIL;
  282.   st = stream->iptr;        /* save start of string */
  283.   n = 0;            /* init string count */
  284.   while (stream->ictr--) {    /* look for end of line */
  285.     d = *stream->iptr++;    /* slurp another character */
  286.     if ((c == '\015') && (d == '\012')) {
  287.       ret = (char *) fs_get (n--);
  288.       memcpy (ret,st,n);    /* copy into a free storage string */
  289.       ret[n] = '\0';        /* tie off string with null */
  290.       return ret;
  291.     }
  292.     n++;            /* count another character searched */
  293.     c = d;            /* remember previous character */
  294.   }
  295.                 /* copy partial string from buffer */
  296.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  297.                 /* get more data from the net */
  298.   if (!tcp_getdata (stream)) return NIL;
  299.                 /* special case of newline broken by buffer */
  300.   if ((c == '\015') && (*stream->iptr == '\012')) {
  301.     stream->iptr++;        /* eat the line feed */
  302.     stream->ictr--;
  303.     ret[n - 1] = '\0';        /* tie off string with null */
  304.   }
  305.                 /* else recurse to get remainder */
  306.   else if (st = tcp_getline (stream)) {
  307.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  308.     memcpy (ret,stp,n);        /* copy first part */
  309.     memcpy (ret + n,st,m);    /* and second part */
  310.     fs_give ((void **) &stp);    /* flush first part */
  311.     fs_give ((void **) &st);    /* flush second part */
  312.     ret[n + m] = '\0';        /* tie off string with null */
  313.   }
  314.   return ret;
  315. }
  316.  
  317. /* TCP/IP receive buffer
  318.  * Accepts: TCP/IP stream
  319.  *        size in bytes
  320.  *        buffer to read into
  321.  * Returns: T if success, NIL otherwise
  322.  */
  323.  
  324. long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
  325. {
  326.   unsigned long n;
  327.   char *bufptr = buffer;
  328.   while (size > 0) {        /* until request satisfied */
  329.     if (!tcp_getdata (stream)) return NIL;
  330.     n = min (size,stream->ictr);/* number of bytes to transfer */
  331.                 /* do the copy */
  332.     memcpy (bufptr,stream->iptr,n);
  333.     bufptr += n;        /* update pointer */
  334.     stream->iptr +=n;
  335.     size -= n;            /* update # of bytes to do */
  336.     stream->ictr -=n;
  337.   }
  338.   bufptr[0] = '\0';        /* tie off string */
  339.   return T;
  340. }
  341.  
  342.  
  343. /* TCP/IP receive data
  344.  * Accepts: TCP/IP stream
  345.  * Returns: T if success, NIL otherwise
  346.  */
  347.  
  348. long tcp_getdata (TCPSTREAM *stream)
  349. {
  350.   fd_set fds;
  351.   FD_ZERO (&fds);        /* initialize selection vector */
  352.   if (stream->tcps < 0) return NIL;
  353.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  354.     FD_SET (stream->tcps,&fds);    /* set bit in selection vector */
  355.                 /* block and read */
  356.     if ((select (stream->tcps+1,&fds,0,0,0) < 0) ||
  357.     ((stream->ictr = soread (stream->tcps,stream->ibuf,BUFLEN)) < 1)) {
  358.       soclose (stream->tcps);    /* nuke the socket */
  359.       stream->tcps = -1;
  360.       return NIL;
  361.     }
  362.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  363.   }
  364.   return T;
  365. }
  366.  
  367. /* TCP/IP send string as record
  368.  * Accepts: TCP/IP stream
  369.  *        string pointer
  370.  * Returns: T if success else NIL
  371.  */
  372.  
  373. long tcp_soutr (TCPSTREAM *stream,char *string)
  374. {
  375.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  376. }
  377.  
  378.  
  379. /* TCP/IP send string
  380.  * Accepts: TCP/IP stream
  381.  *        string pointer
  382.  *        byte count
  383.  * Returns: T if success else NIL
  384.  */
  385.  
  386. long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
  387. {
  388.   long i;
  389.   fd_set fds;
  390.   FD_ZERO (&fds);        /* initialize selection vector */
  391.   if (stream->tcps < 0) return NIL;
  392.   while (size > 0) {        /* until request satisfied */
  393.     FD_SET (stream->tcps,&fds);/* set bit in selection vector */
  394.     if ((select (stream->tcps+1,0,&fds,0,0) < 0) ||
  395.     ((i = sowrite (stream->tcps,string,size)) < 0)) {
  396.       soclose (stream->tcps);    /* nuke the socket */
  397.       stream->tcps = -1;
  398.       return NIL;
  399.     }
  400.     size -= i;            /* count this size */
  401.     string += i;
  402.   }
  403.   return T;            /* all done */
  404. }
  405.  
  406.  
  407. /* TCP/IP close
  408.  * Accepts: TCP/IP stream
  409.  */
  410.  
  411. void tcp_close (TCPSTREAM *stream)
  412. {
  413.                 /* nuke the socket */
  414.   if (stream->tcps >= 0) soclose (stream->tcps);
  415.   stream->tcps = -1;
  416.                 /* flush host names */
  417.   fs_give ((void **) &stream->host);
  418.   fs_give ((void **) &stream->localhost);
  419.   fs_give ((void **) &stream);    /* flush the stream */
  420. }
  421.  
  422. /* TCP/IP get host name
  423.  * Accepts: TCP/IP stream
  424.  * Returns: host name for this stream
  425.  */
  426.  
  427. char *tcp_host (TCPSTREAM *stream)
  428. {
  429.   return stream->host;        /* return host name */
  430. }
  431.  
  432.  
  433. /* TCP/IP get local host name
  434.  * Accepts: TCP/IP stream
  435.  * Returns: local host name
  436.  */
  437.  
  438. char *tcp_localhost (TCPSTREAM *stream)
  439. {
  440.   return stream->localhost;    /* return local host name */
  441. }
  442.  
  443. /* These functions are only used by rfc822.c for calculating cookies.  So this
  444.  * is good enough.  If anything better is needed fancier functions will be
  445.  * needed.
  446.  */
  447.  
  448.  
  449. /* Return host ID
  450.  */
  451.  
  452. unsigned long gethostid ()
  453. {
  454.   return (unsigned long) 0;
  455. }
  456.  
  457.  
  458. /* Return random number
  459.  */
  460.  
  461. long random ()
  462. {
  463.   return rndm *= 0xdae0;
  464. }
  465.  
  466.  
  467. /* Return `process ID'
  468.  */
  469.  
  470. long getpid ()
  471. {
  472.   return 1;
  473. }
  474.