1 /* vi: set sw=4 ts=4: */
3 * pwdx implementation for busybox
5 * Copyright (c) 2004 Nicholas Miell
6 * ported from procps by Pere Orga <gotrunks@gmail.com> 2011
8 * Licensed under GPLv2, see file LICENSE in this source tree.
15 //config: Report current working directory of a process
17 //applet:IF_PWDX(APPLET(pwdx, BB_DIR_USR_BIN, BB_SUID_DROP))
19 //kbuild:lib-$(CONFIG_PWDX) += pwdx.o
21 //usage:#define pwdx_trivial_usage
23 //usage:#define pwdx_full_usage "\n\n"
24 //usage: "Show current directory for PIDs\n"
28 int pwdx_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
29 int pwdx_main(int argc UNUSED_PARAM
, char **argv
)
31 opt_complementary
= "-1";
36 char buf
[sizeof("/proc/%u/cwd") + sizeof(int)*3];
41 // Allowed on the command line:
44 if (strncmp(arg
, "/proc/", 6) == 0)
47 pid
= bb_strtou(arg
, NULL
, 10);
49 bb_error_msg_and_die("invalid process id: '%s'", arg
);
51 sprintf(buf
, "/proc/%u/cwd", pid
);
53 s
= xmalloc_readlink(buf
);
54 // "pwdx /proc/1" says "/proc/1: DIR", not "1: DIR"
55 printf("%s: %s\n", *argv
, s
? s
: strerror(errno
== ENOENT
? ESRCH
: errno
));