Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * pwdx implementation for busybox |
| 4 | * |
| 5 | * Copyright (c) 2004 Nicholas Miell |
| 6 | * ported from procps by Pere Orga <gotrunks@gmail.com> 2011 |
| 7 | * |
| 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 9 | */ |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 10 | //config:config PWDX |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "pwdx (3.5 kb)" |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: Report current working directory of a process |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 15 | |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 16 | //applet:IF_PWDX(APPLET_NOFORK(pwdx, pwdx, BB_DIR_USR_BIN, BB_SUID_DROP, pwdx)) |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 17 | |
| 18 | //kbuild:lib-$(CONFIG_PWDX) += pwdx.o |
| 19 | |
| 20 | //usage:#define pwdx_trivial_usage |
| 21 | //usage: "PID..." |
| 22 | //usage:#define pwdx_full_usage "\n\n" |
Denys Vlasenko | 86031a5 | 2015-01-24 19:46:45 +0100 | [diff] [blame] | 23 | //usage: "Show current directory for PIDs" |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 24 | |
| 25 | #include "libbb.h" |
| 26 | |
| 27 | int pwdx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 28 | int pwdx_main(int argc UNUSED_PARAM, char **argv) |
| 29 | { |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 30 | getopt32(argv, "^" "" "\0" "-1"); |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 31 | argv += optind; |
| 32 | |
| 33 | do { |
| 34 | char buf[sizeof("/proc/%u/cwd") + sizeof(int)*3]; |
| 35 | unsigned pid; |
| 36 | char *s; |
| 37 | char *arg = *argv; |
| 38 | |
| 39 | // Allowed on the command line: |
| 40 | // /proc/NUM |
| 41 | // NUM |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 42 | if (is_prefixed_with(arg, "/proc/")) |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 43 | arg += 6; |
| 44 | |
| 45 | pid = bb_strtou(arg, NULL, 10); |
| 46 | if (errno) |
| 47 | bb_error_msg_and_die("invalid process id: '%s'", arg); |
| 48 | |
| 49 | sprintf(buf, "/proc/%u/cwd", pid); |
| 50 | |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 51 | /* NOFORK: only one alloc is allowed; must free */ |
Pere Orga | cf8b55c | 2011-03-12 18:13:15 +0100 | [diff] [blame] | 52 | s = xmalloc_readlink(buf); |
| 53 | // "pwdx /proc/1" says "/proc/1: DIR", not "1: DIR" |
| 54 | printf("%s: %s\n", *argv, s ? s : strerror(errno == ENOENT ? ESRCH : errno)); |
| 55 | free(s); |
| 56 | } while (*++argv); |
| 57 | |
| 58 | return EXIT_SUCCESS; |
| 59 | } |