tests: add fold(1) test for --bytes option
[coreutils.git] / src / system.h
blobfc038a9797995c484588b1d5d3e0d1ff56a96ce7
1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989-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 this file _after_ system headers if possible. */
19 #include <attribute.h>
21 #include <alloca.h>
23 #include <sys/stat.h>
25 /* Commonly used file permission combination. */
26 #define MODE_RW_UGO (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
28 #if HAVE_SYS_PARAM_H
29 # include <sys/param.h>
30 #endif
32 #include <unistd.h>
34 #include <limits.h>
36 #include "pathmax.h"
37 #ifndef PATH_MAX
38 # define PATH_MAX 8192
39 #endif
41 #include "configmake.h"
43 #include <sys/time.h>
44 #include <time.h>
46 /* Since major is a function on SVR4, we can't use 'ifndef major'. */
47 #if MAJOR_IN_MKDEV
48 # include <sys/mkdev.h>
49 # define HAVE_MAJOR
50 #endif
51 #if MAJOR_IN_SYSMACROS
52 # include <sys/sysmacros.h>
53 # define HAVE_MAJOR
54 #endif
55 #ifdef major /* Might be defined in sys/types.h. */
56 # define HAVE_MAJOR
57 #endif
59 #ifndef HAVE_MAJOR
60 # define major(dev) (((dev) >> 8) & 0xff)
61 # define minor(dev) ((dev) & 0xff)
62 # define makedev(maj, min) (((maj) << 8) | (min))
63 #endif
64 #undef HAVE_MAJOR
66 #if ! defined makedev && defined mkdev
67 # define makedev(maj, min) mkdev (maj, min)
68 #endif
70 #include <stdckdint.h>
71 #include <stddef.h>
72 #include <string.h>
73 #include <errno.h>
75 /* Some systems don't define this; POSIX mentions it but says it is
76 obsolete. gnulib defines it, but only on native Windows systems,
77 and there only because MSVC 10 does. */
78 #ifndef ENODATA
79 # define ENODATA (-1)
80 #endif
82 #include <stdlib.h>
83 #include "version.h"
85 /* Exit statuses for programs like 'env' that exec other programs. */
86 enum
88 EXIT_TIMEDOUT = 124, /* Time expired before child completed. */
89 EXIT_CANCELED = 125, /* Internal error prior to exec attempt. */
90 EXIT_CANNOT_INVOKE = 126, /* Program located, but not usable. */
91 EXIT_ENOENT = 127 /* Could not find program to exec. */
94 #include "exitfail.h"
96 /* Set exit_failure to STATUS if that's not the default already. */
97 static inline void
98 initialize_exit_failure (int status)
100 if (status != EXIT_FAILURE)
101 exit_failure = status;
104 #include <fcntl.h>
105 #ifdef O_PATH
106 enum { O_PATHSEARCH = O_PATH };
107 #else
108 enum { O_PATHSEARCH = O_SEARCH };
109 #endif
111 #include <dirent.h>
112 #ifndef _D_EXACT_NAMLEN
113 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
114 #endif
116 enum
118 NOT_AN_INODE_NUMBER = 0
121 #ifdef D_INO_IN_DIRENT
122 # define D_INO(dp) (dp)->d_ino
123 #else
124 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
125 # define D_INO(dp) NOT_AN_INODE_NUMBER
126 #endif
128 /* include here for SIZE_MAX. */
129 #include <inttypes.h>
131 /* Redirection and wildcarding when done by the utility itself.
132 Generally a noop, but used in particular for OS/2. */
133 #ifndef initialize_main
134 # ifndef __OS2__
135 # define initialize_main(ac, av)
136 # else
137 # define initialize_main(ac, av) \
138 do { _wildcard (ac, av); _response (ac, av); } while (0)
139 # endif
140 #endif
142 #include "stat-macros.h"
144 #include "timespec.h"
146 /* ISDIGIT differs from isdigit, as follows:
147 - Its arg may be any int or unsigned int; it need not be an unsigned char
148 or EOF.
149 - It's typically faster.
150 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
151 isdigit unless it's important to use the locale's definition
152 of 'digit' even when the host does not conform to POSIX. */
153 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
155 /* Convert a possibly-signed character to an unsigned character. This is
156 a bit safer than casting to unsigned char, since it catches some type
157 errors that the cast doesn't. */
158 static inline unsigned char to_uchar (char ch) { return ch; }
160 #include <locale.h>
162 /* Take care of NLS matters. */
164 #include "gettext.h"
165 #if ! ENABLE_NLS
166 # undef textdomain
167 # define textdomain(Domainname) /* empty */
168 # undef bindtextdomain
169 # define bindtextdomain(Domainname, Dirname) /* empty */
170 #endif
172 #define _(msgid) gettext (msgid)
173 #define N_(msgid) msgid
175 /* Return a value that pluralizes the same way that N does, in all
176 languages we know of. */
177 static inline unsigned long int
178 select_plural (uintmax_t n)
180 /* Reduce by a power of ten, but keep it away from zero. The
181 gettext manual says 1000000 should be safe. */
182 enum { PLURAL_REDUCER = 1000000 };
183 return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
186 #define STREQ(a, b) (strcmp (a, b) == 0)
187 #define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0) /* n==-1 means unbounded */
188 #define STRPREFIX(a, b) (strncmp (a, b, strlen (b)) == 0)
190 /* Just like strncmp, but the second argument must be a literal string
191 and you don't specify the length; that comes from the literal. */
192 #define STRNCMP_LIT(s, lit) strncmp (s, "" lit "", sizeof (lit) - 1)
194 #if !HAVE_DECL_GETLOGIN
195 char *getlogin (void);
196 #endif
198 #if !HAVE_DECL_TTYNAME
199 char *ttyname (int);
200 #endif
202 #if !HAVE_DECL_GETEUID
203 uid_t geteuid (void);
204 #endif
206 #if !HAVE_DECL_GETPWUID
207 struct passwd *getpwuid (uid_t);
208 #endif
210 #if !HAVE_DECL_GETGRGID
211 struct group *getgrgid (gid_t);
212 #endif
214 /* Interix has replacements for getgr{gid,nam,ent}, that don't
215 query the domain controller for group members when not required.
216 This speeds up the calls tremendously (<1 ms vs. >3 s). */
217 /* To protect any system that could provide _nomembers functions
218 other than interix, check for HAVE_SETGROUPS, as interix is
219 one of the very few (the only?) platform that lacks it */
220 #if ! HAVE_SETGROUPS
221 # if HAVE_GETGRGID_NOMEMBERS
222 # define getgrgid(gid) getgrgid_nomembers(gid)
223 # endif
224 # if HAVE_GETGRNAM_NOMEMBERS
225 # define getgrnam(nam) getgrnam_nomembers(nam)
226 # endif
227 # if HAVE_GETGRENT_NOMEMBERS
228 # define getgrent() getgrent_nomembers()
229 # endif
230 #endif
232 #if !HAVE_DECL_GETUID
233 uid_t getuid (void);
234 #endif
236 #include "idx.h"
237 #include "xalloc.h"
238 #include "verify.h"
240 /* This is simply a shorthand for the common case in which
241 the third argument to x2nrealloc would be 'sizeof *(P)'.
242 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
243 better to use X2REALLOC, although not strictly necessary. */
244 #define X2NREALLOC(P, PN) verify_expr (sizeof *(P) != 1, \
245 x2nrealloc (P, PN, sizeof *(P)))
247 /* Using x2realloc (when appropriate) usually makes your code more
248 readable than using x2nrealloc, but it also makes it so your
249 code will malfunction if sizeof *(P) ever becomes 2 or greater.
250 So use this macro instead of using x2realloc directly. */
251 #define X2REALLOC(P, PN) verify_expr (sizeof *(P) == 1, \
252 x2realloc (P, PN))
254 #include "unlocked-io.h"
255 #include "same-inode.h"
257 #include "dirname.h"
258 #include "openat.h"
260 static inline bool
261 dot_or_dotdot (char const *file_name)
263 if (file_name[0] == '.')
265 char sep = file_name[(file_name[1] == '.') + 1];
266 return (! sep || ISSLASH (sep));
268 else
269 return false;
272 /* A wrapper for readdir so that callers don't see entries for '.' or '..'. */
273 static inline struct dirent const *
274 readdir_ignoring_dot_and_dotdot (DIR *dirp)
276 while (true)
278 struct dirent const *dp = readdir (dirp);
279 if (dp == nullptr || ! dot_or_dotdot (dp->d_name))
280 return dp;
284 /* Return -1 if DIR is an empty directory,
285 0 if DIR is a nonempty directory,
286 and a positive error number if there was trouble determining
287 whether DIR is an empty or nonempty directory. */
288 enum {
289 DS_UNKNOWN = -2,
290 DS_EMPTY = -1,
291 DS_NONEMPTY = 0,
293 static inline int
294 directory_status (int fd_cwd, char const *dir)
296 DIR *dirp;
297 bool no_direntries;
298 int saved_errno;
299 int fd = openat (fd_cwd, dir,
300 (O_RDONLY | O_DIRECTORY
301 | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK));
303 if (fd < 0)
304 return errno;
306 dirp = fdopendir (fd);
307 if (dirp == nullptr)
309 saved_errno = errno;
310 close (fd);
311 return saved_errno;
314 errno = 0;
315 no_direntries = !readdir_ignoring_dot_and_dotdot (dirp);
316 saved_errno = errno;
317 closedir (dirp);
318 return no_direntries && saved_errno == 0 ? DS_EMPTY : saved_errno;
321 /* Factor out some of the common --help and --version processing code. */
323 /* These enum values cannot possibly conflict with the option values
324 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
325 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
326 enum
328 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
329 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
332 #define GETOPT_HELP_OPTION_DECL \
333 "help", no_argument, nullptr, GETOPT_HELP_CHAR
334 #define GETOPT_VERSION_OPTION_DECL \
335 "version", no_argument, nullptr, GETOPT_VERSION_CHAR
336 #define GETOPT_SELINUX_CONTEXT_OPTION_DECL \
337 "context", optional_argument, nullptr, 'Z'
339 #define case_GETOPT_HELP_CHAR \
340 case GETOPT_HELP_CHAR: \
341 usage (EXIT_SUCCESS); \
342 break;
344 /* Program_name must be a literal string.
345 Usually it is just PROGRAM_NAME. */
346 #define USAGE_BUILTIN_WARNING \
347 _("\n" \
348 "Your shell may have its own version of %s, which usually supersedes\n" \
349 "the version described here. Please refer to your shell's documentation\n" \
350 "for details about the options it supports.\n")
352 #define HELP_OPTION_DESCRIPTION \
353 _(" --help display this help and exit\n")
354 #define VERSION_OPTION_DESCRIPTION \
355 _(" --version output version information and exit\n")
357 #include "closein.h"
358 #include "closeout.h"
360 #define emit_bug_reporting_address unused__emit_bug_reporting_address
361 #include "version-etc.h"
362 #undef emit_bug_reporting_address
364 #include "propername.h"
365 /* Define away proper_name, since it's not worth the cost of adding ~17KB to
366 the x86_64 text size of every single program. This avoids a 40%
367 (almost ~2MB) increase in the file system space utilization for the set
368 of the 100 binaries. */
369 #define proper_name(x) proper_name_lite (x, x)
371 #include "progname.h"
373 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
374 case GETOPT_VERSION_CHAR: \
375 version_etc (stdout, Program_name, PACKAGE_NAME, Version, Authors, \
376 (char *) nullptr); \
377 exit (EXIT_SUCCESS); \
378 break;
380 #include "minmax.h"
381 #include "intprops.h"
383 #ifndef SSIZE_MAX
384 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
385 #endif
387 #ifndef OFF_T_MIN
388 # define OFF_T_MIN TYPE_MINIMUM (off_t)
389 #endif
391 #ifndef OFF_T_MAX
392 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
393 #endif
395 #ifndef UID_T_MAX
396 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
397 #endif
399 #ifndef GID_T_MAX
400 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
401 #endif
403 #ifndef PID_T_MAX
404 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
405 #endif
407 /* Use this to suppress gcc warnings. */
408 #ifdef lint
409 # define IF_LINT(Code) Code
410 #else
411 # define IF_LINT(Code) /* empty */
412 #endif
414 /* main_exit should be called only from the main function. It is
415 equivalent to 'exit'. When checking for lint it calls 'exit', to
416 pacify gcc -fsanitize=lint which would otherwise have false alarms
417 for pointers in the main function's activation record. Otherwise
418 it simply returns from 'main'; this used to be what gcc's static
419 checking preferred and may yet be again. */
420 #ifdef lint
421 # define main_exit(status) exit (status)
422 #else
423 # define main_exit(status) return status
424 #endif
426 #ifdef __GNUC__
427 # define LIKELY(cond) __builtin_expect ((cond), 1)
428 # define UNLIKELY(cond) __builtin_expect ((cond), 0)
429 #else
430 # define LIKELY(cond) (cond)
431 # define UNLIKELY(cond) (cond)
432 #endif
435 #if defined strdupa
436 # define ASSIGN_STRDUPA(DEST, S) \
437 do { DEST = strdupa (S); } while (0)
438 #else
439 # define ASSIGN_STRDUPA(DEST, S) \
440 do \
442 char const *s_ = (S); \
443 size_t len_ = strlen (s_) + 1; \
444 char *tmp_dest_ = alloca (len_); \
445 DEST = memcpy (tmp_dest_, s_, len_); \
447 while (0)
448 #endif
450 #if ! HAVE_SYNC
451 # define sync() /* empty */
452 #endif
454 /* Compute the greatest common divisor of U and V using Euclid's
455 algorithm. U and V must be nonzero. */
457 ATTRIBUTE_CONST
458 static inline size_t
459 gcd (size_t u, size_t v)
463 size_t t = u % v;
464 u = v;
465 v = t;
467 while (v);
469 return u;
472 /* Compute the least common multiple of U and V. U and V must be
473 nonzero. There is no overflow checking, so callers should not
474 specify outlandish sizes. */
476 ATTRIBUTE_CONST
477 static inline size_t
478 lcm (size_t u, size_t v)
480 return u * (v / gcd (u, v));
483 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
484 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
485 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
486 locations. */
488 static inline void *
489 ptr_align (void const *ptr, size_t alignment)
491 char const *p0 = ptr;
492 char const *p1 = p0 + alignment - 1;
493 return (void *) (p1 - (size_t) p1 % alignment);
496 /* Return whether the buffer consists entirely of NULs.
497 Based on memeqzero in CCAN by Rusty Russell under CC0 (Public domain). */
499 ATTRIBUTE_PURE
500 static inline bool
501 is_nul (void const *buf, size_t length)
503 const unsigned char *p = buf;
504 /* Using possibly unaligned access for the first 16 bytes
505 saves about 30-40 cycles, though it is strictly undefined behavior
506 and so would need __attribute__ ((__no_sanitize_undefined__))
507 to avoid -fsanitize=undefined warnings.
508 Considering coreutils is mainly concerned with relatively
509 large buffers, we'll just use the defined behavior. */
510 #if 0 && (_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
511 unsigned long word;
512 #else
513 unsigned char word;
514 #endif
516 if (! length)
517 return true;
519 /* Check len bytes not aligned on a word. */
520 while (UNLIKELY (length & (sizeof word - 1)))
522 if (*p)
523 return false;
524 p++;
525 length--;
526 if (! length)
527 return true;
530 /* Check up to 16 bytes a word at a time. */
531 for (;;)
533 memcpy (&word, p, sizeof word);
534 if (word)
535 return false;
536 p += sizeof word;
537 length -= sizeof word;
538 if (! length)
539 return true;
540 if (UNLIKELY (length & 15) == 0)
541 break;
544 /* Now we know first 16 bytes are NUL, memcmp with self. */
545 return memcmp (buf, p, length) == 0;
548 /* Set Accum = 10*Accum + Digit_val and return true, where Accum is an
549 integer object and Digit_val an integer expression. However, if
550 the result overflows, set Accum to an unspecified value and return
551 false. Accum and Digit_val may be evaluated multiple times. */
553 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val) \
554 (!ckd_mul (&(Accum), Accum, 10) && !ckd_add (&(Accum), Accum, Digit_val))
556 static inline void
557 emit_stdin_note (void)
559 fputs (_("\n\
560 With no FILE, or when FILE is -, read standard input.\n\
561 "), stdout);
563 static inline void
564 emit_mandatory_arg_note (void)
566 fputs (_("\n\
567 Mandatory arguments to long options are mandatory for short options too.\n\
568 "), stdout);
571 static inline void
572 emit_size_note (void)
574 fputs (_("\n\
575 The SIZE argument is an integer and optional unit (example: 10K is 10*1024).\n\
576 Units are K,M,G,T,P,E,Z,Y,R,Q (powers of 1024) or KB,MB,... (powers of 1000).\n\
577 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
578 "), stdout);
581 static inline void
582 emit_blocksize_note (char const *program)
584 printf (_("\n\
585 Display values are in units of the first available SIZE from --block-size,\n\
586 and the %s_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.\n\
587 Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).\n\
588 "), program);
591 static inline void
592 emit_update_parameters_note (void)
594 fputs (_("\
596 UPDATE controls which existing files in the destination are replaced.\n\
597 'all' is the default operation when an --update option is not specified,\n\
598 and results in all existing files in the destination being replaced.\n\
599 'none' is like the --no-clobber option, in that no files in the\n\
600 destination are replaced, and skipped files do not induce a failure.\n\
601 'none-fail' also ensures no files are replaced in the destination,\n\
602 but any skipped files are diagnosed and induce a failure.\n\
603 'older' is the default operation when --update is specified, and results\n\
604 in files being replaced if they're older than the corresponding source file.\n\
605 "), stdout);
608 static inline void
609 emit_backup_suffix_note (void)
611 fputs (_("\
613 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
614 The version control method may be selected via the --backup option or through\n\
615 the VERSION_CONTROL environment variable. Here are the values:\n\
617 "), stdout);
618 fputs (_("\
619 none, off never make backups (even if --backup is given)\n\
620 numbered, t make numbered backups\n\
621 existing, nil numbered if numbered backups exist, simple otherwise\n\
622 simple, never always make simple backups\n\
623 "), stdout);
626 static inline void
627 emit_symlink_recurse_options (char const *default_opt)
629 printf (_("\
631 The following options modify how a hierarchy is traversed when the -R\n\
632 option is also specified. If more than one is specified, only the final\n\
633 one takes effect. %s is the default.\n\
635 -H if a command line argument is a symbolic link\n\
636 to a directory, traverse it\n\
637 -L traverse every symbolic link to a directory\n\
638 encountered\n\
639 -P do not traverse any symbolic links\n\
641 "), default_opt);
644 static inline void
645 emit_exec_status (char const *program)
647 printf (_("\n\
648 Exit status:\n\
649 125 if the %s command itself fails\n\
650 126 if COMMAND is found but cannot be invoked\n\
651 127 if COMMAND cannot be found\n\
652 - the exit status of COMMAND otherwise\n\
653 "), program);
656 static inline void
657 emit_ancillary_info (char const *program)
659 struct infomap { char const *program; char const *node; } const infomap[] = {
660 { "[", "test invocation" },
661 { "coreutils", "Multi-call invocation" },
662 { "sha224sum", "sha2 utilities" },
663 { "sha256sum", "sha2 utilities" },
664 { "sha384sum", "sha2 utilities" },
665 { "sha512sum", "sha2 utilities" },
666 { nullptr, nullptr }
669 char const *node = program;
670 struct infomap const *map_prog = infomap;
672 while (map_prog->program && ! STREQ (program, map_prog->program))
673 map_prog++;
675 if (map_prog->node)
676 node = map_prog->node;
678 printf (_("\n%s online help: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
680 /* Don't output this redundant message for English locales.
681 Note we still output for 'C' so that it gets included in the man page. */
682 char const *lc_messages = setlocale (LC_MESSAGES, nullptr);
683 if (lc_messages && STRNCMP_LIT (lc_messages, "en_"))
685 /* TRANSLATORS: Replace LANG_CODE in this URL with your language code
686 <https://translationproject.org/team/LANG_CODE.html> to form one of
687 the URLs at https://translationproject.org/team/. Otherwise, replace
688 the entire URL with your translation team's email address. */
689 fputs (_("Report any translation bugs to "
690 "<https://translationproject.org/team/>\n"), stdout);
692 /* .htaccess on the coreutils web site maps programs to the appropriate page,
693 however we explicitly handle "[" -> "test" here as the "[" is not
694 recognized as part of a URL by default in terminals. */
695 char const *url_program = STREQ (program, "[") ? "test" : program;
696 printf (_("Full documentation <%s%s>\n"),
697 PACKAGE_URL, url_program);
698 printf (_("or available locally via: info '(coreutils) %s%s'\n"),
699 node, node == program ? " invocation" : "");
702 /* Use a macro rather than an inline function, as this references
703 the global program_name, which causes dynamic linking issues
704 in libstdbuf.so on some systems where unused functions
705 are not removed by the linker. */
706 #define emit_try_help() \
707 do \
709 fprintf (stderr, _("Try '%s --help' for more information.\n"), \
710 program_name); \
712 while (0)
714 #include "inttostr.h"
716 static inline char *
717 timetostr (time_t t, char *buf)
719 return (TYPE_SIGNED (time_t)
720 ? imaxtostr (t, buf)
721 : umaxtostr (t, buf));
724 static inline char *
725 bad_cast (char const *s)
727 return (char *) s;
730 /* Return a boolean indicating whether SB->st_size is defined. */
731 static inline bool
732 usable_st_size (struct stat const *sb)
734 return (S_ISREG (sb->st_mode) || S_ISLNK (sb->st_mode)
735 || S_TYPEISSHM (sb) || S_TYPEISTMO (sb));
738 _Noreturn void usage (int status);
740 #include <error.h>
742 /* Like error(0, 0, ...), but without an implicit newline.
743 Also a noop unless the global DEV_DEBUG is set. */
744 #define devmsg(...) \
745 do \
747 if (dev_debug) \
748 fprintf (stderr, __VA_ARGS__); \
750 while (0)
752 #define emit_cycle_warning(file_name) \
753 do \
755 error (0, 0, _("\
756 WARNING: Circular directory structure.\n\
757 This almost certainly means that you have a corrupted file system.\n\
758 NOTIFY YOUR SYSTEM MANAGER.\n\
759 The following directory is part of the cycle:\n %s\n"), \
760 quotef (file_name)); \
762 while (0)
764 /* exit with a _single_ "write error" diagnostic. */
766 static inline void
767 write_error (void)
769 int saved_errno = errno;
770 fflush (stdout); /* Last attempt to write any buffered data. */
771 fpurge (stdout); /* Ensure nothing buffered that might induce an error. */
772 clearerr (stdout); /* Avoid extraneous diagnostic from close_stdout. */
773 error (EXIT_FAILURE, saved_errno, _("write error"));
776 /* Like stpncpy, but do ensure that the result is NUL-terminated,
777 and do not NUL-pad out to LEN. I.e., when strnlen (src, len) == len,
778 this function writes a NUL byte into dest[len]. Thus, the length
779 of the destination buffer must be at least LEN + 1.
780 The DEST and SRC buffers must not overlap. */
781 static inline char *
782 stzncpy (char *restrict dest, char const *restrict src, size_t len)
784 size_t i;
785 for (i = 0; i < len && *src; i++)
786 *dest++ = *src++;
787 *dest = 0;
788 return dest;
791 #ifndef ARRAY_CARDINALITY
792 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
793 #endif
795 /* Return true if ERR is ENOTSUP or EOPNOTSUPP, otherwise false.
796 This wrapper function avoids the redundant 'or'd comparison on
797 systems like Linux for which they have the same value. It also
798 avoids the gcc warning to that effect. */
799 static inline bool
800 is_ENOTSUP (int err)
802 return err == EOPNOTSUPP || (ENOTSUP != EOPNOTSUPP && err == ENOTSUP);
806 /* How coreutils quotes filenames, to minimize use of outer quotes,
807 but also provide better support for copy and paste when used. */
808 #include "quotearg.h"
810 /* Use these to shell quote only when necessary,
811 when the quoted item is already delimited with colons. */
812 #define quotef(arg) \
813 quotearg_n_style_colon (0, shell_escape_quoting_style, arg)
814 #define quotef_n(n, arg) \
815 quotearg_n_style_colon (n, shell_escape_quoting_style, arg)
817 /* Use these when there are spaces around the file name,
818 in the error message. */
819 #define quoteaf(arg) \
820 quotearg_style (shell_escape_always_quoting_style, arg)
821 #define quoteaf_n(n, arg) \
822 quotearg_n_style (n, shell_escape_always_quoting_style, arg)