home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / PLST_133.SZH / MISC.C < prev    next >
Text File  |  1991-09-20  |  6KB  |  241 lines

  1. /*
  2.                               Nodelist Parser
  3.  
  4.               This module was originally written by Bob Hartman
  5.                        Sysop of FidoNet node 1:104/501
  6.  
  7.  This program source code is being released with the following provisions:
  8.  
  9.  1.  You are  free to make  changes to this source  code for use on your own
  10.  machine,  however,  altered source files may not be distributed without the
  11.  consent of Spark Software.
  12.  
  13.  2.  You may distribute "patches"  or  "diff" files for any changes that you
  14.  have made, provided that the "patch" or "diff" files are also sent to Spark
  15.  Software for inclusion in future releases of the entire package.   A "diff"
  16.  file for the source archives may also contain a compiled version,  provided
  17.  it is  clearly marked as not  being created  from the original source code.
  18.  No other  executable  versions may be  distributed without  the  consent of
  19.  Spark Software.
  20.  
  21.  3.  You are free to include portions of this source code in any program you
  22.  develop, providing:  a) Credit is given to Spark Software for any code that
  23.  may is used, and  b) The resulting program is free to anyone wanting to use
  24.  it, including commercial and government users.
  25.  
  26.  4.  There is  NO  technical support  available for dealing with this source
  27.  code, or the accompanying executable files.  This source  code  is provided
  28.  as is, with no warranty expressed or implied (I hate legalease).   In other
  29.  words, if you don't know what to do with it,  don't use it,  and if you are
  30.  brave enough to use it, you're on your own.
  31.  
  32. */
  33.  
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <dos.h>
  40. #include <time.h>
  41. #include <stdlib.h>
  42.  
  43. #include "types.h"
  44. #include "externs.h"
  45.  
  46. #ifndef OS2
  47. #define dir_findfirst(a,b,c) _dos_findfirst(a,b,c)
  48. #define dir_findnext(a) _dos_findnext(a)
  49. #endif
  50.  
  51. int cmp (struct nidxs *a,struct nidxs *b)
  52. {
  53.    if (a->idxnet < b->idxnet)
  54.       {
  55.       return (-1);
  56.       }
  57.    else if (a->idxnet > b->idxnet)
  58.       {
  59.       return (1);
  60.       }
  61.    else
  62.       {
  63.       return (a->idxnode - b->idxnode);
  64.       }
  65. }
  66.  
  67. /* Returns the next field that ends with a comma or newline */
  68. char *nextfield (char *p,char *s)
  69. {
  70.    while ((*p) && (*p != ',') && (*p != '\n'))
  71.       {
  72.       *s++ = *p++;
  73.       }
  74.    *s = '\0';
  75.    return (++p);
  76. }
  77.  
  78. /* Converts a "special" word to upper case and appends a space */
  79. void spec_word (char *b1)
  80. {
  81.    char *p1;
  82.  
  83.    strupr (b1);
  84.    p1 = b1 + strlen (b1);
  85.    *p1++ = ' ';
  86.    *p1 = '\0';
  87. }
  88.  
  89. /* Uses malloc() to get memory, but exits on allocation error */
  90. char *mymalloc(unsigned int s)
  91. {
  92.    char *p;
  93.   /*  extern char *malloc(); */
  94.  
  95.    p = malloc (s);
  96.    if (p == NULL)
  97.       {
  98.       printf ("\n\nNot Enough Memory - Exiting\n\n");
  99.       exit (1);
  100.       }
  101.    return (p);
  102. }
  103.  
  104. /* Gets the name of the nodelist that should be processed this run */
  105. void nfile_name (char **fn)
  106. {
  107.    int i;
  108.    long t;
  109.    struct tm *tm1, *localtime();
  110.  
  111.    /* Get todays info */
  112.    time (&t);
  113.    tm1 = localtime (&t);
  114.  
  115.    /* find all NODELIST.* files and if end in number, put in array */
  116.    get_nl_list ();
  117.  
  118.    for (i = tm1->tm_yday + 1; i >= 0; i--)
  119.       {
  120.       if (nl[i])
  121.          {
  122.          *fn = mymalloc (13);
  123.          sprintf (*fn, "NODELIST.%03d", i);
  124.          return;
  125.          }
  126.       }
  127.  
  128.    /* If no nodelist files, try last years */
  129.    for (i = 366; i >= tm1->tm_yday; i--)
  130.       {
  131.       if (nl[i])
  132.          {
  133.          *fn = mymalloc (13);
  134.          sprintf (*fn, "NODELIST.%03d", i);
  135.          return;
  136.          }
  137.       }
  138.  
  139.    /* If still nothing, try the big numbers */
  140.    for (i = 999; i >= 367; i--)
  141.       {
  142.       if (nl[i])
  143.          {
  144.          *fn = mymalloc (13);
  145.          sprintf (*fn, "NODELIST.%03d", i);
  146.          return;
  147.          }
  148.       }
  149.  
  150.    printf ("\n\nCannot find a valid NODELIST.xxx file - Exiting\n\n");
  151.    exit (1);
  152. }
  153.  
  154. /* Remove dashes from the string */
  155. void undash (char *str)
  156. {
  157.    char *p, *p1;
  158.  
  159.    p = str;
  160.    p1 = str;
  161.    while (*p)
  162.       {
  163.       if (*p != '-')
  164.          *p1++ = *p;
  165.       ++p;
  166.       }
  167.    *p1 = '\0';
  168. }
  169.  
  170. void
  171. get_nl_list ()
  172. {
  173.    char str[13];
  174.    char init_name[13];
  175.    int i, d;
  176.  
  177.    /* Start out by initializing what we are looking for */
  178.    strcpy (init_name, "NODELIST.*");
  179.    d = 0;
  180.  
  181.    /* Now loop through getting each file name that matches,
  182.       and copying the number into the array */
  183.    do
  184.       {
  185.       (void) filedir (init_name, d, str, 0);
  186.       i = 0;
  187.       d = 1;
  188.       sscanf (str, "NODELIST.%d", &i);
  189.  
  190.       if ((i > 0) &&
  191.           (isdigit (str[9])) &&
  192.           (isdigit (str[10])) &&
  193.           (isdigit (str[11])))
  194.          {
  195.          nl[i] = 1;
  196.          }
  197.       }
  198.    while (str[0] != '\0');
  199. }
  200.  
  201. struct find_t sbuf;
  202.  
  203. char filedir (char *name,int times,char *ret_str,int mode)
  204. {
  205.     if (times == 0)
  206.         {
  207. #ifndef OS2
  208.         if (_dos_findfirst (name, mode, &sbuf))
  209. #else
  210.         if (dir_findfirst (name, mode, &sbuf))
  211. #endif
  212.             sbuf.name[0] = '\0';
  213.         strcpy (ret_str, sbuf.name);
  214.         }
  215.     else
  216.         {
  217. #ifndef OS2
  218.         if (_dos_findnext (&sbuf))
  219. #else
  220.         if (dir_findnext (&sbuf))
  221. #endif
  222.             sbuf.name[0] = '\0';
  223.         strcpy (ret_str, sbuf.name);
  224.         }
  225.  
  226.    return (sbuf.attrib);
  227.     }
  228.  
  229. void get_addr (char *s,int *z,int *net,int *node)
  230. {
  231.    if (sscanf (s, "%d:%d/%d", z, net, node) != 3)
  232.       {
  233.       *z = myzone;
  234.       if (sscanf (s, "%d/%d", net, node) != 2)
  235.          {
  236.          *node = *net;
  237.          *net = mynet;
  238.          }
  239.       }
  240. }
  241.