home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / sqmurd01.zip / sqmurder.c < prev    next >
C/C++ Source or Header  |  1995-12-18  |  8KB  |  245 lines

  1. /*
  2.  * This program was hacked by Harald Kipp
  3.  *
  4.  * Bug reports and wishlists should be sent to
  5.  *
  6.  *  Fido    : 2:2448/434
  7.  *  Internet: harald@os2point.ping.de
  8.  *
  9.  * You may freely use, copy and/or redistribute this software.
  10.  *
  11.  */
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <io.h>
  17. #include <errno.h>
  18. #include <time.h>
  19.  
  20. #include "getopt.h"
  21.  
  22. static char *version = "0.1";
  23.  
  24. static char line[1024];
  25. static char cmd[1024];
  26. static char squishcfg[_MAX_PATH];
  27. static char area[_MAX_PATH];
  28. static char batch[_MAX_PATH];
  29. static char fixprog[_MAX_PATH];
  30. static char chkprog[_MAX_PATH];
  31. static char logfile[_MAX_PATH];
  32.  
  33. static void usage(void);
  34. static char *logtime(char c);
  35.  
  36. /************************************************************************/
  37. /* SQMURDER                                                             */
  38. /*                                                                      */
  39. /* Simply removes all broken squish areas.                              */
  40. /*                                                                      */
  41. /*                                                                      */
  42. /* Returns   0 if all areas are OK                                      */
  43. /*           1 if all bad areas had been fixed                          */
  44. /*           2 if bad areas had been added to murder.cmd                */
  45. /*           3 if bad areas were not listed in murder.cmd               */
  46. /*         255 on nonrecoverable errors                                 */
  47. /*                                                                      */
  48. /************************************************************************/
  49. int main(int argc, char *argv[])
  50. {
  51.     int result = 0;
  52.     char *key;
  53.     char *tag;
  54.     char *path;
  55.     FILE *fpcfg;
  56.     FILE *fpbat = NULL;
  57.     FILE *fplog = NULL;
  58.     int option;
  59.     int max_errors = 20;
  60.     int bad_areas = 0;
  61.     int fixed_areas = 0;
  62.  
  63.     strcpy(squishcfg, "squish.cfg");
  64.     strcpy(batch, "murder.cmd");
  65.     strcpy(chkprog, "sqinfop.exe");
  66.  
  67.     printf("\n               SqMurder version %s\n", version);
  68.     printf(" written %s by Harald Kipp\n\n", __DATE__);
  69.  
  70.     while((option = getopt(argc,argv,"b:c:e:f:i:l:")) != EOF) {
  71.         switch(option) {
  72.         case 'b':
  73.             strcpy(batch, optarg);
  74.             break;
  75.         case 'c':
  76.             strcpy(squishcfg, optarg);
  77.             break;
  78.         case 'e':
  79.             max_errors = atoi(optarg);
  80.             break;
  81.         case 'f':
  82.             strcpy(fixprog, optarg);
  83.             break;
  84.         case 'i':
  85.             strcpy(chkprog, optarg);
  86.             break;
  87.         case 'l':
  88.             strcpy(logfile, optarg);
  89.             break;
  90.         default:
  91.             usage();
  92.             return(255);
  93.         }
  94.     }
  95.  
  96.     if(logfile[0] && (fplog = fopen(logfile, "a")) == NULL)
  97.         perror(batch);
  98.  
  99.     if(fplog) {
  100.         fprintf(fplog, "\n%sBegin, SqMurder Version %s\n", logtime('+'), version);
  101.         fflush(fplog);
  102.     }
  103.  
  104.     if((fpcfg = fopen(squishcfg, "r")) == NULL) {
  105.         perror(squishcfg);
  106.         if(fplog) {
  107.             fprintf(fplog, "%s Can't read %s\n", logtime('!'), squishcfg);
  108.             fclose(fplog);
  109.         }
  110.         return(255);
  111.     }
  112.  
  113.     if(batch[0] == '+')
  114.         fpbat = fopen(batch + 1, "a");
  115.     else
  116.         fpbat = fopen(batch, "w");
  117.     if(fpbat == NULL) {
  118.         perror(batch);
  119.         if(fplog) {
  120.             fprintf(fplog, "%s Can't open %s\n", logtime('!'), batch);
  121.             fclose(fplog);
  122.         }
  123.     }
  124.     while(fgets(line, sizeof(line), fpcfg)) {
  125.         if((key = strtok(line, " \t\n")) == NULL)
  126.             continue;
  127.         if(stricmp(key, "EchoArea"))
  128.             continue;
  129.         if((tag = strtok(NULL, " \t\n")) == NULL)
  130.             continue;
  131.         if((path = strtok(NULL, " \t\n")) == NULL)
  132.             continue;
  133.  
  134.         sprintf(area, "%s.SQD", path);
  135.         if(access(area, 0))
  136.             continue;
  137.  
  138.         sprintf(cmd, "%s %s -e", chkprog, path);
  139.         if(system(cmd)) {
  140.             if(++bad_areas > max_errors)
  141.                 break;
  142.             if(fixprog[0]) {
  143.                 sprintf(cmd, "%s %s", fixprog, path);
  144.                 if(system(cmd) == 0) {
  145.                     fixed_areas++;
  146.                     if(result < 1)
  147.                         result = 1;
  148.                     if(fplog) {
  149.                         fprintf(fplog, "%s Fixed %s\n", logtime('*'), tag);
  150.                         fflush(fplog);
  151.                     }
  152.                     if(fpbat) {
  153.                         fprintf(fpbat, "del %s.XXD\n", path);
  154.                         fprintf(fpbat, "del %s.XXI\n", path);
  155.                         fprintf(fpbat, "del %s.XXL\n", path);
  156.                         if(fflush(fpbat)) {
  157.                             perror(batch);
  158.                             if(fplog) {
  159.                                 fprintf(fplog, "%s Error writing %s\n", logtime('!'), batch);
  160.                                 fflush(fplog);
  161.                             }
  162.                             break;
  163.                         }
  164.                     }
  165.                     continue;
  166.                 }
  167.             }
  168.             if(fplog) {
  169.                 fprintf(fplog, "%s Bad area %s\n", logtime('#'), tag);
  170.                 fflush(fplog);
  171.             }
  172.             if(fpbat) {
  173.                 fprintf(fpbat, "del %s.SQB\n", path);
  174.                 fprintf(fpbat, "del %s.SQD\n", path);
  175.                 fprintf(fpbat, "del %s.SQI\n", path);
  176.                 fprintf(fpbat, "del %s.SQL\n", path);
  177.                 if(fflush(fpbat)) {
  178.                     perror(batch);
  179.                     if(fplog) {
  180.                         fprintf(fplog, "%s Error writing %s\n", logtime('!'), batch);
  181.                         fflush(fplog);
  182.                     }
  183.                     result = 3;
  184.                     break;
  185.                 }
  186.                 else if(result < 2)
  187.                     result = 2;
  188.             }
  189.             else
  190.                 result = 3;
  191.         }
  192.     }
  193.     fclose(fpcfg);
  194.  
  195.     if(fpbat) {
  196.         if(fclose(fpbat)) {
  197.             if(fplog) {
  198.                 fprintf(fplog, "%s Error writing %s\n", logtime('!'), batch);
  199.                 fflush(fplog);
  200.             }
  201.             result = 3;
  202.         }
  203.     }
  204.  
  205.     if(fplog) {
  206.         fprintf(fplog, "%sEnd. Bad=%d, fixed=%d, result=%d\n", logtime('+'), bad_areas, fixed_areas, result);
  207.         fclose(fplog);
  208.     }
  209.     else
  210.         fprintf(stderr, "Result = %d\n", result);
  211.  
  212.     return(result);
  213. }
  214.  
  215.  
  216. /************************************************************************/
  217. /*                                                                      */
  218. /************************************************************************/
  219. static void usage(void)
  220. {
  221.     fprintf(stderr, "\nusage: sqmurder [options]\n\n"
  222.          "options:\n"
  223.          "  -b<batchname>  name of batch output (murder.cmd)\n"
  224.          "  -c<configfile> name of squish config file (squish.cfg)\n"
  225.          "  -e<number>     maximum number of bad areas (20)\n"
  226.          "  -f<fixprog>    name of program to fix area\n"
  227.          "  -i<infoprog>   name of program to check area (sqinfop.exe)\n"
  228.          "  -l<logfile>    name of the logfile\n");
  229. }
  230.  
  231. /************************************************************************/
  232. /*                                                                      */
  233. /************************************************************************/
  234. static char *logtime(char c)
  235. {
  236.     static char buf[32];
  237.     time_t t;
  238.  
  239.     time(&t);
  240.     buf[0] = c;
  241.     strftime(buf + 1, sizeof(buf) - 1, " %d %b %H:%M:%S MRDR ", localtime(&t));
  242.  
  243.     return(buf);
  244. }
  245.