1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2010 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>
28 #if ! (HAVE_MBRLEN && HAVE_MBSTATE_T)
29 # define mbrlen(s, n, ps) 1
30 # define mbstate_t int
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "pathchk"
37 proper_name ("Paul Eggert"), \
38 proper_name ("David MacKenzie"), \
39 proper_name ("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 /* For long options that have no equivalent short option, use a
73 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
76 PORTABILITY_OPTION
= CHAR_MAX
+ 1
79 static struct option
const longopts
[] =
81 {"portability", no_argument
, NULL
, PORTABILITY_OPTION
},
82 {GETOPT_HELP_OPTION_DECL
},
83 {GETOPT_VERSION_OPTION_DECL
},
90 if (status
!= EXIT_SUCCESS
)
91 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
95 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name
);
97 Diagnose invalid or unportable file names.\n\
99 -p check for most POSIX systems\n\
100 -P check for empty names and leading \"-\"\n\
101 --portability check for all POSIX systems (equivalent to -p -P)\n\
103 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
104 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
105 emit_ancillary_info ();
111 main (int argc
, char **argv
)
114 bool check_basic_portability
= false;
115 bool check_extra_portability
= false;
118 initialize_main (&argc
, &argv
);
119 set_program_name (argv
[0]);
120 setlocale (LC_ALL
, "");
121 bindtextdomain (PACKAGE
, LOCALEDIR
);
122 textdomain (PACKAGE
);
124 atexit (close_stdout
);
126 while ((optc
= getopt_long (argc
, argv
, "+pP", longopts
, NULL
)) != -1)
130 case PORTABILITY_OPTION
:
131 check_basic_portability
= true;
132 check_extra_portability
= true;
136 check_basic_portability
= true;
140 check_extra_portability
= true;
143 case_GETOPT_HELP_CHAR
;
145 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
148 usage (EXIT_FAILURE
);
154 error (0, 0, _("missing operand"));
155 usage (EXIT_FAILURE
);
158 for (; optind
< argc
; ++optind
)
159 ok
&= validate_file_name (argv
[optind
],
160 check_basic_portability
, check_extra_portability
);
162 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);
165 /* If FILE contains a component with a leading "-", report an error
166 and return false; otherwise, return true. */
169 no_leading_hyphen (char const *file
)
173 for (p
= file
; (p
= strchr (p
, '-')); p
++)
174 if (p
== file
|| p
[-1] == '/')
176 error (0, 0, _("leading `-' in a component of file name %s"),
184 /* If FILE (of length FILELEN) contains only portable characters,
185 return true, else report an error and return false. */
188 portable_chars_only (char const *file
, size_t filelen
)
190 size_t validlen
= strspn (file
,
192 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
193 "abcdefghijklmnopqrstuvwxyz"
195 char const *invalid
= file
+ validlen
;
199 DECLARE_ZEROED_AGGREGATE (mbstate_t, mbstate
);
200 size_t charlen
= mbrlen (invalid
, filelen
- validlen
, &mbstate
);
202 _("nonportable character %s in file name %s"),
203 quotearg_n_style_mem (1, locale_quoting_style
, invalid
,
204 (charlen
<= MB_LEN_MAX
? charlen
: 1)),
212 /* Return the address of the start of the next file name component in F. */
215 component_start (char *f
)
222 /* Return the size of the file name component F. F must be nonempty. */
225 component_len (char const *f
)
228 for (len
= 1; f
[len
] != '/' && f
[len
]; len
++)
234 strlen (FILE) <= PATH_MAX
235 && strlen (each-existing-directory-in-FILE) <= NAME_MAX
237 If CHECK_BASIC_PORTABILITY is true, compare against _POSIX_PATH_MAX and
238 _POSIX_NAME_MAX instead, and make sure that FILE contains no
239 characters not in the POSIX portable filename character set, which
240 consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
242 If CHECK_BASIC_PORTABILITY is false, make sure that all leading directories
243 along FILE that exist are searchable.
245 If CHECK_EXTRA_PORTABILITY is true, check that file name components do not
248 If either CHECK_BASIC_PORTABILITY or CHECK_EXTRA_PORTABILITY is true,
249 check that the file name is not empty.
251 Return true if all of these tests are successful, false if any fail. */
254 validate_file_name (char *file
, bool check_basic_portability
,
255 bool check_extra_portability
)
257 size_t filelen
= strlen (file
);
259 /* Start of file name component being checked. */
262 /* True if component lengths need to be checked. */
263 bool check_component_lengths
;
265 /* True if the file is known to exist. */
266 bool file_exists
= false;
268 if (check_extra_portability
&& ! no_leading_hyphen (file
))
271 if ((check_basic_portability
|| check_extra_portability
)
274 /* Fail, since empty names are not portable. As of
275 2005-01-06 POSIX does not address whether "pathchk -p ''"
276 should (or is allowed to) fail, so this is not a
277 conformance violation. */
278 error (0, 0, _("empty file name"));
282 if (check_basic_portability
)
284 if (! portable_chars_only (file
, filelen
))
289 /* Check whether a file name component is in a directory that
290 is not searchable, or has some other serious problem.
291 POSIX does not allow "" as a file name, but some non-POSIX
292 hosts do (as an alias for "."), so allow "" if lstat does. */
295 if (lstat (file
, &st
) == 0)
297 else if (errno
!= ENOENT
|| filelen
== 0)
299 error (0, errno
, "%s", file
);
304 if (check_basic_portability
305 || (! file_exists
&& PATH_MAX_MINIMUM
<= filelen
))
309 if (check_basic_portability
)
310 maxsize
= _POSIX_PATH_MAX
;
314 char const *dir
= (*file
== '/' ? "/" : ".");
316 size
= pathconf (dir
, _PC_PATH_MAX
);
317 if (size
< 0 && errno
!= 0)
320 _("%s: unable to determine maximum file name length"),
324 maxsize
= MIN (size
, SSIZE_MAX
);
327 if (maxsize
<= filelen
)
329 unsigned long int len
= filelen
;
330 unsigned long int maxlen
= maxsize
- 1;
331 error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
332 maxlen
, len
, quote (file
));
337 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
338 whether all file name components are so short that they are valid
339 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
340 it's more convenient to check component lengths below. */
342 check_component_lengths
= check_basic_portability
;
343 if (! check_component_lengths
&& ! file_exists
)
345 for (start
= file
; *(start
= component_start (start
)); )
347 size_t length
= component_len (start
);
349 if (NAME_MAX_MINIMUM
< length
)
351 check_component_lengths
= true;
359 if (check_component_lengths
)
361 /* The limit on file name components for the current component.
362 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
363 systems (NFS, say?) where pathconf fails on "." or "/" with
365 size_t name_max
= NAME_MAX_MINIMUM
;
367 /* If nonzero, the known limit on file name components. */
368 size_t known_name_max
= (check_basic_portability
? _POSIX_NAME_MAX
: 0);
370 for (start
= file
; *(start
= component_start (start
)); )
375 name_max
= known_name_max
;
379 char const *dir
= (start
== file
? "." : file
);
383 len
= pathconf (dir
, _PC_NAME_MAX
);
386 name_max
= MIN (len
, SSIZE_MAX
);
391 /* There is no limit. */
396 /* DIR does not exist; use its parent's maximum. */
397 known_name_max
= name_max
;
402 error (0, errno
, "%s", dir
);
408 length
= component_len (start
);
410 if (name_max
< length
)
412 unsigned long int len
= length
;
413 unsigned long int maxlen
= name_max
;
417 _("limit %lu exceeded by length %lu "
418 "of file name component %s"),
419 maxlen
, len
, quote (start
));