diff --git a/on-stock.c b/on-stock.c index 89bd82f..a02a351 100644 --- a/on-stock.c +++ b/on-stock.c @@ -24,9 +24,9 @@ char filename[FILEMAXLENGTH] = "storage.bin"; /* Function prototype */ void list(struct myData *datap, int numRec); -void search(struct myData *datap, int numRec); -void modify(struct myData *datap, int numRec); -void delete(struct myData *datap, int numRec); +void search(struct myData *datap, int numRec, char *name); +void modify(struct myData *datap, int numRec, char *name, char *num); +void delete(struct myData *datap, int numRec, char *name); int new(struct myData *datap, int numRec); void printUsage(char *arg); void printHeader(void); @@ -112,12 +112,18 @@ int main(int argc, char* argv[]) if ( choice == 'l' ) list(data, numRec); - else if ( choice == 's' ) - search(data, numRec); - else if ( choice == 'm' ) - modify(data, numRec); - else if ( choice == 'd' ) - delete(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' ) @@ -149,14 +155,20 @@ void list(struct myData *datap, int numRec) printf("\n"); } -void search(struct myData *datap, int numRec) +void search(struct myData *datap, int numRec, char *name) { char searchword[NAMEMAXLENGTH]; - - printf("Name: "); - fgets(searchword, NAMEMAXLENGTH, stdin); - /* Replace the newline character with a null character */ - searchword[strcspn(searchword, "\n")] = '\0'; + if (name != NULL) + { + strncpy(searchword, name, NAMEMAXLENGTH-1); + } + else + { + printf("Name: "); + fgets(searchword, NAMEMAXLENGTH, stdin); + /* Replace the newline character with a null character */ + searchword[strcspn(searchword, "\n")] = '\0'; + } for (int i = 0; i