Port realpath(1) from FreeBSD

realpath(1) wraps realpath(3) and returns resolved physical path.

This utility shipped with GNU and FreeBSD is sometimes
used in scripts in the wild.
This commit is contained in:
kamil 2020-02-02 21:49:43 +00:00
parent 8b758d6e9b
commit 771c425869
7 changed files with 190 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1226 2020/01/19 20:41:17 riastradh Exp $
# $NetBSD: mi,v 1.1227 2020/02/02 21:49:44 kamil Exp $
#
# Note: Don't delete entries from here - mark them as "obsolete" instead,
# unless otherwise stated below.
@ -923,6 +923,7 @@
./usr/bin/rcsmerge base-rcs-bin
./usr/bin/rdist base-netutil-bin
./usr/bin/readlink base-util-bin
./usr/bin/realpath base-util-bin
./usr/bin/renice base-util-bin
./usr/bin/reset base-util-bin
./usr/bin/rev base-util-bin

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.292 2020/01/19 10:50:26 martin Exp $
# $NetBSD: mi,v 1.293 2020/02/02 21:49:44 kamil Exp $
./etc/mtree/set.debug comp-sys-root
./usr/lib comp-sys-usr compatdir
./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile
@ -735,6 +735,7 @@
./usr/libdata/debug/usr/bin/rcsmerge.debug comp-rcs-debug debug
./usr/libdata/debug/usr/bin/rdist.debug comp-netutil-debug debug
./usr/libdata/debug/usr/bin/readelf.debug comp-util-debug binutils,debug
./usr/libdata/debug/usr/bin/realpath.debug comp-util-debug debug
./usr/libdata/debug/usr/bin/refer.debug comp-groff-debug groff,debug
./usr/libdata/debug/usr/bin/renice.debug comp-util-debug debug
./usr/libdata/debug/usr/bin/rev.debug comp-util-debug debug

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1678 2020/01/29 18:39:04 maya Exp $
# $NetBSD: mi,v 1.1679 2020/02/02 21:49:44 kamil Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@ -480,6 +480,7 @@
./usr/share/man/cat1/rcsmerge.0 man-rcs-catman .cat
./usr/share/man/cat1/rdist.0 man-netutil-catman .cat
./usr/share/man/cat1/readlink.0 man-util-catman .cat
./usr/share/man/cat1/realpath.0 man-util-catman .cat
./usr/share/man/cat1/rehash.0 man-util-catman .cat
./usr/share/man/cat1/repeat.0 man-util-catman .cat
./usr/share/man/cat1/reset.0 man-util-catman .cat
@ -3726,6 +3727,7 @@
./usr/share/man/html1/rcsmerge.html man-rcs-htmlman html
./usr/share/man/html1/rdist.html man-netutil-htmlman html
./usr/share/man/html1/readlink.html man-util-htmlman html
./usr/share/man/html1/realpath.html man-util-htmlman html
./usr/share/man/html1/rehash.html man-util-htmlman html
./usr/share/man/html1/repeat.html man-util-htmlman html
./usr/share/man/html1/reset.html man-util-htmlman html
@ -6622,6 +6624,7 @@
./usr/share/man/man1/rcsmerge.1 man-rcs-man .man
./usr/share/man/man1/rdist.1 man-netutil-man .man
./usr/share/man/man1/readlink.1 man-util-man .man
./usr/share/man/man1/realpath.1 man-util-man .man
./usr/share/man/man1/rehash.1 man-util-man .man
./usr/share/man/man1/repeat.1 man-util-man .man
./usr/share/man/man1/reset.1 man-util-man .man

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.232 2018/07/24 15:26:16 christos Exp $
# $NetBSD: Makefile,v 1.233 2020/02/02 21:49:43 kamil Exp $
# from: @(#)Makefile 8.3 (Berkeley) 1/7/94
.include <bsd.own.mk>
@ -22,7 +22,7 @@ SUBDIR= apply asa at audio audiocfg \
nbperf nc netgroup netstat newgrp newsyslog nfsstat nice nl nohup \
pagesize passwd paste patch pathchk pkill pmap pr \
printenv printf progress pwait pwhash qsubst quota radioctl rdist \
renice rev revoke rfcomm_sppd rlogin rpcgen rpcinfo rs rsh \
realpath renice rev revoke rfcomm_sppd rlogin rpcgen rpcinfo rs rsh \
rup ruptime rusers rwall rwho \
script sdiff sdpquery sed seq shar shlock \
showmount shuffle sockstat sort sortinfo \

View File

@ -0,0 +1,6 @@
# $NetBSD: Makefile,v 1.1 2020/02/02 21:49:44 kamil Exp $
# $FreeBSD: head/bin/realpath/Makefile 298107 2016-04-16 07:45:30Z gjb $
PROG= realpath
.include <bsd.prog.mk>

View File

@ -0,0 +1,81 @@
.\" $NetBSD: realpath.1,v 1.1 2020/02/02 21:49:44 kamil Exp $
.\"-
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)pwd.1 8.2 (Berkeley) 4/28/95
.\" From: src/bin/pwd/pwd.1,v 1.11 2000/11/20 11:39:39 ru Exp
.\" $FreeBSD: head/bin/realpath/realpath.1 314436 2017-02-28 23:42:47Z imp $
.\"
.Dd February 2, 2020
.Dt REALPATH 1
.Os
.Sh NAME
.Nm realpath
.Nd return resolved physical path
.Sh SYNOPSIS
.Nm
.Op Fl q
.Op Ar path ...
.Sh DESCRIPTION
The
.Nm
utility uses the
.Xr realpath 3
function to resolve all symbolic links, extra
.Ql /
characters and references to
.Pa /./
and
.Pa /../
in
.Ar path .
If
.Ar path
is absent, the current working directory
.Pq Sq Pa .\&
is assumed.
.Pp
If
.Fl q
is specified, warnings will not be printed when
.Xr realpath 3
fails.
.Sh EXIT STATUS
.Ex -std
.Sh SEE ALSO
.Xr realpath 3
.Sh HISTORY
The
.Nm
utility first appeared in
.Fx 4.3
and was imported into
.Nx 10 .

View File

@ -0,0 +1,93 @@
/* $NetBSD: realpath.c,v 1.1 2020/02/02 21:49:44 kamil Exp $ */
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if !defined(lint)
#if 0
__FBSDID("$FreeBSD: head/bin/realpath/realpath.c 326025 2017-11-20 19:49:47Z pfg $");
#else
__RCSID("$NetBSD: realpath.c,v 1.1 2020/02/02 21:49:44 kamil Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void usage(void) __dead;
int
main(int argc, char *argv[])
{
char buf[PATH_MAX];
char *p;
const char *path;
int ch, qflag, rval;
setprogname(argv[0]);
qflag = 0;
while ((ch = getopt(argc, argv, "q")) != -1) {
switch (ch) {
case 'q':
qflag = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
path = *argv != NULL ? *argv++ : ".";
rval = 0;
do {
if ((p = realpath(path, buf)) == NULL) {
if (!qflag)
warn("%s", path);
rval = 1;
} else
(void)printf("%s\n", p);
} while ((path = *argv++) != NULL);
exit(rval);
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: %s [-q] [path ...]\n", getprogname());
exit(1);
}