dd: synchronize output after write errors
[coreutils.git] / src / pathchk.c
blobbc2b750dc7890ae326596af8a92ee986e4a7ac73
1 /* pathchk -- check whether file names are valid or portable
2 Copyright (C) 1991-2022 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 "error.h"
25 #include "quote.h"
27 /* The official name of this program (e.g., no 'g' prefix). */
28 #define PROGRAM_NAME "pathchk"
30 #define AUTHORS \
31 proper_name ("Paul Eggert"), \
32 proper_name ("David MacKenzie"), \
33 proper_name ("Jim Meyering")
35 #ifndef _POSIX_PATH_MAX
36 # define _POSIX_PATH_MAX 256
37 #endif
38 #ifndef _POSIX_NAME_MAX
39 # define _POSIX_NAME_MAX 14
40 #endif
42 #ifdef _XOPEN_NAME_MAX
43 # define NAME_MAX_MINIMUM _XOPEN_NAME_MAX
44 #else
45 # define NAME_MAX_MINIMUM _POSIX_NAME_MAX
46 #endif
47 #ifdef _XOPEN_PATH_MAX
48 # define PATH_MAX_MINIMUM _XOPEN_PATH_MAX
49 #else
50 # define PATH_MAX_MINIMUM _POSIX_PATH_MAX
51 #endif
53 #if ! (HAVE_PATHCONF && defined _PC_NAME_MAX && defined _PC_PATH_MAX)
54 # ifndef _PC_NAME_MAX
55 # define _PC_NAME_MAX 0
56 # define _PC_PATH_MAX 1
57 # endif
58 # ifndef pathconf
59 # define pathconf(file, flag) \
60 (flag == _PC_NAME_MAX ? NAME_MAX_MINIMUM : PATH_MAX_MINIMUM)
61 # endif
62 #endif
64 static bool validate_file_name (char *, bool, bool);
66 /* For long options that have no equivalent short option, use a
67 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
68 enum
70 PORTABILITY_OPTION = CHAR_MAX + 1
73 static struct option const longopts[] =
75 {"portability", no_argument, NULL, PORTABILITY_OPTION},
76 {GETOPT_HELP_OPTION_DECL},
77 {GETOPT_VERSION_OPTION_DECL},
78 {NULL, 0, NULL, 0}
81 void
82 usage (int status)
84 if (status != EXIT_SUCCESS)
85 emit_try_help ();
86 else
88 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
89 fputs (_("\
90 Diagnose invalid or unportable file names.\n\
91 \n\
92 -p check for most POSIX systems\n\
93 -P check for empty names and leading \"-\"\n\
94 --portability check for all POSIX systems (equivalent to -p -P)\n\
95 "), stdout);
96 fputs (HELP_OPTION_DESCRIPTION, stdout);
97 fputs (VERSION_OPTION_DESCRIPTION, stdout);
98 emit_ancillary_info (PROGRAM_NAME);
100 exit (status);
104 main (int argc, char **argv)
106 bool ok = true;
107 bool check_basic_portability = false;
108 bool check_extra_portability = false;
109 int optc;
111 initialize_main (&argc, &argv);
112 set_program_name (argv[0]);
113 setlocale (LC_ALL, "");
114 bindtextdomain (PACKAGE, LOCALEDIR);
115 textdomain (PACKAGE);
117 atexit (close_stdout);
119 while ((optc = getopt_long (argc, argv, "+pP", longopts, NULL)) != -1)
121 switch (optc)
123 case PORTABILITY_OPTION:
124 check_basic_portability = true;
125 check_extra_portability = true;
126 break;
128 case 'p':
129 check_basic_portability = true;
130 break;
132 case 'P':
133 check_extra_portability = true;
134 break;
136 case_GETOPT_HELP_CHAR;
138 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
140 default:
141 usage (EXIT_FAILURE);
145 if (optind == argc)
147 error (0, 0, _("missing operand"));
148 usage (EXIT_FAILURE);
151 for (; optind < argc; ++optind)
152 ok &= validate_file_name (argv[optind],
153 check_basic_portability, check_extra_portability);
155 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
158 /* If FILE contains a component with a leading "-", report an error
159 and return false; otherwise, return true. */
161 static bool
162 no_leading_hyphen (char const *file)
164 char const *p;
166 for (p = file; (p = strchr (p, '-')); p++)
167 if (p == file || p[-1] == '/')
169 error (0, 0, _("leading '-' in a component of file name %s"),
170 quoteaf (file));
171 return false;
174 return true;
177 /* If FILE (of length FILELEN) contains only portable characters,
178 return true, else report an error and return false. */
180 static bool
181 portable_chars_only (char const *file, size_t filelen)
183 size_t validlen = strspn (file,
184 ("/"
185 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
186 "abcdefghijklmnopqrstuvwxyz"
187 "0123456789._-"));
188 char const *invalid = file + validlen;
190 if (*invalid)
192 mbstate_t mbstate = { 0, };
193 size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
194 error (0, 0,
195 _("nonportable character %s in file name %s"),
196 quotearg_n_style_mem (1, locale_quoting_style, invalid,
197 (charlen <= MB_LEN_MAX ? charlen : 1)),
198 quoteaf_n (0, file));
199 return false;
202 return true;
205 /* Return the address of the start of the next file name component in F. */
207 ATTRIBUTE_PURE
208 static char *
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 ATTRIBUTE_PURE
219 static size_t
220 component_len (char const *f)
222 size_t len;
223 for (len = 1; f[len] != '/' && f[len]; len++)
224 continue;
225 return len;
228 /* Make sure that
229 strlen (FILE) <= PATH_MAX
230 && strlen (each-existing-directory-in-FILE) <= NAME_MAX
232 If CHECK_BASIC_PORTABILITY is true, compare against _POSIX_PATH_MAX and
233 _POSIX_NAME_MAX instead, and make sure that FILE contains no
234 characters not in the POSIX portable filename character set, which
235 consists of A-Z, a-z, 0-9, ., _, - (plus / for separators).
237 If CHECK_BASIC_PORTABILITY is false, make sure that all leading directories
238 along FILE that exist are searchable.
240 If CHECK_EXTRA_PORTABILITY is true, check that file name components do not
241 begin with "-".
243 If either CHECK_BASIC_PORTABILITY or CHECK_EXTRA_PORTABILITY is true,
244 check that the file name is not empty.
246 Return true if all of these tests are successful, false if any fail. */
248 static bool
249 validate_file_name (char *file, bool check_basic_portability,
250 bool check_extra_portability)
252 size_t filelen = strlen (file);
254 /* Start of file name component being checked. */
255 char *start;
257 /* True if component lengths need to be checked. */
258 bool check_component_lengths;
260 /* True if the file is known to exist. */
261 bool file_exists = false;
263 if (check_extra_portability && ! no_leading_hyphen (file))
264 return false;
266 if ((check_basic_portability || check_extra_portability)
267 && filelen == 0)
269 /* Fail, since empty names are not portable. As of
270 2005-01-06 POSIX does not address whether "pathchk -p ''"
271 should (or is allowed to) fail, so this is not a
272 conformance violation. */
273 error (0, 0, _("empty file name"));
274 return false;
277 if (check_basic_portability)
279 if (! portable_chars_only (file, filelen))
280 return false;
282 else
284 /* Check whether a file name component is in a directory that
285 is not searchable, or has some other serious problem.
286 POSIX does not allow "" as a file name, but some non-POSIX
287 hosts do (as an alias for "."), so allow "" if lstat does. */
289 struct stat st;
290 if (lstat (file, &st) == 0)
291 file_exists = true;
292 else if (errno != ENOENT || filelen == 0)
294 error (0, errno, "%s", quotef (file));
295 return false;
299 if (check_basic_portability
300 || (! file_exists && PATH_MAX_MINIMUM <= filelen))
302 size_t maxsize;
304 if (check_basic_portability)
305 maxsize = _POSIX_PATH_MAX;
306 else
308 long int size;
309 char const *dir = (*file == '/' ? "/" : ".");
310 errno = 0;
311 size = pathconf (dir, _PC_PATH_MAX);
312 if (size < 0 && errno != 0)
314 error (0, errno,
315 _("%s: unable to determine maximum file name length"),
316 dir);
317 return false;
319 maxsize = MIN (size, SSIZE_MAX);
322 if (maxsize <= filelen)
324 unsigned long int len = filelen;
325 unsigned long int maxlen = maxsize - 1;
326 error (0, 0, _("limit %lu exceeded by length %lu of file name %s"),
327 maxlen, len, quoteaf (file));
328 return false;
332 /* Check whether pathconf (..., _PC_NAME_MAX) can be avoided, i.e.,
333 whether all file name components are so short that they are valid
334 in any file system on this platform. If CHECK_BASIC_PORTABILITY, though,
335 it's more convenient to check component lengths below. */
337 check_component_lengths = check_basic_portability;
338 if (! check_component_lengths && ! file_exists)
340 for (start = file; *(start = component_start (start)); )
342 size_t length = component_len (start);
344 if (NAME_MAX_MINIMUM < length)
346 check_component_lengths = true;
347 break;
350 start += length;
354 if (check_component_lengths)
356 /* The limit on file name components for the current component.
357 This defaults to NAME_MAX_MINIMUM, for the sake of non-POSIX
358 systems (NFS, say?) where pathconf fails on "." or "/" with
359 errno == ENOENT. */
360 size_t name_max = NAME_MAX_MINIMUM;
362 /* If nonzero, the known limit on file name components. */
363 size_t known_name_max = (check_basic_portability ? _POSIX_NAME_MAX : 0);
365 for (start = file; *(start = component_start (start)); )
367 size_t length;
369 if (known_name_max)
370 name_max = known_name_max;
371 else
373 long int len;
374 char const *dir = (start == file ? "." : file);
375 char c = *start;
376 errno = 0;
377 *start = '\0';
378 len = pathconf (dir, _PC_NAME_MAX);
379 *start = c;
380 if (0 <= len)
381 name_max = MIN (len, SSIZE_MAX);
382 else
383 switch (errno)
385 case 0:
386 /* There is no limit. */
387 name_max = SIZE_MAX;
388 break;
390 case ENOENT:
391 /* DIR does not exist; use its parent's maximum. */
392 known_name_max = name_max;
393 break;
395 default:
396 *start = '\0';
397 error (0, errno, "%s", quotef (dir));
398 *start = c;
399 return false;
403 length = component_len (start);
405 if (name_max < length)
407 unsigned long int len = length;
408 unsigned long int maxlen = name_max;
409 char c = start[len];
410 start[len] = '\0';
411 error (0, 0,
412 _("limit %lu exceeded by length %lu "
413 "of file name component %s"),
414 maxlen, len, quote (start));
415 start[len] = c;
416 return false;
419 start += length;
423 return true;