pwdx.c (909B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <errno.h> 4 #include <limits.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <unistd.h> 8 9 #include "proc.h" 10 #include "util.h" 11 12 static void 13 usage(void) 14 { 15 eprintf("usage: %s pid...\n", argv0); 16 } 17 18 // ?man pwdx: print working directory of process 19 // ?man arguments: pid... 20 // ?man display the current working directory of a process by pid 21 int 22 main(int argc, char *argv[]) 23 { 24 int ret = 0; 25 char target[PATH_MAX + sizeof(" (deleted)")]; 26 27 ARGBEGIN 28 { 29 default: 30 usage(); 31 } 32 ARGEND; 33 34 if (argc == 0) 35 usage(); 36 37 for (; argc > 0; argc--, argv++) { 38 pid_t pid = (pid_t)estrtonum(*argv, 1, INT_MAX); 39 40 if (proc_cwd(pid, target, sizeof(target)) == 0) { 41 printf("%s: %s\n", *argv, target); 42 continue; 43 } 44 if (errno == ENOENT) 45 errno = ESRCH; 46 weprintf("%s:", *argv); 47 ret = 1; 48 } 49 50 return ret; 51 }