aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-07-10 14:52:14 -0500
committerRob Landley <rob@landley.net>2015-07-10 14:52:14 -0500
commit6292beb86fa9a3999f0f0b76185e0a893af85cd0 (patch)
tree047be7bae09f50534ea5d73c586c060e1ad50fac /lib
parent68771c00530622b5bc52438ae8010c7a6f429791 (diff)
downloadtoybox-6292beb86fa9a3999f0f0b76185e0a893af85cd0.tar.gz
Add a basename_r() and use it in names_to_pid() to avoid basename() overwriting
itself on some inputs. (Which makes killall really impolite. Bug report from Nicholas Noury via Elliott Hughes.)
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c10
-rw-r--r--lib/lib.h1
2 files changed, 10 insertions, 1 deletions
diff --git a/lib/lib.c b/lib/lib.c
index ea65f77a..05e377f8 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -798,6 +798,14 @@ void mode_to_string(mode_t mode, char *buf)
*buf = c;
}
+char *basename_r(char *name)
+{
+ char *s = strrchr(name, '/');
+
+ if (s) return s+1;
+ return name;
+}
+
// Execute a callback for each PID that matches a process name from a list.
void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
{
@@ -816,7 +824,7 @@ void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
for (curname = names; *curname; curname++)
if (**curname == '/' ? !strcmp(cmd, *curname)
- : !strcmp(basename(cmd), basename(*curname)))
+ : !strcmp(basename_r(cmd), basename_r(*curname)))
if (callback(u, *curname)) break;
if (*curname) break;
}
diff --git a/lib/lib.h b/lib/lib.h
index 92bd6b5d..3183f32a 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -235,6 +235,7 @@ char *num_to_sig(int sig);
mode_t string_to_mode(char *mode_str, mode_t base);
void mode_to_string(mode_t mode, char *buf);
+char *basename_r(char *name);
void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
// Functions in need of further review/cleanup