summaryrefslogtreecommitdiffstats
path: root/helpers
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 13:31:42 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 16:40:56 (EDT)
commit40111e108fddaf8b82ff619a2dae77cefc2152db (patch)
treeefa0af97c505ac50edf1c56ea4b1f4fc35b17b06 /helpers
parentaf58538b62920fe0c06b104644813d8e92c55d7c (diff)
mknod: Share one buffer between two strings
Diffstat (limited to 'helpers')
-rw-r--r--helpers/mknod.c21
1 files changed, 4 insertions, 17 deletions
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;
}