]> git.pld-linux.org Git - packages/busybox.git/blob - busybox-printf-gettext.patch
- dropped, use mdadm instead
[packages/busybox.git] / busybox-printf-gettext.patch
1 diff -ur busybox-1.00.orig/coreutils/printf.c busybox-1.00/coreutils/printf.c
2 --- busybox-1.00.orig/coreutils/printf.c        2004-09-15 02:05:23.000000000 +0000
3 +++ busybox-1.00/coreutils/printf.c     2004-11-11 18:16:33.724386928 +0000
4 @@ -47,8 +47,13 @@
5  
6  //   19990508 Busy Boxed! Dave Cinege
7  
8 +// on by default
9 +#define BB_FEATURE_PRINTF_GETTEXT
10 +
11  #include <unistd.h>
12  #include <stdio.h>
13 +#include <sys/mman.h>
14 +#include <sys/stat.h>
15  #include <sys/types.h>
16  #include <string.h>
17  #include <errno.h>
18 @@ -66,18 +71,137 @@
19  static void print_direc __P( (char *start, size_t length,
20                         int field_width, int precision, char *argument));
21  
22 +/*
23 + * Very pure gettext added by Michal Moskal <malekith@pld-linux.org>
24 + * This possibly could be converted into utility function
25 + * and used in other places as well.
26 + */
27 +
28 +#ifdef BB_FEATURE_PRINTF_GETTEXT
29 +/* The magic number of the GNU message catalog format.  */
30 +#define _MAGIC 0x950412de
31 +
32 +/* Header for binary .mo file format.  */
33 +struct mo_file_header
34 +{
35 +       /* The magic number.  */
36 +       u_int32_t magic;
37 +       /* The revision number of the file format.  */
38 +       u_int32_t revision;
39 +       /* The number of strings pairs.  */
40 +       u_int32_t nstrings;
41 +       /* Offset of table with start offsets of original strings.  */
42 +       u_int32_t orig_tab_offset;
43 +       /* Offset of table with start offsets of translation strings.  */
44 +       u_int32_t trans_tab_offset;
45 +       /* Size of hashing table.  */
46 +       u_int32_t hash_tab_size;
47 +       /* Offset of first hashing entry.  */
48 +       u_int32_t hash_tab_offset;
49 +};
50 +
51 +struct string_desc
52 +{
53 +       /* Length of addressed string.  */
54 +       u_int32_t length;
55 +       /* Offset of string in file.  */
56 +       u_int32_t offset;
57 +};
58 +
59 +static u_int32_t swap(u_int32_t i)
60 +{
61 +       return (i << 24) | ((i & 0xff00) << 8) | 
62 +              ((i >> 8) & 0xff00) | (i >> 24);
63 +}
64 +
65 +#define swap_if(a) ((has_to_swap) ? swap(a) : (a))
66 +
67 +static char *getmsg(const char *filename, const char *msgid)
68 +{
69 +       int fd;
70 +       struct mo_file_header *ptr;
71 +       struct stat st;
72 +       int has_to_swap;
73 +       size_t top, bottom;
74 +       struct string_desc *orig_tab, *trans_tab = NULL;
75 +       int act = -1;
76 +       char *ret = (char*)msgid;
77 +               
78 +       if (filename == NULL || stat(filename, &st))
79 +               return ret;
80 +
81 +       fd = open(filename, O_RDONLY);
82 +       if (fd == -1)
83 +               return ret;
84 +
85 +       ptr = (struct mo_file_header *) mmap(NULL, st.st_size, PROT_READ,
86 +                                            MAP_PRIVATE, fd, 0);
87 +       close(fd);
88 +
89 +       if (ptr == (void*)-1)
90 +               return ret;
91 +
92 +       has_to_swap = ptr->magic != _MAGIC;
93 +
94 +       if (swap_if(ptr->magic) != _MAGIC)
95 +               goto oops;
96 +
97 +       /* FIXME: use hash table */
98 +
99 +       orig_tab = (struct string_desc *)
100 +           ((char *) ptr + swap_if(ptr->orig_tab_offset));
101 +       trans_tab = (struct string_desc *)
102 +           ((char *) ptr + swap_if(ptr->trans_tab_offset));
103 +
104 +       bottom = 0;
105 +       top = swap_if(ptr->nstrings);
106 +       while (bottom < top) {
107 +               int cmp_val;
108 +
109 +               act = (bottom + top) / 2;
110 +               cmp_val =
111 +                   strcmp(msgid,
112 +                          ((char *) ptr + swap_if(orig_tab[act].offset)));
113 +               if (cmp_val < 0)
114 +                       top = act;
115 +               else if (cmp_val > 0)
116 +                       bottom = act + 1;
117 +               else
118 +                       break;
119 +               act = -1;
120 +       }
121 +
122 +      oops:
123 +       if (act != -1)
124 +               ret = strdup(((char *) ptr + swap_if(trans_tab[act].offset)));
125 +       munmap(ptr, st.st_size);
126 +       return ret;
127 +}
128 +#else
129 +# define getmsg(a,b) (b)
130 +#endif
131 +
132  int printf_main(int argc, char **argv)
133  {
134         char *format;
135         int args_used;
136 +       int opt;
137 +       const char *nls_file = NULL;
138  
139 -       if (argc <= 1 || **(argv + 1) == '-') {
140 -               bb_show_usage();
141 -       }
142 +       while ((opt = getopt(argc, argv, "n:")) != -1)
143 +               switch (opt) {
144 +               case 'n':
145 +                       nls_file = optarg;
146 +                       break;
147 +               default:
148 +                       bb_show_usage();
149 +                       break;
150 +               }
151 +
152 +       format = getmsg(nls_file, argv[optind++]);
153  
154 -       format = argv[1];
155 -       argc -= 2;
156 -       argv += 2;
157 +       argc -= optind;
158 +       argv += optind;
159  
160         do {
161                 args_used = print_formatted(format, argc, argv);
This page took 0.041034 seconds and 3 git commands to generate.