changeset 1039:75f5e63d79c3 draft

Cleanup pwdx
author Rob Landley <rob@landley.net>
date Mon, 02 Sep 2013 18:48:59 -0500
parents 5b6027634d01
children 7dfad5f21984
files toys/other/pwdx.c
diffstat 1 files changed, 19 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/toys/other/pwdx.c	Mon Sep 02 05:06:05 2013 -0500
+++ b/toys/other/pwdx.c	Mon Sep 02 18:48:59 2013 -0500
@@ -8,33 +8,30 @@
   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)
+void pwdx_main(void)
 {
-  char *path;
-  int num_bytes;
+  for (; *toys.optargs; toys.optargs++) {
+    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;
+    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);
   }
 }
-
-void pwdx_main(void)
-{
-  int i;
-
-  for (i=0; toys.optargs[i]; i++)
-    toys.exitval |= pid_dir(toys.optargs[i]);
-}
-