pwdx.c 3.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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.
 */
albert's avatar
albert committed
12

13 14 15
#include <errno.h>
#include <getopt.h>
#include <limits.h>
albert's avatar
albert committed
16 17
#include <stdio.h>
#include <stdlib.h>
18
#include <string.h>
albert's avatar
albert committed
19 20 21 22
#include <sys/types.h>
#include <unistd.h>

#include "proc/version.h"
Sami Kerola's avatar
Sami Kerola committed
23 24
#include "c.h"
#include "nls.h"
Sami Kerola's avatar
Sami Kerola committed
25
#include "xalloc.h"
albert's avatar
albert committed
26

27
static void __attribute__ ((__noreturn__)) usage(FILE * out)
albert's avatar
albert committed
28
{
Sami Kerola's avatar
Sami Kerola committed
29
	fputs(USAGE_HEADER, out);
30
	fprintf(out, _(" %s [options] pid...\n"), program_invocation_short_name);
Sami Kerola's avatar
Sami Kerola committed
31 32 33 34
	fputs(USAGE_OPTIONS, out);
	fputs(USAGE_HELP, out);
	fputs(USAGE_VERSION, out);
	fprintf(out, USAGE_MAN_TAIL("pwdx(1)"));
albert's avatar
albert committed
35

36
	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
albert's avatar
albert committed
37 38
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
int check_pid_argument(char *input)
{
	int skip = 0;
	long pid;
	char *end = NULL;

	if (!strncmp("/proc/", input, 6))
		skip = 6;
	pid = strtol(input + skip, &end, 10);

	if (errno || input + skip == end || (end && *end))
		return 1;
	if (pid < 1)
		return 1;
	return 0;
}

56
int main(int argc, char *argv[])
albert's avatar
albert committed
57
{
58 59 60 61 62 63 64 65 66 67 68
	char ch;
	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}
	};

69
    program_invocation_name = program_invocation_short_name;
70 71 72 73
	setlocale (LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

74 75 76
	while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
		switch (ch) {
		case 'V':
Sami Kerola's avatar
Sami Kerola committed
77
			printf(PROCPS_NG_VERSION);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
			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;
Sami Kerola's avatar
Sami Kerola committed
95
		ssize_t len, buflen;
96
		/* Constant 10 is the length of strings "/proc/" + "/cwd" + 1 */
Sami Kerola's avatar
Sami Kerola committed
97 98 99
		char *buf;
		buflen = 10 + strlen(argv[i]) + 1;
		buf = xmalloc(buflen);
100

101
		if (check_pid_argument(argv[i]))
Mike Frysinger's avatar
Mike Frysinger committed
102
			xerrx(EXIT_FAILURE, _("invalid process id: %s"),
103
			     argv[i]);
104 105 106 107 108 109
		/*
		 * 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] != '/')
Sami Kerola's avatar
Sami Kerola committed
110
			snprintf(buf, buflen, "/proc/%s/cwd", argv[i]);
111
		else
Sami Kerola's avatar
Sami Kerola committed
112
			snprintf(buf, buflen, "%s/cwd", argv[i]);
113 114 115 116 117 118 119 120 121

		/*
		 * 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);
		}
Sami Kerola's avatar
Sami Kerola committed
122
		free(buf);
123 124 125

		if (len < 0) {
			s = strerror(errno == ENOENT ? ESRCH : errno);
126 127 128
			retval = EXIT_FAILURE;
			fprintf(stderr, "%s: %s\n", argv[i], s);
			continue;
129 130 131 132 133 134 135
		} else {
			pathbuf[len] = 0;
			s = pathbuf;
		}

		printf("%s: %s\n", argv[i], s);
	}
136
	free(pathbuf);
137
	return retval;
albert's avatar
albert committed
138
}