md5sum, sha*sum: improve help for --check related options
[coreutils.git] / src / pathchk.c
blob28567d08130b5f7b7f3071ab4c4313138ac2b1ec
1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2013 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/>. */
17 #include <config.h>
18 #include <stdio.h>
19 #include <getopt.h>
20 #include <sys/types.h>
21 #include <wchar.h>
23 #include "system.h"
24 #include "error.h"
25 #include "quote.h"
26 #include "quotearg.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "pathchk"
31 #define AUTHORS \
32 proper_name ("Paul Eggert"), \
33 proper_name ("David MacKenzie"), \
34 proper_name ("Jim Meyering")
36 #ifndef _POSIX_PATH_MAX
37 # define _POSIX_PATH_MAX 256
38 #endif
39 #ifndef _POSIX_NAME_MAX
40 # define _POSIX_NAME_MAX 14
41 #endif
43 #ifdef _XOPEN_NAME_MAX
44 # define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
45 #else
46 # define NAME_MAX_MINIMUM _POSIX_NAME_MAX
47 #endif
48 #ifdef _XOPEN_PATH_MAX
49 # define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
50 #else
51 # define PATH_MAX_MINIMUM _POSIX_PATH_MAX
52 #endif
54 #if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
55 # ifndef _PC_NAME_MAX
56 # define _PC_NAME_MAX 0
57 # define _PC_PATH_MAX 1
58 # endif
59 # ifndef pathconf
60 # define pathconf(file, flag) \
61 (flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
62 # endif
63 #endif
65 static bool validate_file_name (char *, bool, bool);
67 /* For long options that have no equivalent short option, use a
68 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
69 enum
71 PORTABILITY_OPTION = CHAR_MAX + 1
74 static struct option const longopts[] =
76 {"portability", no_argument, NULL, PORTABILITY_OPTION},
77 {GETOPT_HELP_OPTION_DECL},
78 {GETOPT_VERSION_OPTION_DECL},
79 {NULL, 0, NULL, 0}
82 void
83 usage (int status)
85 if (status != EXIT_SUCCESS)
86 emit_try_help ();
87 else
89 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
90 fputs (_("\
91 Diagnose invalid or unportable file names.\n\
92 \n\
93 -p check for most POSIX systems\n\
94 -P check for empty names and leading \"-\"\n\
95 --portability check for all POSIX systems (equivalent to -p -P)\n\
96 "), stdout);
97 fputs (HELP_OPTION_DESCRIPTION, stdout);
98 fputs (VERSION_OPTION_DESCRIPTION, stdout);
99 emit_ancillary_info ();
101 exit (status);
105 main (int argc, char **argv)
107 bool ok = true;
108 bool check_basic_portability = false;
109 bool check_extra_portability = false;
110 int optc;
112 initialize_main (&argc, &argv);
113 set_program_name (argv[0]);
114 setlocale (LC_ALL, "");
115 bindtextdomain (PACKAGE, LOCALEDIR);
116 textdomain (PACKAGE);
118 atexit (close_stdout);
120 while ((optc = getopt_long (argc, argv, "+pP", longopts, NULL)) != -1)
122 switch (optc)
124 case PORTABILITY_OPTION:
125 check_basic_portability = true;
126 check_extra_portability = true;
127 break;
129 case 'p':
130 check_basic_portability = true;
131 break;
133 case 'P':
134 check_extra_portability = true;
135 break;
137 case_GETOPT_HELP_CHAR;
139 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
141 default:
142 usage (EXIT_FAILURE);
146 if (optind == argc)
148 error (0, 0, _("missing operand"));
149 usage (EXIT_FAILURE);
152 for (; optind < argc; ++optind)
153 ok &= validate_file_name (argv[optind],
154 check_basic_portability, check_extra_portability);
156 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
159 /* If FILE contains a component with a leading "-", report an error
160 and return false; otherwise, return true. */
162 static bool
163 no_leading_hyphen (char const *file)
165 char const *p;
167 for (p = file; (p = strchr (p, '-')); p++)
168 if (p == file || p[-1] == '/')
170 error (0, 0, _("leading '-' in a component of file name %s"),
171 quote (file));
172 return false;
175 return true;
178 /* If FILE (of length FILELEN) contains only portable characters,
179 return true, else report an error and return false. */
181 static bool
182 portable_chars_only (char const *file, size_t filelen)
184 size_t validlen = strspn (file,
185 ("/"
186 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
187 "abcdefghijklmnopqrstuvwxyz"
188 "0123456789._-"));
189 char const *invalid = file + validlen;
191 if (*invalid)
193 mbstate_t mbstate = { 0, };
194 size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
195 error (0, 0,
196 _("nonportable character %s in file name %s"),
197 quotearg_n_style_mem (1, locale_quoting_style, invalid,
198 (charlen <= MB_LEN_MAX ? charlen : 1)),
199 quote_n (0, file));
200 return false;
203 return true;
206 /* Return the address of the start of the next file name component in F. */
208 static char * _GL_ATTRIBUTE_PURE
209 component_start (char *f)
211 while (*f == '/')
212 f++;
213 return f;
216 /* Return the size of the file name component F. F must be nonempty. */
218 static size_t _GL_ATTRIBUTE_PURE
219 component_len (char const *f)
221 size_t len;
222 for (len = 1; f[len] != '/' && f[len]; len++)
223 continue;
224 return len;
227 /* Make sure that
228 strlen (FILE) <= PATH_MAX
229 && strlen (each-existing-directory-in-FILE) <= NAME_MAX
231 If CHECK_BASIC_PORTABILITY is true, compare against _POSIX_PATH_MAX and
232 _POSIX_NAME_MAX instead, and make sure that FILE contains no
233 characters not in the POSIX portable filename character set, which
234 consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
236 If CHECK_BASIC_PORTABILITY is false, make sure that all leading directories
237 along FILE that exist are searchable.
239 If CHECK_EXTRA_PORTABILITY is true, check that file name components do not
240 begin with "-".
242 If either CHECK_BASIC_PORTABILITY or CHECK_EXTRA_PORTABILITY is true,
243 check that the file name is not empty.
245 Return true if all of these tests are successful, false if any fail. */
247 static bool
248 validate_file_name (char *file, bool check_basic_portability,
249 bool check_extra_portability)
251 size_t filelen = strlen (file);
253 /* Start of file name component being checked. */
254 char *start;
256 /* True if component lengths need to be checked. */
257 bool check_component_lengths;
259 /* True if the file is known to exist. */
260 bool file_exists = false;
262 if (check_extra_portability && ! no_leading_hyphen (file))
263 return false;
265 if ((check_basic_portability || check_extra_portability)
266 && filelen == 0)
268 /* Fail, since empty names are not portable. As of
269 2005-01-06 POSIX does not address whether "pathchk -p ''"
270 should (or is allowed to) fail, so this is not a
271 conformance violation. */
272 error (0, 0, _("empty file name"));
273 return false;
276 if (check_basic_portability)
278 if (! portable_chars_only (file, filelen))
279 return false;
281 else
283 /* Check whether a file name component is in a directory that
284 is not searchable, or has some other serious problem.
285 POSIX does not allow "" as a file name, but some non-POSIX
286 hosts do (as an alias for "."), so allow "" if lstat does. */
288 struct stat st;
289 if (lstat (file, &st) == 0)
290 file_exists = true;
291 else if (errno != ENOENT || filelen == 0)
293 error (0, errno, "%s", file);
294 return false;
298 if (check_basic_portability
299 || (! file_exists && PATH_MAX_MINIMUM <= filelen))
301 size_t maxsize;
303 if (check_basic_portability)
304 maxsize = _POSIX_PATH_MAX;
305 else
307 long int size;
308 char const *dir = (*file == '/' ? "/" : ".");
309 errno = 0;
310 size = pathconf (dir, _PC_PATH_MAX);
311 if (size < 0 && errno != 0)
313 error (0, errno,
314 _("%s: unable to determine maximum file name length"),
315 dir);
316 return false;
318 maxsize = MIN (size, SSIZE_MAX);
321 if (maxsize <= filelen)
323 unsigned long int len = filelen;
324 unsigned long int maxlen = maxsize - 1;
325 error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
326 maxlen, len, quote (file));
327 return false;
331 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
332 whether all file name components are so short that they are valid
333 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
334 it's more convenient to check component lengths below. */
336 check_component_lengths = check_basic_portability;
337 if (! check_component_lengths && ! file_exists)
339 for (start = file; *(start = component_start (start)); )
341 size_t length = component_len (start);
343 if (NAME_MAX_MINIMUM < length)
345 check_component_lengths = true;
346 break;
349 start += length;
353 if (check_component_lengths)
355 /* The limit on file name components for the current component.
356 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
357 systems (NFS, say?) where pathconf fails on "." or "/" with
358 errno == ENOENT. */
359 size_t name_max = NAME_MAX_MINIMUM;
361 /* If nonzero, the known limit on file name components. */
362 size_t known_name_max = (check_basic_portability ? _POSIX_NAME_MAX : 0);
364 for (start = file; *(start = component_start (start)); )
366 size_t length;
368 if (known_name_max)
369 name_max = known_name_max;
370 else
372 long int len;
373 char const *dir = (start == file ? "." : file);
374 char c = *start;
375 errno = 0;
376 *start = '\0';
377 len = pathconf (dir, _PC_NAME_MAX);
378 *start = c;
379 if (0 <= len)
380 name_max = MIN (len, SSIZE_MAX);
381 else
382 switch (errno)
384 case 0:
385 /* There is no limit. */
386 name_max = SIZE_MAX;
387 break;
389 case ENOENT:
390 /* DIR does not exist; use its parent's maximum. */
391 known_name_max = name_max;
392 break;
394 default:
395 *start = '\0';
396 error (0, errno, "%s", dir);
397 *start = c;
398 return false;
402 length = component_len (start);
404 if (name_max < length)
406 unsigned long int len = length;
407 unsigned long int maxlen = name_max;
408 char c = start[len];
409 start[len] = '\0';
410 error (0, 0,
411 _("limit %lu exceeded by length %lu "
412 "of file name component %s"),
413 maxlen, len, quote (start));
414 start[len] = c;
415 return false;
418 start += length;
422 return true;