From 77ea441f79aa115f79b47d9c1fc9c0004c5c7111 Mon Sep 17 00:00:00 2001 From: Pádraig Brady
Date: Tue, 27 Dec 2011 00:30:23 +0000
Subject: realpath: a new program to print the resolved path
This program is compatible with other realpath(1)
implementations, and also incorporates relpath like support,
through the --relative options. The relpath support
was suggested by Peng Yu, who also provided an initial
implemenation of that functionality.
* AUTHORS: Add my name.
* NEWS: Mention the new command.
* README: Likewise.
* doc/coreutils.texi (realpath invocation): Add realpath info.
* man/Makefile.am (realpath.1): Add dependency.
* man/realpath.x: New template.
* man/.gitignore: Ignore generated man page.
* po/POTFILES.in: Add src/realpath.c.
* src/.gitignore: Exclude realpath.
* src/Makefile.am (EXTRA_PROGRAMS): Add realpath.
* src/realpath.c: New file.
* scripts/git-hooks/commit-msg: Add realpath to the list of prefixes.
* tests/Makefile.am (TESTS): Add misc/realpath.
* tests/misc/realpath: New file.
---
AUTHORS | 1 +
NEWS | 4 +
README | 10 +-
doc/coreutils.texi | 123 +++++++++++++++-
man/.gitignore | 1 +
man/Makefile.am | 1 +
man/realpath.x | 6 +
po/POTFILES.in | 1 +
scripts/git-hooks/commit-msg | 2 +-
src/.gitignore | 1 +
src/Makefile.am | 3 +
src/realpath.c | 329 +++++++++++++++++++++++++++++++++++++++++++
tests/Makefile.am | 1 +
tests/misc/help-version | 1 +
tests/misc/realpath | 53 +++++++
15 files changed, 527 insertions(+), 10 deletions(-)
create mode 100644 man/realpath.x
create mode 100644 src/realpath.c
create mode 100755 tests/misc/realpath
diff --git a/AUTHORS b/AUTHORS
index 0c15472fa..d1024a56a 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -62,6 +62,7 @@ printf: David MacKenzie
ptx: François Pinard
pwd: Jim Meyering
readlink: Dmitry V. Levin
+realpath: Pádraig Brady
rm: Paul Rubin, David MacKenzie, Richard M. Stallman, Jim Meyering
rmdir: David MacKenzie
runcon: Russell Coker
diff --git a/NEWS b/NEWS
index 32c8eab3b..78a90b672 100644
--- a/NEWS
+++ b/NEWS
@@ -41,6 +41,10 @@ GNU coreutils NEWS -*- outline -*-
argument, tail -f prints a warning with the FS type magic number and a
request to report it to the bug-reporting address.
+** New programs
+
+ realpath: print resolved file names.
+
* Noteworthy changes in release 8.14 (2011-10-12) [stable]
diff --git a/README b/README
index 5c5bc0f7b..90b1d40c3 100644
--- a/README
+++ b/README
@@ -11,11 +11,11 @@ The programs that can be built with this package are:
csplit cut date dd df dir dircolors dirname du echo env expand expr
factor false fmt fold groups head hostid hostname id install join kill
link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup
- nproc od paste pathchk pinky pr printenv printf ptx pwd readlink rm rmdir
- runcon seq sha1sum sha224sum sha256sum sha384sum sha512sum shred shuf
- sleep sort split stat stdbuf stty su sum sync tac tail tee test timeout
- touch tr true truncate tsort tty uname unexpand uniq unlink uptime users
- vdir wc who whoami yes
+ nproc od paste pathchk pinky pr printenv printf ptx pwd readlink realpath
+ rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum sha512sum shred
+ shuf sleep sort split stat stdbuf stty su sum sync tac tail tee test
+ timeout touch tr true truncate tsort tty uname unexpand uniq unlink
+ uptime users vdir wc who whoami yes
See the file NEWS for a list of major changes in the current release.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 6a67db809..8cdc26d24 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -94,6 +94,7 @@
* ptx: (coreutils)ptx invocation. Produce permuted indexes.
* pwd: (coreutils)pwd invocation. Print working directory.
* readlink: (coreutils)readlink invocation. Print referent of a symlink.
+* realpath: (coreutils)readpath invocation. Print resolved file names.
* rm: (coreutils)rm invocation. Remove files.
* rmdir: (coreutils)rmdir invocation. Remove empty directories.
* runcon: (coreutils)runcon invocation. Run in specified SELinux CTX.
@@ -194,7 +195,7 @@ Free Documentation License''.
* Printing text:: echo printf yes
* Conditions:: false true test expr
* Redirection:: tee
-* File name manipulation:: dirname basename pathchk mktemp
+* File name manipulation:: dirname basename pathchk mktemp realpath
* Working context:: pwd stty printenv tty
* User information:: id logname whoami groups users who
* System context:: date arch nproc uname hostname hostid uptime
@@ -381,6 +382,7 @@ File name manipulation
* dirname invocation:: Strip last file name component
* pathchk invocation:: Check file name validity and portability
* mktemp invocation:: Create temporary file or directory
+* realpath invocation:: Print resolved file names
Working context
@@ -9603,7 +9605,6 @@ Set the default SELinux security context to be used for created files.
@cindex displaying value of a symbolic link
@cindex canonical file name
@cindex canonicalize a file name
-@pindex realpath
@findex realpath
@command{readlink} may work in one of two supported modes:
@@ -9686,8 +9687,8 @@ Report error messages.
The @command{readlink} utility first appeared in OpenBSD 2.1.
-There is a @command{realpath} command on some systems
-which operates like @command{readlink} in canonicalize mode.
+The @command{realpath} command without options, operates like
+@command{readlink} in canonicalize mode.
@exitstatus
@@ -12354,6 +12355,7 @@ This section describes commands that manipulate file names.
* dirname invocation:: Strip last file name component.
* pathchk invocation:: Check file name validity and portability.
* mktemp invocation:: Create temporary file or directory.
+* realpath invocation:: Print resolved file names.
@end menu
@@ -12707,6 +12709,119 @@ Exit status:
@end display
+@node realpath invocation
+@section @command{realpath}: Print the resolved file name.
+
+@pindex realpath
+@cindex file names, canonicalization
+@cindex symlinks, resolution
+@cindex canonical file name
+@cindex canonicalize a file name
+@pindex realpath
+@findex realpath
+
+@command{realpath} expands all symbolic links and resolves references to
+@samp{/./}, @samp{/../} and extra @samp{/} characters. By default,
+all but the last component of the specified files must exist. Synopsis:
+
+@example
+realpath [@var{option}]@dots{} @var{file}@dots{}
+@end example
+
+The program accepts the following options. Also see @ref{Common options}.
+
+@table @samp
+
+@item -e
+@itemx --canonicalize-existing
+@opindex -e
+@opindex --canonicalize-existing
+Ensure that all components of the specified file names exist.
+If any component is missing or unavailable, @command{realpath} will output
+a diagnostic unless the @option{-q} option is specified, and exit with a
+nonzero exit code. A trailing slash requires that the name resolve to a
+directory.
+
+@item -m
+@itemx --canonicalize-missing
+@opindex -m
+@opindex --canonicalize-missing
+If any component of a specified file name is missing or unavailable,
+treat it as a directory.
+
+@item -L
+@itemx --logical
+@opindex -L
+@opindex --logical
+Symbolic links are resolved in the specified file names,
+but they are resolved after any subsequent @samp{..} components are processed.
+
+@item -P
+@itemx --physical
+@opindex -P
+@opindex --physical
+Symbolic links are resolved in the specified file names,
+and they are resolved before any subsequent @samp{..} components are processed.
+This is the default mode of operation.
+
+@item -q
+@itemx --quiet
+@opindex -q
+@opindex --quiet
+Suppress diagnostic messages for specified file names.
+
+@item -s
+@itemx --strip
+@itemx --no-symlinks
+@opindex -s
+@opindex --strip
+@opindex --no-symlinks
+Do not resolve symbolic links. Only resolve references to
+@samp{/./}, @samp{/../} and remove extra @samp{/} characters.
+When combined with the @option{-m} option, realpath operates
+only on the file name, and does not touch any actual file.
+
+@item -z
+@itemx --zero
+@opindex -z
+@opindex --zero
+Separate output items with @sc{nul} characters.
+
+@itemx --relative-to=@var{file}
+@opindex --relative-to
+@cindex relpath
+Print the resolved file names relative to the specified file.
+Note this option honors the @option{-m} and @option{-e} options
+pertaining to file existence.
+
+@itemx --relative-base=@var{base}
+@opindex --relative-base
+This option is valid when used with @option{--relative-to}, and will restrict
+the output of @option{--relative-to} so that relative names are output,
+only when @var{file}s are descendants of @var{base}. Otherwise output the
+absolute file name. Note: this option honors the @option{-m} and @option{-e}
+options pertaining to file existence. For example:
+
+@example
+realpath --relative-to=/usr /tmp /usr/bin
+@result{} ../tmp
+@result{} bin
+realpath --relative-base=/usr --relative-to=/usr /tmp /usr/bin
+@result{} /tmp
+@result{} bin
+@end example
+
+@end table
+
+@cindex exit status of @command{realpath}
+Exit status:
+
+@display
+0 if all file names were printed without issue.
+1 otherwise.
+@end display
+
+
@node Working context
@chapter Working context
diff --git a/man/.gitignore b/man/.gitignore
index 2c2ffcdef..9ae1183ed 100644
--- a/man/.gitignore
+++ b/man/.gitignore
@@ -59,6 +59,7 @@ printf.1
ptx.1
pwd.1
readlink.1
+realpath.1
rm.1
rmdir.1
seq.1
diff --git a/man/Makefile.am b/man/Makefile.am
index a8888996b..48a52d226 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -92,6 +92,7 @@ printf.1: $(common_dep) $(srcdir)/printf.x ../src/printf.c
ptx.1: $(common_dep) $(srcdir)/ptx.x ../src/ptx.c
pwd.1: $(common_dep) $(srcdir)/pwd.x ../src/pwd.c
readlink.1: $(common_dep) $(srcdir)/readlink.x ../src/readlink.c
+realpath.1: $(common_dep) $(srcdir)/realpath.x ../src/realpath.c
rm.1: $(common_dep) $(srcdir)/rm.x ../src/rm.c
rmdir.1: $(common_dep) $(srcdir)/rmdir.x ../src/rmdir.c
runcon.1: $(common_dep) $(srcdir)/runcon.x ../src/runcon.c
diff --git a/man/realpath.x b/man/realpath.x
new file mode 100644
index 000000000..a52fa2570
--- /dev/null
+++ b/man/realpath.x
@@ -0,0 +1,6 @@
+[NAME]
+realpath \- print the resolved path
+[DESCRIPTION]
+.\" Add any additional description here
+[SEE ALSO]
+readlink(1), readlink(2), realpath(3)
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ae4d84b5c..b00c0bb9a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -99,6 +99,7 @@ src/printf.c
src/ptx.c
src/pwd.c
src/readlink.c
+src/realpath.c
src/remove.c
src/rm.c
src/rmdir.c
diff --git a/scripts/git-hooks/commit-msg b/scripts/git-hooks/commit-msg
index 22f595ff2..46382ae1d 100755
--- a/scripts/git-hooks/commit-msg
+++ b/scripts/git-hooks/commit-msg
@@ -17,7 +17,7 @@ my @valid = qw(
expr factor false fmt fold groups head hostid hostname id install
join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp
mv nice nl nohup nproc od paste pathchk pinky pr printenv printf
- ptx pwd readlink rm rmdir runcon seq sha1sum sha224sum sha256sum
+ ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum
sha384sum sha512sum shred shuf sleep sort split stat stdbuf stty
su sum sync tac tail tee test timeout touch tr true truncate tsort
tty uname unexpand uniq unlink uptime users vdir wc who whoami yes
diff --git a/src/.gitignore b/src/.gitignore
index d25a8e57b..9c4c9b717 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -68,6 +68,7 @@ printf
ptx
pwd
readlink
+realpath
rm
rmdir
runcon
diff --git a/src/Makefile.am b/src/Makefile.am
index 6e0b3ae2d..db3159f7d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -97,6 +97,7 @@ EXTRA_PROGRAMS = \
ptx \
pwd \
readlink \
+ realpath \
rm \
rmdir \
runcon \
@@ -248,6 +249,7 @@ printf_LDADD = $(LDADD)
ptx_LDADD = $(LDADD)
pwd_LDADD = $(LDADD)
readlink_LDADD = $(LDADD)
+realpath_LDADD = $(LDADD)
rm_LDADD = $(LDADD)
rmdir_LDADD = $(LDADD)
runcon_LDADD = $(LDADD)
@@ -373,6 +375,7 @@ du_LDADD += $(LIBICONV)
getlimits_LDADD += $(LIBICONV)
printf_LDADD += $(LIBICONV)
ptx_LDADD += $(LIBICONV)
+realpath_LDADD += $(LIBICONV)
split_LDADD += $(LIBICONV)
stdbuf_LDADD += $(LIBICONV)
timeout_LDADD += $(LIBICONV)
diff --git a/src/realpath.c b/src/realpath.c
new file mode 100644
index 000000000..cb0ef2f1f
--- /dev/null
+++ b/src/realpath.c
@@ -0,0 +1,329 @@
+/* realpath - print the resolved path
+ Copyright (C) 2011-2012 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see