blob: 84711219492cd8a4c0a9cbd66b185c69f3711944 [file] [log] [blame]
Brad Bishopc68388fc2019-08-26 01:33:31 -04001---
2 locale/programs/c.h | 407 ++++++++++++++++++++++
3 locale/programs/cross-localedef-hardlink.c | 528 +++++++++++++++++++++++++++++
4 locale/programs/xalloc.h | 129 +++++++
5 localedef/Makefile.in | 8
6 4 files changed, 1071 insertions(+), 1 deletion(-)
7
8--- /dev/null
9+++ b/locale/programs/cross-localedef-hardlink.c
10@@ -0,0 +1,528 @@
11+/*
12+ * hardlink - consolidate duplicate files via hardlinks
13+ *
14+ * Copyright (C) 2018 Red Hat, Inc. All rights reserved.
15+ * Written by Jakub Jelinek <jakub@redhat.com>
16+ *
17+ * Copyright (C) 2019 Karel Zak <kzak@redhat.com>
18+ *
19+ * This program is free software; you can redistribute it and/or modify
20+ * it under the terms of the GNU General Public License as published by
21+ * the Free Software Foundation; either version 2 of the License, or
22+ * (at your option) any later version.
23+ *
24+ * This program is distributed in the hope that it would be useful,
25+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+ * GNU General Public License for more details.
28+ *
29+ * You should have received a copy of the GNU General Public License along
30+ * with this program; if not, write to the Free Software Foundation, Inc.,
31+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32+ */
33+#include <sys/types.h>
34+#include <stdlib.h>
35+#include <getopt.h>
36+#include <stdio.h>
37+#include <unistd.h>
38+#include <sys/stat.h>
39+#include <sys/mman.h>
40+#include <string.h>
41+#include <dirent.h>
42+#include <fcntl.h>
43+#include <errno.h>
44+#ifdef HAVE_PCRE
45+# define PCRE2_CODE_UNIT_WIDTH 8
46+# include <pcre2.h>
47+#endif
48+
49+#include "c.h"
50+#include "xalloc.h"
51+#include "nls.h"
52+#include "closestream.h"
53+
54+#define NHASH (1<<17) /* Must be a power of 2! */
55+#define NBUF 64
56+
57+struct hardlink_file;
58+
59+struct hardlink_hash {
60+ struct hardlink_hash *next;
61+ struct hardlink_file *chain;
62+ off_t size;
63+ time_t mtime;
64+};
65+
66+struct hardlink_dir {
67+ struct hardlink_dir *next;
68+ char name[];
69+};
70+
71+struct hardlink_file {
72+ struct hardlink_file *next;
73+ ino_t ino;
74+ dev_t dev;
75+ unsigned int cksum;
76+ char name[];
77+};
78+
79+struct hardlink_dynstr {
80+ char *buf;
81+ size_t alloc;
82+};
83+
84+struct hardlink_ctl {
85+ struct hardlink_dir *dirs;
86+ struct hardlink_hash *hps[NHASH];
87+ char iobuf1[BUFSIZ];
88+ char iobuf2[BUFSIZ];
89+ /* summary counters */
90+ unsigned long long ndirs;
91+ unsigned long long nobjects;
92+ unsigned long long nregfiles;
93+ unsigned long long ncomp;
94+ unsigned long long nlinks;
95+ unsigned long long nsaved;
96+ /* current device */
97+ dev_t dev;
98+ /* flags */
99+ unsigned int verbose;
100+ unsigned int
101+ no_link:1,
102+ content_only:1,
103+ force:1;
104+};
105+/* ctl is in global scope due use in atexit() */
106+struct hardlink_ctl global_ctl;
107+
108+__attribute__ ((always_inline))
109+static inline unsigned int hash(off_t size, time_t mtime)
110+{
111+ return (size ^ mtime) & (NHASH - 1);
112+}
113+
114+__attribute__ ((always_inline))
115+static inline int stcmp(struct stat *st1, struct stat *st2, int content_scope)
116+{
117+ if (content_scope)
118+ return st1->st_size != st2->st_size;
119+
120+ return st1->st_mode != st2->st_mode
121+ || st1->st_uid != st2->st_uid
122+ || st1->st_gid != st2->st_gid
123+ || st1->st_size != st2->st_size
124+ || st1->st_mtime != st2->st_mtime;
125+}
126+
127+static void print_summary(void)
128+{
129+ struct hardlink_ctl const *const ctl = &global_ctl;
130+
131+ if (!ctl->verbose)
132+ return;
133+
134+ if (ctl->verbose > 1 && ctl->nlinks)
135+ fputc('\n', stdout);
136+
137+ printf(_("Directories: %9lld\n"), ctl->ndirs);
138+ printf(_("Objects: %9lld\n"), ctl->nobjects);
139+ printf(_("Regular files: %9lld\n"), ctl->nregfiles);
140+ printf(_("Comparisons: %9lld\n"), ctl->ncomp);
141+ printf( "%s%9lld\n", (ctl->no_link ?
142+ _("Would link: ") :
143+ _("Linked: ")), ctl->nlinks);
144+ printf( "%s %9lld\n", (ctl->no_link ?
145+ _("Would save: ") :
146+ _("Saved: ")), ctl->nsaved);
147+}
148+
149+static void __attribute__((__noreturn__)) usage(void)
150+{
151+ fputs(USAGE_HEADER, stdout);
152+ printf(_(" %s [options] directory...\n"), program_invocation_short_name);
153+
154+ fputs(USAGE_SEPARATOR, stdout);
155+ puts(_("Consolidate duplicate files using hardlinks."));
156+
157+ fputs(USAGE_OPTIONS, stdout);
158+ puts(_(" -c, --content compare only contents, ignore permission, etc."));
159+ puts(_(" -n, --dry-run don't actually link anything"));
160+ puts(_(" -v, --verbose print summary after hardlinking"));
161+ puts(_(" -vv print every hardlinked file and summary"));
162+ puts(_(" -f, --force force hardlinking across filesystems"));
163+ puts(_(" -x, --exclude <regex> exclude files matching pattern"));
164+
165+ fputs(USAGE_SEPARATOR, stdout);
166+ printf(USAGE_HELP_OPTIONS(16)); /* char offset to align option descriptions */
167+ printf(USAGE_MAN_TAIL("hardlink(1)"));
168+ exit(EXIT_SUCCESS);
169+}
170+
171+__attribute__ ((always_inline))
172+static inline size_t add2(size_t a, size_t b)
173+{
174+ size_t sum = a + b;
175+
176+ if (sum < a)
177+ errx(EXIT_FAILURE, _("integer overflow"));
178+ return sum;
179+}
180+
181+__attribute__ ((always_inline))
182+static inline size_t add3(size_t a, size_t b, size_t c)
183+{
184+ return add2(add2(a, b), c);
185+}
186+
187+static void growstr(struct hardlink_dynstr *str, size_t newlen)
188+{
189+ if (newlen < str->alloc)
190+ return;
191+ str->buf = xrealloc(str->buf, str->alloc = add2(newlen, 1));
192+}
193+
194+static void process_path(struct hardlink_ctl *ctl, const char *name)
195+{
196+ struct stat st, st2, st3;
197+ const size_t namelen = strlen(name);
198+
199+ ctl->nobjects++;
200+ if (lstat(name, &st))
201+ return;
202+
203+ if (st.st_dev != ctl->dev && !ctl->force) {
204+ if (ctl->dev)
205+ errx(EXIT_FAILURE,
206+ _("%s is on different filesystem than the rest "
207+ "(use -f option to override)."), name);
208+ ctl->dev = st.st_dev;
209+ }
210+ if (S_ISDIR(st.st_mode)) {
211+ struct hardlink_dir *dp = xmalloc(add3(sizeof(*dp), namelen, 1));
212+ memcpy(dp->name, name, namelen + 1);
213+ dp->next = ctl->dirs;
214+ ctl->dirs = dp;
215+
216+ } else if (S_ISREG(st.st_mode)) {
217+ int fd, i;
218+ struct hardlink_file *fp, *fp2;
219+ struct hardlink_hash *hp;
220+ const char *n1, *n2;
221+ unsigned int buf[NBUF];
222+ int cksumsize = sizeof(buf);
223+ unsigned int cksum;
224+ time_t mtime = ctl->content_only ? 0 : st.st_mtime;
225+ unsigned int hsh = hash(st.st_size, mtime);
226+ off_t fsize;
227+
228+ ctl->nregfiles++;
229+ if (ctl->verbose > 1)
230+ printf("%s\n", name);
231+
232+ fd = open(name, O_RDONLY);
233+ if (fd < 0)
234+ return;
235+
236+ if ((size_t)st.st_size < sizeof(buf)) {
237+ cksumsize = st.st_size;
238+ memset(((char *)buf) + cksumsize, 0,
239+ (sizeof(buf) - cksumsize) % sizeof(buf[0]));
240+ }
241+ if (read(fd, buf, cksumsize) != cksumsize) {
242+ close(fd);
243+ return;
244+ }
245+ cksumsize = (cksumsize + sizeof(buf[0]) - 1) / sizeof(buf[0]);
246+ for (i = 0, cksum = 0; i < cksumsize; i++) {
247+ if (cksum + buf[i] < cksum)
248+ cksum += buf[i] + 1;
249+ else
250+ cksum += buf[i];
251+ }
252+ for (hp = ctl->hps[hsh]; hp; hp = hp->next) {
253+ if (hp->size == st.st_size && hp->mtime == mtime)
254+ break;
255+ }
256+ if (!hp) {
257+ hp = xmalloc(sizeof(*hp));
258+ hp->size = st.st_size;
259+ hp->mtime = mtime;
260+ hp->chain = NULL;
261+ hp->next = ctl->hps[hsh];
262+ ctl->hps[hsh] = hp;
263+ }
264+ for (fp = hp->chain; fp; fp = fp->next) {
265+ if (fp->cksum == cksum)
266+ break;
267+ }
268+ for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next) {
269+ if (fp2->ino == st.st_ino && fp2->dev == st.st_dev) {
270+ close(fd);
271+ return;
272+ }
273+ }
274+ for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next) {
275+
276+ if (!lstat(fp2->name, &st2) && S_ISREG(st2.st_mode) &&
277+ !stcmp(&st, &st2, ctl->content_only) &&
278+ st2.st_ino != st.st_ino &&
279+ st2.st_dev == st.st_dev) {
280+
281+ int fd2 = open(fp2->name, O_RDONLY);
282+ if (fd2 < 0)
283+ continue;
284+
285+ if (fstat(fd2, &st2) || !S_ISREG(st2.st_mode)
286+ || st2.st_size == 0) {
287+ close(fd2);
288+ continue;
289+ }
290+ ctl->ncomp++;
291+ lseek(fd, 0, SEEK_SET);
292+
293+ for (fsize = st.st_size; fsize > 0;
294+ fsize -= (off_t)sizeof(ctl->iobuf1)) {
295+ ssize_t xsz;
296+ ssize_t rsize = fsize > (ssize_t) sizeof(ctl->iobuf1) ?
297+ (ssize_t) sizeof(ctl->iobuf1) : fsize;
298+
299+ if ((xsz = read(fd, ctl->iobuf1, rsize)) != rsize)
300+ warn(_("cannot read %s"), name);
301+ else if ((xsz = read(fd2, ctl->iobuf2, rsize)) != rsize)
302+ warn(_("cannot read %s"), fp2->name);
303+
304+ if (xsz != rsize) {
305+ close(fd);
306+ close(fd2);
307+ return;
308+ }
309+ if (memcmp(ctl->iobuf1, ctl->iobuf2, rsize))
310+ break;
311+ }
312+ close(fd2);
313+ if (fsize > 0)
314+ continue;
315+ if (lstat(name, &st3)) {
316+ warn(_("cannot stat %s"), name);
317+ close(fd);
318+ return;
319+ }
320+ st3.st_atime = st.st_atime;
321+ if (stcmp(&st, &st3, 0)) {
322+ warnx(_("file %s changed underneath us"), name);
323+ close(fd);
324+ return;
325+ }
326+ n1 = fp2->name;
327+ n2 = name;
328+
329+ if (!ctl->no_link) {
330+ const char *suffix =
331+ ".$$$___cleanit___$$$";
332+ const size_t suffixlen = strlen(suffix);
333+ size_t n2len = strlen(n2);
334+ struct hardlink_dynstr nam2 = { NULL, 0 };
335+
336+ growstr(&nam2, add2(n2len, suffixlen));
337+ memcpy(nam2.buf, n2, n2len);
338+ memcpy(&nam2.buf[n2len], suffix,
339+ suffixlen + 1);
340+ /* First create a temporary link to n1 under a new name */
341+ if (link(n1, nam2.buf)) {
342+ warn(_("failed to hardlink %s to %s (create temporary link as %s failed)"),
343+ n1, n2, nam2.buf);
344+ free(nam2.buf);
345+ continue;
346+ }
347+ /* Then rename into place over the existing n2 */
348+ if (rename(nam2.buf, n2)) {
349+ warn(_("failed to hardlink %s to %s (rename temporary link to %s failed)"),
350+ n1, n2, n2);
351+ /* Something went wrong, try to remove the now redundant temporary link */
352+ if (unlink(nam2.buf))
353+ warn(_("failed to remove temporary link %s"), nam2.buf);
354+ free(nam2.buf);
355+ continue;
356+ }
357+ free(nam2.buf);
358+ }
359+ ctl->nlinks++;
360+ if (st3.st_nlink > 1) {
361+ /* We actually did not save anything this time, since the link second argument
362+ had some other links as well. */
363+ if (ctl->verbose > 1)
364+ printf(_(" %s %s to %s\n"),
365+ (ctl->no_link ? _("Would link") : _("Linked")),
366+ n1, n2);
367+ } else {
368+ ctl->nsaved += ((st.st_size + 4095) / 4096) * 4096;
369+ if (ctl->verbose > 1)
370+ printf(_(" %s %s to %s, %s %jd\n"),
371+ (ctl->no_link ? _("Would link") : _("Linked")),
372+ n1, n2,
373+ (ctl->no_link ? _("would save") : _("saved")),
374+ (intmax_t)st.st_size);
375+ }
376+ close(fd);
377+ return;
378+ }
379+ }
380+ fp2 = xmalloc(add3(sizeof(*fp2), namelen, 1));
381+ close(fd);
382+ fp2->ino = st.st_ino;
383+ fp2->dev = st.st_dev;
384+ fp2->cksum = cksum;
385+ memcpy(fp2->name, name, namelen + 1);
386+
387+ if (fp) {
388+ fp2->next = fp->next;
389+ fp->next = fp2;
390+ } else {
391+ fp2->next = hp->chain;
392+ hp->chain = fp2;
393+ }
394+ return;
395+ }
396+}
397+
398+int main(int argc, char **argv)
399+{
400+ int ch;
401+ int i;
402+#ifdef HAVE_PCRE
403+ int errornumber;
404+ PCRE2_SIZE erroroffset;
405+ pcre2_code *re = NULL;
406+ PCRE2_SPTR exclude_pattern = NULL;
407+ pcre2_match_data *match_data = NULL;
408+#endif
409+ struct hardlink_dynstr nam1 = { NULL, 0 };
410+ struct hardlink_ctl *ctl = &global_ctl;
411+
412+ static const struct option longopts[] = {
413+ { "content", no_argument, NULL, 'c' },
414+ { "dry-run", no_argument, NULL, 'n' },
415+ { "exclude", required_argument, NULL, 'x' },
416+ { "force", no_argument, NULL, 'f' },
417+ { "help", no_argument, NULL, 'h' },
418+ { "verbose", no_argument, NULL, 'v' },
419+ { "version", no_argument, NULL, 'V' },
420+ { NULL, 0, NULL, 0 },
421+ };
422+
423+ setlocale(LC_ALL, "");
424+ bindtextdomain(PACKAGE, LOCALEDIR);
425+ textdomain(PACKAGE);
426+ close_stdout_atexit();
427+
428+ while ((ch = getopt_long(argc, argv, "cnvfx:Vh", longopts, NULL)) != -1) {
429+ switch (ch) {
430+ case 'n':
431+ ctl->no_link = 1;
432+ break;
433+ case 'v':
434+ ctl->verbose++;
435+ break;
436+ case 'c':
437+ ctl->content_only = 1;
438+ break;
439+ case 'f':
440+ ctl->force = 1;
441+ break;
442+ case 'x':
443+#ifdef HAVE_PCRE
444+ exclude_pattern = (PCRE2_SPTR) optarg;
445+#else
446+ errx(EXIT_FAILURE,
447+ _("option --exclude not supported (built without pcre2)"));
448+#endif
449+ break;
450+ case 'V':
451+ print_version(EXIT_SUCCESS);
452+ case 'h':
453+ usage();
454+ default:
455+ errtryhelp(EXIT_FAILURE);
456+ }
457+ }
458+
459+ if (optind == argc) {
460+ warnx(_("no directory specified"));
461+ errtryhelp(EXIT_FAILURE);
462+ }
463+
464+#ifdef HAVE_PCRE
465+ if (exclude_pattern) {
466+ re = pcre2_compile(exclude_pattern, /* the pattern */
467+ PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminate */
468+ 0, /* default options */
469+ &errornumber, &erroroffset, NULL); /* use default compile context */
470+ if (!re) {
471+ PCRE2_UCHAR buffer[256];
472+ pcre2_get_error_message(errornumber, buffer,
473+ sizeof(buffer));
474+ errx(EXIT_FAILURE, _("pattern error at offset %d: %s"),
475+ (int)erroroffset, buffer);
476+ }
477+ match_data = pcre2_match_data_create_from_pattern(re, NULL);
478+ }
479+#endif
480+ atexit(print_summary);
481+
482+ for (i = optind; i < argc; i++)
483+ process_path(ctl, argv[i]);
484+
485+ while (ctl->dirs) {
486+ DIR *dh;
487+ struct dirent *di;
488+ struct hardlink_dir *dp = ctl->dirs;
489+ size_t nam1baselen = strlen(dp->name);
490+
491+ ctl->dirs = dp->next;
492+ growstr(&nam1, add2(nam1baselen, 1));
493+ memcpy(nam1.buf, dp->name, nam1baselen);
494+ free(dp);
495+ nam1.buf[nam1baselen++] = '/';
496+ nam1.buf[nam1baselen] = 0;
497+ dh = opendir(nam1.buf);
498+
499+ if (dh == NULL)
500+ continue;
501+ ctl->ndirs++;
502+
503+ while ((di = readdir(dh)) != NULL) {
504+ if (!di->d_name[0])
505+ continue;
506+ if (di->d_name[0] == '.') {
507+ if (!di->d_name[1] || !strcmp(di->d_name, ".."))
508+ continue;
509+ }
510+#ifdef HAVE_PCRE
511+ if (re && pcre2_match(re, /* compiled regex */
512+ (PCRE2_SPTR) di->d_name, strlen(di->d_name), 0, /* start at offset 0 */
513+ 0, /* default options */
514+ match_data, /* block for storing the result */
515+ NULL) /* use default match context */
516+ >=0) {
517+ if (ctl->verbose) {
518+ nam1.buf[nam1baselen] = 0;
519+ printf(_("Skipping %s%s\n"), nam1.buf, di->d_name);
520+ }
521+ continue;
522+ }
523+#endif
524+ {
525+ size_t subdirlen;
526+ growstr(&nam1,
527+ add2(nam1baselen, subdirlen =
528+ strlen(di->d_name)));
529+ memcpy(&nam1.buf[nam1baselen], di->d_name,
530+ add2(subdirlen, 1));
531+ }
532+ process_path(ctl, nam1.buf);
533+ }
534+ closedir(dh);
535+ }
536+
537+ return 0;
538+}
539--- a/localedef/Makefile.in
540+++ b/localedef/Makefile.in
541@@ -40,6 +40,8 @@ WARNFLAGS = -Wall -Wno-format
542 FULLCC = $(CC) $(CPPFLAGS) $(CFLAGS) \
543 $(DEFINES) $(INCLUDES) $(WARNFLAGS)
544
545+CROSS_LOCALEDEF_HARDLINK_OBJS = cross-localedef-hardlink.o
546+
547 LOCALEDEF_OBJS = charmap.o charmap-dir.o ld-address.o ld-collate.o \
548 ld-ctype.o ld-identification.o ld-measurement.o \
549 ld-messages.o ld-monetary.o ld-name.o ld-numeric.o \
550@@ -54,11 +56,14 @@ LOCALEDEF_OBJS = charmap.o charmap-dir.o
551 asprintf.o getdelim.o localedef_extra.o \
552 obstack_printf.o vasprintf.o
553
554-all: localedef$(EXEEXT)
555+all: localedef$(EXEEXT) cross-localedef-hardlink$(EXEEXT)
556
557 localedef$(EXEEXT): $(LOCALEDEF_OBJS)
558 $(CC) -o $@ $(LOCALEDEF_OBJS) $(LIBS)
559
560+cross-localedef-hardlink$(EXEEXT): $(CROSS_LOCALEDEF_HARDLINK_OBJS)
561+ $(CC) -o $@ $(CROSS_LOCALEDEF_HARDLINK_OBJS) $(LIBS)
562+
563 clean:
564 rm -f locale$(EXEEXT) $(LOCALEDEF_OBJS)
565
566@@ -77,6 +82,7 @@ clean:
567 %.o: $(srcdir)/%.c
568 $(FULLCC) -c -o $@ $<
569
570+cross-localedef-hardlink.o: glibc/locale/programs/cross-localedef-hardlink.c
571 charmap.o: glibc/locale/programs/charmap.c
572 charmap-dir.o: glibc/locale/programs/charmap-dir.c
573 ld-address.o: glibc/locale/programs/ld-address.c
574--- /dev/null
575+++ b/locale/programs/c.h
576@@ -0,0 +1,407 @@
577+/*
578+ * Fundamental C definitions.
579+ */
580+
581+#ifndef UTIL_LINUX_C_H
582+#define UTIL_LINUX_C_H
583+
584+#include <limits.h>
585+#include <stddef.h>
586+#include <stdint.h>
587+#include <stdio.h>
588+#include <unistd.h>
589+#include <stdarg.h>
590+#include <stdlib.h>
591+#include <string.h>
592+#include <errno.h>
593+
594+#include <assert.h>
595+
596+#ifdef HAVE_ERR_H
597+# include <err.h>
598+#endif
599+
600+#ifdef HAVE_SYS_SYSMACROS_H
601+# include <sys/sysmacros.h> /* for major, minor */
602+#endif
603+
604+#ifndef LOGIN_NAME_MAX
605+# define LOGIN_NAME_MAX 256
606+#endif
607+
608+#ifndef NAME_MAX
609+# define NAME_MAX PATH_MAX
610+#endif
611+
612+/*
613+ * __GNUC_PREREQ is deprecated in favour of __has_attribute() and
614+ * __has_feature(). The __has macros are supported by clang and gcc>=5.
615+ */
616+#ifndef __GNUC_PREREQ
617+# if defined __GNUC__ && defined __GNUC_MINOR__
618+# define __GNUC_PREREQ(maj, min) \
619+ ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
620+# else
621+# define __GNUC_PREREQ(maj, min) 0
622+# endif
623+#endif
624+
625+#ifdef __GNUC__
626+
627+/* &a[0] degrades to a pointer: a different type from an array */
628+# define __must_be_array(a) \
629+ UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
630+
631+# define ignore_result(x) __extension__ ({ \
632+ __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
633+})
634+
635+#else /* !__GNUC__ */
636+# define __must_be_array(a) 0
637+# define __attribute__(_arg_)
638+# define ignore_result(x) ((void) (x))
639+#endif /* !__GNUC__ */
640+
641+/*
642+ * It evaluates to 1 if the attribute/feature is supported by the current
643+ * compilation targed. Fallback for old compilers.
644+ */
645+#ifndef __has_attribute
646+ #define __has_attribute(x) 0
647+#endif
648+
649+#ifndef __has_feature
650+ #define __has_feature(x) 0
651+#endif
652+
653+/*
654+ * Function attributes
655+ */
656+#ifndef __ul_alloc_size
657+# if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
658+# define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
659+# else
660+# define __ul_alloc_size(s)
661+# endif
662+#endif
663+
664+#ifndef __ul_calloc_size
665+# if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
666+# define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
667+# else
668+# define __ul_calloc_size(n, s)
669+# endif
670+#endif
671+
672+#if __has_attribute(returns_nonnull) || __GNUC_PREREQ (4, 9)
673+# define __ul_returns_nonnull __attribute__((returns_nonnull))
674+#else
675+# define __ul_returns_nonnull
676+#endif
677+
678+/*
679+ * Force a compilation error if condition is true, but also produce a
680+ * result (of value 0 and type size_t), so the expression can be used
681+ * e.g. in a structure initializer (or wherever else comma expressions
682+ * aren't permitted).
683+ */
684+#define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
685+#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
686+
687+#ifndef ARRAY_SIZE
688+# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
689+#endif
690+
691+#ifndef PATH_MAX
692+# define PATH_MAX 4096
693+#endif
694+
695+#ifndef TRUE
696+# define TRUE 1
697+#endif
698+
699+#ifndef FALSE
700+# define FALSE 0
701+#endif
702+
703+#ifndef min
704+# define min(x, y) __extension__ ({ \
705+ __typeof__(x) _min1 = (x); \
706+ __typeof__(y) _min2 = (y); \
707+ (void) (&_min1 == &_min2); \
708+ _min1 < _min2 ? _min1 : _min2; })
709+#endif
710+
711+#ifndef max
712+# define max(x, y) __extension__ ({ \
713+ __typeof__(x) _max1 = (x); \
714+ __typeof__(y) _max2 = (y); \
715+ (void) (&_max1 == &_max2); \
716+ _max1 > _max2 ? _max1 : _max2; })
717+#endif
718+
719+#ifndef cmp_numbers
720+# define cmp_numbers(x, y) __extension__ ({ \
721+ __typeof__(x) _a = (x); \
722+ __typeof__(y) _b = (y); \
723+ (void) (&_a == &_b); \
724+ _a == _b ? 0 : _a > _b ? 1 : -1; })
725+#endif
726+
727+#ifndef offsetof
728+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
729+#endif
730+
731+/*
732+ * container_of - cast a member of a structure out to the containing structure
733+ * @ptr: the pointer to the member.
734+ * @type: the type of the container struct this is embedded in.
735+ * @member: the name of the member within the struct.
736+ */
737+#ifndef container_of
738+#define container_of(ptr, type, member) __extension__ ({ \
739+ const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
740+ (type *)( (char *)__mptr - offsetof(type,member) );})
741+#endif
742+
743+#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
744+# ifdef HAVE___PROGNAME
745+extern char *__progname;
746+# define program_invocation_short_name __progname
747+# else
748+# ifdef HAVE_GETEXECNAME
749+# define program_invocation_short_name \
750+ prog_inv_sh_nm_from_file(getexecname(), 0)
751+# else
752+# define program_invocation_short_name \
753+ prog_inv_sh_nm_from_file(__FILE__, 1)
754+# endif
755+static char prog_inv_sh_nm_buf[256];
756+static inline char *
757+prog_inv_sh_nm_from_file(char *f, char stripext)
758+{
759+ char *t;
760+
761+ if ((t = strrchr(f, '/')) != NULL)
762+ t++;
763+ else
764+ t = f;
765+
766+ strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
767+ prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
768+
769+ if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
770+ *t = '\0';
771+
772+ return prog_inv_sh_nm_buf;
773+}
774+# endif
775+#endif
776+
777+
778+#ifndef HAVE_ERR_H
779+static inline void
780+errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
781+{
782+ fprintf(stderr, "%s: ", program_invocation_short_name);
783+ if (fmt != NULL) {
784+ va_list argp;
785+ va_start(argp, fmt);
786+ vfprintf(stderr, fmt, argp);
787+ va_end(argp);
788+ if (adderr)
789+ fprintf(stderr, ": ");
790+ }
791+ if (adderr)
792+ fprintf(stderr, "%m");
793+ fprintf(stderr, "\n");
794+ if (doexit)
795+ exit(excode);
796+}
797+
798+#ifndef HAVE_ERR
799+# define err(E, FMT...) errmsg(1, E, 1, FMT)
800+#endif
801+
802+#ifndef HAVE_ERRX
803+# define errx(E, FMT...) errmsg(1, E, 0, FMT)
804+#endif
805+
806+#ifndef HAVE_WARN
807+# define warn(FMT...) errmsg(0, 0, 1, FMT)
808+#endif
809+
810+#ifndef HAVE_WARNX
811+# define warnx(FMT...) errmsg(0, 0, 0, FMT)
812+#endif
813+#endif /* !HAVE_ERR_H */
814+
815+
816+/* Don't use inline function to avoid '#include "nls.h"' in c.h
817+ */
818+#define errtryhelp(eval) __extension__ ({ \
819+ fprintf(stderr, _("Try '%s --help' for more information.\n"), \
820+ program_invocation_short_name); \
821+ exit(eval); \
822+})
823+
824+/* After failed execvp() */
825+#define EX_EXEC_FAILED 126 /* Program located, but not usable. */
826+#define EX_EXEC_ENOENT 127 /* Could not find program to exec. */
827+#define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \
828+ _("failed to execute %s"), name)
829+
830+
831+static inline __attribute__((const)) int is_power_of_2(unsigned long num)
832+{
833+ return (num != 0 && ((num & (num - 1)) == 0));
834+}
835+
836+#ifndef HAVE_LOFF_T
837+typedef int64_t loff_t;
838+#endif
839+
840+#if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
841+#include <sys/types.h>
842+#include <dirent.h>
843+static inline int dirfd(DIR *d)
844+{
845+ return d->dd_fd;
846+}
847+#endif
848+
849+/*
850+ * Fallback defines for old versions of glibc
851+ */
852+#include <fcntl.h>
853+
854+#ifdef O_CLOEXEC
855+#define UL_CLOEXECSTR "e"
856+#else
857+#define UL_CLOEXECSTR ""
858+#endif
859+
860+#ifndef O_CLOEXEC
861+#define O_CLOEXEC 0
862+#endif
863+
864+#ifdef __FreeBSD_kernel__
865+#ifndef F_DUPFD_CLOEXEC
866+#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
867+#endif
868+#endif
869+
870+
871+#ifndef AI_ADDRCONFIG
872+#define AI_ADDRCONFIG 0x0020
873+#endif
874+
875+#ifndef IUTF8
876+#define IUTF8 0040000
877+#endif
878+
879+/*
880+ * MAXHOSTNAMELEN replacement
881+ */
882+static inline size_t get_hostname_max(void)
883+{
884+ long len = sysconf(_SC_HOST_NAME_MAX);
885+
886+ if (0 < len)
887+ return len;
888+
889+#ifdef MAXHOSTNAMELEN
890+ return MAXHOSTNAMELEN;
891+#elif HOST_NAME_MAX
892+ return HOST_NAME_MAX;
893+#endif
894+ return 64;
895+}
896+
897+
898+/*
899+ * Constant strings for usage() functions. For more info see
900+ * Documentation/{howto-usage-function.txt,boilerplate.c}
901+ */
902+#define USAGE_HEADER ("\nUsage:\n")
903+#define USAGE_OPTIONS ("\nOptions:\n")
904+#define USAGE_FUNCTIONS ("\nFunctions:\n")
905+#define USAGE_COMMANDS ("\nCommands:\n")
906+#define USAGE_COLUMNS ("\nAvailable output columns:\n")
907+#define USAGE_SEPARATOR "\n"
908+
909+#define USAGE_OPTSTR_HELP ("display this help")
910+#define USAGE_OPTSTR_VERSION ("display version")
911+
912+#define USAGE_HELP_OPTIONS(marg_dsc) \
913+ "%-" #marg_dsc "s%s\n" \
914+ "%-" #marg_dsc "s%s\n" \
915+ , " -h, --help", USAGE_OPTSTR_HELP \
916+ , " -V, --version", USAGE_OPTSTR_VERSION
917+
918+#define USAGE_MAN_TAIL(_man) ("\nFor more details see %s.\n"), _man
919+
920+#define UTIL_LINUX_VERSION ("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
921+
922+#define print_version(eval) __extension__ ({ \
923+ printf(UTIL_LINUX_VERSION); \
924+ exit(eval); \
925+})
926+
927+/*
928+ * scanf modifiers for "strings allocation"
929+ */
930+#ifdef HAVE_SCANF_MS_MODIFIER
931+#define UL_SCNsA "%ms"
932+#elif defined(HAVE_SCANF_AS_MODIFIER)
933+#define UL_SCNsA "%as"
934+#endif
935+
936+/*
937+ * seek stuff
938+ */
939+#ifndef SEEK_DATA
940+# define SEEK_DATA 3
941+#endif
942+#ifndef SEEK_HOLE
943+# define SEEK_HOLE 4
944+#endif
945+
946+
947+/*
948+ * Macros to convert #define'itions to strings, for example
949+ * #define XYXXY 42
950+ * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
951+ */
952+#define stringify_value(s) stringify(s)
953+#define stringify(s) #s
954+
955+/*
956+ * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
957+ * instrumentation shipped with Clang and GCC) to not instrument the
958+ * annotated function. Furthermore, it will prevent the compiler from
959+ * inlining the function because inlining currently breaks the blacklisting
960+ * mechanism of AddressSanitizer.
961+ */
962+#if __has_feature(address_sanitizer) && __has_attribute(no_sanitize_memory) && __has_attribute(no_sanitize_address)
963+# define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
964+#else
965+# define UL_ASAN_BLACKLIST /* nothing */
966+#endif
967+
968+/*
969+ * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
970+ * pwd buffer and in some cases it is not large enough. See POSIX and
971+ * getpwnam_r man page for more details.
972+ */
973+#define UL_GETPW_BUFSIZ (16 * 1024)
974+
975+/*
976+ * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
977+ * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
978+ */
979+#if !defined MAP_ANONYMOUS && defined MAP_ANON
980+# define MAP_ANONYMOUS (MAP_ANON)
981+#endif
982+
983+#endif /* UTIL_LINUX_C_H */
984--- /dev/null
985+++ b/locale/programs/xalloc.h
986@@ -0,0 +1,129 @@
987+/*
988+ * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
989+ *
990+ * This file may be redistributed under the terms of the
991+ * GNU Lesser General Public License.
992+ *
993+ * General memory allocation wrappers for malloc, realloc, calloc and strdup
994+ */
995+
996+#ifndef UTIL_LINUX_XALLOC_H
997+#define UTIL_LINUX_XALLOC_H
998+
999+#include <stdlib.h>
1000+#include <string.h>
1001+
1002+#include "c.h"
1003+
1004+#ifndef XALLOC_EXIT_CODE
1005+# define XALLOC_EXIT_CODE EXIT_FAILURE
1006+#endif
1007+
1008+static inline void __attribute__((__noreturn__))
1009+__err_oom(const char *file, unsigned int line)
1010+{
1011+ err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line);
1012+}
1013+
1014+#define err_oom() __err_oom(__FILE__, __LINE__)
1015+
1016+static inline __ul_alloc_size(1) __ul_returns_nonnull
1017+void *xmalloc(const size_t size)
1018+{
1019+ void *ret = malloc(size);
1020+
1021+ if (!ret && size)
1022+ err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
1023+ return ret;
1024+}
1025+
1026+static inline __ul_alloc_size(2) __ul_returns_nonnull
1027+void *xrealloc(void *ptr, const size_t size)
1028+{
1029+ void *ret = realloc(ptr, size);
1030+
1031+ if (!ret && size)
1032+ err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
1033+ return ret;
1034+}
1035+
1036+static inline __ul_calloc_size(1, 2) __ul_returns_nonnull
1037+void *xcalloc(const size_t nelems, const size_t size)
1038+{
1039+ void *ret = calloc(nelems, size);
1040+
1041+ if (!ret && size && nelems)
1042+ err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
1043+ return ret;
1044+}
1045+
1046+static inline char __attribute__((warn_unused_result)) __ul_returns_nonnull
1047+*xstrdup(const char *str)
1048+{
1049+ char *ret;
1050+
1051+ if (!str)
1052+ return NULL;
1053+
1054+ ret = strdup(str);
1055+
1056+ if (!ret)
1057+ err(XALLOC_EXIT_CODE, "cannot duplicate string");
1058+ return ret;
1059+}
1060+
1061+static inline char * __attribute__((warn_unused_result)) __ul_returns_nonnull
1062+xstrndup(const char *str, size_t size)
1063+{
1064+ char *ret;
1065+
1066+ if (!str)
1067+ return NULL;
1068+
1069+ ret = strndup(str, size);
1070+
1071+ if (!ret)
1072+ err(XALLOC_EXIT_CODE, "cannot duplicate string");
1073+ return ret;
1074+}
1075+
1076+
1077+static inline int __attribute__ ((__format__(printf, 2, 3)))
1078+ xasprintf(char **strp, const char *fmt, ...)
1079+{
1080+ int ret;
1081+ va_list args;
1082+ va_start(args, fmt);
1083+ ret = vasprintf(&(*strp), fmt, args);
1084+ va_end(args);
1085+ if (ret < 0)
1086+ err(XALLOC_EXIT_CODE, "cannot allocate string");
1087+ return ret;
1088+}
1089+
1090+static inline int __attribute__ ((__format__(printf, 2, 0)))
1091+xvasprintf(char **strp, const char *fmt, va_list ap)
1092+{
1093+ int ret = vasprintf(&(*strp), fmt, ap);
1094+ if (ret < 0)
1095+ err(XALLOC_EXIT_CODE, "cannot allocate string");
1096+ return ret;
1097+}
1098+
1099+
1100+static inline char * __attribute__((warn_unused_result)) xgethostname(void)
1101+{
1102+ char *name;
1103+ size_t sz = get_hostname_max() + 1;
1104+
1105+ name = xmalloc(sizeof(char) * sz);
1106+
1107+ if (gethostname(name, sz) != 0) {
1108+ free(name);
1109+ return NULL;
1110+ }
1111+ name[sz - 1] = '\0';
1112+ return name;
1113+}
1114+
1115+#endif