]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
realpath: a new program to print the resolved path
authorPádraig Brady <P@draigBrady.com>
Tue, 27 Dec 2011 00:30:23 +0000 (00:30 +0000)
committerPádraig Brady <P@draigBrady.com>
Tue, 3 Jan 2012 11:36:32 +0000 (11:36 +0000)
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.

15 files changed:
AUTHORS
NEWS
README
doc/coreutils.texi
man/.gitignore
man/Makefile.am
man/realpath.x [new file with mode: 0644]
po/POTFILES.in
scripts/git-hooks/commit-msg
src/.gitignore
src/Makefile.am
src/realpath.c [new file with mode: 0644]
tests/Makefile.am
tests/misc/help-version
tests/misc/realpath [new file with mode: 0755]

diff --git a/AUTHORS b/AUTHORS
index 0c15472fad73142ee005bd662e28b1457d58a0f9..d1024a56afd974961457fa6b16c745c4c7cc754d 100644 (file)
--- 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 32c8eab3b2d454996cf36bcbef85a991dc89f1fd..78a90b672c1b7700cca458e3bea4931836d1f40d 100644 (file)
--- 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 5c5bc0f7bc3231fc50c4410339c717afedc5b5fe..90b1d40c36839e9ec545fa9ee687ee234e200d3c 100644 (file)
--- 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.
 
index 6a67db809d585c389308ae0329883c7056a7ed31..8cdc26d24346e321c9c67d8f5dc35bb9412f3213 100644 (file)
@@ -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
 
index 2c2ffcdef4c4c4963fb0b9c63d0b5b8400a5b165..9ae1183ed07696af094cec50eafda8a63c96ca8e 100644 (file)
@@ -59,6 +59,7 @@ printf.1
 ptx.1
 pwd.1
 readlink.1
+realpath.1
 rm.1
 rmdir.1
 seq.1
index a8888996baa3fc0e7cfbe56f60bc5ecde2abb324..48a52d22654ebbc56fa2fc920f61487e2dfa1652 100644 (file)
@@ -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 (file)
index 0000000..a52fa25
--- /dev/null
@@ -0,0 +1,6 @@
+[NAME]
+realpath \- print the resolved path
+[DESCRIPTION]
+.\" Add any additional description here
+[SEE ALSO]
+readlink(1), readlink(2), realpath(3)
index ae4d84b5c0bf33f7cf049f1059833a017a2b839b..b00c0bb9a50a45eff66e23f27040cc5e0d717330 100644 (file)
@@ -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
index 22f595ff2c6e0c6423bbbf1dd0441975407907cc..46382ae1d07e3ca1b512660b6964f1373a668b45 100755 (executable)
@@ -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
index d25a8e57b097c5f54f268dcd0896973407f40640..9c4c9b7175067f3388f74f5a3b7173923aa90a67 100644 (file)
@@ -68,6 +68,7 @@ printf
 ptx
 pwd
 readlink
+realpath
 rm
 rmdir
 runcon
index 6e0b3ae2d5119921dfdd913f1e1d80b4c3dc9b58..db3159f7d6fa43091aba5dc534f8aca9b67c035d 100644 (file)
@@ -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 (file)
index 0000000..cb0ef2f
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Pádraig Brady.  */
+
+#include <config.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <sys/types.h>
+
+#include "system.h"
+#include "canonicalize.h"
+#include "error.h"
+#include "quote.h"
+
+/* The official name of this program (e.g., no `g' prefix).  */
+#define PROGRAM_NAME "realpath"
+
+#define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
+
+enum
+{
+  RELATIVE_TO_OPTION = CHAR_MAX + 1,
+  RELATIVE_BASE_OPTION
+};
+
+static bool verbose = true;
+static bool logical;
+static bool use_nuls;
+static const char *can_relative_to;
+static const char *can_relative_base;
+
+static struct option const longopts[] =
+{
+  {"canonicalize-existing", no_argument, NULL, 'e'},
+  {"canonicalize-missing", no_argument, NULL, 'm'},
+  {"relative-to", required_argument, NULL, RELATIVE_TO_OPTION},
+  {"relative-base", required_argument, NULL, RELATIVE_BASE_OPTION},
+  {"quiet", no_argument, NULL, 'q'},
+  {"strip", no_argument, NULL, 's' /* FIXME: deprecate in 2013 or so */},
+  {"no-symlinks", no_argument, NULL, 's'},
+  {"zero", no_argument, NULL, 'z'},
+  {"logical", no_argument, NULL, 'L'},
+  {"physical", no_argument, NULL, 'P'},
+  {GETOPT_HELP_OPTION_DECL},
+  {GETOPT_VERSION_OPTION_DECL},
+  {NULL, 0, NULL, 0}
+};
+
+void
+usage (int status)
+{
+  if (status != EXIT_SUCCESS)
+    fprintf (stderr, _("Try `%s --help' for more information.\n"),
+             program_name);
+  else
+    {
+      printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
+      fputs (_("\
+Print the resolved absolute file name;\n\
+all but the last component must exist\n\
+\n\
+"), stdout);
+      fputs (_("\
+  -e, --canonicalize-existing  all components of the path must exist\n\
+  -m, --canonicalize-missing   no components of the path need exist\n\
+  -L, --logical                resolve `..' components before symlinks\n\
+  -P, --physical               resolve symlinks as encountered (default)\n\
+  -q, --quiet                  suppress most error messages\n\
+      --relative-to=FILE       print the resolved path relative to FILE\n\
+      --relative-base=FILE     print absolute paths unless paths below FILE\n\
+  -s, --strip, --no-symlinks   don't expand symlinks\n\
+  -z, --zero                   separate output with NUL rather than newline\n\
+\n\
+"), stdout);
+      fputs (HELP_OPTION_DESCRIPTION, stdout);
+      fputs (VERSION_OPTION_DESCRIPTION, stdout);
+      emit_ancillary_info ();
+    }
+  exit (status);
+}
+
+/* A wrapper around canonicalize_filename_mode(),
+   to call it twice when in LOGICAL mode.  */
+static char *
+realpath_canon (const char *fname, int can_mode)
+{
+  char *can_fname = canonicalize_filename_mode (fname, can_mode);
+  if (logical && can_fname)  /* canonicalize again to resolve symlinks.  */
+    {
+      can_mode &= ~CAN_NOLINKS;
+      char *can_fname2 = canonicalize_filename_mode (can_fname, can_mode);
+      free (can_fname);
+      return can_fname2;
+    }
+  return can_fname;
+}
+
+/* Test whether prefix is parent or match of path.  */
+static bool _GL_ATTRIBUTE_PURE
+path_prefix (const char *prefix, const char *path)
+{
+  while (*prefix && *path)
+    {
+      if (*prefix != *path)
+        break;
+      prefix++;
+      path++;
+    }
+  return (!*prefix && (*path == '/' || !*path));
+}
+
+/* Return the length of the longest common prefix
+   of PATH1 and PATH2, ensuring only full path components
+   are matched.  Return 0 on no match.  */
+static int _GL_ATTRIBUTE_PURE
+path_common_prefix (const char *path1, const char *path2)
+{
+  int i = 0;
+  int ret = 0;
+
+  while (*path1 && *path2)
+    {
+      if (*path1 != *path2)
+        break;
+      if (*path1 == '/')
+        ret = i;
+      path1++;
+      path2++;
+      i++;
+    }
+
+  if (!*path1 && !*path2)
+    ret = i;
+  if (!*path1 && *path2 == '/')
+    ret = i;
+  if (!*path2 && *path1 == '/')
+    ret = i;
+
+  return ret;
+}
+
+/* Output the relative representation if requested.  */
+static bool
+relpath (const char *can_fname)
+{
+  if (can_relative_to)
+    {
+      /* Enforce --relative-base.  */
+      if (can_relative_base)
+        {
+          if (!path_prefix (can_relative_base, can_fname)
+              || !path_prefix (can_relative_base, can_relative_to))
+            return false;
+        }
+
+      /* Skip the prefix common to --relative-to and path.  */
+      int common_index = path_common_prefix (can_relative_to, can_fname);
+      const char *relto_suffix = can_relative_to + common_index;
+      const char *fname_suffix = can_fname + common_index;
+
+      /* Replace remaining components of --relative-to with '..', to get
+         to a common directory.  Then output the remainder of fname.  */
+      if (*relto_suffix)
+        {
+          ++relto_suffix;
+          printf ("%s", "..");
+          for (; *relto_suffix; ++relto_suffix)
+            {
+              if (*relto_suffix == '/')
+                printf ("%s", "/..");
+            }
+
+          printf ("%s", fname_suffix);
+        }
+      else
+        {
+          if (*fname_suffix)
+            printf ("%s", ++fname_suffix);
+          else
+            printf ("%c", '.');
+        }
+
+      putchar (use_nuls ? '\0' : '\n');
+
+      return true;
+    }
+
+  return false;
+}
+
+static bool
+isdir (const char *path)
+{
+  struct stat sb;
+  if (stat (path, &sb) != 0)
+    error (EXIT_FAILURE, errno, _("cannot stat %s"), quote (path));
+  return S_ISDIR (sb.st_mode);
+}
+
+static bool
+process_path (const char *fname, int can_mode)
+{
+  char *can_fname = realpath_canon (fname, can_mode);
+  if (!can_fname)
+    {
+      if (verbose)
+        error (0, errno, "%s", quote (fname));
+      return false;
+    }
+
+  if (!relpath (can_fname))
+    printf ("%s%c", can_fname, (use_nuls ? '\0' : '\n'));
+
+  free (can_fname);
+
+  return true;
+}
+
+int
+main (int argc, char **argv)
+{
+  bool ok = true;
+  int can_mode = CAN_ALL_BUT_LAST;
+  const char *relative_to = NULL;
+  const char *relative_base = NULL;
+
+  initialize_main (&argc, &argv);
+  set_program_name (argv[0]);
+  setlocale (LC_ALL, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+
+  atexit (close_stdout);
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "eLmPqsz", longopts, NULL);
+      if (c == -1)
+        break;
+      switch (c)
+        {
+        case 'e':
+          can_mode &= ~CAN_MODE_MASK;
+          can_mode |= CAN_EXISTING;
+          break;
+        case 'm':
+          can_mode &= ~CAN_MODE_MASK;
+          can_mode |= CAN_MISSING;
+          break;
+        case 'L':
+          can_mode |= CAN_NOLINKS;
+          logical = true;
+          break;
+        case 's':
+          can_mode |= CAN_NOLINKS;
+          logical = false;
+          break;
+        case 'P':
+          can_mode &= ~CAN_NOLINKS;
+          logical = false;
+          break;
+        case 'q':
+          verbose = false;
+          break;
+        case 'z':
+          use_nuls = true;
+          break;
+        case RELATIVE_TO_OPTION:
+          relative_to = optarg;
+          break;
+        case RELATIVE_BASE_OPTION:
+          relative_base = optarg;
+          break;
+        case_GETOPT_HELP_CHAR;
+        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+        default:
+          usage (EXIT_FAILURE);
+        }
+    }
+
+  if (optind >= argc)
+    {
+      error (0, 0, _("missing operand"));
+      usage (EXIT_FAILURE);
+    }
+
+  if (relative_base && !relative_to)
+    {
+      error (0, 0, _("--relative-base requires --relative-to"));
+      usage (EXIT_FAILURE);
+    }
+
+  bool need_dir = (can_mode & CAN_MODE_MASK) == CAN_EXISTING;
+  if (relative_to)
+    {
+      can_relative_to = realpath_canon (relative_to, can_mode);
+      if (!can_relative_to)
+        error (EXIT_FAILURE, errno, "%s", quote (relative_to));
+      if (need_dir && !isdir (can_relative_to))
+        error (EXIT_FAILURE, ENOTDIR, "%s", quote (relative_to));
+    }
+  if (relative_base)
+    {
+      can_relative_base = realpath_canon (relative_base, can_mode);
+      if (!can_relative_base)
+        error (EXIT_FAILURE, errno, "%s", quote (relative_base));
+      if (need_dir && !isdir (can_relative_base))
+        error (EXIT_FAILURE, ENOTDIR, "%s", quote (relative_base));
+    }
+
+  for (; optind < argc; ++optind)
+    ok &= process_path (argv[optind], can_mode);
+
+  exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
index 23cb70fa427a5ab6192551f0336b2193289e1f2f..894fcf8a2da8d3f6333c5a79b39e8695f360a49a 100644 (file)
@@ -220,6 +220,7 @@ TESTS =                                             \
   misc/printf-surprise                         \
   misc/pwd-long                                        \
   misc/readlink-fp-loop                                \
+  misc/realpath                                        \
   misc/runcon-no-reorder                       \
   misc/sha1sum                                 \
   misc/sha1sum-vec                             \
index d89d7e0f580a272fcbbceb32c1d5a20086237c5c..bba6d3663894f29e61d1a094a0cdc32e12a71065 100755 (executable)
@@ -177,6 +177,7 @@ ln_setup () { args="$tmp_in ln-target"; }
 ginstall_setup () { args="$tmp_in $tmp_in2"; }
 mv_setup () { args="$tmp_in $tmp_in2"; }
 mkdir_setup () { args=$tmp_dir/subdir; }
+realpath_setup () { args=$tmp_in; }
 rmdir_setup () { args=$tmp_dir; }
 rm_setup () { args=$tmp_in; }
 shred_setup () { args=$tmp_in; }
diff --git a/tests/misc/realpath b/tests/misc/realpath
new file mode 100755 (executable)
index 0000000..4b1e1f1
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/sh
+# Validate realpath operation
+
+# 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 <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+print_ver_ realpath
+
+# Setup dir, file, symlink structure
+
+mkdir -p dir1/dir2
+ln -s dir1/dir2 ldir2
+touch dir1/f dir1/dir2/f
+
+# Basic operaion
+realpath -Pqz . >/dev/null || fail=1
+# --relative-base and --relative-to require params
+realpath --relative-base --relative-to . && fail=1
+# --relative-base requires --relative-to
+realpath --relative-base=dir1 . && fail=1
+
+# -e --relative-* require directories
+realpath -e --relative-to=dir1/f --relative-base=. . && fail=1
+realpath -e --relative-to=dir1/  --relative-base=. . || fail=1
+
+# Note NUL params are unconditionally rejected by canonicalize_filename_mode
+realpath -m '' && fail=1
+
+# symlink resolution
+this=$(realpath .)
+test $(realpath $relative ldir2/..) = "$this/dir1" || fail=1
+test $(realpath -L $relative ldir2/..) = "$this" || fail=1
+test $(realpath -s $relative ldir2) = "$this/ldir2" || fail=1
+
+# relative string handling
+test $(realpath -m --relative-to=prefix prefixed/1) = '../prefixed/1' || fail=1
+test $(realpath -m --relative-to=prefixed prefix/1) = '../prefix/1' || fail=1
+test $(realpath -m --relative-to=prefixed prefixed/1) = '1' || fail=1
+
+Exit $fail