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>
47 #include "localedef.h"
51 #define TEXT_DOMAIN "SYS_TEST"
58 static char *locname
= NULL
;
59 static char locpath
[PATH_MAX
];
64 switch (get_category()) {
70 return ("LC_COLLATE");
74 return ("LC_MESSAGES");
76 return ("LC_MONETARY");
78 return ("LC_NUMERIC");
91 (void) snprintf(locpath
, sizeof (locpath
), "%s.%s",
92 locname
, category_name());
94 (void) snprintf(locpath
, sizeof (locpath
), "%s/%s",
95 locname
, category_name());
105 (void) printf("Writing category %s: ", category_name());
106 (void) fflush(stdout
);
109 /* make the parent directory */
111 (void) mkdir(dirname(category_file()), 0755);
114 * note that we have to regenerate the file name, as dirname
117 file
= fopen(category_file(), "w");
119 errf(strerror(errno
));
126 close_category(FILE *f
)
128 if (fchmod(fileno(f
), 0644) < 0) {
130 (void) unlink(category_file());
131 errf(strerror(errno
));
134 (void) unlink(category_file());
135 errf(strerror(errno
));
138 (void) fprintf(stdout
, "done.\n");
139 (void) fflush(stdout
);
144 * This function is used when copying the category from another
145 * locale. Note that the copy is actually performed using a hard
146 * link for efficiency.
149 copy_category(char *src
)
151 char srcpath
[PATH_MAX
];
154 (void) snprintf(srcpath
, sizeof (srcpath
), "%s/%s",
155 src
, category_name());
156 rv
= access(srcpath
, R_OK
);
157 if ((rv
!= 0) && (strchr(srcpath
, '/') == NULL
)) {
158 /* Maybe we should try the system locale */
159 (void) snprintf(srcpath
, sizeof (srcpath
),
160 "/usr/lib/locale/%s/%s", src
, category_name());
161 rv
= access(srcpath
, R_OK
);
165 fprintf(stderr
,"source locale data unavailable: %s", src
);
170 (void) printf("Copying category %s from %s: ",
171 category_name(), src
);
172 (void) fflush(stdout
);
175 /* make the parent directory */
177 (void) mkdir(dirname(category_file()), 0755);
179 if (link(srcpath
, category_file()) != 0) {
180 fprintf(stderr
,"unable to copy locale data: %s",
185 (void) printf("done.\n");
190 putl_category(const char *s
, FILE *f
)
192 if (s
&& fputs(s
, f
) == EOF
) {
194 (void) unlink(category_file());
195 errf(strerror(errno
));
198 if (fputc('\n', f
) == EOF
) {
200 (void) unlink(category_file());
201 errf(strerror(errno
));
208 wr_category(void *buf
, size_t sz
, FILE *f
)
213 if (fwrite(buf
, sz
, 1, f
) < 1) {
215 (void) unlink(category_file());
216 errf(strerror(errno
));
227 (void) fprintf(stderr
, "Usage: localedef [options] localename\n");
228 (void) fprintf(stderr
, "[options] are:\n");
229 (void) fprintf(stderr
, " -D : BSD-style output\n");
230 (void) fprintf(stderr
, " -c : ignore warnings\n");
231 (void) fprintf(stderr
, " -v : verbose output\n");
232 (void) fprintf(stderr
, " -U : ignore undefined symbols\n");
233 (void) fprintf(stderr
, " -f charmap : use given charmap file\n");
234 (void) fprintf(stderr
, " -u encoding : assume encoding\n");
235 (void) fprintf(stderr
, " -w widths : use screen widths file\n");
236 (void) fprintf(stderr
, " -i locsrc : source file for locale\n");
241 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(strerror(errno
));
336 (void) closedir(dir
);
337 (void) mkdir(dirname(category_file()), 0755);
342 (void) printf("All done.\n");
344 return (warnings
? 1 : 0);