home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / OFFLINE / UQWK18.ZIP / UQWK18.TAR / misc.c < prev    next >
C/C++ Source or Header  |  1994-02-01  |  6KB  |  317 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "uqwk.h"
  4.  
  5. /*
  6.  *  Miscellaneous uqwk routines
  7.  */
  8.  
  9. char f[PATH_LEN];    /* Static area for ParseFrom() */
  10.  
  11. struct conf_ent *NewConference (name, num)
  12. char *name;
  13. int num;
  14. {
  15.     struct conf_ent *tmp_cp;
  16.     char *c;
  17.  
  18.     /* Get space for new conference */
  19.     if (NULL == (tmp_cp = (struct conf_ent *) malloc
  20.                     (sizeof (struct conf_ent))))
  21.     {
  22.         fprintf (stderr, "%s: out of memory\n", progname);
  23.         exit (0);
  24.     }
  25.  
  26.     /* Get space for name */
  27.     if (NULL == (c = (char *) malloc (1+strlen(name))))
  28.     {
  29.         fprintf (stderr, "%s: out of memory\n", progname);
  30.         exit (0);
  31.     }
  32.  
  33.     /* Fill in conference name */
  34.     tmp_cp->name = c;
  35.     strcpy (tmp_cp->name, name);
  36.  
  37.     /* Fill in conference number */
  38.     tmp_cp->number = num;
  39.  
  40.     /* Article count */
  41.     tmp_cp->count = 0;
  42.  
  43.     /* Add to end of conference list */
  44.     if (last_conf == NULL)
  45.     {
  46.         /* This is first conference */
  47.         conf_list = tmp_cp;
  48.     }
  49.     else
  50.     {
  51.         last_conf->next = tmp_cp;
  52.     }
  53.     tmp_cp->next = NULL;
  54.     last_conf = tmp_cp;
  55.  
  56.     if (slnp_mode)
  57.     {
  58.         /* Open SLNP message file */
  59.         sprintf (msg_fn, "%s/%07d.MSG", home_dir, num);
  60.         if (NULL == (msg_fd = fopen (msg_fn, "w")))
  61.         {
  62.             fprintf (stderr, "%s: can't open %s\n",
  63.                     progname, msg_fn);
  64.             exit (0);
  65.         }
  66.     }
  67.     else if (!zip_mode && !sum_mode)
  68.     {
  69.         /* Else open new QWK index file */
  70.         if (!bw_kludge && !strcmp (name, MAIL_CONF_NAME))
  71.         {
  72.             strcpy (ndx_fn, home_dir);
  73.             strcat (ndx_fn, "/");
  74.             strcat (ndx_fn, "personal.ndx");
  75.         }
  76.         else
  77.         {
  78.             sprintf (ndx_fn, "%s/%03d.ndx", home_dir, num);
  79.         }
  80.  
  81.         if (NULL == (ndx_fd = fopen (ndx_fn, "w")))
  82.         {
  83.             fprintf (stderr, "%s: can't open %s\n",
  84.                     progname, ndx_fn);
  85.             exit (0);
  86.         }
  87.     }
  88.  
  89.     /* Maintain conf_cnt: should always be one greater than highest
  90.        newsgroup conference number encountered */
  91.     if (strcmp (name, MAIL_CONF_NAME))
  92.     {
  93.         if (num >= conf_cnt) conf_cnt = num + 1;
  94.     }
  95.  
  96.     /* Remember no summaries for this newsgroup yet */
  97.     if (sum_mode) sum_flag = 0;
  98.  
  99.     return (tmp_cp);
  100. }
  101.  
  102. PadString (s, c, n)
  103. char *s, *c;
  104. int n;
  105. /*
  106.  *  Take a null-terminated string s and copy it, space-padded or
  107.  *  truncated if necessary, into field c of n characters
  108.  */
  109. {
  110.     int len;
  111.     len = strlen (s);
  112.     if (len >= n)
  113.     {
  114.         strncpy (c, s, n);
  115.     }
  116.     else
  117.     {
  118.         strcpy (c, s);
  119.         Spaces (&c[len], n-len);
  120.     }
  121. }
  122.  
  123. Spaces (c, n)
  124. char *c;
  125. int n;
  126. /*
  127.  *  Fill field of n characters with spaces
  128.  */
  129. {
  130.     int i;
  131.     for (i=0; i<n; i++) c[i]=' ';
  132. }
  133.  
  134. PadNum (i, c, n)
  135. int i, n;
  136. char *c;
  137. /*
  138.  *  Format an integer i and place it, space filled, in
  139.  *  field c of n characters
  140.  */
  141. {
  142.     sprintf (buf, "%d", i);
  143.     PadString (buf, c, n);
  144. }
  145.  
  146. IntNum (i, c)
  147. int i;
  148. char c[2];
  149. /*
  150.  *  Put binary integer i into two bytes
  151.  */
  152. {
  153.     c[0] = i % 256;
  154.     c[1] = (i / 256) % 256;
  155. }
  156.  
  157. char *Fgets (c, n, fd)
  158. char *c;
  159. int n;
  160. FILE *fd;
  161. /*
  162.  *  Same as fgets, but changes trailing linefeed to a null
  163.  */
  164. {
  165.     int i;
  166.  
  167.     if (NULL == fgets (c, n, fd)) return (NULL);
  168.     i = strlen (c);
  169.     if ( (i > 0) && (c[i-1]=='\n') ) c[i-1] = 0;
  170.     if ( (i > 1) && (c[i-2]=='\r') ) c[i-2] = 0;
  171.  
  172.     return (c);
  173. }
  174.  
  175. inttoms (i, c)
  176. int i;
  177. char c[4];
  178. /*
  179.  *  Convert an integer into the Microsoft Basic floating format.
  180.  *  This is the dumbest thing in the whole QWK standard.  Why in
  181.  *  the world store block offsets as floating point numbers?
  182.  *  Stupid!
  183.  */
  184. {
  185.     int m, e;
  186.  
  187.     if (i == 0)
  188.     {
  189.         c[0] = c[1] = c[2] = 0;
  190.         c[3] = 0x80;
  191.         return (0);
  192.     }
  193.  
  194.     e = 152;
  195.     m = 0x7fffff & i;
  196.  
  197.     while (!(0x800000 & m))
  198.     {
  199.         m <<= 1;
  200.         e--;
  201.     }
  202.     c[0] = 0xff & m;
  203.     c[1] = 0xff & (m >> 8);
  204.     c[2] = 0x7f & (m >> 16);
  205.     c[3] = 0xff & e;
  206.     return (0);
  207. }
  208.  
  209. ParseDate (c, hp)
  210. char *c;
  211. struct qwk_hdr *hp;
  212. {
  213.     char s[PATH_LEN];
  214.     int day, mon, year, hour, minute;
  215.     char month[4];
  216.  
  217.     /* Skip white space */
  218.     while ( (*c == ' ') || (*c == 9) ) c++;
  219.  
  220.     /* Dates come in two flavors:  with the weekday, and without.
  221.        we simply look for the comma which follows the weekday */
  222.     if (c[3] == ',')
  223.     {
  224.             sscanf (&c[4], "%d %s %d %d:%d", &day, month, &year,
  225.                                             &hour, &minute);
  226.     }
  227.     else
  228.     {
  229.             sscanf (c, "%d %s %d %d:%d", &day, month, &year,
  230.                                             &hour, &minute);
  231.     }
  232.  
  233.     /* Convert alphabetic month name to integer */
  234.     mon = 0;
  235.     if (!strncmp (month, "Jan", 3)) mon = 1;
  236.     if (!strncmp (month, "Feb", 3)) mon = 2;
  237.     if (!strncmp (month, "Mar", 3)) mon = 3;
  238.     if (!strncmp (month, "Apr", 3)) mon = 4;
  239.     if (!strncmp (month, "May", 3)) mon = 5;
  240.     if (!strncmp (month, "Jun", 3)) mon = 6;
  241.     if (!strncmp (month, "Jul", 3)) mon = 7;
  242.     if (!strncmp (month, "Aug", 3)) mon = 8;
  243.     if (!strncmp (month, "Sep", 3)) mon = 9;
  244.     if (!strncmp (month, "Oct", 3)) mon = 10;
  245.     if (!strncmp (month, "Nov", 3)) mon = 11;
  246.     if (!strncmp (month, "Dec", 3)) mon = 12;
  247.  
  248.     /* Convert date */
  249.     sprintf (s, "%02d-%02d-%02d", mon, day, year%100);
  250.     PadString (s, hp->date, 8);
  251.  
  252.     /* Time */
  253.     sprintf (s, "%02d:%02d", hour, minute);
  254.     PadString (s, hp->time, 5);
  255. }
  256.  
  257. char *ParseFrom (c)
  258. char *c;
  259. /*
  260.  *  Extract the email address from a From: line
  261.  */
  262. {
  263.     int type, n, i, where;
  264.  
  265.     /*
  266.      *  Addresses come in three flavors:
  267.      *
  268.      *      1: seb3@gte.com
  269.      *      2: seb3@gte.com (steve belczyk)
  270.      *      3: steve belczyk <seb3@gte.com>
  271.      */
  272.  
  273.     /* Assume type 1 */
  274.     type = 1;
  275.  
  276.     /* Look through address */
  277.     n = strlen (c);
  278.     for (i=0; i<n; i++)
  279.     {
  280.             /* Change close-angle-bracket to null so we can
  281.                sscanf the address later */
  282.             if (c[i] == '>') c[i] = 0;
  283.  
  284.             /* If we find an open-angle-bracket, assume type 3 */
  285.             if (c[i] == '<')
  286.             {
  287.                     type = 3;
  288.                     where = i+1;
  289.             }
  290.     }
  291.  
  292.     /* Now extract the address */
  293.     if (type == 1)
  294.     {
  295.             /* This works for type 2 addresses too */
  296.             sscanf (c, "%s", f);
  297.     }
  298.     else    /* type == 3 */
  299.     {
  300.             /* Check we don't fly off the end of c.  This should
  301.                never happen, but I've been wrong before. */
  302.             if (where > n)
  303.             {
  304.                     strcpy (f, "unknown");
  305.             }
  306.             else
  307.             {
  308.                     /* Get address */
  309.                     sscanf (&c[where], "%s", f);
  310.             }
  311.     }
  312.  
  313.     /* Done! */
  314.     return (&f[0]);
  315. }
  316.  
  317.