1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2005 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>
28 #include "euidaccess.h"
32 #if ! (HAVE_MBRLEN && HAVE_MBSTATE_T)
33 # define mbrlen(s, n, ps) 1
34 # define mbstate_t int
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "pathchk"
40 #define AUTHORS "Paul Eggert", "David MacKenzie", "Jim Meyering"
42 #ifndef _POSIX_PATH_MAX
43 # define _POSIX_PATH_MAX 256
45 #ifndef _POSIX_NAME_MAX
46 # define _POSIX_NAME_MAX 14
49 #ifdef _XOPEN_NAME_MAX
50 # define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
52 # define NAME_MAX_MINIMUM _POSIX_NAME_MAX
54 #ifdef _XOPEN_PATH_MAX
55 # define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
57 # define PATH_MAX_MINIMUM _POSIX_PATH_MAX
60 #if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
62 # define _PC_NAME_MAX 0
63 # define _PC_PATH_MAX 1
66 # define pathconf(file, flag) \
67 (flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
71 static bool validate_file_name (char *, bool, bool);
73 /* The name this program was run with. */
76 /* For long options that have no equivalent short option, use a
77 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
80 PORTABILITY_OPTION
= CHAR_MAX
+ 1
83 static struct option
const longopts
[] =
85 {"portability", no_argument
, NULL
, PORTABILITY_OPTION
},
86 {GETOPT_HELP_OPTION_DECL
},
87 {GETOPT_VERSION_OPTION_DECL
},
94 if (status
!= EXIT_SUCCESS
)
95 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
99 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name
);
101 Diagnose unportable constructs in NAME.\n\
103 -p check for most POSIX systems\n\
104 -P check for empty names and leading \"-\"\n\
105 --portability check for all POSIX systems (equivalent to -p -P)\n\
107 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
108 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
109 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
115 main (int argc
, char **argv
)
118 bool check_basic_portability
= false;
119 bool check_extra_portability
= false;
122 initialize_main (&argc
, &argv
);
123 program_name
= argv
[0];
124 setlocale (LC_ALL
, "");
125 bindtextdomain (PACKAGE
, LOCALEDIR
);
126 textdomain (PACKAGE
);
128 atexit (close_stdout
);
130 while ((optc
= getopt_long (argc
, argv
, "+pP", longopts
, NULL
)) != -1)
134 case PORTABILITY_OPTION
:
135 check_basic_portability
= true;
136 check_extra_portability
= true;
140 check_basic_portability
= true;
144 check_extra_portability
= true;
147 case_GETOPT_HELP_CHAR
;
149 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
152 usage (EXIT_FAILURE
);
158 error (0, 0, _("missing operand"));
159 usage (EXIT_FAILURE
);
162 for (; optind
< argc
; ++optind
)
163 ok
&= validate_file_name (argv
[optind
],
164 check_basic_portability
, check_extra_portability
);
166 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);
169 /* If FILE contains a component with a leading "-", report an error
170 and return false; otherwise, return true. */
173 no_leading_hyphen (char const *file
)
177 for (p
= file
; (p
= strchr (p
, '-')); p
++)
178 if (p
== file
|| p
[-1] == '/')
180 error (0, 0, _("leading `-' in a component of file name %s"),
188 /* If FILE (of length FILELEN) contains only portable characters,
189 return true, else report an error and return false. */
192 portable_chars_only (char const *file
, size_t filelen
)
194 size_t validlen
= strspn (file
,
196 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
197 "abcdefghijklmnopqrstuvwxyz"
199 char const *invalid
= file
+ validlen
;
203 mbstate_t mbstate
= {0};
204 size_t charlen
= mbrlen (invalid
, filelen
- validlen
, &mbstate
);
206 _("nonportable character %s in file name %s"),
207 quotearg_n_style_mem (1, locale_quoting_style
, invalid
,
208 (charlen
<= MB_LEN_MAX
? charlen
: 1)),
216 /* Return the address of the start of the next file name component in F. */
219 component_start (char *f
)
226 /* Return the size of the file name component F. F must be nonempty. */
229 component_len (char const *f
)
232 for (len
= 1; f
[len
] != '/' && f
[len
]; len
++)
238 strlen (FILE) <= PATH_MAX
239 && strlen (each-existing-directory-in-FILE) <= NAME_MAX
241 If CHECK_BASIC_PORTABILITY is true, compare against _POSIX_PATH_MAX and
242 _POSIX_NAME_MAX instead, and make sure that FILE contains no
243 characters not in the POSIX portable filename character set, which
244 consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
246 If CHECK_BASIC_PORTABILITY is false, make sure that all leading directories
247 along FILE that exist are searchable.
249 If CHECK_EXTRA_PORTABILITY is true, check that file name components do not
252 If either CHECK_BASIC_PORTABILITY or CHECK_EXTRA_PORTABILITY is true,
253 check that the file name is not empty.
255 Return true if all of these tests are successful, false if any fail. */
258 validate_file_name (char *file
, bool check_basic_portability
,
259 bool check_extra_portability
)
261 size_t filelen
= strlen (file
);
263 /* Start of file name component being checked. */
266 /* True if component lengths need to be checked. */
267 bool check_component_lengths
;
269 /* True if the file is known to exist. */
270 bool file_exists
= false;
272 if (check_extra_portability
&& ! no_leading_hyphen (file
))
275 if ((check_basic_portability
| check_extra_portability
)
278 /* Fail, since empty names are not portable. As of
279 2005-01-06 POSIX does not address whether "pathchk -p ''"
280 should (or is allowed to) fail, so this is not a
281 conformance violation. */
282 error (0, 0, _("empty file name"));
286 if (check_basic_portability
)
288 if (! portable_chars_only (file
, filelen
))
293 /* Check whether a file name component is in a directory that
294 is not searchable, or has some other serious problem.
295 POSIX does not allow "" as a file name, but some non-POSIX
296 hosts do (as an alias for "."), so allow "" if lstat does. */
299 if (lstat (file
, &st
) == 0)
301 else if (errno
!= ENOENT
|| filelen
== 0)
303 error (0, errno
, "%s", file
);
308 if (check_basic_portability
309 || (! file_exists
&& PATH_MAX_MINIMUM
<= filelen
))
313 if (check_basic_portability
)
314 maxsize
= _POSIX_PATH_MAX
;
318 char const *dir
= (*file
== '/' ? "/" : ".");
320 size
= pathconf (dir
, _PC_PATH_MAX
);
321 if (size
< 0 && errno
!= 0)
324 _("%s: unable to determine maximum file name length"),
328 maxsize
= MIN (size
, SIZE_MAX
);
331 if (maxsize
<= filelen
)
333 unsigned long int len
= filelen
;
334 unsigned long int maxlen
= maxsize
- 1;
335 error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
336 maxlen
, len
, quote (file
));
341 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
342 whether all file name components are so short that they are valid
343 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
344 it's more convenient to check component lengths below. */
346 check_component_lengths
= check_basic_portability
;
347 if (! check_component_lengths
&& ! file_exists
)
349 for (start
= file
; *(start
= component_start (start
)); )
351 size_t length
= component_len (start
);
353 if (NAME_MAX_MINIMUM
< length
)
355 check_component_lengths
= true;
363 if (check_component_lengths
)
365 /* The limit on file name components for the current component.
366 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
367 systems (NFS, say?) where pathconf fails on "." or "/" with
369 size_t name_max
= NAME_MAX_MINIMUM
;
371 /* If nonzero, the known limit on file name components. */
372 size_t known_name_max
= (check_basic_portability
? _POSIX_NAME_MAX
: 0);
374 for (start
= file
; *(start
= component_start (start
)); )
379 name_max
= known_name_max
;
383 char const *dir
= (start
== file
? "." : file
);
387 len
= pathconf (dir
, _PC_NAME_MAX
);
390 name_max
= MIN (len
, SIZE_MAX
);
395 /* There is no limit. */
400 /* DIR does not exist; use its parent's maximum. */
401 known_name_max
= name_max
;
406 error (0, errno
, "%s", dir
);
412 length
= component_len (start
);
414 if (name_max
< length
)
416 unsigned long int len
= length
;
417 unsigned long int maxlen
= name_max
;
421 _("limit %lu exceeded by length %lu "
422 "of file name component %s"),
423 maxlen
, len
, quote (start
));