14297a3bGarrett D'Amore/*
22d08521Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e586Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3bGarrett D'Amore * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
54297a3bGarrett D'Amore * All rights reserved.
64297a3bGarrett D'Amore *
74297a3bGarrett D'Amore * Redistribution and use in source and binary forms, with or without
84297a3bGarrett D'Amore * modification, are permitted provided that the following conditions
94297a3bGarrett D'Amore * are met:
104297a3bGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
114297a3bGarrett D'Amore *    notice, this list of conditions and the following disclaimer.
124297a3bGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
134297a3bGarrett D'Amore *    notice, this list of conditions and the following disclaimer in the
144297a3bGarrett D'Amore *    documentation and/or other materials provided with the distribution.
154297a3bGarrett D'Amore *
164297a3bGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174297a3bGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184297a3bGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194297a3bGarrett D'Amore * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204297a3bGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214297a3bGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224297a3bGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234297a3bGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244297a3bGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254297a3bGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264297a3bGarrett D'Amore * SUCH DAMAGE.
274297a3bGarrett D'Amore */
284297a3bGarrett D'Amore
294297a3bGarrett D'Amore#include "lint.h"
30eda71b4Garrett D'Amore#include "file64.h"
314297a3bGarrett D'Amore#include <sys/types.h>
324297a3bGarrett D'Amore#include <sys/stat.h>
334297a3bGarrett D'Amore#include <errno.h>
344297a3bGarrett D'Amore#include <fcntl.h>
354297a3bGarrett D'Amore#include <limits.h>
364297a3bGarrett D'Amore#include <stdlib.h>
374297a3bGarrett D'Amore#include <string.h>
384297a3bGarrett D'Amore#include <unistd.h>
396da5aa9Garrett D'Amore#include <stdio.h>
404297a3bGarrett D'Amore
412d08521Garrett D'Amore#include "libc.h"
424297a3bGarrett D'Amore#include "ldpart.h"
434297a3bGarrett D'Amore#include "setlocale.h"
444297a3bGarrett D'Amore
454297a3bGarrett D'Amorestatic int split_lines(char *, const char *);
464297a3bGarrett D'Amore
474297a3bGarrett D'Amoreint
482d08521Garrett D'Amore__part_load_locale(const char *name,
494297a3bGarrett D'Amore    char **locale_buf, const char *category_filename,
504297a3bGarrett D'Amore    int locale_buf_size_max, int locale_buf_size_min,
514297a3bGarrett D'Amore    const char **dst_localebuf)
524297a3bGarrett D'Amore{
534297a3bGarrett D'Amore	int		saverr, fd, i, num_lines;
544297a3bGarrett D'Amore	char		*lbuf, *p;
554297a3bGarrett D'Amore	const char	*plim;
564297a3bGarrett D'Amore	char		filename[PATH_MAX];
574297a3bGarrett D'Amore	struct stat	st;
584297a3bGarrett D'Amore	size_t		namesize, bufsize;
594297a3bGarrett D'Amore
604297a3bGarrett D'Amore	/*
614297a3bGarrett D'Amore	 * Slurp the locale file into the cache.
624297a3bGarrett D'Amore	 */
634297a3bGarrett D'Amore	namesize = strlen(name) + 1;
644297a3bGarrett D'Amore
654297a3bGarrett D'Amore	/* 'PathLocale' must be already set & checked. */
664297a3bGarrett D'Amore
676da5aa9Garrett D'Amore	(void) snprintf(filename, sizeof (filename), "%s/%s/%s/LCL_DATA",
686da5aa9Garrett D'Amore	    _PathLocale, name, category_filename);
696da5aa9Garrett D'Amore
704297a3bGarrett D'Amore	if ((fd = open(filename, O_RDONLY)) < 0)
714297a3bGarrett D'Amore		return (_LDP_ERROR);
724297a3bGarrett D'Amore	if (fstat(fd, &st) != 0)
734297a3bGarrett D'Amore		goto bad_locale;
744297a3bGarrett D'Amore	if (st.st_size <= 0) {
754297a3bGarrett D'Amore		errno = EINVAL;
764297a3bGarrett D'Amore		goto bad_locale;
774297a3bGarrett D'Amore	}
784297a3bGarrett D'Amore	bufsize = namesize + st.st_size;
792d08521Garrett D'Amore	if ((lbuf = libc_malloc(bufsize)) == NULL) {
804297a3bGarrett D'Amore		errno = ENOMEM;
814297a3bGarrett D'Amore		goto bad_locale;
824297a3bGarrett D'Amore	}
834297a3bGarrett D'Amore	(void) strcpy(lbuf, name);
844297a3bGarrett D'Amore	p = lbuf + namesize;
854297a3bGarrett D'Amore	plim = p + st.st_size;
864297a3bGarrett D'Amore	if (read(fd, p, (size_t)st.st_size) != st.st_size)
874297a3bGarrett D'Amore		goto bad_lbuf;
884297a3bGarrett D'Amore	/*
894297a3bGarrett D'Amore	 * Parse the locale file into localebuf.
904297a3bGarrett D'Amore	 */
914297a3bGarrett D'Amore	if (plim[-1] != '\n') {
924297a3bGarrett D'Amore		errno = EINVAL;
934297a3bGarrett D'Amore		goto bad_lbuf;
944297a3bGarrett D'Amore	}
954297a3bGarrett D'Amore	num_lines = split_lines(p, plim);
964297a3bGarrett D'Amore	if (num_lines >= locale_buf_size_max)
974297a3bGarrett D'Amore		num_lines = locale_buf_size_max;
984297a3bGarrett D'Amore	else if (num_lines >= locale_buf_size_min)
994297a3bGarrett D'Amore		num_lines = locale_buf_size_min;
1004297a3bGarrett D'Amore	else {
1014297a3bGarrett D'Amore		errno = EINVAL;
1024297a3bGarrett D'Amore		goto bad_lbuf;
1034297a3bGarrett D'Amore	}
1044297a3bGarrett D'Amore	(void) close(fd);
1054297a3bGarrett D'Amore	/*
1064297a3bGarrett D'Amore	 * Record the successful parse in the cache.
1074297a3bGarrett D'Amore	 */
1084297a3bGarrett D'Amore	*locale_buf = lbuf;
1094297a3bGarrett D'Amore	for (p = *locale_buf, i = 0; i < num_lines; i++)
1104297a3bGarrett D'Amore		dst_localebuf[i] = (p += strlen(p) + 1);
1114297a3bGarrett D'Amore	for (i = num_lines; i < locale_buf_size_max; i++)
1124297a3bGarrett D'Amore		dst_localebuf[i] = NULL;
1134297a3bGarrett D'Amore
1144297a3bGarrett D'Amore	return (_LDP_LOADED);
1154297a3bGarrett D'Amore
1164297a3bGarrett D'Amorebad_lbuf:
1174297a3bGarrett D'Amore	saverr = errno;
1184297a3bGarrett D'Amore	free(lbuf);
1194297a3bGarrett D'Amore	errno = saverr;
1204297a3bGarrett D'Amorebad_locale:
1214297a3bGarrett D'Amore	saverr = errno;
1224297a3bGarrett D'Amore	(void) close(fd);
1234297a3bGarrett D'Amore	errno = saverr;
1244297a3bGarrett D'Amore
1254297a3bGarrett D'Amore	return (_LDP_ERROR);
1264297a3bGarrett D'Amore}
1274297a3bGarrett D'Amore
1284297a3bGarrett D'Amorestatic int
1294297a3bGarrett D'Amoresplit_lines(char *p, const char *plim)
1304297a3bGarrett D'Amore{
1314297a3bGarrett D'Amore	int i;
1324297a3bGarrett D'Amore
1334297a3bGarrett D'Amore	i = 0;
1344297a3bGarrett D'Amore	while (p < plim) {
1354297a3bGarrett D'Amore		if (*p == '\n') {
1364297a3bGarrett D'Amore			*p = '\0';
1374297a3bGarrett D'Amore			i++;
1384297a3bGarrett D'Amore		}
1394297a3bGarrett D'Amore		p++;
1404297a3bGarrett D'Amore	}
1414297a3bGarrett D'Amore	return (i);
1424297a3bGarrett D'Amore}
143