diff --git a/on-stock.c b/on-stock.c index a622019..ec4d972 100644 --- a/on-stock.c +++ b/on-stock.c @@ -41,7 +41,7 @@ int main(int argc, char* argv[]) if (argc < 2) { - printUsage(argv[0]); + printUsage(argv[0]); return 1; } @@ -49,51 +49,51 @@ int main(int argc, char* argv[]) while ((opt = getopt(argc, argv, "hlsmdnf:")) != -1) { switch (opt) - { + { case 'l': - choice = 'l'; - break; + choice = 'l'; + break; case 's': - choice = 's'; - break; + choice = 's'; + break; case 'm': - choice = 'm'; - break; + choice = 'm'; + break; case 'd': - choice = 'd'; - break; + choice = 'd'; + break; case 'n': - choice = 'n'; - break; - case 'f': - strncpy(filename, optarg, FILEMAXLENGTH-1); - break; - case 'h': - printUsage(argv[0]); - return 0; + choice = 'n'; + break; + case 'f': + strncpy(filename, optarg, FILEMAXLENGTH-1); + break; + case 'h': + printUsage(argv[0]); + return 0; default: - printUsage(argv[0]); + printUsage(argv[0]); return 1; - } + } } /* Check if file exists, if it dosen't, create it */ if ( access(filename, R_OK|W_OK) != 0 ) { - fprintf(stderr, "Could not open %s\n", filename); - printf("Create the file and start adding records? (y/n): "); - create = getchar(); - if ( create == 'y' ) - { - numRec = 1; - struct myData *data; - data = calloc(numRec, sizeof(struct myData)); - new(data, numRec); - free(data); - } - else - return 1; + fprintf(stderr, "Could not open %s\n", filename); + printf("Create the file and start adding records? (y/n): "); + create = getchar(); + if ( create == 'y' ) + { + numRec = 1; + struct myData *data; + data = calloc(numRec, sizeof(struct myData)); + new(data, numRec); + free(data); + } + else + return 1; } /* The structure to contain our database */ @@ -102,43 +102,43 @@ int main(int argc, char* argv[]) /* Continue processesing the command-line arugments */ if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'd' ) { - /* Open file in read-mode */ - FILE *fp = fopen(filename, "rb"); + /* Open file in read-mode */ + FILE *fp = fopen(filename, "rb"); - /* Read in the content from the file to the structure */ - fseek(fp, 0, SEEK_END); - numRec = ftell(fp) / sizeof(struct myData); - data = calloc(numRec, sizeof(struct myData)); - rewind(fp); - fread(data, sizeof(struct myData), numRec, fp); - fclose(fp); - - if ( choice == 'l' ) - list(data, numRec); - else if ( choice == 's' && argc >= 2 ) - search(data, numRec, argv[2]); - else if ( choice == 's' && argc <= 2) - search(data, numRec, NULL); - else if ( choice == 'm' && argc >= 3) - modify(data, numRec, argv[2], argv[3]); - else if ( choice == 'm' && argc <= 4) - modify(data, numRec, NULL, NULL); - else if ( choice == 'd' && argc >= 2 ) - delete(data, numRec, argv[2]); - else if ( choice == 'd' && argc <= 2 ) - delete(data, numRec, NULL); + /* Read in the content from the file to the structure */ + fseek(fp, 0, SEEK_END); + numRec = ftell(fp) / sizeof(struct myData); + data = calloc(numRec, sizeof(struct myData)); + rewind(fp); + fread(data, sizeof(struct myData), numRec, fp); + fclose(fp); + + if ( choice == 'l' ) + list(data, numRec); + else if ( choice == 's' && argc >= 2 ) + search(data, numRec, argv[2]); + else if ( choice == 's' && argc <= 2) + search(data, numRec, NULL); + else if ( choice == 'm' && argc >= 3) + modify(data, numRec, argv[2], argv[3]); + else if ( choice == 'm' && argc <= 4) + modify(data, numRec, NULL, NULL); + else if ( choice == 'd' && argc >= 2 ) + delete(data, numRec, argv[2]); + else if ( choice == 'd' && argc <= 2 ) + delete(data, numRec, NULL); } else if ( choice == 'n' ) { - data = calloc(1, sizeof(struct myData)); - if ( new(data, numRec) == 1 ) - return 1; + data = calloc(1, sizeof(struct myData)); + if ( new(data, numRec) == 1 ) + return 1; } else { - printUsage(argv[0]); - return 1; + printUsage(argv[0]); + return 1; } free(data); @@ -151,10 +151,10 @@ void list(struct myData *datap, int numRec) printHeader(); for (int i = 0; iname, NAMEMAXLENGTH, stdin); - datap->name[strcspn(datap->name, "\n")] = '\0'; - - if ( strcmp(datap->name, "done") == 0 ) - { - fclose(fp); - return 0; - } - printf("Quantity: "); scanf("%d", &datap->quantity); - printf("Price: "); scanf("%f", &datap->price); - bytes = fwrite(datap, sizeof(struct myData), 1, fp); - if (bytes != 1) - { - fprintf(stderr, "Could not write to the file!\n"); - return 1; - } + printf("Name ('done' when finished): "); + fgets(datap->name, NAMEMAXLENGTH, stdin); + datap->name[strcspn(datap->name, "\n")] = '\0'; + + if ( strcmp(datap->name, "done") == 0 ) + { + fclose(fp); + return 0; + } + printf("Quantity: "); scanf("%d", &datap->quantity); + printf("Price: "); scanf("%f", &datap->price); + bytes = fwrite(datap, sizeof(struct myData), 1, fp); + if (bytes != 1) + { + fprintf(stderr, "Could not write to the file!\n"); + return 1; + } } } void printUsage(char *arg) { fprintf(stderr, "Usage: %s [-l] [-s [name]] [-m [name (a/s)quantity]]\n" - "[-d [name]] [-n] [-h] [-f filename]\n\n" - "-l = list the articles in the database\n" - "-s = search for an article in the database\n" + "[-d [name]] [-n] [-h] [-f filename]\n\n" + "-l = list the articles in the database\n" + "-s = search for an article in the database\n" " If no name is given as argument, you will be prompted for a name.\n" - "-m = modify a article\n" + "-m = modify a article\n" " If no name is given as argument, you will be prompted for a\n" " name. You'll then have the choice to change name, quantity and price.\n" " If a name is given as argument, the quantity can be changed from the\n" " command line, such as subtracting the stock by three:\n" " ./on-stock -m 'Nailgun' s3\n" - "-d = delete a article\n" + "-d = delete a article\n" " If no name is given as argument, you will be prompted for an article\n" " to delete.\n" " If a name is given as argument, no confirmation will be required to\n" " delete the article from the database.\n" - "-n = create new articles (interactive mode only)\n" - "-h = display this help message\n" - "-f = specifiy a filename for the database\n", arg); + "-n = create new articles (interactive mode only)\n" + "-h = display this help message\n" + "-f = specifiy a filename for the database\n", arg); } void printHeader(void) @@ -387,6 +387,6 @@ void printHeader(void) printf("%s\t", "Quantity"); printf("%s\t\n", "Price"); for (int i = 0; i<=52; i++) - printf("="); + printf("="); printf("\n"); }