From 08995e292e9870964bce2c1a8c008635b769c2c8 Mon Sep 17 00:00:00 2001 From: Robert Watson Date: Sun, 9 Mar 2008 12:46:39 +0000 Subject: [PATCH] Enhance realpath(1) in a number of ways: - Allow realpath to accept multiple paths on the command line. - Add -q to suppress warnings if some paths can't be processed, and use getopt(3) to process flags. - Print the path being requested rather than a possibly partially processed path when a failure occurs so that you can tell which of several passed paths did fail. MFC after: 1 week PR: 112920 Submitted by: Ighighi --- bin/realpath/realpath.1 | 8 ++++++++ bin/realpath/realpath.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/bin/realpath/realpath.1 b/bin/realpath/realpath.1 index 3b395456ce1a..cf904edd49b8 100644 --- a/bin/realpath/realpath.1 +++ b/bin/realpath/realpath.1 @@ -41,7 +41,9 @@ .Nd return resolved physical path .Sh SYNOPSIS .Nm +.Op Fl q .Ar path +.Op Ar ... .Sh DESCRIPTION The .Nm @@ -55,6 +57,12 @@ and .Pa /../ in .Ar path . +.Pp +If +.Fl q +is specified, warnings will not be printed when +.Xr realpath 3 +fails. .Sh EXIT STATUS .Ex -std .Sh SEE ALSO diff --git a/bin/realpath/realpath.c b/bin/realpath/realpath.c index b0a2c9e35349..99f5a9d50266 100644 --- a/bin/realpath/realpath.c +++ b/bin/realpath/realpath.c @@ -44,20 +44,39 @@ main(int argc, char *argv[]) { char buf[PATH_MAX]; char *p; + int ch, i, qflag, rval; - if (argc == 2) { - if ((p = realpath(argv[1], buf)) == NULL) - err(1, "%s", buf); - } else + qflag = 0; + while ((ch = getopt(argc, argv, "q")) != -1) { + switch (ch) { + case 'q': + qflag = 1; + break; + case '?': + default: + usage(); + } + } + argc -= optind; + argv += optind; + if (argc < 1) usage(); - (void)printf("%s\n", p); - exit(0); + rval = 0; + for (i = 0; i < argc; i++) { + if ((p = realpath(argv[i], buf)) == NULL) { + if (!qflag) + warn("%s", argv[i]); + rval = 1; + } else + (void)printf("%s\n", p); + } + exit(rval); } static void usage(void) { - (void)fprintf(stderr, "usage: realpath path\n"); + (void)fprintf(stderr, "usage: realpath [-q] path [...]\n"); exit(1); }