diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2023-04-26 00:54:48 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2023-04-26 00:54:58 -0400 |
commit | 2c8c1166d81a5c0fe6f335742ee24319ce60bfa8 (patch) | |
tree | c58e738644f9595fae67e2ade0ecec35f8b7eca4 /linux.c | |
parent | e71c07b9d5cc1236ca08536ae052faa5fc6b9096 (diff) | |
download | whatami-2c8c1166d81a5c0fe6f335742ee24319ce60bfa8.tar.gz |
Use static storage
* Almost everything returned by the system is immutable
* Fixed block_device array initialization and alphabetical sorting
* Free memory where possible
Diffstat (limited to 'linux.c')
-rw-r--r-- | linux.c | 32 |
1 files changed, 13 insertions, 19 deletions
@@ -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 |