From d9a5736bdfa9db29e94c5431bb8a9bc92a24585c Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 23 Nov 2018 14:13:52 +0100 Subject: Remove some no longer needed cruft Signed-off-by: David Oberhollenzer --- lib/Makemodule.am | 4 +--- lib/include/service.h | 2 ++ lib/include/util.h | 28 ------------------------- lib/init/svcmap.c | 42 ++++++++++++++++++++++--------------- lib/libcfg/rdline.c | 15 +++++++++++--- lib/util/enum_by_name.c | 16 -------------- lib/util/enum_to_name.c | 16 -------------- lib/util/fopenat.c | 54 ------------------------------------------------ lib/util/print_version.c | 18 ---------------- 9 files changed, 40 insertions(+), 155 deletions(-) delete mode 100644 lib/util/enum_by_name.c delete mode 100644 lib/util/enum_to_name.c delete mode 100644 lib/util/fopenat.c delete mode 100644 lib/util/print_version.c (limited to 'lib') diff --git a/lib/Makemodule.am b/lib/Makemodule.am index a60b9f2..258cfe3 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -4,9 +4,7 @@ libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS) -libutil_a_SOURCES = lib/util/argv_exec.c lib/util/enum_by_name.c -libutil_a_SOURCES += lib/util/enum_to_name.c lib/util/print_version.c -libutil_a_SOURCES += lib/util/fopenat.c lib/include/util.h +libutil_a_SOURCES = lib/util/argv_exec.c lib/include/util.h libutil_a_CPPFLAGS = $(AM_CPPFLAGS) libutil_a_CFLAGS = $(AM_CFLAGS) diff --git a/lib/include/service.h b/lib/include/service.h index 594f8ca..e46cd0e 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -19,6 +19,8 @@ enum { it terminates. */ SVC_RESPAWN, + + SVC_MAX }; enum { diff --git a/lib/include/util.h b/lib/include/util.h index 90ca902..8550695 100644 --- a/lib/include/util.h +++ b/lib/include/util.h @@ -15,11 +15,6 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -typedef struct { - const char *name; - int value; -} enum_map_t; - typedef struct exec_t { struct exec_t *next; int argc; /* number of elements in argument vector */ @@ -37,31 +32,8 @@ enum { SOCK_FLAG_DGRAM = 0x04, }; -/* - Search through an array of enum_map_t entries to resolve a string to - a numeric value. The end of the map is indicated by a sentinel entry - with the name set to NULL. -*/ -const enum_map_t *enum_by_name(const enum_map_t *map, const char *name); - -/* - Search through an array of enum_map_t entries to resolve a numeric - value to a string name. The end of the map is indicated by a sentinel - entry with the name set to NULL. -*/ -const char *enum_to_name(const enum_map_t *map, int value); - -/* print a default version info and license string */ -NORETURN void print_version(const char *program); - int setup_tty(const char *tty, bool truncate); NORETURN void argv_exec(exec_t *e); -/* - Similar to openat: opens a file relative to a dirfd, but returns - a FILE pointer instead of an fd. - */ -FILE *fopenat(int fd, const char *filename, const char *mode); - #endif /* UTIL_H */ diff --git a/lib/init/svcmap.c b/lib/init/svcmap.c index fa2932d..6358a4f 100644 --- a/lib/init/svcmap.c +++ b/lib/init/svcmap.c @@ -1,41 +1,49 @@ /* SPDX-License-Identifier: ISC */ +#include #include "service.h" -#include "util.h" -static const enum_map_t type_map[] = { - { "once", SVC_ONCE }, - { "wait", SVC_WAIT }, - { "respawn", SVC_RESPAWN }, - { NULL, 0 }, +static const char *type_map[] = { + "once", + "wait", + "respawn", }; -static const enum_map_t target_map[] = { - { "boot", TGT_BOOT }, - { "shutdown", TGT_SHUTDOWN }, - { "reboot", TGT_REBOOT }, - { NULL, 0 }, +static const char *target_map[] = { + "boot", + "shutdown", + "reboot", }; const char *svc_type_to_string(int type) { - return enum_to_name(type_map, type); + return type >= 0 && type < SVC_MAX ? type_map[type] : NULL; } int svc_type_from_string(const char *type) { - const enum_map_t *ent = enum_by_name(type_map, type); + size_t i; - return ent == NULL ? -1 : ent->value; + for (i = 0; i < ARRAY_SIZE(type_map); ++i) { + if (strcmp(type_map[i], type) == 0) + return i; + } + + return -1; } const char *svc_target_to_string(int target) { - return enum_to_name(target_map, target); + return target >= 0 && target < TGT_MAX ? target_map[target] : NULL; } int svc_target_from_string(const char *target) { - const enum_map_t *ent = enum_by_name(target_map, target); + size_t i; + + for (i = 0; i < ARRAY_SIZE(target_map); ++i) { + if (strcmp(target_map[i], target) == 0) + return i; + } - return ent == NULL ? -1 : ent->value; + return -1; } diff --git a/lib/libcfg/rdline.c b/lib/libcfg/rdline.c index ab7b101..ee9ee8b 100644 --- a/lib/libcfg/rdline.c +++ b/lib/libcfg/rdline.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "libcfg.h" #include "util.h" @@ -12,18 +13,26 @@ int rdline_init(rdline_t *t, int dirfd, const char *filename, int argc, const char *const *argv) { + int fd = openat(dirfd, filename, O_RDONLY); + + if (fd == -1) + goto fail_open; + memset(t, 0, sizeof(*t)); - t->fp = fopenat(dirfd, filename, "r"); + t->fp = fdopen(fd, "r"); if (t->fp == NULL) { - perror(filename); - return -1; + close(fd); + goto fail_open; } t->filename = filename; t->argc = argc; t->argv = argv; return 0; +fail_open: + perror(filename); + return -1; } void rdline_cleanup(rdline_t *t) diff --git a/lib/util/enum_by_name.c b/lib/util/enum_by_name.c deleted file mode 100644 index fa0131b..0000000 --- a/lib/util/enum_by_name.c +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include - -#include "util.h" - -const enum_map_t *enum_by_name(const enum_map_t *map, const char *name) -{ - size_t i; - - for (i = 0; map[i].name != NULL; ++i) { - if (!strcmp(map[i].name, name)) - return map + i; - } - - return NULL; -} diff --git a/lib/util/enum_to_name.c b/lib/util/enum_to_name.c deleted file mode 100644 index 70e3c2f..0000000 --- a/lib/util/enum_to_name.c +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include - -#include "util.h" - -const char *enum_to_name(const enum_map_t *map, int value) -{ - size_t i; - - for (i = 0; map[i].name != NULL; ++i) { - if (map[i].value == value) - return map[i].name; - } - - return NULL; -} diff --git a/lib/util/fopenat.c b/lib/util/fopenat.c deleted file mode 100644 index d47c002..0000000 --- a/lib/util/fopenat.c +++ /dev/null @@ -1,54 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include -#include -#include -#include -#include -#include - -#include "util.h" - -FILE *fopenat(int dirfd, const char *filename, const char *mode) -{ - const char *ptr = mode; - int fd, flags = 0; - FILE *fp; - - switch (*(ptr++)) { - case 'r': - flags = O_RDONLY; - break; - case 'w': - flags = O_WRONLY | O_CREAT | O_TRUNC; - break; - case 'a': - flags = O_WRONLY | O_CREAT | O_APPEND; - break; - default: - errno = EINVAL; - return NULL; - } - - if (*ptr == '+') { - flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; - ++ptr; - } - - if (*ptr == 'b') - ++ptr; - - if (*ptr != '\0') { - errno = EINVAL; - return NULL; - } - - fd = openat(dirfd, filename, flags, 0644); - if (fd == -1) - return NULL; - - fp = fdopen(fd, mode); - if (fp == NULL) - close(fd); - - return fp; -} diff --git a/lib/util/print_version.c b/lib/util/print_version.c deleted file mode 100644 index 47508b5..0000000 --- a/lib/util/print_version.c +++ /dev/null @@ -1,18 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include -#include - -#include "config.h" -#include "util.h" - -static const char *version_string = -"%s (pygos init) " PACKAGE_VERSION "\n" -"Copyright (C) 2018 David Oberhollenzer\n\n" -"This is free software: you are free to change and redistribute it.\n" -"There is NO WARRANTY, to the extent permitted by law.\n"; - -void print_version(const char *program) -{ - fprintf(stdout, version_string, program); - exit(EXIT_SUCCESS); -} -- cgit v1.2.3