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 __FBSDID("$FreeBSD$");
37 #include <sys/endian.h>
39 #include <sys/types.h>
52 #include "localedef.h"
56 #define TEXT_DOMAIN "SYS_TEST"
60 static int byteorder = 0;
64 static char *locname = NULL;
65 static char locpath[PATH_MAX];
71 switch (get_category()) {
77 return ("LC_COLLATE");
81 return ("LC_MESSAGES");
83 return ("LC_MONETARY");
85 return ("LC_NUMERIC");
98 (void) snprintf(locpath, sizeof (locpath), "%s.%s",
99 locname, category_name());
101 (void) snprintf(locpath, sizeof (locpath), "%s/%s",
102 locname, category_name());
112 (void) printf("Writing category %s: ", category_name());
113 (void) fflush(stdout);
116 /* make the parent directory */
118 (void) mkdir(dirname(category_file()), 0755);
121 * note that we have to regenerate the file name, as dirname
124 file = fopen(category_file(), "w");
126 errf("%s", strerror(errno));
133 close_category(FILE *f)
135 if (fchmod(fileno(f), 0644) < 0) {
137 (void) unlink(category_file());
138 errf("%s", strerror(errno));
141 (void) unlink(category_file());
142 errf("%s", strerror(errno));
145 (void) fprintf(stdout, "done.\n");
146 (void) fflush(stdout);
151 * This function is used when copying the category from another
152 * locale. Note that the copy is actually performed using a hard
153 * link for efficiency.
156 copy_category(char *src)
158 char srcpath[PATH_MAX];
161 (void) snprintf(srcpath, sizeof (srcpath), "%s/%s",
162 src, category_name());
163 rv = access(srcpath, R_OK);
164 if ((rv != 0) && (strchr(srcpath, '/') == NULL)) {
165 /* Maybe we should try the system locale */
166 (void) snprintf(srcpath, sizeof (srcpath),
167 "/usr/lib/locale/%s/%s", src, category_name());
168 rv = access(srcpath, R_OK);
172 fprintf(stderr,"source locale data unavailable: %s", src);
177 (void) printf("Copying category %s from %s: ",
178 category_name(), src);
179 (void) fflush(stdout);
182 /* make the parent directory */
184 (void) mkdir(dirname(category_file()), 0755);
186 if (link(srcpath, category_file()) != 0) {
187 fprintf(stderr,"unable to copy locale data: %s",
192 (void) printf("done.\n");
197 putl_category(const char *s, FILE *f)
199 if (s && fputs(s, f) == EOF) {
201 (void) unlink(category_file());
202 errf("%s", strerror(errno));
205 if (fputc('\n', f) == EOF) {
207 (void) unlink(category_file());
208 errf("%s", strerror(errno));
215 wr_category(void *buf, size_t sz, FILE *f)
220 if (fwrite(buf, sz, 1, f) < 1) {
222 (void) unlink(category_file());
223 errf("%s", strerror(errno));
233 if (byteorder == 4321)
234 return (htobe32(arg));
235 else if (byteorder == 1234)
236 return (htole32(arg));
246 (void) fprintf(stderr, "Usage: localedef [options] localename\n");
247 (void) fprintf(stderr, "[options] are:\n");
248 (void) fprintf(stderr, " -D : BSD-style output\n");
249 (void) fprintf(stderr, " -b : big-endian output\n");
250 (void) fprintf(stderr, " -c : ignore warnings\n");
251 (void) fprintf(stderr, " -l : little-endian output\n");
252 (void) fprintf(stderr, " -v : verbose output\n");
253 (void) fprintf(stderr, " -U : ignore undefined symbols\n");
254 (void) fprintf(stderr, " -f charmap : use given charmap file\n");
255 (void) fprintf(stderr, " -u encoding : assume encoding\n");
256 (void) fprintf(stderr, " -w widths : use screen widths file\n");
257 (void) fprintf(stderr, " -i locsrc : source file for locale\n");
258 (void) fprintf(stderr, " -V version : version string for locale\n");
263 main(int argc, char **argv)
283 (void) setlocale(LC_ALL, "");
285 while ((c = getopt(argc, argv, "blw:i:cf:u:vUDV:")) != -1) {
294 byteorder = c == 'b' ? 4321 : 1234;
303 set_wide_encoding(optarg);
326 if ((argc - 1) != (optind)) {
329 locname = argv[argc - 1];
331 (void) printf("Processing locale %s.\n", locname);
334 if (version && strlen(version) >= XLOCALE_DEF_VERSION_LEN) {
335 (void) fprintf(stderr, "Version string too long.\n");
341 (void) printf("Loading charmap %s.\n", cfname);
342 reset_scanner(cfname);
348 (void) printf("Loading widths %s.\n", wfname);
349 reset_scanner(wfname);
354 (void) printf("Loading POSIX portable characters.\n");
359 reset_scanner(lfname);
364 /* make the directory for the locale if not already present */
366 while ((dir = opendir(locname)) == NULL) {
367 if ((errno != ENOENT) ||
368 (mkdir(locname, 0755) < 0)) {
369 errf("%s", strerror(errno));
372 (void) closedir(dir);
373 (void) mkdir(dirname(category_file()), 0755);
378 (void) printf("All done.\n");
380 return (warnings ? 1 : 0);