1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2008 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
30 #if ! (HAVE_MBRLEN && HAVE_MBSTATE_T)
31 # define mbrlen(s, n, ps) 1
32 # define mbstate_t int
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "pathchk"
39 proper_name ("Paul Eggert"), \
40 proper_name ("David MacKenzie"), \
41 proper_name ("Jim Meyering")
43 #ifndef _POSIX_PATH_MAX
44 # define _POSIX_PATH_MAX 256
46 #ifndef _POSIX_NAME_MAX
47 # define _POSIX_NAME_MAX 14
50 #ifdef _XOPEN_NAME_MAX
51 # define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
53 # define NAME_MAX_MINIMUM _POSIX_NAME_MAX
55 #ifdef _XOPEN_PATH_MAX
56 # define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
58 # define PATH_MAX_MINIMUM _POSIX_PATH_MAX
61 #if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
63 # define _PC_NAME_MAX 0
64 # define _PC_PATH_MAX 1
67 # define pathconf(file, flag) \
68 (flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
72 static bool validate_file_name (char *, bool, bool);
74 /* The name this program was run with. */
75 char const *program_name;
77 /* For long options that have no equivalent short option, use a
78 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
81 PORTABILITY_OPTION = CHAR_MAX + 1
84 static struct option const longopts[] =
86 {"portability", no_argument, NULL, PORTABILITY_OPTION},
87 {GETOPT_HELP_OPTION_DECL},
88 {GETOPT_VERSION_OPTION_DECL},
95 if (status != EXIT_SUCCESS)
96 fprintf (stderr, _("Try `%s --help' for more information.\n"),
100 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
102 Diagnose unportable constructs in NAME.\n\
104 -p check for most POSIX systems\n\
105 -P check for empty names and leading \"-\"\n\
106 --portability check for all POSIX systems (equivalent to -p -P)\n\
108 fputs (HELP_OPTION_DESCRIPTION, stdout);
109 fputs (VERSION_OPTION_DESCRIPTION, stdout);
110 emit_bug_reporting_address ();
116 main (int argc, char **argv)
119 bool check_basic_portability = false;
120 bool check_extra_portability = false;
123 initialize_main (&argc, &argv);
124 program_name = argv[0];
125 setlocale (LC_ALL, "");
126 bindtextdomain (PACKAGE, LOCALEDIR);
127 textdomain (PACKAGE);
129 atexit (close_stdout);
131 while ((optc = getopt_long (argc, argv, "+pP", longopts, NULL)) != -1)
135 case PORTABILITY_OPTION:
136 check_basic_portability = true;
137 check_extra_portability = true;
141 check_basic_portability = true;
145 check_extra_portability = true;
148 case_GETOPT_HELP_CHAR;
150 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
153 usage (EXIT_FAILURE);
159 error (0, 0, _("missing operand"));
160 usage (EXIT_FAILURE);
163 for (; optind < argc; ++optind)
164 ok &= validate_file_name (argv[optind],
165 check_basic_portability, check_extra_portability);
167 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
170 /* If FILE contains a component with a leading "-", report an error
171 and return false; otherwise, return true. */
174 no_leading_hyphen (char const *file)
178 for (p = file; (p = strchr (p, '-')); p++)
179 if (p == file || p[-1] == '/')
181 error (0, 0, _("leading `-' in a component of file name %s"),
189 /* If FILE (of length FILELEN) contains only portable characters,
190 return true, else report an error and return false. */
193 portable_chars_only (char const *file, size_t filelen)
195 size_t validlen = strspn (file,
197 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
198 "abcdefghijklmnopqrstuvwxyz"
200 char const *invalid = file + validlen;
204 mbstate_t mbstate = { 0, };
205 size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
207 _("nonportable character %s in file name %s"),
208 quotearg_n_style_mem (1, locale_quoting_style, invalid,
209 (charlen <= MB_LEN_MAX ? charlen : 1)),
217 /* Return the address of the start of the next file name component in F. */
220 component_start (char *f)
227 /* Return the size of the file name component F. F must be nonempty. */
230 component_len (char const *f)
233 for (len = 1; f[len] != '/' && f[len]; len++)
239 strlen (FILE) <= PATH_MAX
240 && strlen (each-existing-directory-in-FILE) <= NAME_MAX
242 If CHECK_BASIC_PORTABILITY is true, compare against _POSIX_PATH_MAX and
243 _POSIX_NAME_MAX instead, and make sure that FILE contains no
244 characters not in the POSIX portable filename character set, which
245 consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
247 If CHECK_BASIC_PORTABILITY is false, make sure that all leading directories
248 along FILE that exist are searchable.
250 If CHECK_EXTRA_PORTABILITY is true, check that file name components do not
253 If either CHECK_BASIC_PORTABILITY or CHECK_EXTRA_PORTABILITY is true,
254 check that the file name is not empty.
256 Return true if all of these tests are successful, false if any fail. */
259 validate_file_name (char *file, bool check_basic_portability,
260 bool check_extra_portability)
262 size_t filelen = strlen (file);
264 /* Start of file name component being checked. */
267 /* True if component lengths need to be checked. */
268 bool check_component_lengths;
270 /* True if the file is known to exist. */
271 bool file_exists = false;
273 if (check_extra_portability && ! no_leading_hyphen (file))
276 if ((check_basic_portability | check_extra_portability)
279 /* Fail, since empty names are not portable. As of
280 2005-01-06 POSIX does not address whether "pathchk -p ''"
281 should (or is allowed to) fail, so this is not a
282 conformance violation. */
283 error (0, 0, _("empty file name"));
287 if (check_basic_portability)
289 if (! portable_chars_only (file, filelen))
294 /* Check whether a file name component is in a directory that
295 is not searchable, or has some other serious problem.
296 POSIX does not allow "" as a file name, but some non-POSIX
297 hosts do (as an alias for "."), so allow "" if lstat does. */
300 if (lstat (file, &st) == 0)
302 else if (errno != ENOENT || filelen == 0)
304 error (0, errno, "%s", file);
309 if (check_basic_portability
310 || (! file_exists && PATH_MAX_MINIMUM <= filelen))
314 if (check_basic_portability)
315 maxsize = _POSIX_PATH_MAX;
319 char const *dir = (*file == '/' ? "/" : ".");
321 size = pathconf (dir, _PC_PATH_MAX);
322 if (size < 0 && errno != 0)
325 _("%s: unable to determine maximum file name length"),
329 maxsize = MIN (size, SIZE_MAX);
332 if (maxsize <= filelen)
334 unsigned long int len = filelen;
335 unsigned long int maxlen = maxsize - 1;
336 error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
337 maxlen, len, quote (file));
342 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
343 whether all file name components are so short that they are valid
344 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
345 it's more convenient to check component lengths below. */
347 check_component_lengths = check_basic_portability;
348 if (! check_component_lengths && ! file_exists)
350 for (start = file; *(start = component_start (start)); )
352 size_t length = component_len (start);
354 if (NAME_MAX_MINIMUM < length)
356 check_component_lengths = true;
364 if (check_component_lengths)
366 /* The limit on file name components for the current component.
367 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
368 systems (NFS, say?) where pathconf fails on "." or "/" with
370 size_t name_max = NAME_MAX_MINIMUM;
372 /* If nonzero, the known limit on file name components. */
373 size_t known_name_max = (check_basic_portability ? _POSIX_NAME_MAX : 0);
375 for (start = file; *(start = component_start (start)); )
380 name_max = known_name_max;
384 char const *dir = (start == file ? "." : file);
388 len = pathconf (dir, _PC_NAME_MAX);
391 name_max = MIN (len, SIZE_MAX);
396 /* There is no limit. */
401 /* DIR does not exist; use its parent's maximum. */
402 known_name_max = name_max;
407 error (0, errno, "%s", dir);
413 length = component_len (start);
415 if (name_max < length)
417 unsigned long int len = length;
418 unsigned long int maxlen = name_max;
422 _("limit %lu exceeded by length %lu "
423 "of file name component %s"),
424 maxlen, len, quote (start));