2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
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$");
40 #include <sys/types.h>
49 #include "localedef.h"
53 #define TEXT_DOMAIN "SYS_TEST"
60 static char *locname = NULL;
61 static char locpath[PATH_MAX];
66 switch (get_category()) {
72 return ("LC_COLLATE");
76 return ("LC_MESSAGES");
78 return ("LC_MONETARY");
80 return ("LC_NUMERIC");
93 (void) snprintf(locpath, sizeof (locpath), "%s.%s",
94 locname, category_name());
96 (void) snprintf(locpath, sizeof (locpath), "%s/%s",
97 locname, category_name());
107 (void) printf("Writing category %s: ", category_name());
108 (void) fflush(stdout);
111 /* make the parent directory */
113 (void) mkdir(dirname(category_file()), 0755);
116 * note that we have to regenerate the file name, as dirname
119 file = fopen(category_file(), "w");
121 errf("%s", strerror(errno));
128 close_category(FILE *f)
130 if (fchmod(fileno(f), 0644) < 0) {
132 (void) unlink(category_file());
133 errf("%s", strerror(errno));
136 (void) unlink(category_file());
137 errf("%s", strerror(errno));
140 (void) fprintf(stdout, "done.\n");
141 (void) fflush(stdout);
146 * This function is used when copying the category from another
147 * locale. Note that the copy is actually performed using a hard
148 * link for efficiency.
151 copy_category(char *src)
153 char srcpath[PATH_MAX];
156 (void) snprintf(srcpath, sizeof (srcpath), "%s/%s",
157 src, category_name());
158 rv = access(srcpath, R_OK);
159 if ((rv != 0) && (strchr(srcpath, '/') == NULL)) {
160 /* Maybe we should try the system locale */
161 (void) snprintf(srcpath, sizeof (srcpath),
162 "/usr/lib/locale/%s/%s", src, category_name());
163 rv = access(srcpath, R_OK);
167 fprintf(stderr,"source locale data unavailable: %s", src);
172 (void) printf("Copying category %s from %s: ",
173 category_name(), src);
174 (void) fflush(stdout);
177 /* make the parent directory */
179 (void) mkdir(dirname(category_file()), 0755);
181 if (link(srcpath, category_file()) != 0) {
182 fprintf(stderr,"unable to copy locale data: %s",
187 (void) printf("done.\n");
192 putl_category(const char *s, FILE *f)
194 if (s && fputs(s, f) == EOF) {
196 (void) unlink(category_file());
197 errf("%s", strerror(errno));
200 if (fputc('\n', f) == EOF) {
202 (void) unlink(category_file());
203 errf("%s", strerror(errno));
210 wr_category(void *buf, size_t sz, FILE *f)
215 if (fwrite(buf, sz, 1, f) < 1) {
217 (void) unlink(category_file());
218 errf("%s", strerror(errno));
229 (void) fprintf(stderr, "Usage: localedef [options] localename\n");
230 (void) fprintf(stderr, "[options] are:\n");
231 (void) fprintf(stderr, " -D : BSD-style output\n");
232 (void) fprintf(stderr, " -c : ignore warnings\n");
233 (void) fprintf(stderr, " -v : verbose output\n");
234 (void) fprintf(stderr, " -U : ignore undefined symbols\n");
235 (void) fprintf(stderr, " -f charmap : use given charmap file\n");
236 (void) fprintf(stderr, " -u encoding : assume encoding\n");
237 (void) fprintf(stderr, " -w widths : use screen widths file\n");
238 (void) fprintf(stderr, " -i locsrc : source file for locale\n");
243 main(int argc, char **argv)
261 (void) setlocale(LC_ALL, "");
263 while ((c = getopt(argc, argv, "w:i:cf:u:vUD")) != -1) {
275 set_wide_encoding(optarg);
295 if ((argc - 1) != (optind)) {
298 locname = argv[argc - 1];
300 (void) printf("Processing locale %s.\n", locname);
305 (void) printf("Loading charmap %s.\n", cfname);
306 reset_scanner(cfname);
312 (void) printf("Loading widths %s.\n", wfname);
313 reset_scanner(wfname);
318 (void) printf("Loading POSIX portable characters.\n");
323 reset_scanner(lfname);
328 /* make the directory for the locale if not already present */
330 while ((dir = opendir(locname)) == NULL) {
331 if ((errno != ENOENT) ||
332 (mkdir(locname, 0755) < 0)) {
333 errf("%s", strerror(errno));
336 (void) closedir(dir);
337 (void) mkdir(dirname(category_file()), 0755);
342 (void) printf("All done.\n");
344 return (warnings ? 1 : 0);