keylogin(1): Fix a warning and raise WARNS to 6.
[dragonfly.git] / lib / libc / locale / aliasname.c
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 06:54:41 joerg Exp $ */
3
4 /*-
5  * Copyright (c)2002 YAMAMOTO Takashi,
6  * All rights reserved.
7  *
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.
16  *
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.
28  */
29
30 #include <assert.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "aliasname_local.h"
35
36 static __inline int     __is_ws(char);
37
38 static __inline int
39 __is_ws(char ch)
40 {
41
42         return(ch == ' ' || ch == '\t');
43 }
44
45 const char *
46 __unaliasname(const char *dbname, const char *alias, void *buf, size_t bufsize)
47 {
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;
54
55         _DIAGASSERT(dbname != NULL);
56         _DIAGASSERT(alias != NULL);
57         _DIAGASSERT(buf != NULL);
58
59         fp = fopen(dbname, "r");
60         if (fp == NULL)
61                 goto quit;
62
63         aliaslen = strlen(alias);
64
65         for (;;) {
66                 p = fgetln(fp, &len);
67                 if (p == NULL)
68                         goto quit; /* eof or error */
69
70                 _DIAGASSERT(len != 0);
71
72                 /* ignore terminating NL */
73                 if (p[len - 1] == '\n')
74                         len--;
75
76                 /* ignore null line and comment */
77                 if (len == 0 || p[0] == '#')
78                         continue;
79
80                 if (aliaslen > len)
81                         continue;
82
83                 if (memcmp(alias, p, aliaslen))
84                         continue;
85
86                 p += aliaslen;
87                 len -= aliaslen;
88
89                 if (len == 0 || !__is_ws(*p))
90                         continue;
91
92                 /* entry was found here */
93                 break;
94
95                 /* NOTREACHED */
96         }
97
98         /* skip white spaces */
99         do {
100                 p++;
101                 len--;
102         } while (len != 0 && __is_ws(*p));
103
104         if (len == 0)
105                 goto quit;
106
107         /* count length of result */
108         resultlen = 0;
109         while (resultlen < len && !__is_ws(*p))
110                 resultlen++;
111
112         /* check if space is enough */
113         if (bufsize < resultlen + 1)
114                 goto quit;
115
116         memcpy(buf, p, resultlen);
117         ((char *)buf)[resultlen] = 0;
118         result = buf;
119
120 quit:
121         if (fp)
122                 fclose(fp);
123
124         return(result);
125 }