1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include <sys/types.h>
31 #if ! (HAVE_MBRLEN && HAVE_MBSTATE_T)
32 # define mbrlen(s, n, ps) 1
33 # define mbstate_t int
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "pathchk"
39 #define AUTHORS "Paul Eggert", "David MacKenzie", "Jim Meyering"
41 #ifndef _POSIX_PATH_MAX
42 # define _POSIX_PATH_MAX 256
44 #ifndef _POSIX_NAME_MAX
45 # define _POSIX_NAME_MAX 14
48 #ifdef _XOPEN_NAME_MAX
49 # define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
51 # define NAME_MAX_MINIMUM _POSIX_NAME_MAX
53 #ifdef _XOPEN_PATH_MAX
54 # define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
56 # define PATH_MAX_MINIMUM _POSIX_PATH_MAX
59 #if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
61 # define _PC_NAME_MAX 0
62 # define _PC_PATH_MAX 1
65 # define pathconf(file, flag) \
66 (flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
70 static bool validate_file_name (char *, bool, bool);
72 /* The name this program was run with. */
75 /* For long options that have no equivalent short option, use a
76 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
79 PORTABILITY_OPTION = CHAR_MAX + 1
82 static struct option const longopts[] =
84 {"portability", no_argument, NULL, PORTABILITY_OPTION},
85 {GETOPT_HELP_OPTION_DECL},
86 {GETOPT_VERSION_OPTION_DECL},
93 if (status != EXIT_SUCCESS)
94 fprintf (stderr, _("Try `%s --help' for more information.\n"),
98 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
100 Diagnose unportable constructs in NAME.\n\
102 -p check for most POSIX systems\n\
103 -P check for empty names and leading \"-\"\n\
104 --portability check for all POSIX systems (equivalent to -p -P)\n\
106 fputs (HELP_OPTION_DESCRIPTION, stdout);
107 fputs (VERSION_OPTION_DESCRIPTION, stdout);
108 emit_bug_reporting_address ();
114 main (int argc, char **argv)
117 bool check_basic_portability = false;
118 bool check_extra_portability = false;
121 initialize_main (&argc, &argv);
122 program_name = argv[0];
123 setlocale (LC_ALL, "");
124 bindtextdomain (PACKAGE, LOCALEDIR);
125 textdomain (PACKAGE);
127 atexit (close_stdout);
129 while ((optc = getopt_long (argc, argv, "+pP", longopts, NULL)) != -1)
133 case PORTABILITY_OPTION:
134 check_basic_portability = true;
135 check_extra_portability = true;
139 check_basic_portability = true;
143 check_extra_portability = true;
146 case_GETOPT_HELP_CHAR;
148 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
151 usage (EXIT_FAILURE);
157 error (0, 0, _("missing operand"));
158 usage (EXIT_FAILURE);
161 for (; optind < argc; ++optind)
162 ok &= validate_file_name (argv[optind],
163 check_basic_portability, check_extra_portability);
165 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
168 /* If FILE contains a component with a leading "-", report an error
169 and return false; otherwise, return true. */
172 no_leading_hyphen (char const *file)
176 for (p = file; (p = strchr (p, '-')); p++)
177 if (p == file || p[-1] == '/')
179 error (0, 0, _("leading `-' in a component of file name %s"),
187 /* If FILE (of length FILELEN) contains only portable characters,
188 return true, else report an error and return false. */
191 portable_chars_only (char const *file, size_t filelen)
193 size_t validlen = strspn (file,
195 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
196 "abcdefghijklmnopqrstuvwxyz"
198 char const *invalid = file + validlen;
202 mbstate_t mbstate = { 0, };
203 size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
205 _("nonportable character %s in file name %s"),
206 quotearg_n_style_mem (1, locale_quoting_style, invalid,
207 (charlen <= MB_LEN_MAX ? charlen : 1)),
215 /* Return the address of the start of the next file name component in F. */
218 component_start (char *f)
225 /* Return the size of the file name component F. F must be nonempty. */
228 component_len (char const *f)
231 for (len = 1; f[len] != '/' && f[len]; len++)
237 strlen (FILE) <= PATH_MAX
238 && strlen (each-existing-directory-in-FILE) <= NAME_MAX
240 If CHECK_BASIC_PORTABILITY is true, compare against _POSIX_PATH_MAX and
241 _POSIX_NAME_MAX instead, and make sure that FILE contains no
242 characters not in the POSIX portable filename character set, which
243 consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
245 If CHECK_BASIC_PORTABILITY is false, make sure that all leading directories
246 along FILE that exist are searchable.
248 If CHECK_EXTRA_PORTABILITY is true, check that file name components do not
251 If either CHECK_BASIC_PORTABILITY or CHECK_EXTRA_PORTABILITY is true,
252 check that the file name is not empty.
254 Return true if all of these tests are successful, false if any fail. */
257 validate_file_name (char *file, bool check_basic_portability,
258 bool check_extra_portability)
260 size_t filelen = strlen (file);
262 /* Start of file name component being checked. */
265 /* True if component lengths need to be checked. */
266 bool check_component_lengths;
268 /* True if the file is known to exist. */
269 bool file_exists = false;
271 if (check_extra_portability && ! no_leading_hyphen (file))
274 if ((check_basic_portability | check_extra_portability)
277 /* Fail, since empty names are not portable. As of
278 2005-01-06 POSIX does not address whether "pathchk -p ''"
279 should (or is allowed to) fail, so this is not a
280 conformance violation. */
281 error (0, 0, _("empty file name"));
285 if (check_basic_portability)
287 if (! portable_chars_only (file, filelen))
292 /* Check whether a file name component is in a directory that
293 is not searchable, or has some other serious problem.
294 POSIX does not allow "" as a file name, but some non-POSIX
295 hosts do (as an alias for "."), so allow "" if lstat does. */
298 if (lstat (file, &st) == 0)
300 else if (errno != ENOENT || filelen == 0)
302 error (0, errno, "%s", file);
307 if (check_basic_portability
308 || (! file_exists && PATH_MAX_MINIMUM <= filelen))
312 if (check_basic_portability)
313 maxsize = _POSIX_PATH_MAX;
317 char const *dir = (*file == '/' ? "/" : ".");
319 size = pathconf (dir, _PC_PATH_MAX);
320 if (size < 0 && errno != 0)
323 _("%s: unable to determine maximum file name length"),
327 maxsize = MIN (size, SIZE_MAX);
330 if (maxsize <= filelen)
332 unsigned long int len = filelen;
333 unsigned long int maxlen = maxsize - 1;
334 error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
335 maxlen, len, quote (file));
340 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
341 whether all file name components are so short that they are valid
342 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
343 it's more convenient to check component lengths below. */
345 check_component_lengths = check_basic_portability;
346 if (! check_component_lengths && ! file_exists)
348 for (start = file; *(start = component_start (start)); )
350 size_t length = component_len (start);
352 if (NAME_MAX_MINIMUM < length)
354 check_component_lengths = true;
362 if (check_component_lengths)
364 /* The limit on file name components for the current component.
365 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
366 systems (NFS, say?) where pathconf fails on "." or "/" with
368 size_t name_max = NAME_MAX_MINIMUM;
370 /* If nonzero, the known limit on file name components. */
371 size_t known_name_max = (check_basic_portability ? _POSIX_NAME_MAX : 0);
373 for (start = file; *(start = component_start (start)); )
378 name_max = known_name_max;
382 char const *dir = (start == file ? "." : file);
386 len = pathconf (dir, _PC_NAME_MAX);
389 name_max = MIN (len, SIZE_MAX);
394 /* There is no limit. */
399 /* DIR does not exist; use its parent's maximum. */
400 known_name_max = name_max;
405 error (0, errno, "%s", dir);
411 length = component_len (start);
413 if (name_max < length)
415 unsigned long int len = length;
416 unsigned long int maxlen = name_max;
420 _("limit %lu exceeded by length %lu "
421 "of file name component %s"),
422 maxlen, len, quote (start));