home *** CD-ROM | disk | FTP | other *** search
- /* cdf.c - find likely files on CD-ROM */
- /*
- *
- * Copyright (c) 1992 Les Biffle
- * All rights reserved.
- *
- * This program scans index text files on a CD-ROM much like
- * a "grep -i" command would, except that it watches for a
- * line starting "Directory " and remembers the most recent one.
- * Please feel free to distribute this to anyone suffering from
- * "index file overload."
- *
- * The strstr string search routine is used here, so a C compiler
- * with that native, or a self-rolled strstr routine would be needed.
- * I compiled it with MSC 6.00A.
- *
- * -Les Biffle (les@anasazi.com)
- */
-
- #include "stdio.h"
- #include "string.h"
- #include "stdlib.h"
-
- /* Version 1.0, 5 September 1992 */
- #define VERSION "CDF Version 1.0"
- static char *version = "CDF V1.0 9/5/92 - Copyright (c) 1992, Les Biffle";
-
- FILE *indexfile;
- char inline[128];
- char directory[128];
- char pattern[128];
- char *cdroot;
- char indexpath[128];
-
- #define NDEFS 2
- char *defindex[] = {
- "INDEX.TXT",
- "00_INDEX.TXT"
- };
-
- int i, j, len;
- int dirpatlen;
- int fthisdir;
-
- /*ARGSUSED*/
- main (argc, argv)
- int argc;
- char **argv;
- {
-
- if ((argc < 2) || (argc >3)) {
- (void)fprintf(stderr, "Usage: %s [pattern] {index}\n", argv[0]);
- (void)fprintf(stderr, " where: pattern is case-insensitive pattern\n");
- (void)fprintf(stderr, " index is alternative index file name");
- exit(-1);
- }
-
- /* compute a length for later */
- dirpatlen = strlen("Directory ");
-
- /* Copy the pattern to its home and translate to upper-case */
- (void)strcpy(pattern, argv[1]);
- len = strlen(pattern);
- for (i=0; i<len; i++) {
- pattern[i] = toupper(pattern[i]);
- }
-
- /* See where the CD-ROM drive is attached */
- if (NULL == (cdroot = getenv("CDROOT"))) {
- fprintf(stderr, "No CDROOT environmental variable found!\n");
- fprintf(stderr,
- "Before you run cdf, set an environmental variable of CDROOT that will\n");
- fprintf(stderr,
- "contain the full path to the CD-ROM drive root directory. On my\n");
- fprintf(stderr,
- "machine I use \"set CDROOT=G:\\\" in autoexec.bat for this.\n");
-
- exit(-1);
- }
-
- /* Concatenate the index file name to the root specification */
- if (argc > 2) {
- (void)strcpy(indexpath, cdroot);
- (void)strcat(indexpath, argv[2]);
- }
-
- /* See if we can open an index file */
- if (argc > 2) {
- if (NULL == (indexfile = fopen(indexpath, "r"))) {
- (void)fprintf(stderr, "Unable to open specified index: %s", indexpath);
- exit(-1);
- }
- }
-
- /* If they haven't provided an overriding index name, look at our list */
- if (argc == 2) {
- for (i=0; i<NDEFS; i++) {
-
- /* build a pathname */
- (void)strcpy(indexpath, cdroot);
- (void)strcat(indexpath, defindex[i]);
-
- /* We couldn't find the index, check the next */
- if (NULL == (indexfile = fopen(indexpath, "r"))) {
- continue;
- }
- break;
- }
-
- /* If we ran out of names, blow it off */
- if (i == NDEFS) {
- (void)fprintf(stderr, "Unable to open index: %s", indexpath);
- exit(-1);
- }
- }
- (void)printf("%s is checking file %s\n", VERSION, indexpath);
- /*PAGE*/
- /*
- * We have opened the file that is known to contain the index.
- * Now run through it, looking for directories.
- */
-
- for ( ; ; ) {
- /* get a line from the index */
- if (NULL == fgets(inline, sizeof(inline), indexfile)) {
- break;
- }
-
- /* Null-terminate in case we read a full buffer */
- inline[sizeof(inline)-1] = '\0';
-
- /* Skip blank lines */
- if (strlen(inline) < 3) {
- continue;
- }
-
- /* See if it's a directory */
- if (!strncmp(inline, "Directory ", dirpatlen)) {
- (void)strcpy(directory, inline);
- fthisdir = 0;
- } else {
- /* See if the line contains the requested string */
- /* But first, toupper it */
- for (j=0; j<strlen(inline); j++){
- inline[j] = toupper(inline[j]);
- }
-
- /* Now we can test with abandon */
- if (NULL != strstr(inline, pattern)) {
- /* Found it! */
- /* If we haven't printed the directory name, do it now */
- if (!fthisdir) {
- (void)printf("\n%s", directory);
- fthisdir++;
- }
-
- /* Print the line from the index */
- (void)printf("%s", inline);
- }
- }
- }
-
- /* Close the file before we split */
- (void)fclose(indexfile);
-
- } /* main */
-