2 * Copyright 2018 Nexenta Systems, Inc.
3 * Copyright 2015 John Marino <draco@marino.st>
5 * This source code is derived from the illumos localedef command, and
6 * provided under BSD-style license terms by Nexenta Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #include <sys/endian.h>
37 #include <sys/types.h>
50 #include "localedef.h"
54 #define TEXT_DOMAIN "SYS_TEST"
58 static int byteorder
= 0;
62 static char *locname
= NULL
;
63 static char locpath
[PATH_MAX
];
69 switch (get_category()) {
75 return ("LC_COLLATE");
79 return ("LC_MESSAGES");
81 return ("LC_MONETARY");
83 return ("LC_NUMERIC");
96 (void) snprintf(locpath
, sizeof (locpath
), "%s.%s",
97 locname
, category_name());
99 (void) snprintf(locpath
, sizeof (locpath
), "%s/%s",
100 locname
, category_name());
110 (void) printf("Writing category %s: ", category_name());
111 (void) fflush(stdout
);
114 /* make the parent directory */
116 (void) mkdir(dirname(category_file()), 0755);
119 * note that we have to regenerate the file name, as dirname
122 file
= fopen(category_file(), "w");
124 errf("%s", strerror(errno
));
131 close_category(FILE *f
)
133 if (fchmod(fileno(f
), 0644) < 0) {
135 (void) unlink(category_file());
136 errf("%s", strerror(errno
));
139 (void) unlink(category_file());
140 errf("%s", strerror(errno
));
143 (void) fprintf(stdout
, "done.\n");
144 (void) fflush(stdout
);
149 * This function is used when copying the category from another
150 * locale. Note that the copy is actually performed using a hard
151 * link for efficiency.
154 copy_category(char *src
)
156 char srcpath
[PATH_MAX
];
159 (void) snprintf(srcpath
, sizeof (srcpath
), "%s/%s",
160 src
, category_name());
161 rv
= access(srcpath
, R_OK
);
162 if ((rv
!= 0) && (strchr(srcpath
, '/') == NULL
)) {
163 /* Maybe we should try the system locale */
164 (void) snprintf(srcpath
, sizeof (srcpath
),
165 "/usr/lib/locale/%s/%s", src
, category_name());
166 rv
= access(srcpath
, R_OK
);
170 fprintf(stderr
,"source locale data unavailable: %s\n", src
);
175 (void) printf("Copying category %s from %s: ",
176 category_name(), src
);
177 (void) fflush(stdout
);
180 /* make the parent directory */
182 (void) mkdir(dirname(category_file()), 0755);
184 if (link(srcpath
, category_file()) != 0) {
185 fprintf(stderr
,"unable to copy locale data: %s\n",
190 (void) printf("done.\n");
195 putl_category(const char *s
, FILE *f
)
197 if (s
&& fputs(s
, f
) == EOF
) {
199 (void) unlink(category_file());
200 errf("%s", strerror(errno
));
203 if (fputc('\n', f
) == EOF
) {
205 (void) unlink(category_file());
206 errf("%s", strerror(errno
));
213 wr_category(void *buf
, size_t sz
, FILE *f
)
218 if (fwrite(buf
, sz
, 1, f
) < 1) {
220 (void) unlink(category_file());
221 errf("%s", strerror(errno
));
231 if (byteorder
== 4321)
232 return (htobe32(arg
));
233 else if (byteorder
== 1234)
234 return (htole32(arg
));
244 (void) fprintf(stderr
, "Usage: localedef [options] localename\n");
245 (void) fprintf(stderr
, "[options] are:\n");
246 (void) fprintf(stderr
, " -D : BSD-style output\n");
247 (void) fprintf(stderr
, " -b : big-endian output\n");
248 (void) fprintf(stderr
, " -c : ignore warnings\n");
249 (void) fprintf(stderr
, " -l : little-endian output\n");
250 (void) fprintf(stderr
, " -v : verbose output\n");
251 (void) fprintf(stderr
, " -U : ignore undefined symbols\n");
252 (void) fprintf(stderr
, " -f charmap : use given charmap file\n");
253 (void) fprintf(stderr
, " -u encoding : assume encoding\n");
254 (void) fprintf(stderr
, " -w widths : use screen widths file\n");
255 (void) fprintf(stderr
, " -i locsrc : source file for locale\n");
256 (void) fprintf(stderr
, " -V version : version string for locale\n");
261 main(int argc
, char **argv
)
281 (void) setlocale(LC_ALL
, "");
283 while ((c
= getopt(argc
, argv
, "blw:i:cf:u:vUDV:")) != -1) {
292 byteorder
= c
== 'b' ? 4321 : 1234;
301 set_wide_encoding(optarg
);
324 if ((argc
- 1) != (optind
)) {
327 locname
= argv
[argc
- 1];
329 (void) printf("Processing locale %s.\n", locname
);
332 if (version
&& strlen(version
) >= XLOCALE_DEF_VERSION_LEN
) {
333 (void) fprintf(stderr
, "Version string too long.\n");
339 (void) printf("Loading charmap %s.\n", cfname
);
340 reset_scanner(cfname
);
346 (void) printf("Loading widths %s.\n", wfname
);
347 reset_scanner(wfname
);
352 (void) printf("Loading POSIX portable characters.\n");
357 reset_scanner(lfname
);
362 /* make the directory for the locale if not already present */
364 while ((dir
= opendir(locname
)) == NULL
) {
365 if ((errno
!= ENOENT
) ||
366 (mkdir(locname
, 0755) < 0)) {
367 errf("%s", strerror(errno
));
370 (void) closedir(dir
);
371 (void) mkdir(dirname(category_file()), 0755);
376 (void) printf("All done.\n");
378 return (warnings
? 1 : 0);