From 2c8c1166d81a5c0fe6f335742ee24319ce60bfa8 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 26 Apr 2023 00:54:48 -0400 Subject: Use static storage * Almost everything returned by the system is immutable * Fixed block_device array initialization and alphabetical sorting * Free memory where possible --- common.h | 4 ++-- darwin.c | 4 ++-- linux.c | 32 +++++++++++++------------------- main.c | 18 ++++++++++++++---- x86.c | 20 +++++--------------- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/common.h b/common.h index 832d85c..9cc3e6b 100644 --- a/common.h +++ b/common.h @@ -32,8 +32,8 @@ union regs_t { }; struct Block_Device { - char path[PATH_MAX]; - char model[254]; + char *path; + char *model; size_t size; }; diff --git a/darwin.c b/darwin.c index 843ee93..4a5986b 100644 --- a/darwin.c +++ b/darwin.c @@ -2,12 +2,12 @@ #include "common.h" char *get_sys_product_darwin() { - char model[100] = {0}; + static char model[100] = {0}; size_t len; len = sizeof(model); sysctlbyname("hw.model", model, &len, NULL, 0); - return strdup(model); + return model; } ssize_t get_sys_memory() { diff --git a/linux.c b/linux.c index 4836955..d518b3d 100644 --- a/linux.c +++ b/linux.c @@ -38,10 +38,10 @@ int get_sys_os_dist(char **name, char **version) { value[strlen(value) - 1] = '\0'; } if (!strcmp(key, "NAME")) { - *name = strdup(value); + (*name) = strdup(value); } if (!strcmp(key, "VERSION") || !strcmp(key, "VERSION_ID") || !strcmp(key, "BUILD_ID")) { - *version = strdup(value); + (*version) = strdup(value); } } fclose(fp); @@ -74,24 +74,16 @@ ssize_t get_sys_memory() { char *get_sys_dmi_product() { FILE *fp; - char *buf; - const int buf_size = 255; - - buf = calloc(buf_size, sizeof(*buf)); - if (!buf) { - return NULL; - } + static char buf[255]; fp = fopen("/sys/class/dmi/id/product_name", "r"); if (!fp) { - free(buf); return NULL; } - if (!fgets(buf, buf_size, fp)) { + if (!fgets(buf, sizeof(buf) - 1, fp)) { perror("Unable to read system vendor"); if (fp != NULL) { - free(buf); fclose(fp); } return NULL; @@ -125,9 +117,10 @@ struct Block_Device **get_block_devices(size_t *total) { } rewinddir(dp); - result = calloc(devices_total + 1, sizeof(result)); - for (size_t d = 0; d < devices_total; d++) { - result[d] = calloc(1, sizeof(*result[0])); + result = calloc(devices_total + 1, sizeof(*result)); + if (!result) { + perror("Unable to allocate device array"); + return NULL; } while ((rec = readdir(dp)) != NULL) { @@ -166,7 +159,7 @@ struct Block_Device **get_block_devices(size_t *total) { // no model file strcpy(device_model, "Unnamed"); } else { - if (!fgets(device_model, sizeof(line) - 1, fp)) { + if (!fgets(device_model, sizeof(device_model) - 1, fp)) { perror("Unable to read device model"); continue; } @@ -174,8 +167,9 @@ struct Block_Device **get_block_devices(size_t *total) { } rstrip(device_model); - strcpy(result[i]->model, device_model); - strncpy(result[i]->path, rec->d_name, sizeof(result[i]->path) - 1); + result[i] = calloc(1, sizeof(*result[0])); + result[i]->model = strdup(device_model); + result[i]->path = strdup(rec->d_name); result[i]->size = device_size; i++; } @@ -184,4 +178,4 @@ struct Block_Device **get_block_devices(size_t *total) { closedir(dp); return result; } -#endif \ No newline at end of file +#endif diff --git a/main.c b/main.c index ca59c79..e173ae5 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,10 @@ #include "common.h" static int cmp_block_device(const void *aa, const void *bb) { - const char *a = ((struct Block_Device *) aa)->path; - const char *b = ((struct Block_Device *) bb)->path; - return strcmp(a, b) == 0; + struct Block_Device *a = *(struct Block_Device **) aa; + struct Block_Device *b = *(struct Block_Device **) bb; + + return strcmp(a->path, b->path); } int main() { @@ -53,7 +54,7 @@ int main() { if (!block_device) { fprintf(stderr, "Unable to enumerate block devices\n"); } else { - qsort(block_device, device_count, sizeof(block_device), cmp_block_device); + qsort(block_device, device_count, sizeof(block_device[0]), cmp_block_device); for (size_t bd = 0; bd < device_count; bd++) { struct Block_Device *p; p = block_device[bd]; @@ -61,5 +62,14 @@ int main() { } } + for (size_t i = 0; i < device_count; i++) { + free(block_device[i]->path); + free(block_device[i]->model); + free(block_device[i]); + } + free(block_device); + free(distro_name); + free(distro_version); + return 0; } diff --git a/x86.c b/x86.c index 647d6ac..e7b2f7e 100644 --- a/x86.c +++ b/x86.c @@ -37,26 +37,21 @@ int is_cpu_virtual() { char *get_sys_product() { union regs_t reg; - char *vendor; + static char vendor[255] = {0}; - vendor = NULL; if (is_cpu_virtual()) { - vendor = calloc(255, sizeof(*vendor)); - if (!vendor) { - return NULL; - } CPUID(0x40000000, ®); strncat(vendor, (char *) ®.bytes[1], sizeof(reg.bytes)); rstrip(vendor); } #if defined(__linux__) if (!vendor || !strlen(vendor)) { - vendor = get_sys_dmi_product(); + strcpy(vendor, get_sys_dmi_product()); rstrip(vendor); } #elif defined(__APPLE__) if (!vendor || !strlen(vendor)) { - vendor = get_sys_product_darwin(); + strcpy(vendor, get_sys_product_darwin()); rstrip(vendor); } #endif @@ -89,13 +84,9 @@ unsigned int get_cpu_count() { char *get_cpu_manufacturer() { union regs_t reg; - char *manufacturer; + static char manufacturer[255] = {0}; CPUID(0, ®); - manufacturer = calloc(sizeof(reg.bytes), sizeof(*reg.bytes)); - if (!manufacturer) { - return NULL; - } strncat(manufacturer, (char *) ®.bytes[1], 4); strncat(manufacturer, (char *) ®.bytes[3], 4); strncat(manufacturer, (char *) ®.bytes[2], 4); @@ -104,9 +95,8 @@ char *get_cpu_manufacturer() { char *get_cpu_vendor() { union regs_t reg; - char *vendor; + static char vendor[255] = {0}; - vendor = calloc(sizeof(reg.bytes) * 3, sizeof(*reg.bytes)); for (unsigned int leaf = 2; leaf < 5; leaf++) { CPUID(0x80000000 + leaf, ®); strncat(vendor, (char *) reg.bytes, sizeof(reg.bytes)); -- cgit