[hackers] [ubase][PATCH] Add pwdx(1)

From: Mattias Andrée <maandree_AT_kth.se>
Date: Sat, 26 Mar 2016 00:30:50 +0100

Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
---
 Makefile |  1 +
 TODO     |  1 -
 pwdx.1   | 13 +++++++++++++
 pwdx.c   | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 pwdx.1
 create mode 100644 pwdx.c
diff --git a/Makefile b/Makefile
index 4ab1856..92d61ab 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -69,6 +69,7 @@ BIN = \
 	pidof             \
 	pivot_root        \
 	ps                \
+	pwdx              \
 	readahead         \
 	respawn           \
 	rmmod             \
diff --git a/TODO b/TODO
index 3e89e68..21f5c20 100644
--- a/TODO
+++ b/TODO
_AT_@ -19,7 +19,6 @@ mkswap [-L]
 partprobe
 pmap
 ps (support for more options)
-pwdx
 rfkill
 rmgroup
 rmuser
diff --git a/pwdx.1 b/pwdx.1
new file mode 100644
index 0000000..aadb6b4
--- /dev/null
+++ b/pwdx.1
_AT_@ -0,0 +1,13 @@
+.Dd March 26, 2015
+.Dt PWDX 1
+.Os ubase
+.Sh NAME
+.Nm pwdx
+.Nd print working directory of other processes
+.Sh SYNOPSIS
+.Nm
+.Ar pid...
+.Sh DESCRIPTION
+.Nm
+Prints the current working directory for each
+.Ar pid .
diff --git a/pwdx.c b/pwdx.c
new file mode 100644
index 0000000..b9836b7
--- /dev/null
+++ b/pwdx.c
_AT_@ -0,0 +1,53 @@
+/* See LICENSE file for copyright and license details. */
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "util.h"
+
+static void
+usage(void)
+{
+	eprintf("usage: %s pid...\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+	int ret = 0;
+	char path[sizeof("/proc//cwd") + 3 * sizeof(pid_t)];
+	char target[PATH_MAX + sizeof(" (deleted)")];
+	ssize_t n;
+
+	ARGBEGIN {
+	default:
+		usage();
+	} ARGEND;
+
+	if (argc == 0)
+		usage();
+
+	for (; argc > 0; argc--, argv++) {
+		if (strlen(*argv) > 3 * sizeof(pid_t)) {
+			fprintf(stderr, "%s: No such process\n", *argv);
+			ret = 1;
+			continue;
+		}
+		sprintf(path, "/proc/%s/cwd", *argv);
+		n = readlink(path, target, sizeof(target) - 1);
+		if (n >= 0) {
+			target[n] = '\0';
+			printf("%s: %s\n", *argv, target);
+		} else if (errno == ENOENT) {
+			fprintf(stderr, "%s: No such process\n", *argv);
+			ret = 1;
+		} else {
+			perror(*argv);
+			ret = 1;
+		}
+	}
+
+	return ret;
+}
-- 
2.7.3
Received on Sat Mar 26 2016 - 00:30:50 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 26 2016 - 00:36:15 CET