2e8e1772077d3f814a50a1d91b1d949b4703c17f
[platform/upstream/coreutils.git] / src / system.h
1 /* system-dependent definitions for coreutils
2    Copyright (C) 1989, 1991-2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Include this file _after_ system headers if possible.  */
18
19 #include <alloca.h>
20
21 /* Include <sys/types.h> before this file.
22    Note this doesn't warn if we're included
23    before all system headers.  */
24
25 #if 2 < __GLIBC__ || ( 2 == ___GLIBC__ && 2 <= __GLIBC_MINOR__ )
26 # if ! defined _SYS_TYPES_H
27 you must include <sys/types.h> before including this file
28 # endif
29 #endif
30
31 #include <sys/stat.h>
32
33 #if !defined HAVE_MKFIFO
34 # define mkfifo(name, mode) mknod (name, (mode) | S_IFIFO, 0)
35 #endif
36
37 #if HAVE_SYS_PARAM_H
38 # include <sys/param.h>
39 #endif
40
41 #include <unistd.h>
42
43 /* limits.h must come before pathmax.h because limits.h on some systems
44    undefs PATH_MAX, whereas pathmax.h sets PATH_MAX.  */
45 #include <limits.h>
46
47 #include "pathmax.h"
48
49 #include "configmake.h"
50
51 #include <sys/time.h>
52 #include <time.h>
53
54 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
55 #if MAJOR_IN_MKDEV
56 # include <sys/mkdev.h>
57 # define HAVE_MAJOR
58 #endif
59 #if MAJOR_IN_SYSMACROS
60 # include <sys/sysmacros.h>
61 # define HAVE_MAJOR
62 #endif
63 #ifdef major                    /* Might be defined in sys/types.h.  */
64 # define HAVE_MAJOR
65 #endif
66
67 #ifndef HAVE_MAJOR
68 # define major(dev)  (((dev) >> 8) & 0xff)
69 # define minor(dev)  ((dev) & 0xff)
70 # define makedev(maj, min)  (((maj) << 8) | (min))
71 #endif
72 #undef HAVE_MAJOR
73
74 #if ! defined makedev && defined mkdev
75 # define makedev(maj, min)  mkdev (maj, min)
76 #endif
77
78 /* Don't use bcopy!  Use memmove if source and destination may overlap,
79    memcpy otherwise.  */
80
81 #include <string.h>
82
83 #include <errno.h>
84
85 /* Some systems don't define this; POSIX mentions it but says it is
86    obsolete, so gnulib does not provide it either.  */
87 #ifndef ENODATA
88 # define ENODATA (-1)
89 #endif
90
91 #include <stdbool.h>
92 #include <stdlib.h>
93 #include "version.h"
94
95 /* Exit statuses for programs like 'env' that exec other programs.  */
96 enum
97 {
98   EXIT_TIMEDOUT = 124, /* Time expired before child completed.  */
99   EXIT_CANCELED = 125, /* Internal error prior to exec attempt.  */
100   EXIT_CANNOT_INVOKE = 126, /* Program located, but not usable.  */
101   EXIT_ENOENT = 127 /* Could not find program to exec.  */
102 };
103
104 #include "exitfail.h"
105
106 /* Set exit_failure to STATUS if that's not the default already.  */
107 static inline void
108 initialize_exit_failure (int status)
109 {
110   if (status != EXIT_FAILURE)
111     exit_failure = status;
112 }
113
114 #include <fcntl.h>
115
116 #include <dirent.h>
117 #ifndef _D_EXACT_NAMLEN
118 # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
119 #endif
120
121 enum
122 {
123   NOT_AN_INODE_NUMBER = 0
124 };
125
126 #ifdef D_INO_IN_DIRENT
127 # define D_INO(dp) (dp)->d_ino
128 #else
129 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs.  */
130 # define D_INO(dp) NOT_AN_INODE_NUMBER
131 #endif
132
133 /* include here for SIZE_MAX.  */
134 #include <inttypes.h>
135
136 /* Get or fake the disk device blocksize.
137    Usually defined by sys/param.h (if at all).  */
138 #if !defined DEV_BSIZE && defined BSIZE
139 # define DEV_BSIZE BSIZE
140 #endif
141 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
142 # define DEV_BSIZE BBSIZE
143 #endif
144 #ifndef DEV_BSIZE
145 # define DEV_BSIZE 4096
146 #endif
147
148 /* Extract or fake data from a `struct stat'.
149    ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
150    ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
151    ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS.  */
152 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
153 # define ST_BLKSIZE(statbuf) DEV_BSIZE
154 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE.  */
155 #  define ST_NBLOCKS(statbuf) \
156   ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
157 # else /* !_POSIX_SOURCE && BSIZE */
158 #  define ST_NBLOCKS(statbuf) \
159   (S_ISREG ((statbuf).st_mode) \
160    || S_ISDIR ((statbuf).st_mode) \
161    ? st_blocks ((statbuf).st_size) : 0)
162 # endif /* !_POSIX_SOURCE && BSIZE */
163 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
164 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
165    Also, when running `rsh hpux11-system cat any-file', cat would
166    determine that the output stream had an st_blksize of 2147421096.
167    Conversely st_blksize can be 2 GiB (or maybe even larger) with XFS
168    on 64-bit hosts.  Somewhat arbitrarily, limit the `optimal' block
169    size to SIZE_MAX / 8 + 1.  (Dividing SIZE_MAX by only 4 wouldn't
170    suffice, since "cat" sometimes multiplies the result by 4.)  If
171    anyone knows of a system for which this limit is too small, please
172    report it as a bug in this code.  */
173 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
174                                && (statbuf).st_blksize <= SIZE_MAX / 8 + 1) \
175                               ? (statbuf).st_blksize : DEV_BSIZE)
176 # if defined hpux || defined __hpux__ || defined __hpux
177 /* HP-UX counts st_blocks in 1024-byte units.
178    This loses when mixing HP-UX and BSD file systems with NFS.  */
179 #  define ST_NBLOCKSIZE 1024
180 # else /* !hpux */
181 #  if defined _AIX && defined _I386
182 /* AIX PS/2 counts st_blocks in 4K units.  */
183 #   define ST_NBLOCKSIZE (4 * 1024)
184 #  else /* not AIX PS/2 */
185 #   if defined _CRAY
186 #    define ST_NBLOCKS(statbuf) \
187   (S_ISREG ((statbuf).st_mode) \
188    || S_ISDIR ((statbuf).st_mode) \
189    ? (statbuf).st_blocks * ST_BLKSIZE (statbuf) / ST_NBLOCKSIZE : 0)
190 #   endif /* _CRAY */
191 #  endif /* not AIX PS/2 */
192 # endif /* !hpux */
193 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
194
195 #ifndef ST_NBLOCKS
196 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
197 #endif
198
199 #ifndef ST_NBLOCKSIZE
200 # ifdef S_BLKSIZE
201 #  define ST_NBLOCKSIZE S_BLKSIZE
202 # else
203 #  define ST_NBLOCKSIZE 512
204 # endif
205 #endif
206
207 /* Redirection and wildcarding when done by the utility itself.
208    Generally a noop, but used in particular for native VMS. */
209 #ifndef initialize_main
210 # define initialize_main(ac, av)
211 #endif
212
213 #include "stat-macros.h"
214
215 #include "timespec.h"
216
217 #include <ctype.h>
218
219 /* ISDIGIT differs from isdigit, as follows:
220    - Its arg may be any int or unsigned int; it need not be an unsigned char
221      or EOF.
222    - It's typically faster.
223    POSIX says that only '0' through '9' are digits.  Prefer ISDIGIT to
224    isdigit unless it's important to use the locale's definition
225    of `digit' even when the host does not conform to POSIX.  */
226 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
227
228 /* Convert a possibly-signed character to an unsigned character.  This is
229    a bit safer than casting to unsigned char, since it catches some type
230    errors that the cast doesn't.  */
231 static inline unsigned char to_uchar (char ch) { return ch; }
232
233 #include <locale.h>
234
235 /* Take care of NLS matters.  */
236
237 #include "gettext.h"
238 #if ! ENABLE_NLS
239 # undef textdomain
240 # define textdomain(Domainname) /* empty */
241 # undef bindtextdomain
242 # define bindtextdomain(Domainname, Dirname) /* empty */
243 #endif
244
245 #define _(msgid) gettext (msgid)
246 #define N_(msgid) msgid
247
248 /* Return a value that pluralizes the same way that N does, in all
249    languages we know of.  */
250 static inline unsigned long int
251 select_plural (uintmax_t n)
252 {
253   /* Reduce by a power of ten, but keep it away from zero.  The
254      gettext manual says 1000000 should be safe.  */
255   enum { PLURAL_REDUCER = 1000000 };
256   return (n <= ULONG_MAX ? n : n % PLURAL_REDUCER + PLURAL_REDUCER);
257 }
258
259 #define STREQ(a, b) (strcmp (a, b) == 0)
260 #define STREQ_LEN(a, b, n) (strncmp (a, b, n) == 0)
261 #define STRPREFIX(a, b) (strncmp(a, b, strlen (b)) == 0)
262
263 /* Just like strncmp, but the first argument must be a literal string
264    and you don't specify the length.  */
265 #define STRNCMP_LIT(s, literal) \
266   strncmp (s, "" literal "", sizeof (literal) - 1)
267
268 #if !HAVE_DECL_GETLOGIN
269 char *getlogin ();
270 #endif
271
272 #if !HAVE_DECL_TTYNAME
273 char *ttyname ();
274 #endif
275
276 #if !HAVE_DECL_GETEUID
277 uid_t geteuid ();
278 #endif
279
280 #if !HAVE_DECL_GETPWUID
281 struct passwd *getpwuid ();
282 #endif
283
284 #if !HAVE_DECL_GETGRGID
285 struct group *getgrgid ();
286 #endif
287
288 #if !HAVE_DECL_GETUID
289 uid_t getuid ();
290 #endif
291
292 #include "xalloc.h"
293 #include "verify.h"
294
295 /* This is simply a shorthand for the common case in which
296    the third argument to x2nrealloc would be `sizeof *(P)'.
297    Ensure that sizeof *(P) is *not* 1.  In that case, it'd be
298    better to use X2REALLOC, although not strictly necessary.  */
299 #define X2NREALLOC(P, PN) ((void) verify_true (sizeof *(P) != 1), \
300                            x2nrealloc (P, PN, sizeof *(P)))
301
302 /* Using x2realloc (when appropriate) usually makes your code more
303    readable than using x2nrealloc, but it also makes it so your
304    code will malfunction if sizeof *(P) ever becomes 2 or greater.
305    So use this macro instead of using x2realloc directly.  */
306 #define X2REALLOC(P, PN) ((void) verify_true (sizeof *(P) == 1), \
307                           x2realloc (P, PN))
308
309 #include "unlocked-io.h"
310 #include "same-inode.h"
311
312 #include "dirname.h"
313 #include "openat.h"
314
315 static inline bool
316 dot_or_dotdot (char const *file_name)
317 {
318   if (file_name[0] == '.')
319     {
320       char sep = file_name[(file_name[1] == '.') + 1];
321       return (! sep || ISSLASH (sep));
322     }
323   else
324     return false;
325 }
326
327 /* A wrapper for readdir so that callers don't see entries for `.' or `..'.  */
328 static inline struct dirent const *
329 readdir_ignoring_dot_and_dotdot (DIR *dirp)
330 {
331   while (1)
332     {
333       struct dirent const *dp = readdir (dirp);
334       if (dp == NULL || ! dot_or_dotdot (dp->d_name))
335         return dp;
336     }
337 }
338
339 /* Return true if DIR is determined to be an empty directory.  */
340 static inline bool
341 is_empty_dir (int fd_cwd, char const *dir)
342 {
343   DIR *dirp;
344   struct dirent const *dp;
345   int saved_errno;
346   int fd = openat (fd_cwd, dir,
347                    (O_RDONLY | O_DIRECTORY
348                     | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK));
349
350   if (fd < 0)
351     return false;
352
353   dirp = fdopendir (fd);
354   if (dirp == NULL)
355     {
356       close (fd);
357       return false;
358     }
359
360   errno = 0;
361   dp = readdir_ignoring_dot_and_dotdot (dirp);
362   saved_errno = errno;
363   closedir (dirp);
364   if (dp != NULL)
365     return false;
366   return saved_errno == 0 ? true : false;
367 }
368
369 /* Factor out some of the common --help and --version processing code.  */
370
371 /* These enum values cannot possibly conflict with the option values
372    ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
373    CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
374 enum
375 {
376   GETOPT_HELP_CHAR = (CHAR_MIN - 2),
377   GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
378 };
379
380 #define GETOPT_HELP_OPTION_DECL \
381   "help", no_argument, NULL, GETOPT_HELP_CHAR
382 #define GETOPT_VERSION_OPTION_DECL \
383   "version", no_argument, NULL, GETOPT_VERSION_CHAR
384 #define GETOPT_SELINUX_CONTEXT_OPTION_DECL \
385   "context", required_argument, NULL, 'Z'
386
387 #define case_GETOPT_HELP_CHAR                   \
388   case GETOPT_HELP_CHAR:                        \
389     usage (EXIT_SUCCESS);                       \
390     break;
391
392 /* Program_name must be a literal string.
393    Usually it is just PROGRAM_NAME.  */
394 #define USAGE_BUILTIN_WARNING \
395   _("\n" \
396 "NOTE: your shell may have its own version of %s, which usually supersedes\n" \
397 "the version described here.  Please refer to your shell's documentation\n" \
398 "for details about the options it supports.\n")
399
400 #define HELP_OPTION_DESCRIPTION \
401   _("      --help     display this help and exit\n")
402 #define VERSION_OPTION_DESCRIPTION \
403   _("      --version  output version information and exit\n")
404
405 #include "closein.h"
406 #include "closeout.h"
407
408 #define emit_bug_reporting_address unused__emit_bug_reporting_address
409 #include "version-etc.h"
410 #undef emit_bug_reporting_address
411
412 #include "propername.h"
413 /* Define away proper_name (leaving proper_name_utf8, which affects far
414    fewer programs), since it's not worth the cost of adding ~17KB to
415    the x86_64 text size of every single program.  This avoids a 40%
416    (almost ~2MB) increase in the on-disk space utilization for the set
417    of the 100 binaries. */
418 #define proper_name(x) (x)
419
420 #include "progname.h"
421
422 #define case_GETOPT_VERSION_CHAR(Program_name, Authors)                 \
423   case GETOPT_VERSION_CHAR:                                             \
424     version_etc (stdout, Program_name, PACKAGE_NAME, Version, Authors,  \
425                  (char *) NULL);                                        \
426     exit (EXIT_SUCCESS);                                                \
427     break;
428
429 #ifndef MAX
430 # define MAX(a, b) ((a) > (b) ? (a) : (b))
431 #endif
432
433 #ifndef MIN
434 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
435 #endif
436
437 #include "intprops.h"
438
439 #ifndef SSIZE_MAX
440 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
441 #endif
442
443 #ifndef OFF_T_MIN
444 # define OFF_T_MIN TYPE_MINIMUM (off_t)
445 #endif
446
447 #ifndef OFF_T_MAX
448 # define OFF_T_MAX TYPE_MAXIMUM (off_t)
449 #endif
450
451 #ifndef UID_T_MAX
452 # define UID_T_MAX TYPE_MAXIMUM (uid_t)
453 #endif
454
455 #ifndef GID_T_MAX
456 # define GID_T_MAX TYPE_MAXIMUM (gid_t)
457 #endif
458
459 #ifndef PID_T_MAX
460 # define PID_T_MAX TYPE_MAXIMUM (pid_t)
461 #endif
462
463 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
464 #ifdef lint
465 # define IF_LINT(Code) Code
466 #else
467 # define IF_LINT(Code) /* empty */
468 #endif
469
470 #ifndef __attribute__
471 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
472 #  define __attribute__(x) /* empty */
473 # endif
474 #endif
475
476 #ifndef ATTRIBUTE_NORETURN
477 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
478 #endif
479
480 #ifndef ATTRIBUTE_UNUSED
481 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
482 #endif
483
484 /* The warn_unused_result attribute appeared first in gcc-3.4.0 */
485 #undef ATTRIBUTE_WARN_UNUSED_RESULT
486 #if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
487 # define ATTRIBUTE_WARN_UNUSED_RESULT /* empty */
488 #else
489 # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
490 #endif
491
492 #if defined strdupa
493 # define ASSIGN_STRDUPA(DEST, S)                \
494   do { DEST = strdupa (S); } while (0)
495 #else
496 # define ASSIGN_STRDUPA(DEST, S)                \
497   do                                            \
498     {                                           \
499       const char *s_ = (S);                     \
500       size_t len_ = strlen (s_) + 1;            \
501       char *tmp_dest_ = alloca (len_);          \
502       DEST = memcpy (tmp_dest_, s_, len_);      \
503     }                                           \
504   while (0)
505 #endif
506
507 #if ! HAVE_SYNC
508 # define sync() /* empty */
509 #endif
510
511 /* Compute the greatest common divisor of U and V using Euclid's
512    algorithm.  U and V must be nonzero.  */
513
514 static inline size_t
515 gcd (size_t u, size_t v)
516 {
517   do
518     {
519       size_t t = u % v;
520       u = v;
521       v = t;
522     }
523   while (v);
524
525   return u;
526 }
527
528 /* Compute the least common multiple of U and V.  U and V must be
529    nonzero.  There is no overflow checking, so callers should not
530    specify outlandish sizes.  */
531
532 static inline size_t
533 lcm (size_t u, size_t v)
534 {
535   return u * (v / gcd (u, v));
536 }
537
538 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
539    ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
540    PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
541    locations.  */
542
543 static inline void *
544 ptr_align (void const *ptr, size_t alignment)
545 {
546   char const *p0 = ptr;
547   char const *p1 = p0 + alignment - 1;
548   return (void *) (p1 - (size_t) p1 % alignment);
549 }
550
551 /* If 10*Accum + Digit_val is larger than the maximum value for Type,
552    then don't update Accum and return false to indicate it would
553    overflow.  Otherwise, set Accum to that new value and return true.
554    Verify at compile-time that Type is Accum's type, and that Type is
555    unsigned.  Accum must be an object, so that we can take its
556    address.  Accum and Digit_val may be evaluated multiple times.
557
558    The "Added check" below is not strictly required, but it causes GCC
559    to return a nonzero exit status instead of merely a warning
560    diagnostic, and that is more useful.  */
561
562 #define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type)                \
563   (                                                                     \
564    (void) (&(Accum) == (Type *) NULL),  /* The type matches.  */        \
565    (void) verify_true (! TYPE_SIGNED (Type)), /* The type is unsigned.  */ \
566    (void) verify_true (sizeof (Accum) == sizeof (Type)), /* Added check.  */ \
567    (((Type) -1 / 10 < (Accum)                                           \
568      || (Type) ((Accum) * 10 + (Digit_val)) < (Accum))                  \
569     ? false : (((Accum) = (Accum) * 10 + (Digit_val)), true))           \
570   )
571
572 static inline void
573 emit_size_note (void)
574 {
575   fputs (_("\n\
576 SIZE may be (or may be an integer optionally followed by) one of following:\n\
577 KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.\n\
578 "), stdout);
579 }
580
581 static inline void
582 emit_blocksize_note (char const *program)
583 {
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);
589 }
590
591 static inline void
592 emit_ancillary_info (void)
593 {
594   printf (_("\nReport %s bugs to %s\n"), last_component (program_name),
595           PACKAGE_BUGREPORT);
596   /* FIXME 2010: use AC_PACKAGE_URL once we require autoconf-2.64 */
597   printf (_("%s home page: <http://www.gnu.org/software/%s/>\n"),
598           PACKAGE_NAME, PACKAGE);
599   fputs (_("General help using GNU software: <http://www.gnu.org/gethelp/>\n"),
600          stdout);
601   /* Don't output this redundant message for English locales.
602      Note we still output for 'C' so that it gets included in the man page.  */
603   const char *lc_messages = setlocale (LC_MESSAGES, NULL);
604   if (lc_messages && STRNCMP_LIT (lc_messages, "en_"))
605     {
606       /* TRANSLATORS: Replace LANG_CODE in this URL with your language code
607          <http://translationproject.org/team/LANG_CODE.html> to form one of
608          the URLs at http://translationproject.org/team/.  Otherwise, replace
609          the entire URL with your translation team's email address.  */
610       printf (_("Report %s translation bugs to "
611                 "<http://translationproject.org/team/>\n"),
612                 last_component (program_name));
613     }
614   printf (_("For complete documentation, run: "
615             "info coreutils '%s invocation'\n"), last_component (program_name));
616 }
617
618 #include "inttostr.h"
619
620 static inline char *
621 timetostr (time_t t, char *buf)
622 {
623   return (TYPE_SIGNED (time_t)
624           ? imaxtostr (t, buf)
625           : umaxtostr (t, buf));
626 }
627
628 static inline char *
629 bad_cast (char const *s)
630 {
631   return (char *) s;
632 }
633
634 /* As of Mar 2009, 32KiB is determined to be the minimium
635    blksize to best minimize system call overhead.
636    This can be tested with this script with the results
637    shown for a 1.7GHz pentium-m with 2GB of 400MHz DDR2 RAM:
638
639    for i in $(seq 0 10); do
640      size=$((8*1024**3)) #ensure this is big enough
641      bs=$((1024*2**$i))
642      printf "%7s=" $bs
643      dd bs=$bs if=/dev/zero of=/dev/null count=$(($size/$bs)) 2>&1 |
644      sed -n 's/.* \([0-9.]* [GM]B\/s\)/\1/p'
645    done
646
647       1024=734 MB/s
648       2048=1.3 GB/s
649       4096=2.4 GB/s
650       8192=3.5 GB/s
651      16384=3.9 GB/s
652      32768=5.2 GB/s
653      65536=5.3 GB/s
654     131072=5.5 GB/s
655     262144=5.7 GB/s
656     524288=5.7 GB/s
657    1048576=5.8 GB/s
658
659    Note that this is to minimize system call overhead.
660    Other values may be appropriate to minimize file system
661    or disk overhead.  For example on my current GNU/Linux system
662    the readahead setting is 128KiB which was read using:
663
664    file="."
665    device=$(df -P --local "$file" | tail -n1 | cut -d' ' -f1)
666    echo $(( $(blockdev --getra $device) * 512 ))
667
668    However there isn't a portable way to get the above.
669    In the future we could use the above method if available
670    and default to io_blksize() if not.
671  */
672 enum { IO_BUFSIZE = 32*1024 };
673 static inline size_t
674 io_blksize (struct stat sb)
675 {
676   return MAX (IO_BUFSIZE, ST_BLKSIZE (sb));
677 }
678
679 void usage (int status) ATTRIBUTE_NORETURN;
680
681 #define emit_cycle_warning(file_name)   \
682   do                                    \
683     {                                   \
684       error (0, 0, _("\
685 WARNING: Circular directory structure.\n\
686 This almost certainly means that you have a corrupted file system.\n\
687 NOTIFY YOUR SYSTEM MANAGER.\n\
688 The following directory is part of the cycle:\n  %s\n"), \
689              quote (file_name));        \
690     }                                   \
691   while (0)
692
693 #ifndef ARRAY_CARDINALITY
694 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
695 #endif