1194638Sdelphij/*-
2194638Sdelphij * Copyright (c) 2005 Ryuichiro Imura
3194638Sdelphij * All rights reserved.
4194638Sdelphij *
5194638Sdelphij * Redistribution and use in source and binary forms, with or without
6194638Sdelphij * modification, are permitted provided that the following conditions
7194638Sdelphij * are met:
8194638Sdelphij * 1. Redistributions of source code must retain the above copyright
9194638Sdelphij *    notice, this list of conditions and the following disclaimer.
10194638Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11194638Sdelphij *    notice, this list of conditions and the following disclaimer in the
12194638Sdelphij *    documentation and/or other materials provided with the distribution.
13194638Sdelphij *
14194638Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15194638Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16194638Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17194638Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18194638Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19194638Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20194638Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21194638Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22194638Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23194638Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24194638Sdelphij * SUCH DAMAGE.
25194638Sdelphij *
26194638Sdelphij * $FreeBSD$
27194638Sdelphij */
28194638Sdelphij
29194638Sdelphij#include <sys/types.h>
30194638Sdelphij#include <sys/iconv.h>
31194638Sdelphij#include <sys/sysctl.h>
32194638Sdelphij
33194638Sdelphij#include <ctype.h>
34194638Sdelphij#include <errno.h>
35194638Sdelphij#include <stdlib.h>
36194638Sdelphij#include <string.h>
37194638Sdelphij
38194638Sdelphijint
39194638Sdelphijkiconv_lookupconv(const char *drvname)
40194638Sdelphij{
41194638Sdelphij	size_t size;
42194638Sdelphij
43194638Sdelphij	if (sysctlbyname("kern.iconv.drvlist", NULL, &size, NULL, 0) == -1)
44194638Sdelphij		return (errno);
45194638Sdelphij	if (size > 0) {
46194638Sdelphij		char *drivers, *drvp;
47194638Sdelphij
48194638Sdelphij		drivers = malloc(size);
49194638Sdelphij		if (drivers == NULL)
50194638Sdelphij			return (ENOMEM);
51194638Sdelphij		if (sysctlbyname("kern.iconv.drvlist", drivers, &size, NULL, 0) == -1) {
52194638Sdelphij			free(drivers);
53194638Sdelphij			return (errno);
54194638Sdelphij		}
55194638Sdelphij		for (drvp = drivers; *drvp != '\0'; drvp += strlen(drvp) + 1)
56194638Sdelphij			if (strcmp(drvp, drvname) == 0) {
57194638Sdelphij				free(drivers);
58194638Sdelphij				return (0);
59194638Sdelphij			}
60194638Sdelphij	}
61194638Sdelphij	return (ENOENT);
62194638Sdelphij}
63194638Sdelphij
64194638Sdelphijint
65194638Sdelphijkiconv_lookupcs(const char *tocode, const char *fromcode)
66194638Sdelphij{
67194638Sdelphij	size_t i, size;
68194638Sdelphij	struct iconv_cspair_info *csi, *csip;
69194638Sdelphij
70194638Sdelphij	if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1)
71194638Sdelphij		return (errno);
72194638Sdelphij	if (size > 0) {
73194638Sdelphij		csi = malloc(size);
74194638Sdelphij		if (csi == NULL)
75194638Sdelphij			return (ENOMEM);
76194638Sdelphij		if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) {
77194638Sdelphij			free(csi);
78194663Sdelphij			return (errno);
79194638Sdelphij		}
80194638Sdelphij		for (i = 0, csip = csi; i < (size/sizeof(*csi)); i++, csip++){
81194638Sdelphij			if (strcmp(csip->cs_to, tocode) == 0 &&
82194638Sdelphij			    strcmp(csip->cs_from, fromcode) == 0) {
83194638Sdelphij				free(csi);
84194638Sdelphij				return (0);
85194638Sdelphij			}
86194638Sdelphij		}
87194638Sdelphij	}
88194638Sdelphij	return (ENOENT);
89194638Sdelphij}
90