bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

proposed pathchk change, in response to today's POSIX interpretation


From: Paul Eggert
Subject: proposed pathchk change, in response to today's POSIX interpretation
Date: Thu, 06 Jan 2005 16:41:11 -0800

Here's a proposed patch to pathchk.  It's not urgent, as pathchk
conforms to POSIX now, but it implements a new -P option suggested
in a POSIX interpretation released today.

2005-01-06  Paul Eggert  <address@hidden>

        Respond to today's POSIX interpretation about pathchk -p.
        * NEWS: Document the changes.
        * doc/coreutils.texi (pathchk invocation): Likewise.
        * src/pathchk.c (PORTABILITY_OPTION): New constant.
        (longopts, usage, main, validate_file_name):
        Add support for new -P option.
        Reject empty file names (unless -p is not specified and the
        current system allows empty file names).
        Change --portability so that is now equivalent to -p -P.
        (no_leading_hyphen): New function.

Index: NEWS
===================================================================
RCS file: /fetish/cu/NEWS,v
retrieving revision 1.260
diff -p -u -r1.260 NEWS
--- NEWS        5 Jan 2005 08:01:09 -0000       1.260
+++ NEWS        7 Jan 2005 00:15:06 -0000
@@ -83,8 +83,20 @@ GNU coreutils NEWS                      
     It now consistently adjusts out-of-range nice values to the
     closest values in range; formerly it sometimes reported an error.
 
-  pathchk no longer accepts trailing options, e.g., "pathchk -p foo -b"
-  now treats -b as a file name to check, not as an invalid option.
+  pathchk changes:
+
+    It no longer accepts trailing options, e.g., "pathchk -p foo -b"
+    now treats -b as a file name to check, not as an invalid option.
+
+    It now rejects the empty name in the normal case.  That is,
+    "pathchk -p ''" now fails, and "pathchk ''" fails unless the
+    current host (contra POSIX) allows empty file names.
+
+    The new -P option checks whether a file name component has leading "-",
+    as suggested in interpretation "Austin-039:XCU:pathchk:pathchk -p"
+    <http://www.opengroup.org/austin/interps/doc.tpl?gdid=6232>.
+
+    The --portability option is now equivalent to -p -P.
 
   pr now supports page numbers up to 2**64 on most hosts, and it
   detects page number overflow instead of silently wrapping around.
Index: doc/coreutils.texi
===================================================================
RCS file: /fetish/cu/doc/coreutils.texi,v
retrieving revision 1.235
diff -p -u -r1.235 coreutils.texi
--- doc/coreutils.texi  4 Jan 2005 18:29:38 -0000       1.235
+++ doc/coreutils.texi  7 Jan 2005 00:15:15 -0000
@@ -10239,21 +10239,39 @@ its file system's maximum.
 A nonexistent @var{name} is not an error, so long a file with that
 name could be created under the above conditions.
 
-The program accepts the following option.  Also see @ref{Common options}.
+The program accepts the following options.  Also see @ref{Common options}.
 Options must precede operands.
 
 @table @samp
 
 @item -p
address@hidden --portability
 @opindex -p
address@hidden --portability
-Do not perform checks based on the underlying file system.  Instead,
-check the length of each file name and its components against the
address@hidden minimum limits for portability.  Also check that the file
-name contains only characters that are in the portable file name
+Instead of performing checks based on the underlying file system,
+print an message if any of these conditions is true:
+
address@hidden
address@hidden
+A file name is empty.
+
address@hidden
+The length of a file name or one of its components exceeds the
address@hidden minimum limits for portability.
+
address@hidden
+A file name contains a character outside the portable file name
 character set, namely, the ASCII letters and digits, @samp{-},
 @samp{.}, @samp{/}, and @samp{_}.
address@hidden enumerate
+
address@hidden -P
address@hidden -P
+Print a message if a file name contains a component that begins with
address@hidden
+
address@hidden --portability
address@hidden --portability
+Print a message if a file name is not portable to all @acronym{POSIX}
+hosts.  This option is equivalent to @samp{-p -P}.
 
 @end table
 
Index: src/pathchk.c
===================================================================
RCS file: /fetish/cu/src/pathchk.c,v
retrieving revision 1.83
diff -p -u -r1.83 pathchk.c
--- src/pathchk.c       18 Oct 2004 08:19:26 -0000      1.83
+++ src/pathchk.c       7 Jan 2005 00:15:51 -0000
@@ -68,14 +68,21 @@
 # endif
 #endif
 
-static bool validate_file_name (char *file, bool portability);
+static bool validate_file_name (char *, bool, bool);
 
 /* The name this program was run with. */
 char *program_name;
 
+/* For long options that have no equivalent short option, use a
+   non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
+enum
+{
+  PORTABILITY_OPTION = CHAR_MAX + 1
+};
+
 static struct option const longopts[] =
 {
-  {"portability", no_argument, NULL, 'p'},
+  {"portability", no_argument, NULL, PORTABILITY_OPTION},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -93,7 +100,9 @@ usage (int status)
       fputs (_("\
 Diagnose unportable constructs in NAME.\n\
 \n\
-  -p, --portability   check for all POSIX systems, not only this one\n\
+  -p                  check for most POSIX systems\n\
+  -P                  check for leading \"-\"\n\
+      --portability   check for all POSIX systems (equivalent to -p -P)\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -106,7 +115,8 @@ int
 main (int argc, char **argv)
 {
   bool ok = true;
-  bool check_portability = false;
+  bool check_posix_portability = false;
+  bool check_leading_hyphen = false;
   int optc;
 
   initialize_main (&argc, &argv);
@@ -117,12 +127,21 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  while ((optc = getopt_long (argc, argv, "+p", longopts, NULL)) != -1)
+  while ((optc = getopt_long (argc, argv, "+pP", longopts, NULL)) != -1)
     {
       switch (optc)
        {
+       case PORTABILITY_OPTION:
+         check_posix_portability = true;
+         check_leading_hyphen = true;
+         break;
+
        case 'p':
-         check_portability = true;
+         check_posix_portability = true;
+         break;
+
+       case 'P':
+         check_leading_hyphen = true;
          break;
 
        case_GETOPT_HELP_CHAR;
@@ -141,11 +160,31 @@ main (int argc, char **argv)
     }
 
   for (; optind < argc; ++optind)
-    ok &= validate_file_name (argv[optind], check_portability);
+    ok &= validate_file_name (argv[optind],
+                             check_posix_portability, check_leading_hyphen);
 
   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
+/* If FILE contains a component with a leading "-", report an error
+   and return false; otherwise, return true.  */
+
+static bool
+no_leading_hyphen (char const *file)
+{
+  char const *p;
+
+  for (p = file;  (p = strchr (p, '-'));  p++)
+    if (p == file || p[-1] == '/')
+      {
+       error (0, 0, _("leading `-' in a component of file name %s"),
+              quote (file));
+       return false;
+      }
+
+  return true;
+}
+
 /* If FILE (of length FILELEN) contains only portable characters,
    return true, else report an error and return false.  */
 
@@ -199,18 +238,22 @@ component_len (char const *f)
    strlen (FILE) <= PATH_MAX
    && strlen (each-existing-directory-in-FILE) <= NAME_MAX
 
-   If PORTABILITY is true, compare against _POSIX_PATH_MAX and
+   If CHECK_POSIX_PORTABILITY is true, compare against _POSIX_PATH_MAX and
    _POSIX_NAME_MAX instead, and make sure that FILE contains no
    characters not in the POSIX portable filename character set, which
    consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
 
-   If PORTABILITY is false, make sure that all leading directories
+   If CHECK_POSIX_PORTABILITY is false, make sure that all leading directories
    along FILE that exist are searchable.
 
+   If CHECK_LEADING_HYPHEN is true, check that file name components do not
+   begin with "-".
+
    Return true if all of these tests are successful, false if any fail.  */
 
 static bool
-validate_file_name (char *file, bool portability)
+validate_file_name (char *file, bool check_posix_portability,
+                   bool check_leading_hyphen)
 {
   size_t filelen = strlen (file);
 
@@ -220,17 +263,45 @@ validate_file_name (char *file, bool por
   /* True if component lengths need to be checked.  */
   bool check_component_lengths;
 
-  if (portability && ! portable_chars_only (file, filelen))
+  if (check_leading_hyphen && ! no_leading_hyphen (file))
     return false;
 
-  if (*file == '\0')
-    return true;
+  if (check_posix_portability)
+    {
+      if (! portable_chars_only (file, filelen))
+       return false;
+
+      if (filelen == 0)
+       {
+         /* Fail, since empty names are not portable.  As of
+            2005-01-06 POSIX does not address whether "pathchk -p ''"
+            should (or is allowed to) fail, so this is not a
+            conformance violation.  */
+         error (0, 0, _("empty file name"));
+         return false;
+       }
+    }
+  else
+    {
+      /* Check whether a file name component is in a directory that
+        is not searchable, or has some other serious problem.
+        POSIX does not allow "" as a file name, but some non-POSIX
+        hosts do (as an alias for "."), so allow "" if lstat does.  */
+
+      struct stat st;
+      if (! (lstat (file, &st) == 0
+            || (errno == ENOENT && filelen != 0)))
+       {
+         error (0, errno, "%s", file);
+         return false;
+       }
+    }
 
-  if (portability || PATH_MAX_MINIMUM <= filelen)
+  if (check_posix_portability || PATH_MAX_MINIMUM <= filelen)
     {
       size_t maxsize;
 
-      if (portability)
+      if (check_posix_portability)
        maxsize = _POSIX_PATH_MAX;
       else
        {
@@ -258,25 +329,12 @@ validate_file_name (char *file, bool por
        }
     }
 
-  if (! portability)
-    {
-      /* Check whether a file name component is in a directory that
-        is not searchable, or has some other serious problem.  */
-
-      struct stat st;
-      if (lstat (file, &st) != 0 && errno != ENOENT)
-       {
-         error (0, errno, "%s", file);
-         return false;
-       }
-    }
-
   /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
      whether all file name components are so short that they are valid
-     in any file system on this platform.  If PORTABILITY, though,
+     in any file system on this platform.  If CHECK_POSIX_PORTABILITY, though,
      it's more convenient to check component lengths below.  */
 
-  check_component_lengths = portability;
+  check_component_lengths = check_posix_portability;
   if (! check_component_lengths)
     {
       for (start = file; *(start = component_start (start)); )
@@ -302,7 +360,7 @@ validate_file_name (char *file, bool por
       size_t name_max = NAME_MAX_MINIMUM;
 
       /* If nonzero, the known limit on file name components.  */
-      size_t known_name_max = (portability ? _POSIX_NAME_MAX : 0);
+      size_t known_name_max = (check_posix_portability ? _POSIX_NAME_MAX : 0);
 
       for (start = file; *(start = component_start (start)); )
        {





reply via email to

[Prev in Thread] Current Thread [Next in Thread]