build: update gnulib submodule to latest
[coreutils.git] / src / pathchk.c
blob52d5a1a04ca51d168b3a8a1119e0952318757109
1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2024 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 <https://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 "quote.h"
26 /* The official name of this program (e.g., no 'g' prefix). */
27 #define PROGRAM_NAME "pathchk"
29 #define AUTHORS \
30 proper_name ("Paul Eggert"), \
31 proper_name ("David MacKenzie"), \
32 proper_name ("Jim Meyering")
34 #ifndef _POSIX_PATH_MAX
35 # define _POSIX_PATH_MAX 256
36 #endif
37 #ifndef _POSIX_NAME_MAX
38 # define _POSIX_NAME_MAX 14
39 #endif
41 #ifdef _XOPEN_NAME_MAX
42 # define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
43 #else
44 # define NAME_MAX_MINIMUM _POSIX_NAME_MAX
45 #endif
46 #ifdef _XOPEN_PATH_MAX
47 # define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
48 #else
49 # define PATH_MAX_MINIMUM _POSIX_PATH_MAX
50 #endif
52 #if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
53 # ifndef _PC_NAME_MAX
54 # define _PC_NAME_MAX 0
55 # define _PC_PATH_MAX 1
56 # endif
57 # ifndef pathconf
58 # define pathconf(file, flag) \
59 (flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
60 # endif
61 #endif
63 static bool validate_file_name (char *, bool, bool);
65 /* For long options that have no equivalent short option, use a
66 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
67 enum
69 PORTABILITY_OPTION = CHAR_MAX + 1
72 static struct option const longopts[] =
74 {"portability", no_argument, nullptr, PORTABILITY_OPTION},
75 {GETOPT_HELP_OPTION_DECL},
76 {GETOPT_VERSION_OPTION_DECL},
77 {nullptr, 0, nullptr, 0}
80 void
81 usage (int status)
83 if (status != EXIT_SUCCESS)
84 emit_try_help ();
85 else
87 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
88 fputs (_("\
89 Diagnose invalid or non-portable file names.\n\
90 \n\
91 -p check for most POSIX systems\n\
92 -P check for empty names and leading \"-\"\n\
93 --portability check for all POSIX systems (equivalent to -p -P)\n\
94 "), stdout);
95 fputs (HELP_OPTION_DESCRIPTION, stdout);
96 fputs (VERSION_OPTION_DESCRIPTION, stdout);
97 emit_ancillary_info (PROGRAM_NAME);
99 exit (status);
103 main (int argc, char **argv)
105 bool ok = true;
106 bool check_basic_portability = false;
107 bool check_extra_portability = false;
108 int optc;
110 initialize_main (&argc, &argv);
111 set_program_name (argv[0]);
112 setlocale (LC_ALL, "");
113 bindtextdomain (PACKAGE, LOCALEDIR);
114 textdomain (PACKAGE);
116 atexit (close_stdout);
118 while ((optc = getopt_long (argc, argv, "+pP", longopts, nullptr)) != -1)
120 switch (optc)
122 case PORTABILITY_OPTION:
123 check_basic_portability = true;
124 check_extra_portability = true;
125 break;
127 case 'p':
128 check_basic_portability = true;
129 break;
131 case 'P':
132 check_extra_portability = true;
133 break;
135 case_GETOPT_HELP_CHAR;
137 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
139 default:
140 usage (EXIT_FAILURE);
144 if (optind == argc)
146 error (0, 0, _("missing operand"));
147 usage (EXIT_FAILURE);
150 for (; optind < argc; ++optind)
151 ok &= validate_file_name (argv[optind],
152 check_basic_portability, check_extra_portability);
154 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
157 /* If FILE contains a component with a leading "-", report an error
158 and return false; otherwise, return true. */
160 static bool
161 no_leading_hyphen (char const *file)
163 char const *p;
165 for (p = file; (p = strchr (p, '-')); p++)
166 if (p == file || p[-1] == '/')
168 error (0, 0, _("leading '-' in a component of file name %s"),
169 quoteaf (file));
170 return false;
173 return true;
176 /* If FILE (of length FILELEN) contains only portable characters,
177 return true, else report an error and return false. */
179 static bool
180 portable_chars_only (char const *file, size_t filelen)
182 size_t validlen = strspn (file,
183 ("/"
184 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
185 "abcdefghijklmnopqrstuvwxyz"
186 "0123456789._-"));
187 char const *invalid = file + validlen;
189 if (*invalid)
191 mbstate_t mbstate; mbszero (&mbstate);
192 size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
193 error (0, 0,
194 _("non-portable character %s in file name %s"),
195 quotearg_n_style_mem (1, locale_quoting_style, invalid,
196 (charlen <= MB_LEN_MAX ? charlen : 1)),
197 quoteaf_n (0, file));
198 return false;
201 return true;
204 /* Return the address of the start of the next file name component in F. */
206 ATTRIBUTE_PURE
207 static char *
208 component_start (char *f)
210 while (*f == '/')
211 f++;
212 return f;
215 /* Return the size of the file name component F. F must be nonempty. */
217 ATTRIBUTE_PURE
218 static size_t
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 idx_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", quotef (file));
294 return false;
298 if (check_basic_portability
299 || (! file_exists && PATH_MAX_MINIMUM <= filelen))
301 idx_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, MIN (SSIZE_MAX, IDX_MAX));
321 if (maxsize <= filelen)
323 error (0, 0, _("limit %td exceeded by length %td of file name %s"),
324 maxsize - 1, filelen, quoteaf (file));
325 return false;
329 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
330 whether all file name components are so short that they are valid
331 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
332 it's more convenient to check component lengths below. */
334 check_component_lengths = check_basic_portability;
335 if (! check_component_lengths && ! file_exists)
337 for (start = file; *(start = component_start (start)); )
339 size_t length = component_len (start);
341 if (NAME_MAX_MINIMUM < length)
343 check_component_lengths = true;
344 break;
347 start += length;
351 if (check_component_lengths)
353 /* The limit on file name components for the current component.
354 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
355 systems (NFS, say?) where pathconf fails on "." or "/" with
356 errno == ENOENT. */
357 idx_t name_max = NAME_MAX_MINIMUM;
359 /* If nonzero, the known limit on file name components. */
360 idx_t known_name_max = check_basic_portability ? _POSIX_NAME_MAX : 0;
362 for (start = file; *(start = component_start (start)); )
364 idx_t length;
366 if (known_name_max)
367 name_max = known_name_max;
368 else
370 long int len;
371 char const *dir = (start == file ? "." : file);
372 char c = *start;
373 errno = 0;
374 *start = '\0';
375 len = pathconf (dir, _PC_NAME_MAX);
376 *start = c;
377 if (0 <= len)
378 name_max = MIN (len, MIN (SSIZE_MAX, IDX_MAX));
379 else
380 switch (errno)
382 case 0:
383 /* There is no limit. */
384 name_max = IDX_MAX;
385 break;
387 case ENOENT:
388 /* DIR does not exist; use its parent's maximum. */
389 known_name_max = name_max;
390 break;
392 default:
393 *start = '\0';
394 error (0, errno, "%s", quotef (dir));
395 *start = c;
396 return false;
400 length = component_len (start);
402 if (name_max < length)
404 char c = start[length];
405 start[length] = '\0';
406 error (0, 0,
407 _("limit %td exceeded by length %td "
408 "of file name component %s"),
409 name_max, length, quote (start));
410 start[length] = c;
411 return false;
414 start += length;
418 return true;