#include #include #include #include #include "btree.h" #define DEFAULTBEG 0 #define DEFAULTEND -1 #define DEFAULTFILENAME "/usr/local/csci/343/testdata/prog01.bt" main(int argc, char *argv[]) { int rc ; int errflg = 0 ; char *begArg = NULL ; char *endArg = NULL ; char *filArg = NULL ; char *recArg = NULL ; char *atoiHack = NULL ; int begPos ; int endPos ; FILE * dbFile ; int dbLen ; char usage[] = "ezsearch [-b start-pos] [-e end-pos] [-f file-name] record-key\n" ; simpRecT buff ; optarg = NULL ; while ((rc = getopt(argc, argv, "b:e:f:")) != -1) switch (rc) { case 'b': begArg = optarg ; break ; case 'e': endArg = optarg ; break ; case 'f': filArg = optarg ; break ; default: errflg = 1 ; } if (optind == argc-1) recArg = argv[optind] ; else errflg = 1 ; if (errflg) { (void) fputs(usage, stderr) ; exit(1) ; } errno = 0 ; if (begArg == NULL) begPos = DEFAULTBEG ; else { begPos = (int) strtol(begArg, &atoiHack, 10) ; if (begPos<0 || errno || atoiHack[0]) { (void) fprintf(stderr, "Bad option for -b: %s\n", begArg) ; errflg = 1 ; errno = 0 ; } } if (endArg == NULL) endPos = DEFAULTEND ; else { endPos = (int) strtol(endArg, &atoiHack, 10) ; if (endPos<0 || errno || atoiHack[0]) { (void) fprintf(stderr, "Bad option for -e: %s\n", endArg) ; errflg = 1 ; errno = 0 ; } } if (endPos >= 0 && (begPos>endPos)) { (void) fprintf(stdout, "-b option (%d) greater than -e option (%d)\n", begPos, endPos) ; errflg = 1 ; } if (filArg == NULL) filArg = DEFAULTFILENAME ; if (errflg) exit(1) ; if ((dbFile = fopen(filArg, "r")) == (FILE *)NULL) { (void) fprintf(stderr, "Unable to open %s\n", filArg) ; perror("fopen") ; exit(1) ; } if (fseek(dbFile, 0L, SEEK_END)) { /* very, very unlikely */ (void) fprintf(stderr, "Unable to seek %s\n", filArg) ; perror("fseek") ; exit(1) ; } dbLen = ftell(dbFile)/sizeof(simpRecT) ; rewind(dbFile) ; if (begPos >= dbLen) { (void) fprintf(stderr, "-b option (%d) exceeds records in file\n",begPos) ; exit(1) ; } if (endPos == -1) endPos = dbLen-1 ; else if (endPos > dbLen) { (void) fprintf(stderr, "-e option (%d) exceeds records in file\n",endPos) ; exit(1) ; } (void) fprintf(stdout, "Searching for %s\n", recArg); (void) fprintf(stdout, " in %s\n", filArg) ; (void) fprintf(stdout, " from %d to %d\n\n", begPos, endPos) ; if (rangeSearch(recArg, dbFile, begPos, endPos, &buff)) (void) fprintf(stdout, "%.12s found with key %d (%08x)\n", buff.name, buff.value, buff.value) ; else (void) fputs("Record not found!\n", stdout) ; (void) fclose(dbFile) ; }