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 <ighighi@gmail.com>
This commit is contained in:
Robert Watson 2008-03-09 12:46:39 +00:00
parent 1b21ffa615
commit 08995e292e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=176977
2 changed files with 34 additions and 7 deletions

View File

@ -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

View File

@ -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);
}