summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 13:22:17 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 16:39:43 (EDT)
commitd9cfde3ff8ceef8bf437c7814f865426a8fc22e1 (patch)
tree4654161c104500f7ea95b5f3f9c2788fca529ab3
parent49f4126464df857d9e0d1aa965a97355074bda4d (diff)
mknod: Factor out error printing
-rw-r--r--configure.ac14
-rw-r--r--helpers/mknod.c88
2 files changed, 47 insertions, 55 deletions
diff --git a/configure.ac b/configure.ac
index ad067cc..28e5d94 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,11 +43,12 @@ test -d "${srcdir}/.git" || CFLAGS="${save_CFLAGS}"
funcs_missing=false
AC_CHECK_FUNCS(
[\
- calloc execve fclose fdopen feof ferror fopen fprintf fputs \
- fread free fscanf fseek fwrite getgrgid getpwuid localtime \
- lstat malloc memcmp memcpy memset mkdir mkfifo open printf \
- puts readlink rename scandir sleep snprintf sprintf stat \
- strchr strcmp strcpy strftime strlen strncpy strtol unlink
+ calloc execve fclose fdopen feof ferror fopen fprintf fputc \
+ fputs fread free fscanf fseek fwrite getgrgid getpwuid \
+ localtime lstat malloc memcmp memcpy memset mkdir mkfifo open \
+ printf puts readlink rename scandir sleep snprintf sprintf \
+ stat strchr strcmp strcpy strftime strlen strncpy strtol \
+ unlink vfprintf
],
[],
[funcs_missing=true])
@@ -62,6 +63,9 @@ fi
AC_CHECK_DECLS([[major(dev_t)], [minor(dev_t)]], [],
[AC_MSG_ERROR([required macros are missing])],
[#include <sys/sysmacros.h>])
+AC_CHECK_DECLS([[va_start(ap, last)], [va_end(ap)]], [],
+ [AC_MSG_ERROR([required macros are missing])],
+ [#include <stdarg.h>])
PKG_PROG_PKG_CONFIG()
PKG_CHECK_MODULES([ZLIB], [zlib])
diff --git a/helpers/mknod.c b/helpers/mknod.c
index ef7ce98..58b0b03 100644
--- a/helpers/mknod.c
+++ b/helpers/mknod.c
@@ -21,6 +21,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -57,6 +58,18 @@ static const unsigned int _TIMEOUT = 10U;
static char *_program_name;
+static void __attribute__((format(__printf__, 1, 2)))
+_err(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s: ", _program_name);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
static int
_strtol_or_err(const char *str, long int *l)
{
@@ -64,8 +77,7 @@ _strtol_or_err(const char *str, long int *l)
*l = strtol(str, &end, 10);
if (*end != '\0') {
- fprintf(stderr, _("%s: Invalid number \"%s\"\n"),
- _program_name, str);
+ _err(_("Invalid number \"%s\""), str);
return OPKG_OPK_ERROR;
}
return OPKG_OPK_OK;
@@ -104,23 +116,18 @@ main(int argc, char *argv[])
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);
- fprintf(stderr, _("%s: Possibly building package dependent on "
- "opkg-opk with opkbuild too old to use "
- "it\n"), _program_name);
- fprintf(stderr, _("%s: Executing system mknod instead\n"),
- _program_name);
+ _err(_("OPK_WORK_AREA environment variable not set"));
+ _err(_("Possibly building package dependent on opkg-opk with "
+ "opkbuild too old to use it"));
+ _err(_("Executing system mknod instead"));
exec_argv = calloc(argc + 1, sizeof(*exec_argv));
if (exec_argv == NULL) {
- fprintf(stderr, _("%s: Failed to allocate memory\n"),
- _program_name);
+ _err(_("Failed to allocate memory"));
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);
+ _err(_("Failed to allocate memory"));
free(exec_argv);
return EXIT_FAILURE;
}
@@ -130,8 +137,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);
+ _err(_("Failed to execute system mknod"));
free(exec_argv[0]);
free(exec_argv);
return EXIT_FAILURE;
@@ -151,16 +157,13 @@ main(int argc, char *argv[])
mode = 0666 ^ mode;
if (opkg_opk_helper_mode_parse(mode, optarg,
&mode) != OPKG_OPK_OK) {
- fprintf(stderr, _("%s: Invalid mode "
- "\"%s\"\n"),
- _program_name, optarg);
+ _err(_("Invalid mode \"%s\""), optarg);
return EXIT_FAILURE;
}
break;
default:
- fprintf(stderr, _("%s: Warning: Unknown option:"
- " \"%c\"\n"),
- _program_name, optopt);
+ _err(_("Warning: Unknown option: \"%c\""),
+ optopt);
return EXIT_FAILURE;
}
}
@@ -168,8 +171,7 @@ main(int argc, char *argv[])
argv += optind;
if (argc < 2) {
- fprintf(stderr, _("%s: Wrong number of operands\n"),
- _program_name);
+ _err(_("Wrong number of operands"));
return EXIT_FAILURE;
}
name = argv[0];
@@ -183,27 +185,21 @@ main(int argc, char *argv[])
break;
case 'p':
if (argc != 2) {
- fprintf(stderr, _("%s: Wrong number of "
- "operands\n"),
- _program_name);
+ _err(_("Wrong number of operands"));
return EXIT_FAILURE;
}
if (mkfifo(name, mode) != 0) {
- fprintf(stderr, _("%s: Failed to create link "
- "\"%s\"\n"),
- _program_name, name);
+ _err(_("Failed to create link \"%s\""), name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
default:
- fprintf(stderr, _("%s: Invalid node type \"%c\"\n"),
- _program_name, argv[1][0]);
+ _err(_("Invalid node type \"%c\""), argv[1][0]);
return EXIT_FAILURE;
}
if (argc != 4) {
- fprintf(stderr, _("%s: Wrong number of operands\n"),
- _program_name);
+ _err(_("Wrong number of operands"));
return EXIT_FAILURE;
}
if (_strtol_or_err(argv[2], &major) != OPKG_OPK_OK ||
@@ -214,15 +210,13 @@ main(int argc, char *argv[])
specials_file_name = malloc(strlen(work_area) + strlen("/specials")
+ 1 /* "\0" */);
if (specials_file_name == NULL) {
- fprintf(stderr, _("%s: Failed to allocate memory\n"),
- _program_name);
+ _err(_("Failed to allocate memory"));
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);
+ _err(_("Failed to allocate memory"));
free(specials_file_name);
return EXIT_FAILURE;
}
@@ -234,8 +228,7 @@ 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);
+ _err(_("Failed to create node"));
free(specials_file_name);
free(specials_lock_name);
return EXIT_FAILURE;
@@ -249,7 +242,7 @@ main(int argc, char *argv[])
goto opened;
}
}
- fprintf(stderr, _("%s: Failed to lock specials file\n"), _program_name);
+ _err(_("Failed to lock specials file"));
free(specials_file_name);
free(specials_lock_name);
unlink(name);
@@ -258,8 +251,7 @@ main(int argc, char *argv[])
opened:
specials_file = fdopen(specials_fd, "a");
if (specials_file == NULL) {
- fprintf(stderr, _("%s: Failed to open specials file stream\n"),
- _program_name);
+ _err(_("Failed to open specials file stream"));
close(specials_fd);
free(specials_file_name);
free(specials_lock_name);
@@ -270,8 +262,7 @@ main(int argc, char *argv[])
/* If "specials" doesn't exist, write header. */
if (stat(specials_file_name, &stat_buf) != 0) {
if (errno != ENOENT) {
- fprintf(stderr, _("%s: Failed to stat specials file\n"),
- _program_name);
+ _err(_("Failed to stat specials file"));
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
@@ -281,8 +272,7 @@ main(int argc, char *argv[])
if (fputs("version=1\n", specials_file) < 0 ||
fputs("type major minor name\n",
specials_file) < 0) {
- fprintf(stderr, _("%s: Failed to write specials file\n")
- , _program_name);
+ _err(_("Failed to write specials file"));
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
@@ -292,8 +282,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);
+ _err(_("Failed to write specials file"));
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
@@ -303,8 +292,7 @@ main(int argc, char *argv[])
fclose(specials_file);
if (rename(specials_lock_name, specials_file_name) != 0) {
- fprintf(stderr, _("%s: Failed to move specials file\n"),
- _program_name);
+ _err(_("Failed to move specials file"));
free(specials_file_name);
free(specials_lock_name);
unlink(name);