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.
38 #include <sys/types.h>
48 #include "localedef.h"
52 #define TEXT_DOMAIN "SYS_TEST"
59 static char *locname
= NULL
;
60 static char locpath
[PATH_MAX
];
65 switch (get_category()) {
71 return ("LC_COLLATE");
75 return ("LC_MESSAGES");
77 return ("LC_MONETARY");
79 return ("LC_NUMERIC");
92 (void) snprintf(locpath
, sizeof (locpath
), "%s.%s",
93 locname
, category_name());
95 (void) snprintf(locpath
, sizeof (locpath
), "%s/%s",
96 locname
, category_name());
106 (void) printf("Writing category %s: ", category_name());
107 (void) fflush(stdout
);
110 /* make the parent directory */
112 (void) mkdir(dirname(category_file()), 0755);
115 * note that we have to regenerate the file name, as dirname
118 file
= fopen(category_file(), "w");
120 errf(strerror(errno
));
127 close_category(FILE *f
)
129 if (fchmod(fileno(f
), 0644) < 0) {
131 (void) unlink(category_file());
132 errf(strerror(errno
));
135 (void) unlink(category_file());
136 errf(strerror(errno
));
139 (void) fprintf(stdout
, "done.\n");
140 (void) fflush(stdout
);
145 * This function is used when copying the category from another
146 * locale. Note that the copy is actually performed using a hard
147 * link for efficiency.
150 copy_category(char *src
)
152 char srcpath
[PATH_MAX
];
155 (void) snprintf(srcpath
, sizeof (srcpath
), "%s/%s",
156 src
, category_name());
157 rv
= access(srcpath
, R_OK
);
158 if ((rv
!= 0) && (strchr(srcpath
, '/') == NULL
)) {
159 /* Maybe we should try the system locale */
160 (void) snprintf(srcpath
, sizeof (srcpath
),
161 "/usr/lib/locale/%s/%s", src
, category_name());
162 rv
= access(srcpath
, R_OK
);
166 fprintf(stderr
,"source locale data unavailable: %s", src
);
171 (void) printf("Copying category %s from %s: ",
172 category_name(), src
);
173 (void) fflush(stdout
);
176 /* make the parent directory */
178 (void) mkdir(dirname(category_file()), 0755);
180 if (link(srcpath
, category_file()) != 0) {
181 fprintf(stderr
,"unable to copy locale data: %s",
186 (void) printf("done.\n");
191 putl_category(const char *s
, FILE *f
)
193 if (s
&& fputs(s
, f
) == EOF
) {
195 (void) unlink(category_file());
196 errf(strerror(errno
));
199 if (fputc('\n', f
) == EOF
) {
201 (void) unlink(category_file());
202 errf(strerror(errno
));
209 wr_category(void *buf
, size_t sz
, FILE *f
)
214 if (fwrite(buf
, sz
, 1, f
) < 1) {
216 (void) unlink(category_file());
217 errf(strerror(errno
));
228 (void) fprintf(stderr
, "Usage: localedef [options] localename\n");
229 (void) fprintf(stderr
, "[options] are:\n");
230 (void) fprintf(stderr
, " -D : BSD-style output\n");
231 (void) fprintf(stderr
, " -c : ignore warnings\n");
232 (void) fprintf(stderr
, " -v : verbose output\n");
233 (void) fprintf(stderr
, " -U : ignore undefined symbols\n");
234 (void) fprintf(stderr
, " -f charmap : use given charmap file\n");
235 (void) fprintf(stderr
, " -u encoding : assume encoding\n");
236 (void) fprintf(stderr
, " -w widths : use screen widths file\n");
237 (void) fprintf(stderr
, " -i locsrc : source file for locale\n");
242 main(int argc
, char **argv
)
260 (void) setlocale(LC_ALL
, "");
262 while ((c
= getopt(argc
, argv
, "w:i:cf:u:vUD")) != -1) {
274 set_wide_encoding(optarg
);
294 if ((argc
- 1) != (optind
)) {
297 locname
= argv
[argc
- 1];
299 (void) printf("Processing locale %s.\n", locname
);
304 (void) printf("Loading charmap %s.\n", cfname
);
305 reset_scanner(cfname
);
311 (void) printf("Loading widths %s.\n", wfname
);
312 reset_scanner(wfname
);
317 (void) printf("Loading POSIX portable characters.\n");
322 reset_scanner(lfname
);
327 /* make the directory for the locale if not already present */
329 while ((dir
= opendir(locname
)) == NULL
) {
330 if ((errno
!= ENOENT
) ||
331 (mkdir(locname
, 0755) < 0)) {
332 errf(strerror(errno
));
335 (void) closedir(dir
);
336 (void) mkdir(dirname(category_file()), 0755);
341 (void) printf("All done.\n");
343 return (warnings
? 1 : 0);