1 diff -urNp -x '*.orig' busybox-1.31.1.org/coreutils/printf.c busybox-1.31.1/coreutils/printf.c
2 --- busybox-1.31.1.org/coreutils/printf.c 2019-06-10 12:50:53.000000000 +0200
3 +++ busybox-1.31.1/coreutils/printf.c 2021-10-21 10:28:37.833148938 +0200
5 //usage: "$ printf \"Val=%d\\n\" 5\n"
9 +#define BB_FEATURE_PRINTF_GETTEXT
13 /* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
14 @@ -400,11 +403,132 @@ static char **print_formatted(char *f, c
19 + * Very pure gettext added by Michal Moskal <malekith@pld-linux.org>
20 + * This possibly could be converted into utility function
21 + * and used in other places as well.
24 +#ifdef BB_FEATURE_PRINTF_GETTEXT
25 +/* The magic number of the GNU message catalog format. */
26 +#define _MAGIC 0x950412de
28 +/* Header for binary .mo file format. */
29 +struct mo_file_header
31 + /* The magic number. */
33 + /* The revision number of the file format. */
35 + /* The number of strings pairs. */
37 + /* Offset of table with start offsets of original strings. */
38 + u_int32_t orig_tab_offset;
39 + /* Offset of table with start offsets of translation strings. */
40 + u_int32_t trans_tab_offset;
41 + /* Size of hashing table. */
42 + u_int32_t hash_tab_size;
43 + /* Offset of first hashing entry. */
44 + u_int32_t hash_tab_offset;
49 + /* Length of addressed string. */
51 + /* Offset of string in file. */
55 +static u_int32_t swap(u_int32_t i)
57 + return (i << 24) | ((i & 0xff00) << 8) |
58 + ((i >> 8) & 0xff00) | (i >> 24);
60 +#define swap_if(a) ((has_to_swap) ? swap(a) : (a))
61 +static char *getmsg(const char *filename, const char *msgid)
64 + struct mo_file_header *ptr;
68 + struct string_desc *orig_tab, *trans_tab = NULL;
70 + char *ret = (char*)msgid;
72 + if (filename == NULL || stat(filename, &st))
75 + fd = open(filename, O_RDONLY);
79 + ptr = (struct mo_file_header *) mmap(NULL, st.st_size, PROT_READ,
80 + MAP_PRIVATE, fd, 0);
83 + if (ptr == (void*)-1)
86 + has_to_swap = ptr->magic != _MAGIC;
88 + if (swap_if(ptr->magic) != _MAGIC)
91 + /* FIXME: use hash table */
93 + orig_tab = (struct string_desc *)
94 + ((char *) ptr + swap_if(ptr->orig_tab_offset));
95 + trans_tab = (struct string_desc *)
96 + ((char *) ptr + swap_if(ptr->trans_tab_offset));
99 + top = swap_if(ptr->nstrings);
100 + while (bottom < top) {
102 + act = (bottom + top) / 2;
105 + ((char *) ptr + swap_if(orig_tab[act].offset)));
108 + else if (cmp_val > 0)
117 + ret = strdup(((char *) ptr + swap_if(trans_tab[act].offset)));
118 + munmap(ptr, st.st_size);
122 +# define getmsg(a,b) (b)
125 int printf_main(int argc UNUSED_PARAM, char **argv)
131 + const char *nls_file = NULL;
133 + while ((opt = getopt(argc, argv, "n:")) != -1)
143 + format = getmsg(nls_file, argv[optind++]);
145 /* We must check that stdout is not closed.
146 * The reason for this is highly non-obvious.
147 @@ -436,8 +560,8 @@ int printf_main(int argc UNUSED_PARAM, c