home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / PLST_133.SZH / EDIT.C < prev    next >
Text File  |  1991-06-26  |  11KB  |  426 lines

  1. #include    <dos.h>
  2. #include    <stdio.h>
  3. #include    <ctype.h>
  4. #include    <stdlib.h>
  5. #include    <math.h>
  6. #include    <malloc.h>
  7. #include    <string.h>
  8.  
  9. #include    "types.h"
  10. #include    "externs.h"
  11.  
  12. #ifndef OS2
  13. #define dir_findfirst(a,b,c) _dos_findfirst(a,b,c)
  14. #define dir_findnext(a) _dos_findnext(a)
  15. #endif
  16.  
  17. /*
  18.  *
  19.  * -rev 04-16-87  (abbreviated)
  20.  *  The CRC-16 routines used by XMODEM, YMODEM, and ZMODEM
  21.  *  are also in this file, a fast table driven macro version
  22.  */
  23.  
  24.  
  25. /* crctab calculated by Mark G. Mendel, Network Systems Corporation */
  26. static unsigned short crctab[256] = {
  27.              0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  28.              0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  29.              0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  30.              0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  31.              0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  32.              0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  33.              0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  34.              0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  35.              0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  36.              0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  37.              0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  38.              0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  39.              0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  40.              0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  41.              0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  42.              0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  43.              0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  44.              0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  45.              0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  46.              0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  47.              0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  48.              0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  49.              0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  50.              0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  51.              0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  52.              0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  53.              0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  54.              0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  55.              0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  56.              0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  57.              0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  58.              0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  59. };
  60.  
  61. /*
  62.  * updcrc macro derived from article Copyright (C) 1986 Stephen Satchell.
  63.  *  NOTE: First srgument must be in range 0 to 255.
  64.  *        Second argument is referenced twice.
  65.  *
  66.  * Programmers may incorporate any or all code into their programs,
  67.  * giving proper credit within the source. Publication of the
  68.  * source routines is permitted so long as proper credit is given
  69.  * to Stephen Satchell, Satchell Evaluations and Chuck Forsberg,
  70.  * Omen Technology.
  71.  */
  72.  
  73. #define updcrc(cp, crc) ( crctab[((crc >> 8) & 255) ^ cp] ^ (crc << 8))
  74.  
  75. char            old_name[13] = "NODELIST.*";
  76. char            new_name[13] = "NODELIST.";
  77. char            diff_name[13] = "NODEDIFF.*";
  78. FILE           *old_file, *diff_file, *new_file;
  79. unsigned        official_crc = 0;
  80. unsigned        calc_crc = 0;   /* Until I find out how to do the CRC */
  81.  
  82. unsigned crcstr (char *buf,unsigned crc)
  83. {
  84.    char           *p;
  85.  
  86.    p = buf;
  87.    fix_up (p);
  88.  
  89.    while (*p)
  90.       {
  91.       crc = updcrc (*p++, crc);
  92.       }
  93.  
  94.    return (crc);
  95. }
  96.  
  97. void fix_up (char *buf)
  98. {
  99.    char           *p;
  100.  
  101.    p = buf + strlen (buf) - 1;
  102.    if (*p == '\n')
  103.       --p;
  104.    if (*p == '\r')
  105.       --p;
  106.    while (*p == ' ')
  107.       --p;
  108.    strcpy (++p, "\r\n");
  109.  
  110.    /* Now get rid of rotten chars */
  111.    p = buf;
  112.    while (*p)
  113.       {
  114.       if (*p >= 128)
  115.          *p = '?';
  116.  
  117.       ++p;
  118.       }
  119. }
  120.  
  121. /* Add NODEDIFF to NODELIST */
  122. void edit ()
  123. {
  124.    int             results;
  125.  
  126.    get_old_name (old_name);
  127.    if (!get_diff_name (diff_name))
  128.    {
  129.       results = open_infiles (old_name, diff_name);
  130.    }
  131.    else
  132.    {
  133.       results = 1;
  134.    }
  135.    if (results == 0)
  136.       add_files ();
  137.    close_files ();
  138.    return;
  139. }
  140.  
  141. void add_files ()                    /* Actually put the new lines in here */
  142. {
  143.    char            old_line[256];
  144.    char            diff_line[256];
  145.    char           *pnt_line;
  146.    char            first_char;
  147.    char           *diff_result;
  148.    char           *old_result;
  149.    char           *ptr;
  150.    int             i, j, k;
  151.    int             first_diff;
  152.  
  153.    first_diff = 1;
  154.  
  155.    pnt_line = (char *) _nmalloc (256);
  156.  
  157.    while ((diff_result = fgets (diff_line, 256, diff_file)) != NULL)
  158.    {
  159.       strcpy (pnt_line, diff_line);
  160.       first_char = diff_line[0];
  161.  
  162.       switch (first_char)
  163.       {
  164.          case ';':
  165.             continue;
  166.  
  167.          case 'D':
  168.             j = atoi (pnt_line + 1);
  169.             for (i = 0; i < j; i++)
  170.             {
  171.                old_result = fgets (old_line, 256, old_file);
  172.             }
  173.             break;
  174.  
  175.          case 'A':
  176.             j = atoi (pnt_line + 1);
  177.             for (i = 0; i < j; i++)
  178.             {
  179.                diff_result = fgets (diff_line, 256, diff_file);
  180.                if (!first_diff)
  181.                   calc_crc = crcstr (diff_line, calc_crc);
  182.                else
  183.                {
  184.                   first_diff = 0;
  185.                   ptr = strrchr (diff_line, ':');
  186.                   if (ptr != NULL)
  187.                   {
  188.                      ++ptr;
  189.                      official_crc = atoi (ptr);
  190.                   }
  191.                }
  192.                k = fputs (diff_line, new_file);
  193.             }
  194.             break;
  195.  
  196.          case 'C':
  197.             j = atoi (pnt_line + 1);
  198.             for (i = 0; i < j; i++)
  199.             {
  200.                diff_result = fgets (old_line, 256, old_file);
  201.                calc_crc = crcstr (old_line, calc_crc);
  202.                k = fputs (old_line, new_file);
  203.             }
  204.             break;
  205.  
  206.          default:
  207.             printf ("\n\tI don't understand this line.\n");
  208.             break;
  209.       }
  210.    }
  211.  
  212.    if (calc_crc != official_crc)
  213.    {
  214.       fputs (";A This nodelist does not have the proper CRC!\r\n", new_file);
  215.       printf ("This nodelist does not have the proper CRC %u/%u!\n",
  216.          calc_crc, official_crc);
  217.       printf ("Please check out the problem and correct it.\n\n");
  218.    }
  219.  
  220.    fprintf (new_file, "%c", '\032');
  221.  
  222.    return;
  223. }
  224.  
  225. void close_files ()
  226. {
  227.    if (old_file != NULL)
  228.       fclose (old_file);
  229.    if (new_file != NULL)
  230.       fclose (new_file);
  231.    if (diff_file != NULL)
  232.       fclose (diff_file);
  233.    return;
  234. }
  235.  
  236. int open_infiles (char *nodelist, char *difflist)
  237. {
  238.    char            first_line[256], *result;
  239.    char            temp[11];
  240.    int             i, j=0;
  241.    struct find_t   c_file;
  242.  
  243. #ifndef OS2
  244.    i = _dos_findfirst (new_name, _A_NORMAL, &c_file);
  245. #else
  246.    i = dir_findfirst (new_name, _A_NORMAL, &c_file);
  247. #endif
  248.  
  249.    if (i == 0)
  250.    {
  251.       printf ("%s already exists.\n", new_name);
  252.       return (1);
  253.    }
  254.  
  255.    if (!(old_file = fopen (nodelist, "rb")))
  256.    {
  257.       printf ("Error opening old nodelist file!\n");
  258.       return (1);
  259.    }
  260.  
  261.    if (!(diff_file = fopen (difflist, "rb")))
  262.    {
  263.       printf ("Error opening nodediff file!\n");
  264.       return (1);
  265.    }
  266.  
  267.    result = fgets (first_line, 256, diff_file);
  268.  
  269.    for (i = 0; i < 80; i++)
  270.       if (first_line[i] == ':')
  271.          j = i;
  272.  
  273.    for (i = 0; i < 10; i++)
  274.       temp[i] = first_line[i + j + 1];
  275.  
  276.    official_crc = atoi (temp);
  277.    j -= 5;
  278.  
  279.    for (i = 0; i < 5; i++)
  280.       temp[i] = first_line[i + j];
  281.  
  282.    temp[5] = '\0';
  283.    j = atoi (temp);
  284.    result = strrchr (nodelist, '.');
  285.    result++;
  286.    i = atoi (result);
  287.    if (i != j)
  288.    {
  289.       printf ("NodeDiff doesn't match to existing Nodelist file! Skipping...\n");
  290.       return (1);
  291.    }
  292.    new_file = fopen (new_name, "wb");
  293.    if (new_file == NULL)
  294.    {
  295.       printf ("Can't open new nodelist file!\n");
  296.       return (1);
  297.    }
  298.    printf ("Applying '%s' to '%s'\n", difflist, nodelist);
  299.    return (0);
  300. }
  301.  
  302. void get_old_name (char *temp_name)
  303. {
  304.    char           *temp;
  305.    int             i=0, j, k;
  306.    struct find_t   c_file;
  307.  
  308.    temp = (char *) _nmalloc (20);
  309.  
  310. #ifndef OS2
  311.    j = _dos_findfirst (temp_name, _A_NORMAL, &c_file);
  312. #else
  313.    j = dir_findfirst (temp_name, _A_NORMAL, &c_file);
  314. #endif
  315.  
  316.    while (j == 0)
  317.    {
  318.       temp = strrchr (c_file.name, '.');
  319.       temp++;
  320.       if (isdigit (*temp) && isdigit (*(temp+1)) && isdigit (*(temp+2)))
  321.       {
  322.          i = atoi (temp);
  323.          strcpy (old_name, c_file.name);
  324.          break;
  325.       }
  326.       else
  327.       {
  328. #ifndef OS2
  329.          j = _dos_findnext (&c_file);
  330. #else
  331.          j = dir_findnext (&c_file);
  332. #endif
  333.       }
  334.    }
  335.  
  336.    while (j == 0)
  337.    {
  338. #ifndef OS2
  339.       j = _dos_findnext (&c_file);
  340. #else
  341.       j = dir_findnext (&c_file);
  342. #endif
  343.       temp = strrchr (c_file.name, '.');
  344.       temp++;
  345.       if (isdigit (*temp) && isdigit (*(temp+1)) && isdigit (*(temp+2)))
  346.       {
  347.          k = atoi (temp);
  348.  
  349.          if ((i < k && (k - i) < 300) ||
  350.              (k < i && (k - i) > 300))
  351.          {
  352.             i = k;
  353.             strcpy (old_name, c_file.name);
  354.          }
  355.       }
  356.    }
  357.    return;
  358. }
  359.  
  360. int get_diff_name (char *temp_name)
  361. {
  362.    char           *temp;
  363.    int             i=0, j, k;
  364.    struct find_t   c_file;
  365.  
  366.    temp = (char *) _nmalloc (80);
  367. #ifndef OS2
  368.    j = _dos_findfirst (temp_name, _A_NORMAL, &c_file);
  369. #else
  370.    j = dir_findfirst (temp_name, _A_NORMAL, &c_file);
  371. #endif
  372.    if (j)
  373.    {
  374.       return (1);
  375.    }
  376.  
  377.    while (j == 0)
  378.    {
  379.       temp = strrchr (c_file.name, '.');
  380.       temp++;
  381.       if (isdigit (*temp) && isdigit (*(temp+1)) && isdigit (*(temp+2)))
  382.       {
  383.          i = atoi (temp);
  384.          strcpy (diff_name, c_file.name);
  385.          break;
  386.       }
  387.       else
  388.       {
  389. #ifndef OS2
  390.          j = _dos_findnext (&c_file);
  391. #else
  392.          j = dir_findnext (&c_file);
  393. #endif
  394.       }
  395.    }
  396.  
  397.    if (j != 0)
  398.       return (1);
  399.  
  400.    while (j == 0)
  401.    {
  402. #ifndef OS2
  403.       j = _dos_findnext (&c_file);
  404. #else
  405.       j = dir_findnext (&c_file);
  406. #endif
  407.       temp = strrchr (c_file.name, '.');
  408.       temp++;
  409.       if (isdigit (*temp) && isdigit (*(temp+1)) && isdigit (*(temp+2)))
  410.       {
  411.          k = atoi (temp);
  412.          if ((i < k && (k - i) < 300) ||
  413.              (k < i && (k - i) > 300))
  414.          {
  415.             i = k;
  416.             strcpy (diff_name, c_file.name);
  417.          }
  418.       }
  419.    }
  420.  
  421.    temp = strrchr (diff_name, '.');
  422.    temp++;
  423.    strcat (new_name, temp);
  424.    return (0);
  425. }
  426.