]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /* $NetBSD: aliasname.c,v 1.2 2005/02/09 21:35:46 kleink Exp $ */\r |
2 | \r | |
3 | /*-\r | |
4 | * Copyright (c)2002 YAMAMOTO Takashi,\r | |
5 | * All rights reserved.\r | |
6 | *\r | |
7 | * Redistribution and use in source and binary forms, with or without\r | |
8 | * modification, are permitted provided that the following conditions\r | |
9 | * are met:\r | |
10 | * 1. Redistributions of source code must retain the above copyright\r | |
11 | * notice, this list of conditions and the following disclaimer.\r | |
12 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
13 | * notice, this list of conditions and the following disclaimer in the\r | |
14 | * documentation and/or other materials provided with the distribution.\r | |
15 | *\r | |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\r | |
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r | |
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
26 | * SUCH DAMAGE.\r | |
27 | */\r | |
28 | #include <LibConfig.h>\r | |
29 | #include <sys/EfiCdefs.h>\r | |
30 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
31 | __RCSID("$NetBSD: aliasname.c,v 1.2 2005/02/09 21:35:46 kleink Exp $");\r | |
32 | #endif /* LIBC_SCCS and not lint */\r | |
33 | \r | |
34 | #include "namespace.h"\r | |
35 | #include <assert.h>\r | |
36 | #include <stdio.h>\r | |
37 | #include <string.h>\r | |
38 | \r | |
39 | #include "aliasname_local.h"\r | |
40 | \r | |
41 | __inline int __is_ws(char);\r | |
42 | \r | |
43 | __inline int __is_ws(char ch)\r | |
44 | {\r | |
45 | \r | |
46 | return (ch == ' ' || ch == '\t');\r | |
47 | }\r | |
48 | \r | |
49 | const char *\r | |
50 | __unaliasname(const char *dbname, const char *alias, void *buf, size_t bufsize)\r | |
51 | {\r | |
52 | FILE *fp = NULL;\r | |
53 | const char *result = alias;\r | |
54 | size_t resultlen;\r | |
55 | size_t aliaslen;\r | |
56 | const char *p;\r | |
57 | size_t len;\r | |
58 | \r | |
59 | _DIAGASSERT(dbname != NULL);\r | |
60 | _DIAGASSERT(alias != NULL);\r | |
61 | _DIAGASSERT(buf != NULL);\r | |
62 | \r | |
63 | fp = fopen(dbname, "r");\r | |
64 | if (fp == NULL)\r | |
65 | goto quit;\r | |
66 | \r | |
67 | aliaslen = strlen(alias);\r | |
68 | \r | |
69 | while (/*CONSTCOND*/ 1) {\r | |
70 | p = fgetln(fp, &len);\r | |
71 | if (p == NULL)\r | |
72 | goto quit; /* eof or error */\r | |
73 | \r | |
74 | _DIAGASSERT(len != 0);\r | |
75 | \r | |
76 | /* ignore terminating NL */\r | |
77 | if (p[len - 1] == '\n')\r | |
78 | len--;\r | |
79 | \r | |
80 | /* ignore null line and comment */\r | |
81 | if (len == 0 || p[0] == '#')\r | |
82 | continue;\r | |
83 | \r | |
84 | if (aliaslen > len)\r | |
85 | continue;\r | |
86 | \r | |
87 | if (memcmp(alias, p, aliaslen))\r | |
88 | continue;\r | |
89 | \r | |
90 | p += aliaslen;\r | |
91 | len -= aliaslen;\r | |
92 | \r | |
93 | if (len == 0 || !__is_ws(*p))\r | |
94 | continue;\r | |
95 | \r | |
96 | /* entry was found here */\r | |
97 | break;\r | |
98 | \r | |
99 | /* NOTREACHED */\r | |
100 | }\r | |
101 | \r | |
102 | /* skip white spaces */\r | |
103 | do {\r | |
104 | p++;\r | |
105 | len--;\r | |
106 | } while (len != 0 && __is_ws(*p));\r | |
107 | \r | |
108 | if (len == 0)\r | |
109 | goto quit;\r | |
110 | \r | |
111 | /* count length of result */\r | |
112 | resultlen = 0;\r | |
113 | while (resultlen < len && !__is_ws(*p))\r | |
114 | resultlen++;\r | |
115 | \r | |
116 | /* check if space is enough */\r | |
117 | if (bufsize < resultlen + 1)\r | |
118 | goto quit;\r | |
119 | \r | |
120 | memcpy(buf, p, resultlen);\r | |
121 | ((char *)buf)[resultlen] = 0;\r | |
122 | result = buf;\r | |
123 | \r | |
124 | quit:\r | |
125 | if (fp)\r | |
126 | fclose(fp);\r | |
127 | \r | |
128 | return result;\r | |
129 | }\r |