initctl: enable plain mode for ls and utmp commands

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-03-09 21:30:11 +01:00
parent 905c2678a3
commit b966d031c8
2 changed files with 52 additions and 19 deletions
+27 -16
View File
@@ -95,20 +95,28 @@ static void col_widths(void)
pw = 3;
}
static void print_header(FILE *fp, const char *line, int nl)
void print_header(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (plain) {
int len;
if (!fp)
fp = stdout;
fprintf(fp, "%s%s\n", nl ? "\n" : "", line);
vfprintf(stdout, fmt, ap);
fputs("\n", stdout);
for (len = 0; len < screen_cols; len++)
fputc('=', fp);
fputs("\n", fp);
} else
printheader(fp, line, nl);
fputc('=', stdout);
fputs("\n", stdout);
} else {
char buf[screen_cols];
vsnprintf(buf, sizeof(buf), fmt, ap);
printheader(stdout, buf, 0);
}
va_end(ap);
}
static int runlevel_get(int *prevlevel)
@@ -278,7 +286,7 @@ static int do_cond_dump(char *arg)
snprintf(title, sizeof(title), "%-*s %-*s %-6s %s", pw, "PID",
iw, "IDENT", "STATUS", "CONDITION");
print_header(NULL, title, 0);
print_header(title);
}
if (nftw(_PATH_COND, dump_one_cond, 20, 0) == -1) {
@@ -332,7 +340,7 @@ static int do_cond_show(char *arg)
snprintf(title, sizeof(title), "%-*s %-*s %-6s %s", pw, "PID",
iw, "IDENT", "STATUS", "CONDITION (+ ON, ~ FLUX, - OFF)");
print_header(NULL, title, 0);
print_header(title);
}
for (svc = client_svc_iterator(1); svc; svc = client_svc_iterator(0)) {
@@ -409,15 +417,18 @@ int do_suspend (char *arg) { return do_cmd(INIT_CMD_SUSPEND); }
int utmp_show(char *file)
{
time_t sec;
struct utmp *ut;
static int once = 0;
struct tm *sectm;
struct utmp *ut;
time_t sec;
int pid;
char id[sizeof(ut->ut_id) + 1], user[sizeof(ut->ut_user) + 1], when[80];
if (heading)
print_header(NULL, file, 0);
if (heading) {
print_header("%s%s ", once ? "\n" : "", file);
once++;
}
utmpname(file);
setutent();
@@ -560,7 +571,7 @@ static int show_status(char *arg)
else
strlcat(title, "COMMAND", sizeof(title));
print_header(NULL, title, 0);
print_header(title);
}
for (svc = client_svc_iterator(1); svc; svc = client_svc_iterator(0)) {
+25 -3
View File
@@ -34,6 +34,10 @@
extern int icreate; /* initctl -c */
extern int iforce; /* initctl -f */
extern int plain; /* initctl -p */
extern int heading; /* initctl -t */
extern void print_header(const char *fmt, ...);
static int calc_width(char *arr[], size_t len)
@@ -53,7 +57,7 @@ static int calc_width(char *arr[], size_t len)
static void do_list(const char *path)
{
char buf[screen_cols];
static int once = 0;
int width, num, prev;
glob_t gl;
size_t i;
@@ -67,8 +71,26 @@ static void do_list(const char *path)
if (gl.gl_pathc <= 0)
goto done;
snprintf(buf, sizeof(buf), "%s ", path);
printheader(NULL, buf, 0);
if (plain) {
if (heading)
print_header("%s%s ", once ? "\n" : "", path);
once++;
for (i = 0; i < gl.gl_pathc; i++) {
if (!heading) {
char buf[512];
paste(buf, sizeof(buf), path, gl.gl_pathv[i]);
puts(buf);
} else
puts(gl.gl_pathv[i]);
}
goto done;
}
if (heading)
print_header("%s ", path);
width = calc_width(gl.gl_pathv, gl.gl_pathc);
if (width <= 0)