1 // Copyright 2004 Nicholas Miell
3 // This file may be used subject to the terms and conditions of the
4 // GNU Library General Public License Version 2 as published by the
5 // Free Software Foundation.This program is distributed in the hope
6 // that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the GNU Library General Public License for more
14 #include <sys/types.h>
20 #include "proc/version.h"
22 static void die(const char *msg) NORETURN;
23 static void die(const char *msg)
29 static void version(void) NORETURN;
30 static void version(void)
32 printf("pwdx (%s)\n", procps_version);
36 int main(int argc, char* argv[])
43 die("Usage: pwdx pid...\n");
45 // Allowed on the command line:
52 // where nnnn is any number that doesn't begin with 0.
54 // If --version or -V are present, further arguments are ignored
57 regcomp(&re, "^((/proc/+)?[1-9][0-9]*|-V|--version)$",
58 REG_EXTENDED|REG_NOSUB);
60 for (i = 1; i < argc; i++) {
61 if (regexec(&re, argv[i], 0, NULL, 0) != 0) {
62 snprintf(buf, sizeof buf, "pwdx: invalid process id: %s\n", argv[i]);
65 if (!strcmp("-V", argv[i]) || !strcmp("--version", argv[i]))
71 for (i = 1; i < argc; i++) {
75 // At this point, all arguments are in the form /proc/nnnn
76 // or nnnn, so a simple check based on the first char is
78 if (argv[i][0] != '/')
79 snprintf(buf, sizeof buf, "/proc/%s/cwd", argv[i]);
81 snprintf(buf, sizeof buf, "%s/cwd", argv[i]);
83 // buf contains /proc/nnnn/cwd symlink name on entry, the
84 // target of that symlink on return
85 if ((len = readlink(buf, buf, PATH_MAX)) < 0) {
86 s = strerror(errno == ENOENT ? ESRCH : errno);
91 printf("%s: %s\n", argv[i], s);