summaryrefslogtreecommitdiffstats
path: root/helpers
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 12:38:50 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 12:38:50 (EDT)
commit49f4126464df857d9e0d1aa965a97355074bda4d (patch)
tree809870b38105b6f27b1a9ec996e62c2261e1146d /helpers
parent83d5903fdc1732bb4e1f7a395cf37d3edb98c4c5 (diff)
mknod: Factor strtol() out of main()
Diffstat (limited to 'helpers')
-rw-r--r--helpers/mknod.c80
1 files changed, 44 insertions, 36 deletions
diff --git a/helpers/mknod.c b/helpers/mknod.c
index 7328d66..ef7ce98 100644
--- a/helpers/mknod.c
+++ b/helpers/mknod.c
@@ -55,10 +55,25 @@ static const struct option _LONGOPTS[] = {
static const unsigned int _TIMEOUT = 10U;
+static char *_program_name;
+
+static int
+_strtol_or_err(const char *str, long int *l)
+{
+ char *end;
+
+ *l = strtol(str, &end, 10);
+ if (*end != '\0') {
+ fprintf(stderr, _("%s: Invalid number \"%s\"\n"),
+ _program_name, str);
+ return OPKG_OPK_ERROR;
+ }
+ return OPKG_OPK_OK;
+}
+
int
main(int argc, char *argv[])
{
- char *program_name;
char *work_area;
char **exec_argv;
int arg;
@@ -66,7 +81,6 @@ main(int argc, char *argv[])
int opt;
char *name;
char type;
- char *end;
long int major;
long int minor;
char *specials_file_name;
@@ -86,27 +100,27 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif
- program_name = argv[0];
+ _program_name = argv[0];
work_area = getenv("OPK_WORK_AREA");
if (work_area == NULL || work_area[0] == '\0') {
fprintf(stderr, _("%s: OPK_WORK_AREA environment variable not "
- "set\n"), program_name);
+ "set\n"), _program_name);
fprintf(stderr, _("%s: Possibly building package dependent on "
"opkg-opk with opkbuild too old to use "
- "it\n"), program_name);
+ "it\n"), _program_name);
fprintf(stderr, _("%s: Executing system mknod instead\n"),
- program_name);
+ _program_name);
exec_argv = calloc(argc + 1, sizeof(*exec_argv));
if (exec_argv == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
- program_name);
+ _program_name);
return EXIT_FAILURE;
}
exec_argv[0] = malloc(strlen(MKNOD) + 1);
if (exec_argv[0] == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
- program_name);
+ _program_name);
free(exec_argv);
return EXIT_FAILURE;
}
@@ -117,7 +131,7 @@ main(int argc, char *argv[])
exec_argv[argc] = NULL;
execve(MKNOD, exec_argv, NULL); /* Shouldn't return */
fprintf(stderr, _("%s: Failed to execute system mknod\n"),
- program_name);
+ _program_name);
free(exec_argv[0]);
free(exec_argv);
return EXIT_FAILURE;
@@ -139,14 +153,14 @@ main(int argc, char *argv[])
&mode) != OPKG_OPK_OK) {
fprintf(stderr, _("%s: Invalid mode "
"\"%s\"\n"),
- program_name, optarg);
+ _program_name, optarg);
return EXIT_FAILURE;
}
break;
default:
fprintf(stderr, _("%s: Warning: Unknown option:"
" \"%c\"\n"),
- program_name, optopt);
+ _program_name, optopt);
return EXIT_FAILURE;
}
}
@@ -155,7 +169,7 @@ main(int argc, char *argv[])
if (argc < 2) {
fprintf(stderr, _("%s: Wrong number of operands\n"),
- program_name);
+ _program_name);
return EXIT_FAILURE;
}
name = argv[0];
@@ -171,37 +185,29 @@ main(int argc, char *argv[])
if (argc != 2) {
fprintf(stderr, _("%s: Wrong number of "
"operands\n"),
- program_name);
+ _program_name);
return EXIT_FAILURE;
}
if (mkfifo(name, mode) != 0) {
fprintf(stderr, _("%s: Failed to create link "
"\"%s\"\n"),
- program_name, name);
+ _program_name, name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
default:
fprintf(stderr, _("%s: Invalid node type \"%c\"\n"),
- program_name, argv[1][0]);
+ _program_name, argv[1][0]);
return EXIT_FAILURE;
}
if (argc != 4) {
fprintf(stderr, _("%s: Wrong number of operands\n"),
- program_name);
+ _program_name);
return EXIT_FAILURE;
}
- major = strtol(argv[2], &end, 10);
- if (*end != '\0') {
- fprintf(stderr, _("%s: Invalid number \"%s\"\n"),
- program_name, argv[2]);
- return EXIT_FAILURE;
- }
- minor = strtol(argv[3], &end, 10);
- if (*end != '\0') {
- fprintf(stderr, _("%s: Invalid number \"%s\"\n"),
- program_name, argv[3]);
+ if (_strtol_or_err(argv[2], &major) != OPKG_OPK_OK ||
+ _strtol_or_err(argv[3], &minor) != OPKG_OPK_OK) {
return EXIT_FAILURE;
}
@@ -209,14 +215,14 @@ main(int argc, char *argv[])
+ 1 /* "\0" */);
if (specials_file_name == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
- program_name);
+ _program_name);
return EXIT_FAILURE;
}
specials_lock_name = malloc(strlen(work_area) + strlen("/specials~")
+ 1 /* "\0" */);
if (specials_lock_name == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
- program_name);
+ _program_name);
free(specials_file_name);
return EXIT_FAILURE;
}
@@ -228,7 +234,8 @@ main(int argc, char *argv[])
*/
fd = open(name, O_CREAT | O_EXCL, mode);
if (fd < 0 || close(fd) < 0) {
- fprintf(stderr, _("%s: Failed to create node\n"), program_name);
+ fprintf(stderr, _("%s: Failed to create node\n"),
+ _program_name);
free(specials_file_name);
free(specials_lock_name);
return EXIT_FAILURE;
@@ -236,12 +243,13 @@ main(int argc, char *argv[])
/* Acquire lock ("specials~"). */
for (timeout = _TIMEOUT; timeout > 0; --timeout) {
- specials_fd = open(specials_lock_name, O_CREAT | O_EXCL, 0644);
+ specials_fd = open(specials_lock_name,
+ O_CREAT | O_EXCL | O_WRONLY, 0644);
if (specials_fd >= 0) {
goto opened;
}
}
- fprintf(stderr, _("%s: Failed to lock specials file\n"), program_name);
+ fprintf(stderr, _("%s: Failed to lock specials file\n"), _program_name);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
@@ -251,7 +259,7 @@ main(int argc, char *argv[])
specials_file = fdopen(specials_fd, "a");
if (specials_file == NULL) {
fprintf(stderr, _("%s: Failed to open specials file stream\n"),
- program_name);
+ _program_name);
close(specials_fd);
free(specials_file_name);
free(specials_lock_name);
@@ -263,7 +271,7 @@ main(int argc, char *argv[])
if (stat(specials_file_name, &stat_buf) != 0) {
if (errno != ENOENT) {
fprintf(stderr, _("%s: Failed to stat specials file\n"),
- program_name);
+ _program_name);
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
@@ -274,7 +282,7 @@ main(int argc, char *argv[])
fputs("type major minor name\n",
specials_file) < 0) {
fprintf(stderr, _("%s: Failed to write specials file\n")
- , program_name);
+ , _program_name);
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
@@ -285,7 +293,7 @@ main(int argc, char *argv[])
if (fprintf(specials_file, "%c %8ld %8ld %s\n", type, major, minor,
name) < 0) {
fprintf(stderr, _("%s: Failed to write specials file\n"),
- program_name);
+ _program_name);
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
@@ -296,7 +304,7 @@ main(int argc, char *argv[])
if (rename(specials_lock_name, specials_file_name) != 0) {
fprintf(stderr, _("%s: Failed to move specials file\n"),
- program_name);
+ _program_name);
free(specials_file_name);
free(specials_lock_name);
unlink(name);