4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2001, 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "gnu_msgfmt.h"
30 #include "../../lib/libc/inc/msgfmt.h"
33 search_msg(struct catalog
*p
, const char *id
, unsigned int hash_val
)
35 unsigned int i
, idx
, inc
;
38 idx
= hash_val
% p
->thash_size
;
39 inc
= 1 + (hash_val
% (p
->thash_size
- 2));
44 for (m
= p
->msg
; (i
= p
->thash
[idx
]) != 0;
45 idx
= (idx
+ inc
) % p
->thash_size
) {
46 if (strcmp(m
[i
- 1].id
, id
) == 0) {
55 msg_cmp(struct messages
*m1
, struct messages
*m2
)
57 return (strcmp(m1
->id
, m2
->id
));
61 output_all_gnu_mo_files(void)
63 struct catalog
*p
, *op
;
65 size_t id_len
, str_len
, id_off
, str_off
, ids_top
, strs_top
;
66 unsigned int *hash_tbl
;
67 unsigned int hash_size
;
68 unsigned int num
= 0, fnum
= 0, unum
= 0;
71 struct msgtbl
*id_tbl
, *str_tbl
;
72 struct gnu_msg_info header
;
86 * no message in this file
87 * skip generating a mo
95 p
->msg
= (struct messages
*)Xrealloc(p
->msg
,
96 sizeof (struct messages
) * p
->nmsg
);
99 * Sort the message array
101 qsort(p
->msg
, p
->nmsg
, sizeof (struct messages
),
102 (int (*)(const void *, const void *))msg_cmp
);
105 hash_size
= find_prime(p
->nmsg
);
106 hash_tbl
= (unsigned int *)Xcalloc(hash_size
,
107 sizeof (unsigned int));
110 /* Setting Header info */
111 header
.magic
= GNU_MAGIC
;
112 header
.revision
= GNU_REVISION
;
113 header
.num_of_str
= p
->nmsg
;
114 header
.off_msgid_tbl
= sizeof (struct gnu_msg_info
);
115 header
.off_msgstr_tbl
= sizeof (struct gnu_msg_info
) +
116 p
->nmsg
* sizeof (struct msgtbl
);
117 header
.sz_hashtbl
= hash_size
;
118 header
.off_hashtbl
= header
.off_msgstr_tbl
+
119 p
->nmsg
* sizeof (struct msgtbl
);
125 for (i
= 0; i
< p
->nmsg
; i
++) {
126 id_len
+= m
[i
].id_len
;
127 str_len
+= m
[i
].str_len
;
129 ids
= (char *)Xmalloc(id_len
);
130 strs
= (char *)Xmalloc(str_len
);
131 id_tbl
= (struct msgtbl
*)Xmalloc(sizeof (struct msgtbl
) *
133 str_tbl
= (struct msgtbl
*)Xmalloc(sizeof (struct msgtbl
) *
137 ids_top
= header
.off_hashtbl
+
138 sizeof (unsigned int) * hash_size
;
139 strs_top
= ids_top
+ id_len
;
140 for (i
= 0; i
< p
->nmsg
; i
++) {
144 idx
= get_hash_index(hash_tbl
, m
[i
].hash
, hash_size
);
145 hash_tbl
[idx
] = i
+ 1;
148 * rearrange msgid and msgstr
150 id_tbl
[i
].len
= m
[i
].id_len
- 1;
151 str_tbl
[i
].len
= m
[i
].str_len
- 1;
152 id_tbl
[i
].offset
= id_off
+ ids_top
;
153 str_tbl
[i
].offset
= str_off
+ strs_top
;
154 (void) memcpy(ids
+ id_off
, m
[i
].id
, m
[i
].id_len
);
155 (void) memcpy(strs
+ str_off
, m
[i
].str
, m
[i
].str_len
);
156 id_off
+= m
[i
].id_len
;
157 str_off
+= m
[i
].str_len
;
162 if ((out
= fopen(p
->fname
, "w")) == NULL
) {
163 error(gettext(ERR_OPEN_FAILED
), p
->fname
);
168 (void) fwrite(&header
, sizeof (struct gnu_msg_info
),
171 /* writing msgid offset table */
172 (void) fwrite(id_tbl
, sizeof (struct msgtbl
),
174 /* writing msgstr offset table */
175 (void) fwrite(str_tbl
, sizeof (struct msgtbl
),
177 /* writing hash table */
178 (void) fwrite(hash_tbl
, sizeof (unsigned int),
180 /* writing msgid table */
181 (void) fwrite(ids
, id_len
, 1, out
);
182 /* writing msgstr table */
183 (void) fwrite(strs
, str_len
, 1, out
);
199 diag(gettext(DIAG_RESULTS
), num
, fnum
, unum
);