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