From 40111e108fddaf8b82ff619a2dae77cefc2152db Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Wed, 05 Jul 2023 13:31:42 -0400 Subject: mknod: Share one buffer between two strings --- diff --git a/helpers/mknod.c b/helpers/mknod.c index a8bee45..e6bfea8 100644 --- a/helpers/mknod.c +++ b/helpers/mknod.c @@ -95,6 +95,7 @@ main(int argc, char *argv[]) char type; long int major; long int minor; + size_t specials_file_name_len; char *specials_file_name; char *specials_lock_name; int fd; @@ -206,19 +207,13 @@ main(int argc, char *argv[]) return EXIT_FAILURE; } - specials_file_name = malloc(strlen(work_area) + strlen("/specials") - + 1 /* "\0" */); + specials_file_name_len = strlen(work_area) + strlen("/specials") + 1; + specials_file_name = malloc(specials_file_name_len * 2 + 1); if (specials_file_name == NULL) { _err(_("Failed to allocate memory")); return EXIT_FAILURE; } - specials_lock_name = malloc(strlen(work_area) + strlen("/specials~") - + 1 /* "\0" */); - if (specials_lock_name == NULL) { - _err(_("Failed to allocate memory")); - free(specials_file_name); - return EXIT_FAILURE; - } + specials_lock_name = specials_file_name + specials_file_name_len ; sprintf(specials_file_name, "%s/specials", work_area); sprintf(specials_lock_name, "%s/specials~", work_area); @@ -229,7 +224,6 @@ main(int argc, char *argv[]) if (fd < 0 || close(fd) < 0) { _err(_("Failed to create node")); free(specials_file_name); - free(specials_lock_name); return EXIT_FAILURE; } @@ -243,7 +237,6 @@ main(int argc, char *argv[]) } _err(_("Failed to lock specials file")); free(specials_file_name); - free(specials_lock_name); unlink(name); return EXIT_FAILURE; @@ -253,7 +246,6 @@ main(int argc, char *argv[]) _err(_("Failed to open specials file stream")); close(specials_fd); free(specials_file_name); - free(specials_lock_name); unlink(name); return EXIT_FAILURE; } @@ -264,7 +256,6 @@ main(int argc, char *argv[]) _err(_("Failed to stat specials file")); fclose(specials_file); free(specials_file_name); - free(specials_lock_name); unlink(name); return EXIT_FAILURE; } @@ -274,7 +265,6 @@ main(int argc, char *argv[]) _err(_("Failed to write specials file")); fclose(specials_file); free(specials_file_name); - free(specials_lock_name); unlink(name); return EXIT_FAILURE; } @@ -284,7 +274,6 @@ main(int argc, char *argv[]) _err(_("Failed to write specials file")); fclose(specials_file); free(specials_file_name); - free(specials_lock_name); unlink(name); return EXIT_FAILURE; } @@ -293,13 +282,11 @@ main(int argc, char *argv[]) if (rename(specials_lock_name, specials_file_name) != 0) { _err(_("Failed to move specials file")); free(specials_file_name); - free(specials_lock_name); unlink(name); return EXIT_FAILURE; } free(specials_file_name); - free(specials_lock_name); return EXIT_SUCCESS; } -- cgit v0.9.1