home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / utils / cdf / cdf.c next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  4.4 KB  |  167 lines

  1. /* cdf.c - find likely files on CD-ROM */
  2. /*
  3.  *
  4.  *  Copyright (c) 1992 Les Biffle
  5.  *  All rights reserved.
  6.  *
  7.  *  This program scans index text files on a CD-ROM much like
  8.  *  a "grep -i" command would, except that it watches for a
  9.  *  line starting "Directory " and remembers the most recent one.
  10.  *  Please feel free to distribute this to anyone suffering from
  11.  *  "index file overload."
  12.  *
  13.  *  The strstr string search routine is used here, so a C compiler
  14.  *  with that native, or a self-rolled strstr routine would be needed.
  15.  *  I compiled it with MSC 6.00A.
  16.  *
  17.  *  -Les Biffle  (les@anasazi.com)
  18.  */
  19.  
  20. #include "stdio.h"
  21. #include "string.h"
  22. #include "stdlib.h"
  23.  
  24. /* Version 1.0, 5 September 1992 */
  25. #define VERSION "CDF Version 1.0"
  26. static char *version = "CDF V1.0 9/5/92 - Copyright (c) 1992, Les Biffle";
  27.  
  28. FILE *indexfile;
  29. char inline[128];
  30. char directory[128];
  31. char pattern[128];
  32. char *cdroot;
  33. char indexpath[128];
  34.  
  35. #define NDEFS 2
  36. char *defindex[] = {
  37.     "INDEX.TXT",
  38.     "00_INDEX.TXT"
  39. };
  40.  
  41. int i, j, len;
  42. int dirpatlen;
  43. int fthisdir;
  44.  
  45. /*ARGSUSED*/
  46. main (argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50.  
  51.   if ((argc < 2) || (argc >3)) {
  52.     (void)fprintf(stderr, "Usage: %s [pattern] {index}\n", argv[0]);
  53.     (void)fprintf(stderr, "       where:  pattern is case-insensitive pattern\n");
  54.     (void)fprintf(stderr, "               index is alternative index file name");
  55.     exit(-1);
  56.   }
  57.  
  58.   /* compute a length for later */
  59.   dirpatlen = strlen("Directory ");
  60.   
  61.   /* Copy the pattern to its home and translate to upper-case */
  62.   (void)strcpy(pattern, argv[1]);
  63.   len = strlen(pattern);
  64.   for (i=0; i<len; i++) {
  65.     pattern[i] = toupper(pattern[i]);
  66.   }
  67.   
  68.   /* See where the CD-ROM drive is attached */
  69.   if (NULL == (cdroot = getenv("CDROOT"))) {
  70.     fprintf(stderr, "No CDROOT environmental variable found!\n");
  71.     fprintf(stderr,
  72.     "Before you run cdf, set an environmental variable of CDROOT that will\n");
  73.     fprintf(stderr,
  74.     "contain the full path to the CD-ROM drive root directory.  On my\n");
  75.     fprintf(stderr,
  76.     "machine I use \"set CDROOT=G:\\\" in autoexec.bat for this.\n");
  77.     
  78.     exit(-1);
  79.   }
  80.   
  81.   /* Concatenate the index file name to the root specification */
  82.   if (argc > 2) {
  83.     (void)strcpy(indexpath, cdroot);
  84.     (void)strcat(indexpath, argv[2]);
  85.   }
  86.   
  87.   /* See if we can open an index file */
  88.   if (argc > 2) {
  89.     if (NULL == (indexfile = fopen(indexpath, "r"))) {
  90.       (void)fprintf(stderr, "Unable to open specified index: %s", indexpath);
  91.       exit(-1);
  92.     }
  93.   }
  94.   
  95.   /* If they haven't provided an overriding index name, look at our list */
  96.   if (argc == 2) {
  97.     for (i=0; i<NDEFS; i++) {
  98.     
  99.       /* build a pathname */
  100.       (void)strcpy(indexpath, cdroot);
  101.       (void)strcat(indexpath, defindex[i]);
  102.       
  103.       /* We couldn't find the index, check the next */
  104.       if (NULL == (indexfile = fopen(indexpath, "r"))) {
  105.         continue;
  106.       }
  107.       break;
  108.     }
  109.     
  110.     /* If we ran out of names, blow it off */
  111.     if (i == NDEFS) {
  112.       (void)fprintf(stderr, "Unable to open index: %s", indexpath);
  113.       exit(-1);
  114.     }
  115.   }
  116.   (void)printf("%s is checking file %s\n", VERSION, indexpath);
  117. /*PAGE*/
  118. /*
  119.  * We have opened the file that is known to contain the index.
  120.  * Now run through it, looking for directories.
  121.  */
  122.  
  123.   for ( ; ; ) {
  124.     /* get a line from the index */
  125.     if (NULL == fgets(inline, sizeof(inline), indexfile)) {
  126.       break;
  127.     }
  128.     
  129.     /* Null-terminate in case we read a full buffer */
  130.     inline[sizeof(inline)-1] = '\0';
  131.     
  132.     /* Skip blank lines */
  133.     if (strlen(inline) < 3) {
  134.       continue;
  135.     }
  136.     
  137.     /* See if it's a directory */
  138.     if (!strncmp(inline, "Directory ", dirpatlen)) {
  139.       (void)strcpy(directory, inline);
  140.       fthisdir = 0;
  141.     } else {
  142.       /* See if the line contains the requested string */
  143.       /* But first, toupper it */
  144.       for (j=0; j<strlen(inline); j++){
  145.         inline[j] = toupper(inline[j]);
  146.       }
  147.       
  148.       /* Now we can test with abandon */
  149.       if (NULL != strstr(inline, pattern)) {
  150.         /* Found it! */
  151.         /* If we haven't printed the directory name, do it now */
  152.         if (!fthisdir) {
  153.           (void)printf("\n%s", directory);
  154.           fthisdir++;
  155.         }
  156.       
  157.         /* Print the line from the index */
  158.         (void)printf("%s", inline);
  159.       }
  160.     }
  161.   }
  162.       
  163.   /* Close the file before we split */
  164.   (void)fclose(indexfile);
  165.   
  166. } /* main */
  167.