1 diff -ur busybox-0.60.1/printf.c busybox-0.60.1.new/printf.c
2 --- busybox-0.60.1/printf.c Mon Mar 19 20:25:49 2001
3 +++ busybox-0.60.1.new/printf.c Thu May 31 16:41:31 2001
6 // 19990508 Busy Boxed! Dave Cinege
9 +#define BB_FEATURE_PRINTF_GETTEXT
13 #include <sys/types.h>
21 +#include <sys/stat.h>
22 +#include <sys/mman.h>
26 @@ -117,19 +125,140 @@
27 /* The value to return to the calling program. */
28 static int exit_status;
32 + * Very pure gettext added by Michal Moskal <malekith@pld-linux.org>
33 + * This possibly could be converted into utility function
34 + * and used in other places as well.
37 +#ifdef BB_FEATURE_PRINTF_GETTEXT
38 +/* The magic number of the GNU message catalog format. */
39 +#define _MAGIC 0x950412de
41 +/* Header for binary .mo file format. */
42 +struct mo_file_header
44 + /* The magic number. */
46 + /* The revision number of the file format. */
48 + /* The number of strings pairs. */
50 + /* Offset of table with start offsets of original strings. */
51 + u_int32_t orig_tab_offset;
52 + /* Offset of table with start offsets of translation strings. */
53 + u_int32_t trans_tab_offset;
54 + /* Size of hashing table. */
55 + u_int32_t hash_tab_size;
56 + /* Offset of first hashing entry. */
57 + u_int32_t hash_tab_offset;
62 + /* Length of addressed string. */
64 + /* Offset of string in file. */
68 +static u_int32_t swap(u_int32_t i)
70 + return (i << 24) | ((i & 0xff00) << 8) |
71 + ((i >> 8) & 0xff00) | (i >> 24);
74 +#define swap_if(a) ((has_to_swap) ? swap(a) : (a))
76 +static char *getmsg(const char *filename, const char *msgid)
79 + struct mo_file_header *ptr;
83 + struct string_desc *orig_tab, *trans_tab = NULL;
85 + char *ret = (char*)msgid;
87 + if (filename == NULL || stat(filename, &st))
90 + fd = open(filename, O_RDONLY);
94 + ptr = (struct mo_file_header *) mmap(NULL, st.st_size, PROT_READ,
95 + MAP_PRIVATE, fd, 0);
98 + if (ptr == (void*)-1)
101 + has_to_swap = ptr->magic != _MAGIC;
103 + if (swap_if(ptr->magic) != _MAGIC)
106 + /* FIXME: use hash table */
108 + orig_tab = (struct string_desc *)
109 + ((char *) ptr + swap_if(ptr->orig_tab_offset));
110 + trans_tab = (struct string_desc *)
111 + ((char *) ptr + swap_if(ptr->trans_tab_offset));
114 + top = swap_if(ptr->nstrings);
115 + while (bottom < top) {
118 + act = (bottom + top) / 2;
121 + ((char *) ptr + swap_if(orig_tab[act].offset)));
124 + else if (cmp_val > 0)
133 + ret = strdup(((char *) ptr + swap_if(trans_tab[act].offset)));
134 + munmap(ptr, st.st_size);
138 +# define getmsg(a,b) (b)
141 int printf_main(int argc, char **argv)
146 + const char *nls_file = NULL;
149 - if (argc <= 1 || **(argv + 1) == '-') {
156 + while ((opt = getopt(argc, argv, "n:")) != -1)
166 + format = getmsg(nls_file, argv[optind++]);
172 args_used = print_formatted(format, argc, argv);