I just wrote a new program for shell-utils: realpath

It does the same thing as the standard library call of the same name; it
gets the canonical path to a given file.  It takes one argument, the path
name to canonicalize.  Currently it only supports the standard options.

I based my implementation off of dirname.c

The source to realpath.c is attached, as is a patch to sh-utils.texi adding
documentation for realpath.

To get better support of non-GNU systms it might be needed to include an
implementation of the realpath function.  I looked at the implementation in
glibc (in the file /stdlib/canonicalize.c) but I don't know how to modify
it to fit into the shellutils build.

I'm afraid I don't grok autoconf well enough to patch the build scripts.

Please let me know if there's anything else you might need to include this
program in GNU shell utils.  I'm willing to assign copyright to the FSF if
you tell me how to go about it.

-- 
Steven Barker                                      [EMAIL PROTECTED]
  "But don't you worry, its for a cause -- feeding global corporations' paws."
GnuPG public key: http://www.students.uiuc.edu/~scbarker/pubkey.asc
Fingerprint: 272A 3EC8 52CE F22B F745  775E 5292 F743 EBD5 936B
/* realpath - return the canonicalized absolute pathname
   Copyright (C) 2001 Steven Barker

   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 2, 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, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Written by Steven Barker. */

#include <config.h>
#include <stdio.h>
#include <sys/types.h>

/* this should be changed to use a generic realpath
   function if libc does not have one of its own */
#include <stdlib.h>
#include <limits.h>

#include "system.h"
#include "long-options.h"
#include "error.h"
#include "closeout.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "realpath"

#define AUTHORS "Steven Barker"

/* The name this program was run with. */
char *program_name;

void
usage (int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
	     program_name);
  else
    {
      printf (_("\
Usage: %s PATH\n\
  or:  %s OPTION\n\
"),
	      program_name, program_name);
      printf (_("\
Print PATH with all references to `.', `..', and symlinks resolved.\n\
\n\
  --help      display this help and exit\n\
  --version   output version information and exit\n\
"));
      puts (_("\nReport bugs to <[EMAIL PROTECTED]>."));
    }
  exit (status);
}

int
main (int argc, char **argv)
{
  char *result;
  long int path_max;

  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);

  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
		      AUTHORS, usage);
  /* The above handles --help and --version.
     Since there is no other invocation of getopt, handle `--' here.  */
  if (argc > 1 && STREQ (argv[1], "--"))
    {
      --argc;
      ++argv;
    }

  if (argc != 2)
    {
      error (0, 0, argc < 2 ? _("too few arguments")
	     : _("too many arguments"));
      usage (1);
    }

#ifdef PATH_MAX
  path_max = PATH_MAX;
#else
  path_max = pathconf (name, _PC_PATH_MAX);
  if (path_max <= 0)
    path_max = 4096;
#endif

  result = malloc(path_max);
   
  if(! realpath (argv[1], result))
    {
      free(result);
      error(1, errno, argv[1]);
    }

  printf ("%s\n", result);

  free(result);

  exit (0);
}
diff -u shellutils-2.0.11/doc/sh-utils.texi shellutils-withrealpath/doc/sh-utils.texi
--- shellutils-2.0.11/doc/sh-utils.texi Wed Oct 18 15:42:43 2000
+++ shellutils/doc/sh-utils.texi        Sat Oct  6 06:23:42 2001
@@ -44,6 +44,7 @@
 * printenv: (sh-utils)printenv invocation.      Print environment variables.
 * printf: (sh-utils)printf invocation.          Format and print data.
 * pwd: (sh-utils)pwd invocation.                Print working directory.
+* realpath: (sh-utils)realpath invocation.      Print canonicalized path name.
 * seq: (sh-utils)seq invocation.                Print numeric sequences
 * sleep: (sh-utils)sleep invocation.            Delay for a specified time.
 * stty: (sh-utils)stty invocation.              Print/change terminal settings.
@@ -131,7 +132,7 @@
 * Printing text::               echo printf yes
 * Conditions::                  false true test expr
 * Redirection::                 tee
-* File name manipulation::      dirname basename pathchk
+* File name manipulation::      dirname basename realpath pathchk
 * Working context::             pwd stty printenv tty
 * User information::            id logname whoami groups users who
 * System context::              date uname hostname
@@ -1045,6 +1046,7 @@
 @menu
 * basename invocation::         Strip directory and suffix from a file name.
 * dirname invocation::          Strip non-directory suffix from a file name.
+* realpath invocation::         Print canonical path to a file.
 * pathchk invocation::          Check file name portability.
 @end menu
 
@@ -1091,6 +1093,27 @@
 
 If @var{name} is a single component, @code{dirname} prints @samp{.}
 (meaning the current directory).
+
+The only options are @samp{--help} and @samp{--version}.  @xref{Common
+options}.
+
+
+@node realpath invocation
+@section @code{realpath}: Print canonicalized path to a file.
+
+@pindex realpath
+@cindex file names, canonicalizing
+@cindex canonical file names, finding
+@cindex symlinks, expanding
+
+@code{realpath} finds the canonical path to a file.  Synopsis:
+
+@example
+realpath @var{name}
+@end example
+
+@code{realpath} expands all symbolic links and resolves references to
+@samp{/./} and @samp{/../} and extra @samp{/} characters in @var{name}.
 
 The only options are @samp{--help} and @samp{--version}.  @xref{Common
 options}.

Attachment: msg00639/pgp00000.pgp
Description: PGP signature

Reply via email to