summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick 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)
commit351fc50f385f6b6cac0571355b910377bd077781 (patch)
treeb309303c9be9bd6c691f762b3f01d782665dac9a
parent2615b187c5e446bcba965f21def342e313656201 (diff)
mknod: Factor out error functions into common code
-rw-r--r--TODO2
-rw-r--r--common/error.c57
-rw-r--r--common/error.h32
-rw-r--r--helpers/local.mk2
-rw-r--r--helpers/mknod.c88
5 files changed, 124 insertions, 57 deletions
diff --git a/TODO b/TODO
index 0334fea..458948d 100644
--- a/TODO
+++ b/TODO
@@ -1,5 +1,3 @@
-Write `common/error.[ch]`:
- `opkg_opk_set_program_name()`, `opkg_opk_error()`, `opkg_opk_errno()`
Ownership override
Finish options handling, remove cli.txt
Reconsider error handling (enum?), prepend program name to messages (see above)
diff --git a/common/error.c b/common/error.c
new file mode 100644
index 0000000..7b46826
--- /dev/null
+++ b/common/error.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2023 Patrick McDermott
+ *
+ * This file is part of opkg-opk.
+ *
+ * opkg-opk is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * opkg-opk is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with opkg-opk. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "error.h"
+
+static const char *_opkg_opk_program_name;
+
+void
+opkg_opk_set_program_name(const char *program_name)
+{
+ _opkg_opk_program_name = program_name;
+}
+
+void __attribute__((format(__printf__, 1, 2)))
+opkg_opk_error(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s: ", _opkg_opk_program_name);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
+void __attribute__((format(__printf__, 1, 2)))
+opkg_opk_errno(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s: ", _opkg_opk_program_name);
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, ": %s\n", strerror(errno));
+}
diff --git a/common/error.h b/common/error.h
new file mode 100644
index 0000000..3654696
--- /dev/null
+++ b/common/error.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2023 Patrick McDermott
+ *
+ * This file is part of opkg-opk.
+ *
+ * opkg-opk is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * opkg-opk is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with opkg-opk. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef OPKG_OPK_ERROR_H_
+#define OPKG_OPK_ERROR_H_
+
+void
+opkg_opk_set_program_name(const char *program_name);
+
+void __attribute__((format(__printf__, 1, 2)))
+opkg_opk_error(const char *fmt, ...);
+
+void __attribute__((format(__printf__, 1, 2)))
+opkg_opk_errno(const char *fmt, ...);
+
+#endif /* OPKG_OPK_ERROR_H_ */
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;
}