-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 According to Eric Blake on 3/23/2009 2:53 PM: > Follow-up Comment #3, bug #24949 (project coreutils): > > How about the attached patch?
To make it easier to review, this is my proposed patch to make pwd understand -L and -P, per POSIX. I made the choice of having the behavior depend on POSIXLY_CORRECT, because I disagree with POSIX' choice of mandating the -L option as the default (too many scripts expect /bin/pwd to behave differently from pwd). - -- Don't work too hard, make some time for fun as well! Eric Blake e...@byu.net -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (Cygwin) Comment: Public key at home.comcast.net/~ericblake/eblake.gpg Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAknI0PUACgkQ84KuGfSFAYAtggCgyoC7CyXAmKYhJE4PrHLN020c ytIAnRcN7uZxTRHy+GdpiVm4nVH+tOoE =C96Q -----END PGP SIGNATURE-----
From 596f7e825631434335477be3370724b1702810dd Mon Sep 17 00:00:00 2001 From: Eric Blake <e...@byu.net> Date: Mon, 23 Mar 2009 14:48:19 -0600 Subject: [PATCH] pwd: support -L and -P * src/pwd.c (longopts): New variable. (logical_getcwd): New function. (main): Use it. (usage): Document new options. * doc/coreutils.texi (pwd invocation): Likewise. * NEWS: Likewise. * tests/misc/pwd-option: New file. * tests/Makefile.am (TESTS): Add test. * THANKS: Update. Reported by Paul D. Smith, in savannah bug 24949. --- NEWS | 6 +++ THANKS | 1 + doc/coreutils.texi | 38 ++++++++++++++++++--- src/pwd.c | 86 +++++++++++++++++++++++++++++++++++++++++++++--- tests/Makefile.am | 1 + tests/misc/pwd-option | 64 ++++++++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 12 deletions(-) create mode 100755 tests/misc/pwd-option diff --git a/NEWS b/NEWS index 766f271..3d64d87 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,12 @@ GNU coreutils NEWS -*- outline -*- * Noteworthy changes in release ?.? (????-??-??) [?] +** New features + + pwd now accepts the options --logical (-L) and --physical (-P). For + compatibility with existing scripts, -P is the default behavior + unless POSIXLY_CORRECT is requested. + ** Bug fixes cat once again immediately outputs data it has processed. diff --git a/THANKS b/THANKS index d4b2d61..665a9ef 100644 --- a/THANKS +++ b/THANKS @@ -437,6 +437,7 @@ Oliver Kiddle okid...@yahoo.co.uk Oskar Liljeblad o...@hem.passagen.se Pádraig Brady p...@draigbrady.com Patrick Mauritz oxyg...@studentenbude.ath.cx +Paul D. Smith psm...@gnu.org Paul Eggert egg...@twinsun.com Paul Ghaleb paul.gha...@st.com Paul Jarc p...@po.cwru.edu diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 04db676..55f601f 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -11872,13 +11872,39 @@ pwd invocation @cindex current working directory, printing @cindex working directory, printing -...@cindex symbolic links and @command{pwd} -...@command{pwd} prints the fully resolved name of the current directory. -That is, all components of the printed name will be actual directory -names---none will be symbolic links. -The only options are a lone @option{--help} or -...@option{--version}. @xref{Common options}. +...@command{pwd} prints the name of the current directory. Synopsis: + +...@example +pwd [...@var{option}]@dots{} +...@end example + +The program accepts the following options. Also see @ref{Common options}. + +...@table @samp +...@item -L +...@itemx --logical +...@opindex -L +...@opindex --logical +If the contents of the environment variable @env{PWD} provide an +absolute name of the current directory with no @samp{.} or @samp{..} +components, but possibly with symbolic links, then output those +contents. Otherwise, fall back to default @option{-P} handling. + +...@item -P +...@itemx --physical +...@opindex -P +...@opindex --physical +Print a fully resolved name for the current directory. That is, all +components of the printed name will be actual directory names---none +will be symbolic links. +...@end table + +...@cindex symbolic links and @command{pwd} +If @option{-L} and @option{-P} are both given, the last one takes +precedence. If neither option is given, then this implementation uses +...@option{-p} as the default unless the @env{POSIXLY_CORRECT} +environment variable is set. @mayConflictWithShellBuiltIn{pwd} diff --git a/src/pwd.c b/src/pwd.c index ac59155..21bb3d7 100644 --- a/src/pwd.c +++ b/src/pwd.c @@ -1,5 +1,5 @@ /* pwd - print current directory - Copyright (C) 1994-1997, 1999-2008 Free Software Foundation, Inc. + Copyright (C) 1994-1997, 1999-2009 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 @@ -38,6 +38,15 @@ struct file_name char *start; }; +static struct option const longopts[] = +{ + {"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) { @@ -46,11 +55,15 @@ usage (int status) program_name); else { - printf (_("Usage: %s [OPTION]\n"), program_name); + printf (_("Usage: %s [OPTION]...\n"), program_name); fputs (_("\ Print the full filename of the current working directory.\n\ \n\ "), stdout); + fputs (_("\ + -L, --logical use PWD from environment, even if it contains symlinks\n\ + -P, --physical avoid all symlinks\n\ +"), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME); @@ -279,10 +292,43 @@ robust_getcwd (struct file_name *file_name) file_name_prepend (file_name, "", 0); } + +/* Return PWD from the environment if it is acceptable for 'pwd -L' + output, otherwise NULL. */ +static char * +logical_getcwd (void) +{ + struct stat st1; + struct stat st2; + char *wd = getenv ("PWD"); + char *p; + + /* Textual validation first. */ + if (!wd || wd[0] != '/') + return NULL; + p = wd; + while ((p = strstr (p, "/."))) + { + if (!p[2] || p[2] == '/' + || (p[2] == '.' && (!p[3] || p[3] == '/'))) + return NULL; + p++; + } + + /* System call validation. */ + if (stat (wd, &st1) == 0 && stat (".", &st2) == 0 + && st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) + return wd; + return NULL; +} + + int main (int argc, char **argv) { char *wd; + /* POSIX requires a default of -L, but most scripts expect -P. */ + bool logical = (getenv ("POSIXLY_CORRECT") != NULL); initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -292,14 +338,42 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", NULL, NULL) != -1) - usage (EXIT_FAILURE); + while (1) + { + int c = getopt_long (argc, argv, "LP", longopts, NULL); + if (c == -1) + break; + switch (c) + { + case 'L': + logical = true; + break; + case 'P': + logical = false; + break; + + case_GETOPT_HELP_CHAR; + + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + + default: + usage (EXIT_FAILURE); + } + } if (optind < argc) error (0, 0, _("ignoring non-option arguments")); + if (logical) + { + wd = logical_getcwd (); + if (wd) + { + puts (wd); + exit (EXIT_SUCCESS); + } + } + wd = xgetcwd (); if (wd != NULL) { diff --git a/tests/Makefile.am b/tests/Makefile.am index 3a15a87..5f150ad 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -142,6 +142,7 @@ TESTS = \ misc/join \ pr/pr-tests \ misc/df-P \ + misc/pwd-option \ misc/pwd-unreadable-parent \ misc/chcon-fail \ misc/cut \ diff --git a/tests/misc/pwd-option b/tests/misc/pwd-option new file mode 100755 index 0000000..d6f0e41 --- /dev/null +++ b/tests/misc/pwd-option @@ -0,0 +1,64 @@ +#!/bin/sh +# Ensure that pwd options work. + +# Copyright (C) 2009 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/>. + +if test "$VERBOSE" = yes; then + set -x + env -- pwd --version +fi + +. $srcdir/test-lib.sh + +mkdir -p a/b || framework_failure +ln -s a/b c || framework_failure +base=`env -- pwd` + +# Remove any logical paths from $PWD. +cd "$base" || framework_failure +test "x$PWD" = "x$base" || framework_failure + +# Enter a logical directory. +cd c || fail=1 +test "x$PWD" = "x$base/c" || skip_test_ "cd does not properly update \$PWD" + +fail=0 +env -- pwd -L > out || fail=1 +printf %s\\n "$base/c" > exp || fail=1 + +env -- pwd --logical -P >> out || fail=1 +printf %s\\n "$base/a/b" >> exp || fail=1 + +env -- pwd --physical >> out || fail=1 +printf %s\\n "$base/a/b" >> exp || fail=1 + +# By default, we use -P unless POSIXLY_CORRECT. +env -- pwd >> out || fail=1 +printf %s\\n "$base/a/b" >> exp || fail=1 + +env -- POSIXLY_CORRECT=1 pwd >> out || fail=1 +printf %s\\n "$base/c" >> exp || fail=1 + +# Make sure we reject bogus values, and silently fall back to -P. +env -- PWD=$PWD/. pwd >> out || fail=1 +printf %s\\n "$base/a/b" >> exp || fail=1 + +env -- PWD=bogus pwd >> out || fail=1 +printf %s\\n "$base/a/b" >> exp || fail=1 + +compare out exp || fail=1 + +Exit $fail -- 1.6.1.2
_______________________________________________ Bug-coreutils mailing list Bug-coreutils@gnu.org http://lists.gnu.org/mailman/listinfo/bug-coreutils