initctl: refactor list command slightly

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-02-27 19:27:22 +01:00
1 parent 83cdb884d9
commit c7d189f199
1 file changed
+21 -22
+21 -22
View File
@@ -32,21 +32,8 @@
#include "util.h" #include "util.h"
static const char *cwd;
static const char *available = FINIT_RCSD "/available";
static const char *enabled = FINIT_RCSD "/enabled";
extern int icreate; /* initctl -c */ extern int icreate; /* initctl -c */
static void pushd(const char *dir)
{
cwd = get_current_dir_name();
chdir(dir);
}
static void popd(void)
{
chdir(cwd);
}
static int calc_width(char *arr[], size_t len) static int calc_width(char *arr[], size_t len)
{ {
@@ -70,11 +57,9 @@ static void do_list(const char *path)
glob_t gl; glob_t gl;
size_t i; size_t i;
pushd(path); chdir(path);
if (glob("*.conf", 0, NULL, &gl)) { if (glob("*.conf", 0, NULL, &gl))
chdir(cwd);
return; return;
}
if (gl.gl_pathc <= 0) if (gl.gl_pathc <= 0)
goto done; goto done;
@@ -106,15 +91,29 @@ static void do_list(const char *path)
done: done:
globfree(&gl); globfree(&gl);
popd();
} }
int serv_list(char *arg) int serv_list(char *arg)
{ {
if (fisdir(available)) char path[256];
do_list(available);
if (fisdir(enabled)) if (arg && arg[0]) {
do_list(enabled); paste(path, sizeof(path), FINIT_RCSD, arg);
if (fisdir(path)) {
do_list(path);
return 0;
}
/* fall back to list all */
}
paste(path, sizeof(path), FINIT_RCSD, "available");
if (fisdir(path))
do_list(path);
paste(path, sizeof(path), FINIT_RCSD, "enabled");
if (fisdir(path))
do_list(path);
if (fisdir(FINIT_RCSD)) if (fisdir(FINIT_RCSD))
do_list(FINIT_RCSD); do_list(FINIT_RCSD);