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[])
42
die("Usage: pwdx pid...\n");
44
// Allowed on the command line:
51
// where nnnn is any number that doesn't begin with 0.
53
// If --version or -V are present, further arguments are ignored
56
regcomp(&re, "^((/proc/+)?[1-9][0-9]*|-V|--version)$",
57
REG_EXTENDED|REG_NOSUB);
59
for (i = 1; i < argc; i++) {
60
if (regexec(&re, argv[i], 0, NULL, 0) != 0) {
61
char buf[27 + strlen (argv[i]) + 1]; // Constant 27 is the length of the error string "pwdx: ... "
62
snprintf(buf, sizeof buf, "pwdx: invalid process id: %s\n", argv[i]);
65
if (!strcmp("-V", argv[i]) || !strcmp("--version", argv[i]))
72
char *pathbuf = malloc(alloclen);
74
for (i = 1; i < argc; i++) {
77
char buf[10 + strlen(argv[i]) + 1]; // Constant 10 is the length of strings "/proc/" + "/cwd" + 1
79
// At this point, all arguments are in the form /proc/nnnn
80
// or nnnn, so a simple check based on the first char is
82
if (argv[i][0] != '/')
83
snprintf(buf, sizeof buf, "/proc/%s/cwd", argv[i]);
85
snprintf(buf, sizeof buf, "%s/cwd", argv[i]);
87
// buf contains /proc/nnnn/cwd symlink name on entry, the
88
// target of that symlink on return
89
while ((len = readlink(buf, pathbuf, alloclen)) == alloclen) {
91
pathbuf = realloc(pathbuf, alloclen);
95
s = strerror(errno == ENOENT ? ESRCH : errno);
101
printf("%s: %s\n", argv[i], s);