Untitled
unknown
plain_text
a year ago
5.2 kB
2
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> struct Item { int id; char name[100]; char model[100]; char config[100]; char hash[100]; }; void createCustomDirectory(const char *directoryName) { char documentsPath[200]; const char *homeDirectory = getenv("HOME"); if (homeDirectory == NULL) { fprintf(stderr, "ERROR: Unable to get user's home directory\n"); exit(1); } snprintf(documentsPath, sizeof(documentsPath), "%s/Documents", homeDirectory); char customDirPath[250]; snprintf(customDirPath, sizeof(customDirPath), "%s/%s", documentsPath, directoryName); char *dir = customDirPath; while ((dir = strchr(dir + 1, '/')) != NULL) { *dir = '\0'; if (access(customDirPath, F_OK) == -1) { if (mkdir(customDirPath, 0700) == -1) { fprintf(stderr, "ERROR: Unable to create custom directory\n"); exit(1); } } *dir = '/'; } if (access(customDirPath, F_OK) == -1) { if (mkdir(customDirPath, 0700) == -1) { fprintf(stderr, "ERROR: Unable to create custom directory\n"); exit(1); } } } void displayMenu() { printf("Menu:\n"); printf("1. Add Item\n"); printf("2. View Items\n"); printf("3. Search Item by ID\n"); printf("4. Save Items to .snet File\n"); printf("5. Exit Program\n"); } void addItem(struct Item **items, int *itemCount) { struct Item item; printf("Enter Item ID: "); scanf("%d", &item.id); printf("Enter Item Name: "); scanf("%s", item.name); printf("Enter Item Model: "); scanf("%s", item.model); printf("Enter Item Config: "); scanf("%s", item.config); printf("Enter Item Hash: "); scanf("%s", item.hash); (*itemCount)++; *items = (struct Item *)realloc(*items, (*itemCount) * sizeof(struct Item)); if (*items == NULL) { fprintf(stderr, "ERROR: Unable to allocate memory\n"); exit(1); } (*items)[(*itemCount) - 1] = item; printf("SUCCESS: Item added.\n"); } int compareItems(const void *a, const void *b) { return ((struct Item *)a)->id - ((struct Item *)b)->id; } void viewItems(struct Item *items, int itemCount) { if (itemCount == 0) { printf("ERROR: No items to display\n"); return; } qsort(items, itemCount, sizeof(struct Item), compareItems); printf("Items:\n"); printf("ID\tName\tModel\tConfig\tHash\n"); for (int i = 0; i < itemCount; i++) { printf("%d\t%s\t%s\t%s\t%s\n", items[i].id, items[i].name, items[i].model, items[i].config, items[i].hash); } } void searchItemByID(struct Item *items, int itemCount, int searchID) { for (int i = 0; i < itemCount; i++) { if (items[i].id == searchID) { printf("SUCCESS: Item Found\n"); printf("ID\tName\tModel\tConfig\tHash\n"); printf("%d\t%s\t%s\t%s\t%s\n", items[i].id, items[i].name, items[i].model, items[i].config, items[i].hash); return; } } printf("ERROR: Item with ID %d not found\n", searchID); } void saveItemsToFile(struct Item *items, int itemCount, const char *fileName) { FILE *file = fopen(fileName, "w"); if (file == NULL) { fprintf(stderr, "ERROR: Unable to open file for writing: %s\n", fileName); return; } for (int i = 0; i < itemCount; i++) { fprintf(file, "%d\t%s\t%s\t%s\t%s\n", items[i].id, items[i].name, items[i].model, items[i].config, items[i].hash); } fclose(file); printf("SUCCESS: Items have been saved to %s\n", fileName); } int main() { const char *customDirectoryName = "Documents/Projects/bcs1_c_l_hello_world_1"; createCustomDirectory(customDirectoryName); struct Item *items = NULL; int itemCount = 0; int choice; char fileName[110]; do { displayMenu(); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: addItem(&items, &itemCount); break; case 2: viewItems(items, itemCount); break; case 3: printf("Enter ID to search: "); int searchID; scanf("%d", &searchID); searchItemByID(items, itemCount, searchID); break; case 4: printf("Enter file name to save (.snet extension will be added): "); scanf("%s", fileName); strcat(fileName, ".snet"); saveItemsToFile(items, itemCount, fileName); break; case 5: printf("Exiting program...\n"); break; default: printf("ERROR: Invalid choice\n"); break; } } while (choice != 5); free(items); return 0; }