du: --apparent counts only symlinks and regular
[coreutils.git] / src / system.h
blob2aa5d69787b03e66d3c491c616f211177df9e95d
1 /* system-dependent definitions for coreutils
2 Copyright (C) 1989-2023 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 <string.h>
71 #include <errno.h>
73 /* Some systems don't define this; POSIX mentions it but says it is
74 obsolete. gnulib defines it, but only on native Windows systems,
75 and there only because MSVC 10 does. */
76 #ifndef ENODATA
77 # define ENODATA (-1)
78 #endif
80 #include <stdlib.h>
81 #include "version.h"
83 /* Exit statuses for programs like 'env' that exec other programs. */
84 enum
86 EXIT_TIMEDOUT = 124, /* Time expired before child completed. */
87 EXIT_CANCELED = 125, /* Internal error prior to exec attempt. */
88 EXIT_CANNOT_INVOKE = 126, /* Program located, but not usable. */
89 EXIT_ENOENT = 127 /* Could not find program to exec. */
92 #include "exitfail.h"
94 /* Set exit_failure to STATUS if that's not the default already. */
95 static inline void
96 initialize_exit_failure (int status)
98 if (status != EXIT_FAILURE)
99 exit_failure = status;
102 #include <fcntl.h>
103 #ifdef O_PATH
104 enum { O_PATHSEARCH = O_PATH };
105 #else
106 enum { O_PATHSEARCH = O_SEARCH };
107 #endif
109 #include <dirent.h>
110 #ifndef _D_EXACT_NAMLEN
111 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
112 #endif
114 enum
116 NOT_AN_INODE_NUMBER = 0
119 #ifdef D_INO_IN_DIRENT
120 # define D_INO(dp) (dp)->d_ino
121 #else
122 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
123 # define D_INO(dp) NOT_AN_INODE_NUMBER
124 #endif
126 /* include here for SIZE_MAX. */
127 #include <inttypes.h>
129 /* Redirection and wildcarding when done by the utility itself.
130 Generally a noop, but used in particular for OS/2. */
131 #ifndef initialize_main
132 # ifndef __OS2__
133 # define initialize_main(ac, av)
134 # else
135 # define initialize_main(ac, av) \
136 do { _wildcard (ac, av); _response (ac, av); } while (0)
137 # endif
138 #endif
140 #include "stat-macros.h"
142 #include "timespec.h"
144 #include <ctype.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 /* '\n' is considered a field separator with --zero-terminated. */
161 static inline bool
162 field_sep (unsigned char ch)
164 return isblank (ch) || ch == '\n';
167 #include <locale.h>
169 /* Take care of NLS matters. */
171 #include "gettext.h"
172 #if ! ENABLE_NLS
173 # undef textdomain
174 # define textdomain(Domainname) /* empty */
175 # undef bindtextdomain
176 # define bindtextdomain(Domainname, Dirname) /* empty */
177 #endif
179 #define _(msgid) gettext (msgid)
180 #define N_(msgid) msgid
182 /* Return a value that pluralizes the same way that N does, in all
183 languages we know of. */
184 static inline unsigned long int
185 select_plural (uintmax_t n)
187 /* Reduce by a power of ten, but keep it away from zero. The
188 gettext manual says 1000000 should be safe. */
189 enum { PLURAL_REDUCER = 1000000 };
190 return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
193 #define STREQ(a, b) (strcmp (a, b) == 0)
194 #define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0)
195 #define STRPREFIX(a, b) (strncmp (a, b, strlen (b)) == 0)
197 /* Just like strncmp, but the second argument must be a literal string
198 and you don't specify the length; that comes from the literal. */
199 #define STRNCMP_LIT(s, lit) strncmp (s, "" lit "", sizeof (lit) - 1)
201 #if !HAVE_DECL_GETLOGIN
202 char *getlogin (void);
203 #endif
205 #if !HAVE_DECL_TTYNAME
206 char *ttyname (int);
207 #endif
209 #if !HAVE_DECL_GETEUID
210 uid_t geteuid (void);
211 #endif
213 #if !HAVE_DECL_GETPWUID
214 struct passwd *getpwuid (uid_t);
215 #endif
217 #if !HAVE_DECL_GETGRGID
218 struct group *getgrgid (gid_t);
219 #endif
221 /* Interix has replacements for getgr{gid,nam,ent}, that don't
222 query the domain controller for group members when not required.
223 This speeds up the calls tremendously (<1 ms vs. >3 s). */
224 /* To protect any system that could provide _nomembers functions
225 other than interix, check for HAVE_SETGROUPS, as interix is
226 one of the very few (the only?) platform that lacks it */
227 #if ! HAVE_SETGROUPS
228 # if HAVE_GETGRGID_NOMEMBERS
229 # define getgrgid(gid) getgrgid_nomembers(gid)
230 # endif
231 # if HAVE_GETGRNAM_NOMEMBERS
232 # define getgrnam(nam) getgrnam_nomembers(nam)
233 # endif
234 # if HAVE_GETGRENT_NOMEMBERS
235 # define getgrent() getgrent_nomembers()
236 # endif
237 #endif
239 #if !HAVE_DECL_GETUID
240 uid_t getuid (void);
241 #endif
243 #include "xalloc.h"
244 #include "verify.h"
246 /* This is simply a shorthand for the common case in which
247 the third argument to x2nrealloc would be 'sizeof *(P)'.
248 Ensure that sizeof *(P) is *not* 1. In that case, it'd be
249 better to use X2REALLOC, although not strictly necessary. */
250 #define X2NREALLOC(P, PN) verify_expr (sizeof *(P) != 1, \
251 x2nrealloc (P, PN, sizeof *(P)))
253 /* Using x2realloc (when appropriate) usually makes your code more
254 readable than using x2nrealloc, but it also makes it so your
255 code will malfunction if sizeof *(P) ever becomes 2 or greater.
256 So use this macro instead of using x2realloc directly. */
257 #define X2REALLOC(P, PN) verify_expr (sizeof *(P) == 1, \
258 x2realloc (P, PN))
260 #include "unlocked-io.h"
261 #include "same-inode.h"
263 #include "dirname.h"
264 #include "openat.h"
266 static inline bool
267 dot_or_dotdot (char const *file_name)
269 if (file_name[0] == '.')
271 char sep = file_name[(file_name[1] == '.') + 1];
272 return (! sep || ISSLASH (sep));
274 else
275 return false;
278 /* A wrapper for readdir so that callers don't see entries for '.' or '..'. */
279 static inline struct dirent const *
280 readdir_ignoring_dot_and_dotdot (DIR *dirp)
282 while (true)
284 struct dirent const *dp = readdir (dirp);
285 if (dp == NULL || ! dot_or_dotdot (dp->d_name))
286 return dp;
290 /* Return -1 if DIR is an empty directory,
291 0 if DIR is a nonempty directory,
292 and a positive error number if there was trouble determining
293 whether DIR is an empty or nonempty directory. */
294 enum {
295 DS_UNKNOWN = -2,
296 DS_EMPTY = -1,
297 DS_NONEMPTY = 0,
299 static inline int
300 directory_status (int fd_cwd, char const *dir)
302 DIR *dirp;
303 bool no_direntries;
304 int saved_errno;
305 int fd = openat (fd_cwd, dir,
306 (O_RDONLY | O_DIRECTORY
307 | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK));
309 if (fd < 0)
310 return errno;
312 dirp = fdopendir (fd);
313 if (dirp == NULL)
315 saved_errno = errno;
316 close (fd);
317 return saved_errno;
320 errno = 0;
321 no_direntries = !readdir_ignoring_dot_and_dotdot (dirp);
322 saved_errno = errno;
323 closedir (dirp);
324 return no_direntries && saved_errno == 0 ? DS_EMPTY : saved_errno;
327 /* Factor out some of the common --help and --version processing code. */
329 /* These enum values cannot possibly conflict with the option values
330 ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
331 CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value. */
332 enum
334 GETOPT_HELP_CHAR = (CHAR_MIN - 2),
335 GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
338 #define GETOPT_HELP_OPTION_DECL \
339 "help", no_argument, NULL, GETOPT_HELP_CHAR
340 #define GETOPT_VERSION_OPTION_DECL \
341 "version", no_argument, NULL, GETOPT_VERSION_CHAR
342 #define GETOPT_SELINUX_CONTEXT_OPTION_DECL \
343 "context", optional_argument, NULL, 'Z'
345 #define case_GETOPT_HELP_CHAR \
346 case GETOPT_HELP_CHAR: \
347 usage (EXIT_SUCCESS); \
348 break;
350 /* Program_name must be a literal string.
351 Usually it is just PROGRAM_NAME. */
352 #define USAGE_BUILTIN_WARNING \
353 _("\n" \
354 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
355 "the version described here. Please refer to your shell's documentation\n" \
356 "for details about the options it supports.\n")
358 #define HELP_OPTION_DESCRIPTION \
359 _(" --help display this help and exit\n")
360 #define VERSION_OPTION_DESCRIPTION \
361 _(" --version output version information and exit\n")
363 #include "closein.h"
364 #include "closeout.h"
366 #define emit_bug_reporting_address unused__emit_bug_reporting_address
367 #include "version-etc.h"
368 #undef emit_bug_reporting_address
370 #include "propername.h"
371 /* Define away proper_name (leaving proper_name_utf8, which affects far
372 fewer programs), since it's not worth the cost of adding ~17KB to
373 the x86_64 text size of every single program. This avoids a 40%
374 (almost ~2MB) increase in the file system space utilization for the set
375 of the 100 binaries. */
376 #define proper_name(x) (x)
378 #include "progname.h"
380 #define case_GETOPT_VERSION_CHAR(Program_name, Authors) \
381 case GETOPT_VERSION_CHAR: \
382 version_etc (stdout, Program_name, PACKAGE_NAME, Version, Authors, \
383 (char *) NULL); \
384 exit (EXIT_SUCCESS); \
385 break;
387 #include "minmax.h"
388 #include "intprops.h"
390 #ifndef SSIZE_MAX
391 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
392 #endif
394 #ifndef OFF_T_MIN
395 # define OFF_T_MIN TYPE_MINIMUM (off_t)
396 #endif
398 #ifndef OFF_T_MAX
399 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
400 #endif
402 #ifndef UID_T_MAX
403 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
404 #endif
406 #ifndef GID_T_MAX
407 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
408 #endif
410 #ifndef PID_T_MAX
411 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
412 #endif
414 /* Use this to suppress gcc warnings. */
415 #ifdef lint
416 # define IF_LINT(Code) Code
417 #else
418 # define IF_LINT(Code) /* empty */
419 #endif
421 /* main_exit should be called only from the main function. It is
422 equivalent to 'exit'. When checking for lint it calls 'exit', to
423 pacify gcc -fsanitize=lint which would otherwise have false alarms
424 for pointers in the main function's activation record. Otherwise
425 it simply returns from 'main'; this used to be what gcc's static
426 checking preferred and may yet be again. */
427 #ifdef lint
428 # define main_exit(status) exit (status)
429 #else
430 # define main_exit(status) return status
431 #endif
433 #ifdef __GNUC__
434 # define LIKELY(cond) __builtin_expect ((cond), 1)
435 # define UNLIKELY(cond) __builtin_expect ((cond), 0)
436 #else
437 # define LIKELY(cond) (cond)
438 # define UNLIKELY(cond) (cond)
439 #endif
442 #if defined strdupa
443 # define ASSIGN_STRDUPA(DEST, S) \
444 do { DEST = strdupa (S); } while (0)
445 #else
446 # define ASSIGN_STRDUPA(DEST, S) \
447 do \
449 char const *s_ = (S); \
450 size_t len_ = strlen (s_) + 1; \
451 char *tmp_dest_ = alloca (len_); \
452 DEST = memcpy (tmp_dest_, s_, len_); \
454 while (0)
455 #endif
457 #if ! HAVE_SYNC
458 # define sync() /* empty */
459 #endif
461 /* Compute the greatest common divisor of U and V using Euclid's
462 algorithm. U and V must be nonzero. */
464 ATTRIBUTE_CONST
465 static inline size_t
466 gcd (size_t u, size_t v)
470 size_t t = u % v;
471 u = v;
472 v = t;
474 while (v);
476 return u;
479 /* Compute the least common multiple of U and V. U and V must be
480 nonzero. There is no overflow checking, so callers should not
481 specify outlandish sizes. */
483 ATTRIBUTE_CONST
484 static inline size_t
485 lcm (size_t u, size_t v)
487 return u * (v / gcd (u, v));
490 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
491 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
492 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
493 locations. */
495 static inline void *
496 ptr_align (void const *ptr, size_t alignment)
498 char const *p0 = ptr;
499 char const *p1 = p0 + alignment - 1;
500 return (void *) (p1 - (size_t) p1 % alignment);
503 /* Return whether the buffer consists entirely of NULs.
504 Based on memeqzero in CCAN by Rusty Russell under CC0 (Public domain). */
506 ATTRIBUTE_PURE
507 static inline bool
508 is_nul (void const *buf, size_t length)
510 const unsigned char *p = buf;
511 /* Using possibly unaligned access for the first 16 bytes
512 saves about 30-40 cycles, though it is strictly undefined behavior
513 and so would need __attribute__ ((__no_sanitize_undefined__))
514 to avoid -fsanitize=undefined warnings.
515 Considering coreutils is mainly concerned with relatively
516 large buffers, we'll just use the defined behavior. */
517 #if 0 && (_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
518 unsigned long word;
519 #else
520 unsigned char word;
521 #endif
523 if (! length)
524 return true;
526 /* Check len bytes not aligned on a word. */
527 while (UNLIKELY (length & (sizeof word - 1)))
529 if (*p)
530 return false;
531 p++;
532 length--;
533 if (! length)
534 return true;
537 /* Check up to 16 bytes a word at a time. */
538 for (;;)
540 memcpy (&word, p, sizeof word);
541 if (word)
542 return false;
543 p += sizeof word;
544 length -= sizeof word;
545 if (! length)
546 return true;
547 if (UNLIKELY (length & 15) == 0)
548 break;
551 /* Now we know first 16 bytes are NUL, memcmp with self. */
552 return memcmp (buf, p, length) == 0;
555 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
556 then don't update Accum and return false to indicate it would
557 overflow. Otherwise, set Accum to that new value and return true.
558 Verify at compile-time that Type is Accum's type, and that Type is
559 unsigned. Accum must be an object, so that we can take its
560 address. Accum and Digit_val may be evaluated multiple times.
562 The "Added check" below is not strictly required, but it causes GCC
563 to return a nonzero exit status instead of merely a warning
564 diagnostic, and that is more useful. */
566 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type) \
568 (void) (&(Accum) == (Type *) NULL), /* The type matches. */ \
569 verify_expr (! TYPE_SIGNED (Type), /* The type is unsigned. */ \
570 (((Type) -1 / 10 < (Accum) \
571 || (Type) ((Accum) * 10 + (Digit_val)) < (Accum)) \
572 ? false \
573 : (((Accum) = (Accum) * 10 + (Digit_val)), true))) \
576 static inline void
577 emit_stdin_note (void)
579 fputs (_("\n\
580 With no FILE, or when FILE is -, read standard input.\n\
581 "), stdout);
583 static inline void
584 emit_mandatory_arg_note (void)
586 fputs (_("\n\
587 Mandatory arguments to long options are mandatory for short options too.\n\
588 "), stdout);
591 static inline void
592 emit_size_note (void)
594 fputs (_("\n\
595 The SIZE argument is an integer and optional unit (example: 10K is 10*1024).\n\
596 Units are K,M,G,T,P,E,Z,Y,R,Q (powers of 1024) or KB,MB,... (powers of 1000).\n\
597 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
598 "), stdout);
601 static inline void
602 emit_blocksize_note (char const *program)
604 printf (_("\n\
605 Display values are in units of the first available SIZE from --block-size,\n\
606 and the %s_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.\n\
607 Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).\n\
608 "), program);
611 static inline void
612 emit_backup_suffix_note (void)
614 fputs (_("\
616 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
617 The version control method may be selected via the --backup option or through\n\
618 the VERSION_CONTROL environment variable. Here are the values:\n\
620 "), stdout);
621 fputs (_("\
622 none, off never make backups (even if --backup is given)\n\
623 numbered, t make numbered backups\n\
624 existing, nil numbered if numbered backups exist, simple otherwise\n\
625 simple, never always make simple backups\n\
626 "), stdout);
629 static inline void
630 emit_exec_status (char const *program)
632 printf (_("\n\
633 Exit status:\n\
634 125 if the %s command itself fails\n\
635 126 if COMMAND is found but cannot be invoked\n\
636 127 if COMMAND cannot be found\n\
637 - the exit status of COMMAND otherwise\n\
638 "), program);
641 static inline void
642 emit_ancillary_info (char const *program)
644 struct infomap { char const *program; char const *node; } const infomap[] = {
645 { "[", "test invocation" },
646 { "coreutils", "Multi-call invocation" },
647 { "sha224sum", "sha2 utilities" },
648 { "sha256sum", "sha2 utilities" },
649 { "sha384sum", "sha2 utilities" },
650 { "sha512sum", "sha2 utilities" },
651 { NULL, NULL }
654 char const *node = program;
655 struct infomap const *map_prog = infomap;
657 while (map_prog->program && ! STREQ (program, map_prog->program))
658 map_prog++;
660 if (map_prog->node)
661 node = map_prog->node;
663 printf (_("\n%s online help: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
665 /* Don't output this redundant message for English locales.
666 Note we still output for 'C' so that it gets included in the man page. */
667 char const *lc_messages = setlocale (LC_MESSAGES, NULL);
668 if (lc_messages && STRNCMP_LIT (lc_messages, "en_"))
670 /* TRANSLATORS: Replace LANG_CODE in this URL with your language code
671 <https://translationproject.org/team/LANG_CODE.html> to form one of
672 the URLs at https://translationproject.org/team/. Otherwise, replace
673 the entire URL with your translation team's email address. */
674 fputs (_("Report any translation bugs to "
675 "<https://translationproject.org/team/>\n"), stdout);
677 /* .htaccess on the coreutils web site maps programs to the appropriate page,
678 however we explicitly handle "[" -> "test" here as the "[" is not
679 recognized as part of a URL by default in terminals. */
680 char const *url_program = STREQ (program, "[") ? "test" : program;
681 printf (_("Full documentation <%s%s>\n"),
682 PACKAGE_URL, url_program);
683 printf (_("or available locally via: info '(coreutils) %s%s'\n"),
684 node, node == program ? " invocation" : "");
687 /* Use a macro rather than an inline function, as this references
688 the global program_name, which causes dynamic linking issues
689 in libstdbuf.so on some systems where unused functions
690 are not removed by the linker. */
691 #define emit_try_help() \
692 do \
694 fprintf (stderr, _("Try '%s --help' for more information.\n"), \
695 program_name); \
697 while (0)
699 #include "inttostr.h"
701 static inline char *
702 timetostr (time_t t, char *buf)
704 return (TYPE_SIGNED (time_t)
705 ? imaxtostr (t, buf)
706 : umaxtostr (t, buf));
709 static inline char *
710 bad_cast (char const *s)
712 return (char *) s;
715 /* Return a boolean indicating whether SB->st_size is defined. */
716 static inline bool
717 usable_st_size (struct stat const *sb)
719 return (S_ISREG (sb->st_mode) || S_ISLNK (sb->st_mode)
720 || S_TYPEISSHM (sb) || S_TYPEISTMO (sb));
723 _Noreturn void usage (int status);
725 /* Like error(0, 0, ...), but without an implicit newline.
726 Also a noop unless the global DEV_DEBUG is set. */
727 #define devmsg(...) \
728 do \
730 if (dev_debug) \
731 fprintf (stderr, __VA_ARGS__); \
733 while (0)
735 #define emit_cycle_warning(file_name) \
736 do \
738 error (0, 0, _("\
739 WARNING: Circular directory structure.\n\
740 This almost certainly means that you have a corrupted file system.\n\
741 NOTIFY YOUR SYSTEM MANAGER.\n\
742 The following directory is part of the cycle:\n %s\n"), \
743 quotef (file_name)); \
745 while (0)
747 /* Like stpncpy, but do ensure that the result is NUL-terminated,
748 and do not NUL-pad out to LEN. I.e., when strnlen (src, len) == len,
749 this function writes a NUL byte into dest[len]. Thus, the length
750 of the destination buffer must be at least LEN + 1.
751 The DEST and SRC buffers must not overlap. */
752 static inline char *
753 stzncpy (char *restrict dest, char const *restrict src, size_t len)
755 char const *src_end = src + len;
756 while (src < src_end && *src)
757 *dest++ = *src++;
758 *dest = 0;
759 return dest;
762 #ifndef ARRAY_CARDINALITY
763 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
764 #endif
766 /* Return true if ERR is ENOTSUP or EOPNOTSUPP, otherwise false.
767 This wrapper function avoids the redundant 'or'd comparison on
768 systems like Linux for which they have the same value. It also
769 avoids the gcc warning to that effect. */
770 static inline bool
771 is_ENOTSUP (int err)
773 return err == EOPNOTSUPP || (ENOTSUP != EOPNOTSUPP && err == ENOTSUP);
777 /* How coreutils quotes filenames, to minimize use of outer quotes,
778 but also provide better support for copy and paste when used. */
779 #include "quotearg.h"
781 /* Use these to shell quote only when necessary,
782 when the quoted item is already delimited with colons. */
783 #define quotef(arg) \
784 quotearg_n_style_colon (0, shell_escape_quoting_style, arg)
785 #define quotef_n(n, arg) \
786 quotearg_n_style_colon (n, shell_escape_quoting_style, arg)
788 /* Use these when there are spaces around the file name,
789 in the error message. */
790 #define quoteaf(arg) \
791 quotearg_style (shell_escape_always_quoting_style, arg)
792 #define quoteaf_n(n, arg) \
793 quotearg_n_style (n, shell_escape_always_quoting_style, arg)