From 2e9d5017306c760c54030a42d0e1b7128ae49f03 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 13 Jan 2025 16:10:19 -0600 Subject: [PATCH] Move anystr() to lib and have ps.c use it. --- lib/lib.c | 11 +++++++++++ lib/lib.h | 1 + toys/pending/sh.c | 8 -------- toys/posix/ps.c | 11 +++-------- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 75ac51dc..b4ff4f79 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -532,6 +532,17 @@ int anystart(char *s, char **try) return 0; } +// does this entire string match one of the strings in try[]? +// Returns 0 if not, index+1 if so +int anystr(char *s, char **try) +{ + char **and = try; + + while (*try) if (!strcmp(s, *try++)) return try-and; + + return 0; +} + int same_file(struct stat *st1, struct stat *st2) { return st1->st_ino==st2->st_ino && st1->st_dev==st2->st_dev; diff --git a/lib/lib.h b/lib/lib.h index dd18cdf2..e353921b 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -233,6 +233,7 @@ char *strend(char *str, char *suffix); int strstart(char **a, char *b); int strcasestart(char **a, char *b); int anystart(char *s, char **try); +int anystr(char *s, char **try); int same_file(struct stat *st1, struct stat *st2); int same_dev_ino(struct stat *st, struct dev_ino *di); off_t fdlength(int fd); diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 7643892e..921bc449 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -849,14 +849,6 @@ static int utf8chr(char *wc, char *chrs, int *len) return 0; } -// does this entire string match one of the strings in try[] -static int anystr(char *s, char **try) -{ - while (*try) if (!strcmp(s, *try++)) return 1; - - return 0; -} - // Update $IFS cache in function call stack after variable assignment static void cache_ifs(char *s, struct sh_fcall *ff) { diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 0e95c9eb..87dc1af4 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -864,14 +864,9 @@ static int get_ps(struct dirtree *new) while ((line = xgetline(fp))) { if ((s = strstr(line, ":cpuset:/"))) { s += strlen(":cpuset:/"); - if (!*s || !strcmp(s, "foreground")) strcpy(tb->pcy, "fg"); - else if (!strcmp(s, "system-background")) strcpy(tb->pcy, " "); - else if (!strcmp(s, "background")) strcpy(tb->pcy, "bg"); - else if (!strcmp(s, "top-app")) strcpy(tb->pcy, "ta"); - else if (!strcmp(s, "restricted")) strcpy(tb->pcy, "rs"); - else if (!strcmp(s, "foreground_window")) strcpy(tb->pcy, "wi"); - else if (!strcmp(s, "camera-daemon")) strcpy(tb->pcy, "cd"); - else strcpy(tb->pcy, "?"); + sprintf(tb->pcy, "%.2s","? fgfg bgtarswicd"+2*anystr(s, (char *[]){ + "", "foreground", "system-background", "background", "top-app", + "restricted", "foreground_window", "camera-daemon", 0})); } free(line); } -- 2.39.5