/* A simple inventory tool written in C, by Jack-Benny Persson (jack-benny@cyberinfo.se). Released under GNU GPLv2. */ #define _XOPEN_SOURCE 500 #include #include #include #include /* Macros */ #define NAMEMAXLENGTH 30 /* Globals */ struct myData { char name[NAMEMAXLENGTH]; int quantity; float price; }; char filename[] = "storage.bin"; /* Function prototypes */ void list(struct myData *datap, int numRec); void search(struct myData *datap, int numRec, char *name); void modify(struct myData *datap, int numRec, char *name); void delete(struct myData *datap, int numRec, char *name); int new(struct myData *datap, int numRec); void printUsage(char *arg); void printHeader(void); int main(int argc, char* argv[]) { FILE *fp; int newart = 0; int numRec; int create; int opt; struct myData *data; /* Print help and exit if no arguments are given */ if (argc < 2) { printUsage(argv[0]); return 1; } /* Check file access mode */ 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' ) { /* Start createing records */ numRec = 1; data = calloc(numRec, sizeof(struct myData)); new(data, numRec); free(data); } else return 1; } fp = fopen(filename, "rb"); 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); /* Process arguments with getopt() */ while ((opt = getopt(argc, argv, "hls:m:d:n")) != -1) { switch (opt) { case 'l': list(data, numRec); break; case 's': search(data, numRec, optarg); break; case 'm': modify(data, numRec, optarg); break; case 'd': delete(data, numRec, optarg); break; case 'n': newart = 1; break; case 'h': printUsage(argv[0]); return 0; default: printUsage(argv[0]); return 1; } } /* Continue here */ if ( newart == 1 ) { data = calloc(1, sizeof(struct myData)); if ( new(data, numRec) == 1 ) return 1; } /* Finished */ free(data); return 0; } void list(struct myData *datap, int numRec) { int i; printHeader(); for (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; } } } void printUsage(char *arg) { fprintf(stderr, "Usage: %s [-l] [-s name]" "[-m name \n" "[-d name] [-n] [-h] [-f filename]\n\n" "-l = list the articles in the database\n" "-s name = search for an 'name' in the database\n" "-m name = modify the article named 'name' in the database\n" " You'll then have the choice to change name, quantity " "and price.\n" "-d name = delete the article named 'name'\n" "-n = create new articles (interactive mode only)\n" "-h = display this help message\n", arg); } void printHeader(void) { int i; printf("\n%-30s\t", "Name"); printf("%s\t", "Quantity"); printf("%s\t\n", "Price"); for (i = 0; i<=52; i++) printf("="); printf("\n"); }