ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

pwdx.c (857B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <limits.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include <unistd.h>
      7 
      8 #include "util.h"
      9 
     10 static void
     11 usage(void)
     12 {
     13 	eprintf("usage: %s pid...\n", argv0);
     14 }
     15 
     16 int
     17 main(int argc, char *argv[])
     18 {
     19 	int ret = 0;
     20 	char path[PATH_MAX];
     21 	char target[PATH_MAX + sizeof(" (deleted)")];
     22 	ssize_t n;
     23 
     24 	ARGBEGIN {
     25 	default:
     26 		usage();
     27 	} ARGEND;
     28 
     29 	if (argc == 0)
     30 		usage();
     31 
     32 	for (; argc > 0; argc--, argv++) {
     33 		n = snprintf(path, sizeof(path), "/proc/%s/cwd", *argv);
     34 		if (n < 0 || n >= sizeof(path)) {
     35 			errno = ESRCH;
     36 		} else {
     37 			n = readlink(path, target, sizeof(target) - 1);
     38 			if (n >= 0) {
     39 				target[n] = '\0';
     40 				printf("%s: %s\n", *argv, target);
     41 				continue;
     42 			}
     43 		}
     44 		if (errno == ENOENT)
     45 			errno = ESRCH;
     46 		weprintf("%s:", *argv);
     47 		ret = 1;
     48 	}
     49 
     50 	return ret;
     51 }