From: kn Date: Wed, 13 Oct 2021 15:04:53 +0000 (+0000) Subject: Provide realpath(1) X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9a9b50cee7c3331056b036749cf34a8c922cfe8c;p=openbsd Provide realpath(1) A tiny realpath(3) wrapper to make a porter's life easier. Feedback kettenis deraadt cheloha sthen OK cheloha martijn deraadt --- diff --git a/distrib/sets/lists/base/mi b/distrib/sets/lists/base/mi index ab88ca9b98f..dbdb7860c1d 100644 --- a/distrib/sets/lists/base/mi +++ b/distrib/sets/lists/base/mi @@ -602,6 +602,7 @@ ./usr/bin/rdist ./usr/bin/rdistd ./usr/bin/readlink +./usr/bin/realpath ./usr/bin/renice ./usr/bin/reset ./usr/bin/rev diff --git a/distrib/sets/lists/man/mi b/distrib/sets/lists/man/mi index 3a28fd1213a..de5af1404cc 100644 --- a/distrib/sets/lists/man/mi +++ b/distrib/sets/lists/man/mi @@ -416,6 +416,7 @@ ./usr/share/man/man1/rdist.1 ./usr/share/man/man1/rdistd.1 ./usr/share/man/man1/readlink.1 +./usr/share/man/man1/realpath.1 ./usr/share/man/man1/register-plist.1 ./usr/share/man/man1/rev.1 ./usr/share/man/man1/rlog.1 diff --git a/lib/libc/stdlib/realpath.3 b/lib/libc/stdlib/realpath.3 index 51abd93652f..1dec10fef48 100644 --- a/lib/libc/stdlib/realpath.3 +++ b/lib/libc/stdlib/realpath.3 @@ -28,9 +28,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: realpath.3,v 1.25 2019/12/11 20:01:50 bluhm Exp $ +.\" $OpenBSD: realpath.3,v 1.26 2021/10/13 15:04:53 kn Exp $ .\" -.Dd $Mdocdate: December 11 2019 $ +.Dd $Mdocdate: October 13 2021 $ .Dt REALPATH 3 .Os .Sh NAME @@ -136,6 +136,7 @@ Sufficient storage space is unavailable for allocation. .El .Sh SEE ALSO .Xr readlink 1 , +.Xr realpath 1 , .Xr getcwd 3 .Sh STANDARDS The diff --git a/usr.bin/Makefile b/usr.bin/Makefile index 89f72a713d3..22027a4f753 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.164 2021/09/01 19:55:53 job Exp $ +# $OpenBSD: Makefile,v 1.165 2021/10/13 15:04:53 kn Exp $ .include @@ -20,7 +20,7 @@ SUBDIR= apply arch at aucat audioctl awk banner \ nfsstat nice nm nl nohup openssl pagesize passwd paste patch pctr \ pkg-config pkill \ pr printenv printf quota radioctl rcs rdist rdistd \ - readlink renice rev rpcgen rpcinfo rs rsync rup rusers rwall \ + readlink realpath renice rev rpcgen rpcinfo rs rsync rup rusers rwall \ sdiff script sed sendbug shar showmount signify skey \ skeyaudit skeyinfo skeyinit sndioctl sndiod snmp \ sort spell split ssh stat su systat \ diff --git a/usr.bin/readlink/readlink.1 b/usr.bin/readlink/readlink.1 index 68076637956..fefa73795fc 100644 --- a/usr.bin/readlink/readlink.1 +++ b/usr.bin/readlink/readlink.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: readlink.1,v 1.13 2010/09/03 11:09:29 jmc Exp $ +.\" $OpenBSD: readlink.1,v 1.14 2021/10/13 15:04:53 kn Exp $ .\" .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -30,7 +30,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: September 3 2010 $ +.Dd $Mdocdate: October 13 2021 $ .Dt READLINK 1 .Os .Sh NAME @@ -70,6 +70,7 @@ Do not print a trailing newline character. .Sh EXIT STATUS .Ex -std readlink .Sh SEE ALSO +.Xr realpath 1 , .Xr readlink 2 , .Xr realpath 3 .Sh HISTORY diff --git a/usr.bin/realpath/Makefile b/usr.bin/realpath/Makefile new file mode 100644 index 00000000000..2eb65371682 --- /dev/null +++ b/usr.bin/realpath/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2021/10/13 15:04:53 kn Exp $ + +PROG= realpath + +.include diff --git a/usr.bin/realpath/realpath.1 b/usr.bin/realpath/realpath.1 new file mode 100644 index 00000000000..5bc4d49d196 --- /dev/null +++ b/usr.bin/realpath/realpath.1 @@ -0,0 +1,56 @@ +.\" $OpenBSD: realpath.1,v 1.1 2021/10/13 15:04:53 kn Exp $ +.\" +.\" Copyright (c) 2021 Klemens Nanni +.\" +.\" Permission to use, copy, modify, and distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd $Mdocdate: October 13 2021 $ +.Dt REALPATH 1 +.Os +.Sh NAME +.Nm realpath +.Nd print the canonicalized absolute pathname +.Sh SYNOPSIS +.Nm +.Op Fl q +.Ar file +.Sh DESCRIPTION +The +.Nm +utility uses +.Xr realpath 3 +to resolve all symbolic links, extra +.Dq / +characters and references to +.Pa /./ +and +.Pa /../ +in +.Ar file , +and prints the absolute pathname on standard output. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl q +Do not print errors. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr readlink 1 , +.Xr realpath 3 +.Sh HISTORY +The +.Nm +utility first appeared in +.Ox 7.1 . diff --git a/usr.bin/realpath/realpath.c b/usr.bin/realpath/realpath.c new file mode 100644 index 00000000000..0f66121a489 --- /dev/null +++ b/usr.bin/realpath/realpath.c @@ -0,0 +1,63 @@ +/* $OpenBSD: realpath.c,v 1.1 2021/10/13 15:04:53 kn Exp $ */ +/* + * Copyright (c) 2021 Klemens Nanni + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +void __dead +usage(void) +{ + fprintf(stderr, "usage: %s [-q] file\n", getprogname()); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + int ch, qflag = 0; + char *buf; + + if (pledge("stdio rpath", NULL) == -1) + err(1, "pledge"); + + while ((ch = getopt(argc, argv, "q")) != -1) { + switch (ch) { + case 'q': + qflag = 1; + break; + default: + usage(); + } + } + argc -= optind; + argv += optind; + if (argc != 1) + usage(); + + buf = realpath(argv[0], NULL); + if (buf == NULL) { + if (qflag) + return (1); + err(1, "%s", argv[0]); + } + + printf("%s\n", buf); + return (0); +}