2011-03-13 01:13:15 +08:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* pwdx implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Nicholas Miell
|
|
|
|
* ported from procps by Pere Orga <gotrunks@gmail.com> 2011
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config PWDX
|
2023-07-10 23:25:21 +08:00
|
|
|
//config: bool "pwdx (3.9 kb)"
|
2011-03-13 01:13:15 +08:00
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: Report current working directory of a process
|
2011-03-13 01:13:15 +08:00
|
|
|
|
2017-08-04 01:00:01 +08:00
|
|
|
//applet:IF_PWDX(APPLET_NOFORK(pwdx, pwdx, BB_DIR_USR_BIN, BB_SUID_DROP, pwdx))
|
2011-03-13 01:13:15 +08:00
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_PWDX) += pwdx.o
|
|
|
|
|
|
|
|
//usage:#define pwdx_trivial_usage
|
|
|
|
//usage: "PID..."
|
|
|
|
//usage:#define pwdx_full_usage "\n\n"
|
2015-01-25 02:46:45 +08:00
|
|
|
//usage: "Show current directory for PIDs"
|
2011-03-13 01:13:15 +08:00
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
int pwdx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int pwdx_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
2017-08-09 03:55:02 +08:00
|
|
|
getopt32(argv, "^" "" "\0" "-1");
|
2011-03-13 01:13:15 +08:00
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
do {
|
|
|
|
char buf[sizeof("/proc/%u/cwd") + sizeof(int)*3];
|
|
|
|
unsigned pid;
|
|
|
|
char *s;
|
|
|
|
char *arg = *argv;
|
|
|
|
|
|
|
|
// Allowed on the command line:
|
|
|
|
// /proc/NUM
|
|
|
|
// NUM
|
2015-03-13 00:48:34 +08:00
|
|
|
if (is_prefixed_with(arg, "/proc/"))
|
2011-03-13 01:13:15 +08:00
|
|
|
arg += 6;
|
|
|
|
|
|
|
|
pid = bb_strtou(arg, NULL, 10);
|
|
|
|
if (errno)
|
|
|
|
bb_error_msg_and_die("invalid process id: '%s'", arg);
|
|
|
|
|
|
|
|
sprintf(buf, "/proc/%u/cwd", pid);
|
|
|
|
|
2017-08-04 01:00:01 +08:00
|
|
|
/* NOFORK: only one alloc is allowed; must free */
|
2011-03-13 01:13:15 +08:00
|
|
|
s = xmalloc_readlink(buf);
|
|
|
|
// "pwdx /proc/1" says "/proc/1: DIR", not "1: DIR"
|
|
|
|
printf("%s: %s\n", *argv, s ? s : strerror(errno == ENOENT ? ESRCH : errno));
|
|
|
|
free(s);
|
|
|
|
} while (*++argv);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|