From 7f8f9ec5113bff8c0f7bd3e9341b688d8af91258 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 2 Sep 2013 18:48:59 -0500 Subject: Cleanup pwdx --- toys/other/pwdx.c | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'toys/other/pwdx.c') diff --git a/toys/other/pwdx.c b/toys/other/pwdx.c index c3f134f7..5be49d62 100644 --- a/toys/other/pwdx.c +++ b/toys/other/pwdx.c @@ -8,33 +8,30 @@ config PWDX bool "pwdx" default y help - usage: pwdx pids ... + usage: pwdx PID... + + Print working directory of processes listed on command line. */ #include "toys.h" -int pid_dir(char *pid) -{ - char *path; - int num_bytes; - - path = xmsprintf("/proc/%s/cwd",pid); - num_bytes = readlink(path,toybuf,sizeof(toybuf)); - if(num_bytes==-1){ - xprintf("%s: %s\n",pid,strerror(errno)); - return 1; - }else{ - toybuf[num_bytes]='\0'; - xprintf("%s: %s\n",pid,toybuf); - return 0; - } -} - void pwdx_main(void) { - int i; - - for (i=0; toys.optargs[i]; i++) - toys.exitval |= pid_dir(toys.optargs[i]); + for (; *toys.optargs; toys.optargs++) { + char *path; + int num_bytes; + + path = xmsprintf("/proc/%s/cwd", *toys.optargs); + num_bytes = readlink(path, toybuf, sizeof(toybuf)-1); + free(path); + + if (num_bytes==-1) { + path = strerror(errno); + toys.exitval = 1; + } else { + path = toybuf; + toybuf[num_bytes] = 0; + } + xprintf("%s: %s\n", *toys.optargs, path); + } } - -- cgit v1.2.3