home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / EXTR_NET.ZIP / extr_net.c < prev    next >
C/C++ Source or Header  |  2003-03-07  |  25KB  |  817 lines

  1. #define TITLE   "EXTR_NET"      /* Program Name */
  2. #define DATES   "1988-2003"     /* Copyright Date */
  3. #define VERSION "2.30"          /* Version Number */
  4.  
  5. /****************************************************************************
  6.  
  7.    This program is designed to read an input nodelist file and extract
  8.    selected Networks, Regions and/or Zones.  The extracted portions of
  9.    the nodelist are stored in a file specified on the command line.
  10.  
  11.    The format for using this program is as follows:
  12.  
  13.      EXTR_NET [-q] [-i] -s "srcfile" -d "destfile" <net1> [<net2>...<net20>]
  14.  
  15.    where "srcfile"    is the name of the nodelist file to be read
  16.          "destfile"   is the name of the output file created
  17.          -q           operate in "quiet" mode (suppress screen output)
  18.          -i           optionally include all Zone entries
  19.          <net..>      are the numbers of the Net/Region/Zones to be extracted
  20.                       and may include zone specifiers for Nets and Regions
  21.                       (the program is presently set to handle 20 numbers).
  22.                       These numbers may be preceded by a N, R or Z in order
  23.                       to extract the entire Net, Region or Zone.  Examples
  24.                       are: 123 N342 n2:201 r17 Z1
  25.  
  26.    The source and destination files may be specified with a full drive and
  27.    path.  If there are spaces in the name or path, the entry must be enclosed
  28.    in quotes ("").
  29.  
  30.    The inclusion of Zone lines is controlled in two ways and they operate
  31.    differently.  The first way is to use the optional '-i' parameter, which
  32.    will include all Zone lines in the nodelist.  The other way is to use a
  33.    zone-specifier with the search criteria.  This will only include those
  34.    Zone lines which are specified in the search criteria.  For example, if
  35.    your command line looks like this:
  36.  
  37.         extr_net -s nodelist.123 -d net-342.123 -i n342
  38.  
  39.    the output would include all Zone listings as well as all of the entries
  40.    in Net 342 in every zone where Net 342 appears.  If the command line is
  41.    modified as:
  42.  
  43.         extr_net -s nodelist.123 -d net-342.123 n342
  44.  
  45.    the output would not include any Zone listings but would include all of
  46.    the entries in Net 342 in every zone where Net 342 appears.  If the
  47.    command line is further modified as:
  48.  
  49.         extr_net -s nodelist.123 -d net-342.123 n1:342
  50.  
  51.    the output would not include any Zone listings except Zone 1, and would
  52.    include all of the entries for Net 342 in Zone 1.  Note that use of the
  53.    '-i' parameter will always result in the inclusion of all Zone lines.
  54.  
  55.    The program will display a VERY brief set of instructions if it is called
  56.    without any arguments or if it encounters an error.  The following is a
  57.    list of the error codes returned by the program:
  58.  
  59.                   0 - No errors.  Normal termination.
  60.                   1 - Bad or missing command line argument.
  61.                   2 - Unable to open the input file.
  62.                   3 - Unable to open the output file.
  63.                   4 - Problem writing to output file.
  64.                   5 - Problem closing input file.
  65.                   6 - Problem closing output file.
  66.                   7 - No extraction criteria specified.
  67.                  -1 - Help screen (called with -?)
  68.  
  69.    When an error is encountered, the program will exit immediately and will
  70.    attempt to properly close all files.
  71.  
  72.    Although I have chosen to retain all rights to this program, you are free
  73.    to use it under the following conditions:
  74.  
  75.             - You realize that there is NO Warrantee of any sort.
  76.               It was tested pretty thoroughly here before release
  77.               but who knows what bugs may be lurking within.
  78.  
  79.             - You will not modify the code and release a new version
  80.               of the program.  I welcome suggestions for improvement
  81.               (especially when accompanied by code) but I make no
  82.               guarantee of future releases.
  83.  
  84.             - If you find the program useful, I ask that you do
  85.               something to brighten somebody else's day.  Just
  86.               exactly what, I will leave up to you.
  87.  
  88.    You may freely distribute this program provided that you distribute only
  89.    the complete archive.
  90.  
  91.                                                    Bob Swift
  92.                                                    FidoNet: 1:342/5
  93.                                                    Internet: bswift@shaw.ca
  94.  
  95. ****************************************************************************/
  96.  
  97. #ifdef OS2
  98. #define OPSYSTEM "OS/2"
  99. #endif
  100. #ifdef DOS
  101. #define OPSYSTEM "DOS"
  102. #endif
  103. #ifdef WIN
  104. #define OPSYSTEM "Windows"
  105. #endif
  106. #ifdef LINUX
  107. #define OPSYSTEM "Linux"
  108. #endif
  109.  
  110. #include <stdio.h>
  111. #include <string.h>
  112. #include <ctype.h>
  113. #include <time.h>
  114. #ifdef LINUX
  115. #include <unistd.h>
  116. #else
  117. #include <mem.h>
  118. #endif
  119.  
  120. /****************************************************************************
  121. The CRC polynomial. This is used by XMODEM (almost CCITT). If you
  122. change P, you must change crctab[]'s initial value to what is
  123. printed by initcrctab()
  124. ****************************************************************************/
  125. #define   P    0x1021
  126.  
  127. /* number of bits in CRC: don't change it. */
  128. #define W 16
  129.  
  130. /* this the number of bits per char: don't change it. */
  131. #define B 8
  132.  
  133. /* calculated by initcrctab() */
  134. static unsigned short crctab[1 << B] = {
  135.   0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  136.   0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  137.   0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  138.   0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  139.   0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  140.   0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  141.   0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  142.   0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  143.   0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  144.   0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  145.   0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  146.   0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  147.   0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  148.   0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  149.   0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  150.   0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  151.   0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  152.   0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  153.   0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  154.   0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  155.   0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  156.   0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  157.   0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  158.   0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  159.   0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  160.   0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  161.   0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  162.   0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  163.   0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  164.   0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  165.   0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  166.   0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  167. };
  168.  
  169. int cfputs(char *s, FILE * stream);
  170. unsigned short updcrc(unsigned short icrc, char *icp, unsigned int icnt);
  171. void helpscrn(int ernum);
  172. void lf_to_crlf(char s[]);
  173. int min(int, int);
  174. int myfputs(char *s, FILE * stream);
  175. int linetest(char *linetst1, char *inputline);
  176. char version[] = { VERSION };
  177. char title[] = { TITLE };
  178. char dates[] = { DATES };
  179. char opsystem[] = { OPSYSTEM };
  180. unsigned short crc;
  181.  
  182. #define CR 13
  183. #define LF 10
  184.  
  185. /*******************************************************************/
  186.  
  187. int main(int argc, char *argv[])
  188. {
  189.  
  190.   FILE *infile, *outfile;
  191.   char sfilename[255];
  192.   char dfilename[255];
  193.   char inputline[358];
  194.   char lintest[21][20];
  195.   char zontest[21][10];
  196.   char linshow[21][40];
  197.   int lineflag[21];
  198.   char semi[20];
  199.   char temp[1024];
  200.   char spaces[80];
  201.   char curzone[10];
  202.   time_t t;
  203.  
  204. #ifdef LINUX
  205.   long filepos;
  206. #else
  207.   fpos_t filepos;
  208. #endif
  209.   int i, j, k;
  210.   int sub1;
  211.   int count;
  212.   int flag;
  213.   int zoneflag;
  214.   int quiet;
  215.  
  216.   memset(spaces, ' ', 79);
  217.   spaces[79] = '\0';
  218.   strcpy(curzone, "0");
  219.   zoneflag = 0;
  220.   t = time(NULL);
  221.  
  222.   strcpy(sfilename, "");        /* Input File */
  223.   strcpy(dfilename, "");        /* Output File */
  224.  
  225.   quiet = 0;
  226.   sub1 = 0;
  227.  
  228.   if (argc < 2)
  229.     helpscrn(-1);
  230.   for (k = 1; k < argc; k++) {
  231.     if (argv[k][0] == '-' && tolower(argv[k][1]) == 's') {
  232.       k++;
  233.       k = min(k, argc - 1);
  234.       strcpy(sfilename, argv[k]);
  235.     }
  236.     else if (argv[k][0] == '-' && tolower(argv[k][1]) == 'd') {
  237.       k++;
  238.       k = min(k, argc - 1);
  239.       strcpy(dfilename, argv[k]);
  240.     }
  241.     else if (argv[k][0] == '-' && tolower(argv[k][1]) == 'i') {
  242.       sub1++;
  243.       strcpy(lintest[sub1], "I,");
  244.       strcpy(zontest[sub1], "0");
  245.       strcpy(linshow[sub1], "Include all ZONE entries.");
  246.       lineflag[sub1] = 'I';
  247.       zoneflag = 1;
  248.     }
  249.     else if (argv[k][0] == '-' && tolower(argv[k][1]) == 'q') {
  250.       quiet = 1;
  251.     }
  252.     else if (argv[k][0] == '-' && argv[k][1] == '-'
  253.              && tolower(argv[k][2]) == 'h') {
  254.       helpscrn(-1);
  255.     }
  256.     else if (argv[k][0] == '-' && tolower(argv[k][1]) == '?') {
  257.       helpscrn(-1);
  258.     }
  259.     else {
  260.       strcpy(semi, argv[k]);
  261.       switch (semi[0]) {
  262.         case 'n':;
  263.         case 'N':
  264.           sub1++;
  265.           strcpy(lintest[sub1], "Host,");
  266.           strcpy(zontest[sub1], "0");
  267.           strcpy(linshow[sub1], "Net ");
  268.           lineflag[sub1] = 'N';
  269.           i = 0;
  270.           j = 1;
  271.           strcpy(temp, "0");
  272.           while (semi[j] != '\0') {
  273.             temp[i] = semi[j];
  274.             i++;
  275.             j++;
  276.             temp[i] = '\0';
  277.             if (semi[j] == ':') {
  278.               j++;
  279.               strcpy(zontest[sub1], temp);
  280.               i = 0;
  281.               strcpy(temp, "0");
  282.             }
  283.           }
  284.           strcat(lintest[sub1], temp);
  285.           strcat(lintest[sub1], ",");
  286.           strcat(linshow[sub1], temp);
  287.           if (zontest[sub1][0] != '0') {
  288.             strcat(linshow[sub1], "  (Zone ");
  289.             strcat(linshow[sub1], zontest[sub1]);
  290.             strcat(linshow[sub1], ")");
  291.           }
  292.           break;
  293.         case 'r':;
  294.         case 'R':
  295.           sub1++;
  296.           strcpy(lintest[sub1], "Region,");
  297.           strcpy(zontest[sub1], "0");
  298.           strcpy(linshow[sub1], "Region ");
  299.           lineflag[sub1] = 'R';
  300.           i = 0;
  301.           j = 1;
  302.           strcpy(temp, "0");
  303.           while (semi[j] != '\0') {
  304.             temp[i] = semi[j];
  305.             i++;
  306.             j++;
  307.             temp[i] = '\0';
  308.             if (semi[j] == ':') {
  309.               j++;
  310.               strcpy(zontest[sub1], temp);
  311.               i = 0;
  312.               strcpy(temp, "0");
  313.             }
  314.           }
  315.           strcat(lintest[sub1], temp);
  316.           strcat(lintest[sub1], ",");
  317.           strcat(linshow[sub1], temp);
  318.           if (zontest[sub1][0] != '0') {
  319.             strcat(linshow[sub1], "  (Zone ");
  320.             strcat(linshow[sub1], zontest[sub1]);
  321.             strcat(linshow[sub1], ")");
  322.           }
  323.           break;
  324.         case 'z':;
  325.         case 'Z':
  326.           sub1++;
  327.           strcpy(lintest[sub1], "Zone,");
  328.           strcpy(zontest[sub1], "0");
  329.           strcpy(linshow[sub1], "Zone ");
  330.           lineflag[sub1] = 'Z';
  331.           i = 5;
  332.           j = 1;
  333.           strcpy(temp, "0");
  334.           while (semi[j] != '\0') {
  335.             lintest[sub1][i] = semi[j];
  336.             linshow[sub1][i] = semi[j];
  337.             i++;
  338.             j++;
  339.           }
  340.           lintest[sub1][i] = '\0';
  341.           linshow[sub1][i] = '\0';
  342.           strcat(lintest[sub1], ",");
  343.           break;
  344.         default:
  345.         {
  346.           sub1++;
  347.           lineflag[sub1] = 'A';
  348.           strcpy(lintest[sub1], "Zone,");
  349.           strcpy(zontest[sub1], "0");
  350.           strcpy(linshow[sub1], "Zone ");
  351.           i = 0;
  352.           j = 0;
  353.           strcpy(temp, "0");
  354.           while (semi[j] != '\0') {
  355.             temp[i] = semi[j];
  356.             i++;
  357.             j++;
  358.             temp[i] = '\0';
  359.             if (semi[j] == ':') {
  360.               j++;
  361.               strcpy(zontest[sub1], temp);
  362.               i = 0;
  363.               strcpy(temp, "0");
  364.             }
  365.           }
  366.           strcat(lintest[sub1], temp);
  367.           strcat(lintest[sub1], ",");
  368.           strcat(linshow[sub1], temp);
  369.           sub1++;
  370.           lineflag[sub1] = 'A';
  371.           strcpy(lintest[sub1], "Region,");
  372.           strcpy(zontest[sub1], zontest[sub1 - 1]);
  373.           strcpy(zontest[sub1 - 1], "0");
  374.           strcpy(linshow[sub1], "Region ");
  375.           strcat(lintest[sub1], temp);
  376.           strcat(lintest[sub1], ",");
  377.           strcat(linshow[sub1], temp);
  378.           if (zontest[sub1][0] != '0') {
  379.             strcat(linshow[sub1], "  (Zone ");
  380.             strcat(linshow[sub1], zontest[sub1]);
  381.             strcat(linshow[sub1], ")");
  382.           }
  383.           sub1++;
  384.           lineflag[sub1] = 'A';
  385.           strcpy(lintest[sub1], "Host,");
  386.           strcpy(zontest[sub1], zontest[sub1 - 1]);
  387.           strcpy(linshow[sub1], "Net ");
  388.           strcat(lintest[sub1], temp);
  389.           strcat(lintest[sub1], ",");
  390.           strcat(linshow[sub1], temp);
  391.           if (zontest[sub1][0] != '0') {
  392.             strcat(linshow[sub1], "  (Zone ");
  393.             strcat(linshow[sub1], zontest[sub1]);
  394.             strcat(linshow[sub1], ")");
  395.           }
  396.         }
  397.       }
  398.     }
  399.   }
  400.  
  401.   if (sub1 < 1)
  402.     helpscrn(7);
  403.  
  404.   infile = fopen(sfilename, "rt");
  405.   if (infile == NULL)
  406.     helpscrn(2);
  407.  
  408.   if ((outfile = fopen(dfilename, "w+t")) == NULL)
  409.     helpscrn(3);
  410.  
  411.   if (quiet == 0)
  412.     printf("\n%s Version %s for %s\n", title, version, opsystem);
  413.   if (quiet == 0)
  414.     printf("Copyright (c) Bob Swift, %s.  All Rights Reserved.\n", dates);
  415.  
  416.   if (quiet == 0)
  417.     printf("Extracting:   ");   /* Echo Input To User */
  418.   for (count = 1; count <= sub1; count++) {
  419.     if (quiet == 0)
  420.       printf("%s\n              ", linshow[count]);
  421.   }
  422.   if (quiet == 0)
  423.     printf("\n");
  424.  
  425.   if (fgets(inputline, 256, infile) == NULL) {
  426.     strcpy(temp, ";A Extracted Nodelist Segment : 00000\n");
  427.   }
  428.   else {
  429.     strcpy(temp, ";A Extracted ");
  430.     strcat(temp, inputline + 3);
  431.   }
  432.   lf_to_crlf(temp);
  433.   if (myfputs(temp, outfile) == EOF)
  434.     helpscrn(4);
  435.   crc = 0;
  436. #ifdef LINUX
  437.   filepos = ftell(outfile) - 7L;
  438. #else
  439.   if (fgetpos(outfile, &filepos) != 0)
  440.     helpscrn(4);
  441.   filepos = filepos - 7L;
  442. #endif
  443.  
  444.   if (cfputs(";A \n;A Extracted with ", outfile) == EOF)
  445.     helpscrn(4);
  446.   if (cfputs(title, outfile) == EOF)
  447.     helpscrn(4);
  448.   if (cfputs(" Version ", outfile) == EOF)
  449.     helpscrn(4);
  450.   if (cfputs(version, outfile) == EOF)
  451.     helpscrn(4);
  452.   if (cfputs(" for ", outfile) == EOF)
  453.     helpscrn(4);
  454.   if (cfputs(opsystem, outfile) == EOF)
  455.     helpscrn(4);
  456.   if (cfputs("\n;A ", outfile) == EOF)
  457.     helpscrn(4);
  458.   if (cfputs("Copyright (c) Bob Swift, ", outfile) == EOF)
  459.     helpscrn(4);
  460.   if (cfputs(dates, outfile) == EOF)
  461.     helpscrn(4);
  462.   if (cfputs(".  All Rights Reserved.", outfile) == EOF)
  463.     helpscrn(4);
  464.   if (cfputs("\n;A \n;A Extracted from ", outfile) == EOF)
  465.     helpscrn(4);
  466.   i = 0;
  467.   j = 0;
  468.   while (sfilename[i] != '\0') {
  469.     if (sfilename[i] == '/') {
  470.       j = 0;
  471.     }
  472.     else {
  473.       temp[j] = toupper(sfilename[i]);
  474.       j++;
  475.     }
  476.     temp[j] = '\0';
  477.     i++;
  478.   }
  479.  
  480. /****************************************************************************
  481. if (cfputs(sfilename,outfile) == EOF) helpscrn(4);
  482. if (cfputs(" on ",outfile) == EOF) helpscrn(4); 
  483. if (cfputs(ctime(&t),outfile) == EOF) helpscrn(4);
  484. ****************************************************************************/
  485.   if (cfputs(temp, outfile) == EOF)
  486.     helpscrn(4);
  487.   if (cfputs("\n;A \n;A Extracting:     ", outfile) == EOF)
  488.     helpscrn(4);
  489.   if (cfputs(linshow[1], outfile) == EOF)
  490.     helpscrn(4);
  491.   for (count = 2; count <= sub1; count++) {
  492.     if (cfputs("\n;A                 ", outfile) == EOF)
  493.       helpscrn(4);
  494.     if (cfputs(linshow[count], outfile) == EOF)
  495.       helpscrn(4);
  496.   }
  497.   if (cfputs("\n;A \n;\n", outfile) == EOF)
  498.     helpscrn(4);
  499.  
  500.   count = sub1;
  501.   flag = 1;
  502.   j = 0;
  503.  
  504.   while (fgets(inputline, 256, infile) != NULL) {
  505.     if (inputline[0] == 'Z') {
  506.       i = 0;
  507.       for (sub1 = 5;
  508.            (inputline[sub1] != ',') && (sub1 < strlen(inputline)); sub1++) {
  509.         curzone[i] = inputline[sub1];
  510.         i++;
  511.         curzone[i] = '\0';
  512.       }
  513.     }
  514.     strcpy(semi, ";");
  515.     i = linetest(semi, inputline);
  516.     strcpy(semi, ";\n");
  517.     if ((linetest(semi, inputline) == 1 && flag == 0) || i == 0) {
  518.  
  519.       if (flag == 0) {
  520.         strcpy(semi, "Host");
  521.         i = linetest(semi, inputline);
  522.         if (i == 1 && (j == 'N' || j == 'A'))
  523.           flag = 1;
  524.         if (flag == 1)
  525.           if (cfputs(";\n", outfile) == EOF)
  526.             helpscrn(4);
  527.       }
  528.  
  529.       if (flag == 0) {
  530.         strcpy(semi, "Region");
  531.         i = linetest(semi, inputline);
  532.         if (i == 1 && (j == 'N' || j == 'R' || j == 'A'))
  533.           flag = 1;
  534.         if (flag == 1)
  535.           if (cfputs(";\n", outfile) == EOF)
  536.             helpscrn(4);
  537.       }
  538.  
  539.       if (flag == 0) {
  540.         strcpy(semi, "Zone");
  541.         flag = linetest(semi, inputline);
  542.         if (flag == 1)
  543.           if (cfputs(";\n", outfile) == EOF)
  544.             helpscrn(4);
  545.       }
  546.  
  547.       if (flag == 0) {
  548.         if (cfputs(inputline, outfile) == EOF)
  549.           helpscrn(4);
  550.       }
  551.  
  552.       if (flag == 1) {
  553.  
  554. /* Cycle through the search patterns */
  555.         for (sub1 = 1;
  556.              sub1 <= count
  557.              && linetest(lintest[sub1], inputline) == 0; sub1++) {
  558.         }
  559.  
  560.         if (sub1 <= count)
  561.           i = linetest(curzone, zontest[sub1]);
  562.         if ((sub1 <= count)
  563.             && ((zontest[sub1][0] == '0') || (i == 1))) {
  564.           if (cfputs(inputline, outfile) == EOF)
  565.             helpscrn(4);
  566.           flag = 0;
  567.           j = lineflag[sub1];
  568.         }
  569.       }
  570.  
  571.       if (flag == 1) {
  572.  
  573.         strcpy(semi, "Zone");
  574.         if (linetest(semi, inputline) == 1) {
  575.           i = 0;
  576.           for (sub1 = 1; sub1 <= count; sub1++) {
  577.             if (linetest(zontest[sub1], curzone) == 1)
  578.               i = 1;
  579.           }
  580.           if ((i == 1) || (zoneflag == 1)) {
  581.             if (cfputs(inputline, outfile) == EOF)
  582.               helpscrn(4);
  583.             if (cfputs(";\n", outfile) == EOF)
  584.               helpscrn(4);
  585.           }
  586.         }
  587.  
  588.       }
  589.     }
  590.     if (inputline[0] == 'Z' || inputline[0] == 'R'
  591.         || (inputline[0] == 'H' && inputline[2] == 's')) {
  592.       inputline[strlen(inputline) - 1] = '\0';
  593.       i = 0;
  594.       for (sub1 = 0; sub1 <= strlen(inputline) && i < 3; sub1++) {
  595.         if (inputline[sub1] == ',') {
  596.           i++;
  597.           if (i >= 3)
  598.             inputline[sub1] = '\0';
  599.           if (i == 1)
  600.             inputline[sub1] = ' ';
  601.         }
  602.         if (inputline[sub1] == '_')
  603.           inputline[sub1] = ' ';
  604.       }
  605.       strcat(inputline, spaces);
  606.       inputline[79] = '\0';
  607.       if (quiet == 0)
  608.         printf("%s", inputline);
  609.       if (inputline[0] == 'Z') {
  610.         if (quiet == 0)
  611.           printf("\n");
  612.       }
  613.       else {
  614.         if (quiet == 0)
  615.           printf("\r");
  616.       }
  617.     }
  618.   }
  619.   if (cfputs(";S End of extracted file.\n;S \n", outfile) == EOF)
  620.     helpscrn(4);
  621.  
  622.   if (fclose(infile) != 0)
  623.     helpscrn(5);
  624.  
  625. #ifdef LINUX
  626.   if (fseek(outfile, filepos, SEEK_SET) != 0)
  627.     helpscrn(4);
  628. #else
  629.   if (fsetpos(outfile, &filepos) != 0)
  630.     helpscrn(4);
  631. #endif
  632.   fprintf(outfile, "%05u", crc);
  633.   if (quiet == 0)
  634.     printf("%s", spaces);
  635.   if (quiet == 0)
  636.     printf("\nCRC: %u\n", crc);
  637.  
  638.   if (fclose(outfile) != 0)
  639.     helpscrn(6);
  640.  
  641.   if (quiet == 0)
  642.     printf("%s", spaces);
  643.   if (quiet == 0)
  644.     printf("\nExtraction complete.  Thank-you for using %s.\n\n", title);
  645.   exit(0);
  646. }
  647.  
  648. /*******************************************************************/
  649.  
  650. int cfputs(char *s, FILE * stream)
  651. {
  652.   char string[1024];
  653.   int i;
  654.  
  655.   strcpy(string, s);
  656.   lf_to_crlf(string);
  657.   i = 0;
  658.   while (string[i] != '\0') {
  659.     i++;
  660.   }
  661.   crc = updcrc(crc, string, i);
  662.   return (myfputs(string, stream));
  663. }
  664.  
  665. /*******************************************************************/
  666.  
  667. int myfputs(char *s, FILE * stream)
  668. {
  669.   char string[1024];
  670.   int i, j, k;
  671.  
  672.   strcpy(string, s);
  673.   i = 0;
  674.   j = 0;
  675.   k = 0;
  676.   while (string[j] != '\0') {
  677.     i = fputc(string[j], stream);
  678.     j++;
  679.     if (k != EOF) {
  680.       k = i;
  681.     }
  682.   }
  683.   return k;
  684. }
  685.  
  686. /*******************************************************************/
  687.  
  688. int min(one, two)
  689. int one, two;
  690. {
  691.   int three;
  692.  
  693.   three = one;
  694.   if (two < one)
  695.     three = two;
  696.   return (three);
  697. }
  698.  
  699. /*******************************************************************/
  700.  
  701. unsigned short updcrc(unsigned short icrc, char *icp, unsigned int icnt)
  702. {
  703.   register unsigned short crc1 = icrc;
  704.   register char *cp = icp;
  705.   register unsigned int cnt = icnt;
  706.  
  707.   while (cnt--)
  708.     crc1 = (crc1 << B) ^ crctab[(crc1 >> (W - B)) ^ *cp++];
  709.  
  710.   return (crc1);
  711. }
  712.  
  713. /*******************************************************************/
  714.  
  715. void lf_to_crlf(char s[])
  716. {
  717.   char string[1024];
  718.   int i, j;
  719.  
  720.   i = 0;
  721.   j = 0;
  722.   string[0] = '\0';
  723.   while (s[j] != '\0') {
  724.     if (s[j] == CR || s[j] == LF) {
  725.       string[i] = CR;
  726.       i++;
  727.       string[i] = LF;
  728.       i++;
  729.       string[i] = '\0';
  730.       j++;
  731.       while (s[j] == CR || s[j] == LF) {
  732.         j++;
  733.       }
  734.     }
  735.     else {
  736.       string[i] = s[j];
  737.       i++;
  738.       string[i] = '\0';
  739.       j++;
  740.     }
  741.   }
  742.   strcpy(s, string);
  743. }
  744.  
  745. /*******************************************************************/
  746.  
  747. int linetest(char *linetst1, char *inputline)
  748. {
  749.   int j;
  750.  
  751.   for (j = 0; linetst1[j] == inputline[j]; j++)
  752.     if (linetst1[j + 1] == '\0')
  753.       return (1);
  754.   return (0);
  755. }
  756.  
  757. /*******************************************************************/
  758.  
  759. void helpscrn(int ernum)
  760. {
  761.   printf("\n");
  762.   switch (ernum) {
  763.  
  764.     case 1:
  765.       printf("     **  Bad or missing command line argument  **\n\n");
  766.       break;
  767.  
  768.     case 2:
  769.       printf("     **  Unable to open input file  **\n\n");
  770.       break;
  771.  
  772.     case 3:
  773.       printf("     **  Unable to open output file  **\n\n");
  774.       break;
  775.  
  776.     case 4:
  777.       printf("     **  Problem writing to output file  **\n\n");
  778.       break;
  779.  
  780.     case 5:
  781.       printf("     **  Problem closing input file  **\n\n");
  782.       break;
  783.  
  784.     case 6:
  785.       printf("     **  Problem closing output file  **\n\n");
  786.       break;
  787.  
  788.     case 7:
  789.       printf("     **  No extraction criteria specified  **\n\n");
  790.       break;
  791.  
  792.     default:
  793.       printf("%s Version %s for %s\nCopyright ", title, version, opsystem);
  794.       printf("(c) Bob Swift, %s.  All Rights Reserved.\n\n", dates);
  795.       printf("This program is designed to read an input nodelist ");
  796.       printf("file and extract\nselected Networks, Regions and/or ");
  797.       printf("Zones.  The extracted portions of\nthe nodelist are ");
  798.       printf("stored in a file specified on the command line.\n\n");
  799.       printf("Usage: EXTR_NET [-q] [-i] -s \"srcfile\" -d \"destfi");
  800.       printf("le\" <net1> [<net2>...<net20>]\n\n   where \"srcfile");
  801.       printf("\"    is the name of the nodelist file to be read\n");
  802.       printf("         \"destfile\"   is the name of the output ");
  803.       printf("file created\n         -q           operate in \"quiet");
  804.       printf("\" mode (suppress screen output)\n         -i       ");
  805.       printf("    optionally include all Zone entries\n         <n");
  806.       printf("et..>      are the numbers of the Net/Region/Zones to ");
  807.       printf("be extracted\n                      and may include ");
  808.       printf("zone specifiers for Nets and Regions\n              ");
  809.       printf("        (the program is presently set to handle 20 n");
  810.       printf("umbers).\n                      These numbers may be ");
  811.       printf("preceded by a N, R or Z in order\n                  ");
  812.       printf("    to extract the entire Net, Region or Zone.  Exam");
  813.       printf("ples\n                      are: 123 N342 n2:201 r17 Z1\n");
  814.   }
  815.   exit(ernum);
  816. }
  817.