pwdx.c: new usage & fix coding style

Coding style fixed and new help output.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-06-04 18:56:08 +02:00
parent 4581ac2240
commit 4f7b9c661b

193
pwdx.c
View File

@ -1,108 +1,105 @@
// Copyright 2004 Nicholas Miell /*
// * 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 * This file may be used subject to the terms and conditions of the
// Free Software Foundation.This program is distributed in the hope * GNU Library General Public License Version 2 as published by the
// that it will be useful, but WITHOUT ANY WARRANTY; without even the * Free Software Foundation.This program is distributed in the hope
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * that it will be useful, but WITHOUT ANY WARRANTY; without even the
// PURPOSE. See the GNU Library General Public License for more * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// details. * PURPOSE. See the GNU Library General Public License for more
* details.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "proc/version.h" #include "proc/version.h"
static void die(const char *msg) NORETURN; static void __attribute__ ((__noreturn__)) usage(FILE * out)
static void die(const char *msg)
{ {
fputs(msg, stderr); fprintf(out, "\nUsage:\n"
exit(1); " %s [options] pid...\n", program_invocation_short_name);
fprintf(out,
"\nOptions:\n"
" -V, --version output version information and exit\n"
" -h, --help output help screen and exit\n\n");
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
} }
static void version(void) NORETURN; int main(int argc, char *argv[])
static void version(void)
{ {
printf("pwdx (%s)\n", procps_version); char ch;
exit(0); int retval = 0, i;
} int alloclen = 128;
char *pathbuf;
int main(int argc, char* argv[])
{ static const struct option longopts[] = {
regex_t re; {"version", no_argument, 0, 'V'},
int i; {"help", no_argument, 0, 'h'},
{NULL, 0, 0, 0}
if (argc < 2) };
die("Usage: pwdx pid...\n");
while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
// Allowed on the command line: switch (ch) {
// case 'V':
// --version display_version();
// -V return EXIT_SUCCESS;
// /proc/nnnn case 'h':
// nnnn usage(stdout);
// default:
// where nnnn is any number that doesn't begin with 0. usage(stderr);
// }
// If --version or -V are present, further arguments are ignored
// completely. argc -= optind;
argv += optind;
regcomp(&re, "^((/proc/+)?[1-9][0-9]*|-V|--version)$",
REG_EXTENDED|REG_NOSUB); if (argc == 0)
usage(stderr);
for (i = 1; i < argc; i++) {
if (regexec(&re, argv[i], 0, NULL, 0) != 0) { pathbuf = malloc(alloclen);
/* Constant 27 is the length of the error string "pwdx: ... " */
char buf[27 + strlen (argv[i]) + 1]; for (i = 0; i < argc; i++) {
snprintf(buf, sizeof buf, "pwdx: invalid process id: %s\n", argv[i]); char *s;
buf[sizeof(buf)-1] = '\0'; ssize_t len;
die(buf); /* Constant 10 is the length of strings "/proc/" + "/cwd" + 1 */
} char buf[10 + strlen(argv[i]) + 1];
if (!strcmp("-V", argv[i]) || !strcmp("--version", argv[i]))
version(); /*
} * At this point, all arguments are in the form
* /proc/NNNN or NNNN, so a simple check based on
regfree(&re); * the first char is possible
*/
int alloclen = 128; if (argv[i][0] != '/')
char *pathbuf = malloc(alloclen); snprintf(buf, sizeof buf, "/proc/%s/cwd", argv[i]);
else
for (i = 1; i < argc; i++) { snprintf(buf, sizeof buf, "%s/cwd", argv[i]);
char * s;
int len; /*
/* Constant 10 is the length of strings "/proc/" + "/cwd" + 1 */ * buf contains /proc/NNNN/cwd symlink name
char buf[10 + strlen(argv[i]) + 1]; * on entry, the target of that symlink on return
*/
// At this point, all arguments are in the form /proc/nnnn while ((len = readlink(buf, pathbuf, alloclen)) == alloclen) {
// or nnnn, so a simple check based on the first char is alloclen *= 2;
// possible pathbuf = realloc(pathbuf, alloclen);
if (argv[i][0] != '/') }
snprintf(buf, sizeof buf, "/proc/%s/cwd", argv[i]);
else if (len < 0) {
snprintf(buf, sizeof buf, "%s/cwd", argv[i]); s = strerror(errno == ENOENT ? ESRCH : errno);
retval = 1;
// buf contains /proc/nnnn/cwd symlink name on entry, the } else {
// target of that symlink on return pathbuf[len] = 0;
while ((len = readlink(buf, pathbuf, alloclen)) == alloclen) { s = pathbuf;
alloclen *= 2; }
pathbuf = realloc(pathbuf, alloclen);
} printf("%s: %s\n", argv[i], s);
}
if (len < 0) {
s = strerror(errno == ENOENT ? ESRCH : errno); return retval;
} else {
pathbuf[len] = 0;
s = pathbuf;
}
printf("%s: %s\n", argv[i], s);
}
return 0;
} }