Add a manual page explaining the format of /etc/manpath.config.
[dragonfly/vkernel-mp.git] / lib / libc / locale / aliasname.c
blob9cb97ee64d9c0468bbf81fe64018d9cb1a583904
1 /* $NetBSD: src/lib/libc/locale/aliasname.c,v 1.1 2002/02/13 07:45:52 yamt Exp $ */
2 /* $DragonFly: src/lib/libc/locale/aliasname.c,v 1.1 2005/03/16 07:54:41 joerg Exp $ */
4 /*-
5 * Copyright (c)2002 YAMAMOTO Takashi,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <assert.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include "aliasname_local.h"
36 static __inline int __is_ws(char);
38 static __inline int
39 __is_ws(char ch)
42 return(ch == ' ' || ch == '\t');
45 const char *
46 __unaliasname(const char *dbname, const char *alias, void *buf, size_t bufsize)
48 FILE *fp = NULL;
49 const char *result = alias;
50 size_t resultlen;
51 size_t aliaslen;
52 const char *p;
53 size_t len;
55 _DIAGASSERT(dbname != NULL);
56 _DIAGASSERT(alias != NULL);
57 _DIAGASSERT(buf != NULL);
59 fp = fopen(dbname, "r");
60 if (fp == NULL)
61 goto quit;
63 aliaslen = strlen(alias);
65 for (;;) {
66 p = fgetln(fp, &len);
67 if (p == NULL)
68 goto quit; /* eof or error */
70 _DIAGASSERT(len != 0);
72 /* ignore terminating NL */
73 if (p[len - 1] == '\n')
74 len--;
76 /* ignore null line and comment */
77 if (len == 0 || p[0] == '#')
78 continue;
80 if (aliaslen > len)
81 continue;
83 if (memcmp(alias, p, aliaslen))
84 continue;
86 p += aliaslen;
87 len -= aliaslen;
89 if (len == 0 || !__is_ws(*p))
90 continue;
92 /* entry was found here */
93 break;
95 /* NOTREACHED */
98 /* skip white spaces */
99 do {
100 p++;
101 len--;
102 } while (len != 0 && __is_ws(*p));
104 if (len == 0)
105 goto quit;
107 /* count length of result */
108 resultlen = 0;
109 while (resultlen < len && !__is_ws(*p))
110 resultlen++;
112 /* check if space is enough */
113 if (bufsize < resultlen + 1)
114 goto quit;
116 memcpy(buf, p, resultlen);
117 ((char *)buf)[resultlen] = 0;
118 result = buf;
120 quit:
121 if (fp)
122 fclose(fp);
124 return(result);