2011-06-04 22:26:08 +05:30
|
|
|
/*
|
2012-03-02 17:59:36 +05:30
|
|
|
* pwdx.c - print process working directory
|
2011-06-04 22:26:08 +05:30
|
|
|
* Copyright 2004 Nicholas Miell
|
|
|
|
*
|
2012-03-02 17:59:36 +05:30
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2011-06-04 22:26:08 +05:30
|
|
|
*/
|
2004-10-16 13:29:02 +05:30
|
|
|
|
2011-06-04 22:26:08 +05:30
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
2004-10-16 13:29:02 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-06-04 22:26:08 +05:30
|
|
|
#include <string.h>
|
2004-10-16 13:29:02 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-10-09 05:40:05 +05:30
|
|
|
#include "c.h"
|
|
|
|
#include "nls.h"
|
2012-02-26 02:54:58 +05:30
|
|
|
#include "xalloc.h"
|
2012-03-23 18:02:24 +05:30
|
|
|
#include "fileutils.h"
|
2004-10-16 13:29:02 +05:30
|
|
|
|
2011-06-04 22:26:08 +05:30
|
|
|
static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
2004-10-16 13:29:02 +05:30
|
|
|
{
|
2011-10-09 05:40:05 +05:30
|
|
|
fputs(USAGE_HEADER, out);
|
2011-12-17 23:02:47 +05:30
|
|
|
fprintf(out, _(" %s [options] pid...\n"), program_invocation_short_name);
|
2011-10-09 05:40:05 +05:30
|
|
|
fputs(USAGE_OPTIONS, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("pwdx(1)"));
|
2004-10-16 13:29:02 +05:30
|
|
|
|
2011-06-04 22:26:08 +05:30
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2004-10-16 13:29:02 +05:30
|
|
|
}
|
|
|
|
|
2011-12-26 23:12:05 +05:30
|
|
|
int check_pid_argument(char *input)
|
|
|
|
{
|
|
|
|
int skip = 0;
|
|
|
|
long pid;
|
|
|
|
char *end = NULL;
|
|
|
|
|
|
|
|
if (!strncmp("/proc/", input, 6))
|
|
|
|
skip = 6;
|
2013-08-05 16:22:22 +05:30
|
|
|
errno = 0;
|
2011-12-26 23:12:05 +05:30
|
|
|
pid = strtol(input + skip, &end, 10);
|
|
|
|
|
|
|
|
if (errno || input + skip == end || (end && *end))
|
|
|
|
return 1;
|
|
|
|
if (pid < 1)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-04 22:26:08 +05:30
|
|
|
int main(int argc, char *argv[])
|
2004-10-16 13:29:02 +05:30
|
|
|
{
|
2012-06-14 18:03:49 +05:30
|
|
|
int ch;
|
2011-06-04 22:26:08 +05:30
|
|
|
int retval = 0, i;
|
|
|
|
int alloclen = 128;
|
|
|
|
char *pathbuf;
|
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"version", no_argument, 0, 'V'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{NULL, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2013-02-20 23:01:48 +05:30
|
|
|
#ifdef HAVE_PROGRAM_INVOCATION_NAME
|
|
|
|
program_invocation_name = program_invocation_short_name;
|
|
|
|
#endif
|
2011-12-07 17:57:21 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-03-23 18:02:24 +05:30
|
|
|
atexit(close_stdout);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
2011-06-04 22:26:08 +05:30
|
|
|
while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'V':
|
2011-10-09 05:40:05 +05:30
|
|
|
printf(PROCPS_NG_VERSION);
|
2011-06-04 22:26:08 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
usage(stderr);
|
|
|
|
|
|
|
|
pathbuf = malloc(alloclen);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
char *s;
|
2012-02-26 02:54:58 +05:30
|
|
|
ssize_t len, buflen;
|
2011-06-04 22:26:08 +05:30
|
|
|
/* Constant 10 is the length of strings "/proc/" + "/cwd" + 1 */
|
2012-02-26 02:54:58 +05:30
|
|
|
char *buf;
|
|
|
|
buflen = 10 + strlen(argv[i]) + 1;
|
|
|
|
buf = xmalloc(buflen);
|
2011-06-04 22:26:08 +05:30
|
|
|
|
2011-12-26 23:12:05 +05:30
|
|
|
if (check_pid_argument(argv[i]))
|
2012-01-24 11:10:28 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("invalid process id: %s"),
|
2011-12-26 23:12:05 +05:30
|
|
|
argv[i]);
|
2011-06-04 22:26:08 +05:30
|
|
|
/*
|
|
|
|
* 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] != '/')
|
2012-02-26 02:54:58 +05:30
|
|
|
snprintf(buf, buflen, "/proc/%s/cwd", argv[i]);
|
2011-06-04 22:26:08 +05:30
|
|
|
else
|
2012-02-26 02:54:58 +05:30
|
|
|
snprintf(buf, buflen, "%s/cwd", argv[i]);
|
2011-06-04 22:26:08 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* buf contains /proc/NNNN/cwd symlink name
|
|
|
|
* on entry, the target of that symlink on return
|
|
|
|
*/
|
|
|
|
while ((len = readlink(buf, pathbuf, alloclen)) == alloclen) {
|
|
|
|
alloclen *= 2;
|
|
|
|
pathbuf = realloc(pathbuf, alloclen);
|
|
|
|
}
|
2012-02-26 02:54:58 +05:30
|
|
|
free(buf);
|
2011-06-04 22:26:08 +05:30
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
s = strerror(errno == ENOENT ? ESRCH : errno);
|
2011-12-18 20:07:02 +05:30
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[i], s);
|
|
|
|
continue;
|
2011-06-04 22:26:08 +05:30
|
|
|
} else {
|
|
|
|
pathbuf[len] = 0;
|
|
|
|
s = pathbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s: %s\n", argv[i], s);
|
|
|
|
}
|
2012-02-06 01:14:39 +05:30
|
|
|
free(pathbuf);
|
2011-06-04 22:26:08 +05:30
|
|
|
return retval;
|
2004-10-16 13:29:02 +05:30
|
|
|
}
|