From 92ba425ed6c1121f70b740eb5314825e7ca9a644 Mon Sep 17 00:00:00 2001 From: albert <> Date: Sat, 16 Oct 2004 07:59:02 +0000 Subject: [PATCH] new pwdx command --- pwdx.1 | 37 +++++++++++++++++++++++ pwdx.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 pwdx.1 create mode 100644 pwdx.c diff --git a/pwdx.1 b/pwdx.1 new file mode 100644 index 00000000..728157d5 --- /dev/null +++ b/pwdx.1 @@ -0,0 +1,37 @@ +'\" t +.\" (The preceding line is a note to broken versions of man to tell +.\" them to pre-process this man page with tbl) +.\" Man page for pwdx +.\" Licensed under version 2 of the GNU General Public License. +.\" Copyright 2004 Nicholas Miell. +.\" Based on the pmap(1) man page by Albert Cahalan. +.\" +.TH PWDX 1 "September 8, 2004" "Linux" "Linux User's Manual" +.SH NAME +pwdx \- report current working directory of a process + +.SH SYNOPSIS +.nf +pwdx pids... +pwdx -V +.fi + +.SH DESCRIPTION +The pwdx command reports the current working directory of a process or +processes. + +.SH "GENERAL OPTIONS" +.TS +l l l. +-V show version Displays version of program. +.TE + +.SH "SEE ALSO" +ps(1) pgrep(1) + +.SH STANDARDS +No standards apply, but pwdx looks an awful lot like a SunOS command. + +.SH AUTHOR +Nicholas Miell wrote pwdx in 2004. Please send bug +reports to . diff --git a/pwdx.c b/pwdx.c new file mode 100644 index 00000000..0a59333c --- /dev/null +++ b/pwdx.c @@ -0,0 +1,95 @@ +// Copyright 2004 Nicholas Miell +// +// This file may be used subject to the terms and conditions of the +// GNU Library General Public License Version 2 as published by the +// Free Software Foundation.This program is distributed in the hope +// that it will be useful, but WITHOUT ANY WARRANTY; without even the +// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU Library General Public License for more +// details. + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "proc/version.h" + +static void die(const char *msg) NORETURN; +static void die(const char *msg) +{ + fprintf(stderr, msg); + exit(1); +} + +static void version(void) NORETURN; +static void version(void) +{ + printf("pwdx (%s)\n", procps_version); + exit(0); +} + +int main(int argc, char* argv[]) +{ + char buf[PATH_MAX]; + regex_t re; + int i; + + if (argc < 2) + die("Usage: pwdx pid...\n"); + + // Allowed on the command line: + // + // --version + // -V + // /proc/nnnn + // nnnn + // + // where nnnn is any number that doesn't begin with 0. + // + // If --version or -V are present, further arguments are ignored + // completely. + + regcomp(&re, "^((/proc/+)?[1-9][0-9]*|-V|--version)$", + REG_EXTENDED|REG_NOSUB); + + for (i = 1; i < argc; i++) { + if (regexec(&re, argv[i], 0, NULL, 0) != 0) { + sprintf(buf, "pwdx: invalid process id: %s\n", argv[i]); // FIXME (overflow) + die(buf); + } + if (!strcmp("-V", argv[i]) || !strcmp("--version", argv[i])) + version(); + } + + regfree(&re); + + for (i = 1; i < argc; i++) { + char * s = buf; + int len; + + // At this point, all arguments are in the form /proc/nnnn + // or nnnn, so a simple check based on the first char is + // possible + if (argv[i][0] != '/') + sprintf(buf, "/proc/%s/cwd", argv[i]); + else + sprintf(buf, "%s/cwd", argv[i]); + + // buf contains /proc/nnnn/cwd symlink name on entry, the + // target of that symlink on return + if ((len = readlink(buf, buf, PATH_MAX)) < 0) { + s = strerror(errno == ENOENT ? ESRCH : errno); + } else { + buf[len] = 0; + } + + printf("%s: %s\n", argv[i], s); + } + + return 0; +} -- 2.40.0