diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-07-05 13:39:15 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-07-05 16:40:56 (EDT) |
commit | 69bf47e45df055d3b8a818b2b8b7657767c9a171 (patch) | |
tree | 3407622bdf40d4120726940b1aced03f70051b1f | |
parent | 40111e108fddaf8b82ff619a2dae77cefc2152db (diff) |
mknod: Factor getenv()/execve()/etc. out of main()
-rw-r--r-- | helpers/mknod.c | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/helpers/mknod.c b/helpers/mknod.c index e6bfea8..23683d8 100644 --- a/helpers/mknod.c +++ b/helpers/mknod.c @@ -71,6 +71,43 @@ _err(const char *fmt, ...) } static int +_get_work_area(int argc, char *argv[], char **work_area) +{ + char **exec_argv; + int arg; + + *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")); + exec_argv = calloc(argc + 1, sizeof(*exec_argv)); + if (exec_argv == NULL) { + _err(_("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")); + free(exec_argv); + return OPKG_OPK_ERROR; + } + strcpy(exec_argv[0], MKNOD); + for (arg = 1; arg < argc; ++arg) { + exec_argv[arg] = argv[arg]; + } + exec_argv[argc] = NULL; + execve(MKNOD, exec_argv, NULL); /* Shouldn't return */ + _err(_("Failed to execute system mknod")); + free(exec_argv[0]); + free(exec_argv); + return OPKG_OPK_ERROR; + } + return OPKG_OPK_OK; +} + +static int _strtol_or_err(const char *str, long int *l) { char *end; @@ -87,8 +124,6 @@ int main(int argc, char *argv[]) { char *work_area; - char **exec_argv; - int arg; mode_t mode; int opt; char *name; @@ -115,32 +150,7 @@ main(int argc, char *argv[]) _program_name = argv[0]; - 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")); - exec_argv = calloc(argc + 1, sizeof(*exec_argv)); - if (exec_argv == NULL) { - _err(_("Failed to allocate memory")); - return EXIT_FAILURE; - } - exec_argv[0] = malloc(strlen(MKNOD) + 1); - if (exec_argv[0] == NULL) { - _err(_("Failed to allocate memory")); - free(exec_argv); - return EXIT_FAILURE; - } - strcpy(exec_argv[0], MKNOD); - for (arg = 1; arg < argc; ++arg) { - exec_argv[arg] = argv[arg]; - } - exec_argv[argc] = NULL; - execve(MKNOD, exec_argv, NULL); /* Shouldn't return */ - _err(_("Failed to execute system mknod")); - free(exec_argv[0]); - free(exec_argv); + if (_get_work_area(argc, argv, &work_area) != OPKG_OPK_OK) { return EXIT_FAILURE; } |