Annotation of /trunk/mkinitrd-magellan/klibc/usr/dash/mystring.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 4622 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 4622 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /*- |
2 | * Copyright (c) 1991, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * Copyright (c) 1997-2005 | ||
5 | * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. | ||
6 | * | ||
7 | * This code is derived from software contributed to Berkeley by | ||
8 | * Kenneth Almquist. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * 3. Neither the name of the University nor the names of its contributors | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | /* | ||
36 | * String functions. | ||
37 | * | ||
38 | * equal(s1, s2) Return true if strings are equal. | ||
39 | * scopy(from, to) Copy a string. | ||
40 | * scopyn(from, to, n) Like scopy, but checks for overflow. | ||
41 | * number(s) Convert a string of digits to an integer. | ||
42 | * is_number(s) Return true if s is a string of digits. | ||
43 | */ | ||
44 | |||
45 | #include <stdlib.h> | ||
46 | #include "shell.h" | ||
47 | #include "syntax.h" | ||
48 | #include "error.h" | ||
49 | #include "mystring.h" | ||
50 | #include "memalloc.h" | ||
51 | #include "parser.h" | ||
52 | #include "system.h" | ||
53 | |||
54 | |||
55 | char nullstr[1]; /* zero length string */ | ||
56 | const char spcstr[] = " "; | ||
57 | const char snlfmt[] = "%s\n"; | ||
58 | const char dolatstr[] = { CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' }; | ||
59 | const char illnum[] = "Illegal number: %s"; | ||
60 | const char homestr[] = "HOME"; | ||
61 | |||
62 | /* | ||
63 | * equal - #defined in mystring.h | ||
64 | */ | ||
65 | |||
66 | /* | ||
67 | * scopy - #defined in mystring.h | ||
68 | */ | ||
69 | |||
70 | |||
71 | #if 0 | ||
72 | /* | ||
73 | * scopyn - copy a string from "from" to "to", truncating the string | ||
74 | * if necessary. "To" is always nul terminated, even if | ||
75 | * truncation is performed. "Size" is the size of "to". | ||
76 | */ | ||
77 | |||
78 | void | ||
79 | scopyn(const char *from, char *to, int size) | ||
80 | { | ||
81 | |||
82 | while (--size > 0) { | ||
83 | if ((*to++ = *from++) == '\0') | ||
84 | return; | ||
85 | } | ||
86 | *to = '\0'; | ||
87 | } | ||
88 | #endif | ||
89 | |||
90 | |||
91 | /* | ||
92 | * prefix -- see if pfx is a prefix of string. | ||
93 | */ | ||
94 | |||
95 | char * | ||
96 | prefix(const char *string, const char *pfx) | ||
97 | { | ||
98 | while (*pfx) { | ||
99 | if (*pfx++ != *string++) | ||
100 | return 0; | ||
101 | } | ||
102 | return (char *) string; | ||
103 | } | ||
104 | |||
105 | |||
106 | /* | ||
107 | * Convert a string of digits to an integer, printing an error message on | ||
108 | * failure. | ||
109 | */ | ||
110 | |||
111 | int | ||
112 | number(const char *s) | ||
113 | { | ||
114 | |||
115 | if (! is_number(s)) | ||
116 | sh_error(illnum, s); | ||
117 | return atoi(s); | ||
118 | } | ||
119 | |||
120 | |||
121 | |||
122 | /* | ||
123 | * Check for a valid number. This should be elsewhere. | ||
124 | */ | ||
125 | |||
126 | int | ||
127 | is_number(const char *p) | ||
128 | { | ||
129 | do { | ||
130 | if (! is_digit(*p)) | ||
131 | return 0; | ||
132 | } while (*++p != '\0'); | ||
133 | return 1; | ||
134 | } | ||
135 | |||
136 | |||
137 | /* | ||
138 | * Produce a possibly single quoted string suitable as input to the shell. | ||
139 | * The return string is allocated on the stack. | ||
140 | */ | ||
141 | |||
142 | char * | ||
143 | single_quote(const char *s) { | ||
144 | char *p; | ||
145 | |||
146 | STARTSTACKSTR(p); | ||
147 | |||
148 | do { | ||
149 | char *q; | ||
150 | size_t len; | ||
151 | |||
152 | len = strchrnul(s, '\'') - s; | ||
153 | |||
154 | q = p = makestrspace(len + 3, p); | ||
155 | |||
156 | *q++ = '\''; | ||
157 | q = mempcpy(q, s, len); | ||
158 | *q++ = '\''; | ||
159 | s += len; | ||
160 | |||
161 | STADJUST(q - p, p); | ||
162 | |||
163 | len = strspn(s, "'"); | ||
164 | if (!len) | ||
165 | break; | ||
166 | |||
167 | q = p = makestrspace(len + 3, p); | ||
168 | |||
169 | *q++ = '"'; | ||
170 | q = mempcpy(q, s, len); | ||
171 | *q++ = '"'; | ||
172 | s += len; | ||
173 | |||
174 | STADJUST(q - p, p); | ||
175 | } while (*s); | ||
176 | |||
177 | USTPUTC(0, p); | ||
178 | |||
179 | return stackblock(); | ||
180 | } | ||
181 | |||
182 | /* | ||
183 | * Like strdup but works with the ash stack. | ||
184 | */ | ||
185 | |||
186 | char * | ||
187 | sstrdup(const char *p) | ||
188 | { | ||
189 | size_t len = strlen(p) + 1; | ||
190 | return memcpy(stalloc(len), p, len); | ||
191 | } | ||
192 | |||
193 | /* | ||
194 | * Wrapper around strcmp for qsort/bsearch/... | ||
195 | */ | ||
196 | int | ||
197 | pstrcmp(const void *a, const void *b) | ||
198 | { | ||
199 | return strcmp(*(const char *const *) a, *(const char *const *) b); | ||
200 | } | ||
201 | |||
202 | /* | ||
203 | * Find a string is in a sorted array. | ||
204 | */ | ||
205 | const char *const * | ||
206 | findstring(const char *s, const char *const *array, size_t nmemb) | ||
207 | { | ||
208 | return bsearch(&s, array, nmemb, sizeof(const char *), pstrcmp); | ||
209 | } |