home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / src / addto.c next >
C/C++ Source or Header  |  1992-03-10  |  3KB  |  134 lines

  1. /* Copyright 1985 Robert W. Baldwin */
  2. /* Copyright 1986 Robert W. Baldwin */
  3. static    char    *notice85 = "Copyright 1985 Robert W. Baldwin";
  4. static    char    *notice86 = "Copyright 1986 Robert W. Baldwin";
  5.  
  6. /*
  7.   August 15, added "Warning!"  To prepend warning messages.
  8.     -- dan farmer
  9. */
  10.  
  11.  
  12. /* 
  13.  * Add a goal, check for duplicates and completions.
  14.  * Trace messages written to stdout, success messages written to stderr.
  15.  * Usage: addto fileroot key comments
  16.  * Files are arranged in families based on a root name; for example,
  17.  *    uids.k  -- uids we Know how to access
  18.  *    uids.n  -- uids to process Next
  19.  *    uids.p  -- uids Pending results (for duplicate detection)
  20.  *    uids.x  -- uids being eXamined currently
  21.  */
  22.  
  23.  
  24. #include    <stdio.h>
  25.  
  26. #define    LINELEN    600        /* Max chars in a line. */
  27. #define    SUCCESS    "Success"    /* Filename to put success messages. */
  28.  
  29. main(argc, argv)
  30. int    argc;
  31. char    *argv[];
  32. {
  33.     char    *type = argv[1];
  34.     char    *key = argv[2];
  35.     int    i;
  36.     char    linebuf[LINELEN];
  37.      char    keypending[150];
  38.     char    filename[150];
  39.     FILE    *tmpfile;
  40.  
  41.     if (argc < 3)  {
  42.         fprintf(stderr, "addto: missing arguments\n");
  43.         exit(1);
  44.         }
  45.         
  46.     tmpfile = NULL;
  47.         
  48.      keypending[0] = NULL;
  49.     strcat(keypending, key);
  50.     strcat(keypending, " ");
  51. /*
  52.  * If the uid is known, print out the comments and exit.
  53.  */
  54.     filename[0] = NULL;
  55.     strcat(filename, type);
  56.     strcat(filename, ".k");
  57.     if ((tmpfile = fopen(filename, "r")) == NULL)  {
  58.         fprintf(stderr, "addto: can't open %s.\n", filename);
  59.         exit(1);
  60.         }
  61.     while (fgets(linebuf, LINELEN, tmpfile) != NULL)  {
  62.         if (strncmp(linebuf, key, strlen(key)) == 0)  {
  63.             if ((tmpfile = freopen(SUCCESS,"a",tmpfile)) == NULL) {
  64.                 fprintf(stderr, "addto: can't open %s.\n",
  65.                     SUCCESS);
  66.                 exit(1);
  67.                 }
  68.             fprintf(stderr, "Success^G^G\t");
  69.             fprintf(tmpfile, "Warning!  ");
  70.             for (i = 1 ; i < argc ; i++)  {
  71.                 fprintf(tmpfile, argv[i]);
  72.                 fprintf(tmpfile, " ");
  73.                 fprintf(stderr, argv[i]);
  74.                 fprintf(stderr, " ");
  75.                 }
  76.             fprintf(tmpfile, "\n");
  77.             fprintf(stderr, "\n");
  78.             
  79.             exit(0);
  80.             }
  81.         }
  82. /*
  83.  * If a duplicate, don't add it.
  84.  */
  85.     filename[0] = NULL;
  86.     strcat(filename, type);
  87.     strcat(filename, ".p");
  88.     if (freopen(filename, "r", tmpfile) == NULL)  {
  89.         fprintf(stderr, "addto: can't open %s.\n", filename);
  90.         exit(1);
  91.         }
  92.     while (fgets(linebuf, LINELEN, tmpfile) != NULL)  {
  93.         if (strncmp(linebuf, keypending, strlen(keypending)) == 0)  {
  94.             exit(0);    /* Its a duplicate. */
  95.             }
  96.         }
  97. /*
  98.  * Add the goal to the pending file. 
  99.  */
  100.     filename[0] = NULL;
  101.     strcat(filename, type);
  102.     strcat(filename, ".p");
  103.     if (freopen(filename, "a", tmpfile) == NULL)  {
  104.         fprintf(stderr,"addto: can't open %s for append.\n", filename);
  105.         exit(1);
  106.         }
  107.     fprintf(tmpfile, keypending);
  108.     fprintf(tmpfile, "\n");
  109. /*
  110.  * Add the goal to the next goal (type) file.
  111.  */
  112.     filename[0] = NULL;
  113.     strcat(filename, type);
  114.     strcat(filename, ".n");
  115.     if (freopen(filename, "a", tmpfile) == NULL)  {
  116.         fprintf(stderr,"addto: can't open %s for append.\n", filename);
  117.         exit(1);
  118.         }
  119.     fprintf(stdout, "        ");
  120.     fprintf(stdout, "%s %s ", argv[0], argv[1]);
  121.     for (i = 2 ; i < argc ; i++)  {
  122.         fprintf(tmpfile, argv[i]);
  123.         fprintf(tmpfile, " ");
  124.         fprintf(stdout, argv[i]);
  125.         fprintf(stdout, " ");
  126.         }
  127.     fprintf(tmpfile, "\n");
  128.     fprintf(stdout, "\n");
  129.     exit(0);
  130. }
  131.  
  132.  
  133.  
  134.