diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-07-06 12:29:35 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-07-06 12:38:09 (EDT) |
commit | 351fc50f385f6b6cac0571355b910377bd077781 (patch) | |
tree | b309303c9be9bd6c691f762b3f01d782665dac9a /helpers | |
parent | 2615b187c5e446bcba965f21def342e313656201 (diff) |
mknod: Factor out error functions into common code
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/local.mk | 2 | ||||
-rw-r--r-- | helpers/mknod.c | 88 |
2 files changed, 35 insertions, 55 deletions
diff --git a/helpers/local.mk b/helpers/local.mk index 2d82b25..aa3c21d 100644 --- a/helpers/local.mk +++ b/helpers/local.mk @@ -1,5 +1,7 @@ helpers_mknod_SOURCES = \ common/defs.h \ + common/error.c \ + common/error.h \ common/mode.c \ common/mode.h \ %reldir%/mknod.c diff --git a/helpers/mknod.c b/helpers/mknod.c index bafe663..07a6fbe 100644 --- a/helpers/mknod.c +++ b/helpers/mknod.c @@ -19,15 +19,14 @@ #include "config.h" -#include <errno.h> #include <fcntl.h> -#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "defs.h" +#include "error.h" #include "mode.h" #ifdef ENABLE_NLS @@ -56,32 +55,6 @@ static const struct option _LONGOPTS[] = { 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 void __attribute__((format(__printf__, 1, 2))) -_errno(const char *fmt, ...) -{ - va_list ap; - - fprintf(stderr, "%s: ", _program_name); - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fprintf(stderr, ": %s\n", strerror(errno)); -} - static int _get_work_area(int argc, char *argv[], char **work_area) { @@ -90,18 +63,19 @@ _get_work_area(int argc, char *argv[], char **work_area) *work_area = getenv("OPK_WORK_AREA"); if (*work_area == NULL || *work_area[0] == '\0') { - _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")); + opkg_opk_error(_("OPK_WORK_AREA environment variable not set")); + opkg_opk_error(_("Possibly building package dependent on " + "opkg-opk with opkbuild too old to use " + "it")); + opkg_opk_error(_("Executing system mknod instead")); exec_argv = calloc(argc + 1, sizeof(*exec_argv)); if (exec_argv == NULL) { - _err(_("Failed to allocate memory")); + opkg_opk_error(_("Failed to allocate memory")); return OPKG_OPK_ERROR; } exec_argv[0] = malloc(strlen(MKNOD) + 1); if (exec_argv[0] == NULL) { - _err(_("Failed to allocate memory")); + opkg_opk_error(_("Failed to allocate memory")); free(exec_argv); return OPKG_OPK_ERROR; } @@ -111,7 +85,7 @@ _get_work_area(int argc, char *argv[], char **work_area) } exec_argv[argc] = NULL; execve(MKNOD, exec_argv, NULL); /* Shouldn't return */ - _err(_("Failed to execute system mknod")); + opkg_opk_error(_("Failed to execute system mknod")); free(exec_argv[0]); free(exec_argv); return OPKG_OPK_ERROR; @@ -126,7 +100,7 @@ _strtol_or_err(const char *str, long int *l) *l = strtol(str, &end, 10); if (*end != '\0') { - _err(_("Invalid number \"%s\""), str); + opkg_opk_error(_("Invalid number \"%s\""), str); return OPKG_OPK_ERROR; } return OPKG_OPK_OK; @@ -161,7 +135,7 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - _program_name = argv[0]; + opkg_opk_set_program_name(argv[0]); if (_get_work_area(argc, argv, &work_area) != OPKG_OPK_OK) { return EXIT_FAILURE; @@ -181,20 +155,21 @@ main(int argc, char *argv[]) mode = 0666 ^ mode; if (opkg_opk_helper_mode_parse(mode, optarg, &mode) != OPKG_OPK_OK) { - _err(_("Invalid mode \"%s\""), optarg); + opkg_opk_error(_("Invalid mode \"%s\""), + optarg); return EXIT_FAILURE; } break; default: - _err(_("Warning: Unknown option: \"%c\""), - optopt); + opkg_opk_error(_("Warning: Unknown option: " + "\"%c\""), optopt); } } argc -= optind; argv += optind; if (argc < 2) { - _err(_("Wrong number of operands")); + opkg_opk_error(_("Wrong number of operands")); return EXIT_FAILURE; } name = argv[0]; @@ -208,21 +183,23 @@ main(int argc, char *argv[]) break; case 'p': if (argc != 2) { - _err(_("Wrong number of operands")); + opkg_opk_error(_("Wrong number of operands")); return EXIT_FAILURE; } if (mkfifo(name, mode) != 0) { - _errno(_("Failed to create link \"%s\""), name); + opkg_opk_errno(_("Failed to create link " + "\"%s\""), name); return EXIT_FAILURE; } return EXIT_SUCCESS; default: - _err(_("Invalid node type \"%c\""), argv[1][0]); + opkg_opk_error(_("Invalid node type \"%c\""), + argv[1][0]); return EXIT_FAILURE; } if (argc != 4) { - _err(_("Wrong number of operands")); + opkg_opk_error(_("Wrong number of operands")); return EXIT_FAILURE; } if (_strtol_or_err(argv[2], &major) != OPKG_OPK_OK || @@ -238,15 +215,16 @@ main(int argc, char *argv[]) } if (strncmp(name, work_area, work_area_len) != 0 || name[work_area_len] != '/') { - _err(_("Node \"%s\" not within work area \"%s\""), + opkg_opk_error(_("Node \"%s\" not within work area \"%s\""), name, work_area); return EXIT_FAILURE; } do { ++work_area_len; if (name[work_area_len] == '\0') { - _err(_("Node \"%s\" not within a binary package data " - "directory"), name); + opkg_opk_error(_("Node \"%s\" not within a binary " + "package data directory"), + name); return EXIT_FAILURE; } } while (name[work_area_len] != '/'); @@ -254,7 +232,7 @@ main(int argc, char *argv[]) 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")); + opkg_opk_error(_("Failed to allocate memory")); return EXIT_FAILURE; } specials_lock_name = specials_file_name + specials_file_name_len ; @@ -266,7 +244,7 @@ main(int argc, char *argv[]) */ fd = open(name, O_CREAT | O_EXCL, mode); if (fd < 0 || close(fd) < 0) { - _errno(_("Failed to create node")); + opkg_opk_errno(_("Failed to create node")); goto err0; } @@ -278,7 +256,7 @@ main(int argc, char *argv[]) goto opened; } } - _errno(_("Failed to lock specials file")); + opkg_opk_errno(_("Failed to lock specials file")); goto err1; opened: @@ -286,27 +264,27 @@ main(int argc, char *argv[]) specials_file = fopen(specials_file_name, "a"); if (specials_file == NULL) { - _errno(_("Failed to open specials file stream")); + opkg_opk_errno(_("Failed to open specials file stream")); goto err2; } /* If "specials" is empty, write header. */ if (fstat(fileno(specials_file), &stat_buf) != 0) { - _errno(_("Failed to stat specials file")); + opkg_opk_errno(_("Failed to stat specials file")); goto err3; } if (stat_buf.st_size == 0) { if (fputs("version=1\n", specials_file) < 0 || fputs("type major minor name\n", specials_file) < 0) { - _errno(_("Failed to write specials file")); + opkg_opk_errno(_("Failed to write specials file")); goto err3; } } if (fprintf(specials_file, "%c %8ld %8ld %s\n", type, major, minor, name + work_area_len) < 0) { - _errno(_("Failed to write specials file")); + opkg_opk_errno(_("Failed to write specials file")); goto err3; } |