1 /*
2  * Copyright (c) 2004
3  *	Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef HAVE_ISALPHA
30 #define isalnum _isalnum
31 #define iscntrl _iscntrl
32 #define islower _islower
33 #define isspace _isspace
34 #define isalpha _isalpha
35 #define isdigit _isdigit
36 #define isprint _isprint
37 #define isupper _isupper
38 #define isblank _isblank
39 #define isgraph _isgraph
40 #define ispunct _ispunct
41 #define isxdigit _isxdigit
42 #include <ctype.h>
43 #undef isalnum
44 #undef iscntrl
45 #undef islower
46 #undef isspace
47 #undef isalpha
48 #undef isdigit
49 #undef isprint
50 #undef isupper
51 #undef isblank
52 #undef isgraph
53 #undef ispunct
54 #undef isxdigit
55 #endif
56 
57 #include <signal.h>
58 #include <string.h>
59 
60 #include "error.h"
61 #include "output.h"
62 #include "system.h"
63 
64 #ifndef HAVE_MEMPCPY
mempcpy(void * dest,const void * src,size_t n)65 void *mempcpy(void *dest, const void *src, size_t n)
66 {
67 	return memcpy(dest, src, n) + n;
68 }
69 #endif
70 
71 #ifndef HAVE_STPCPY
stpcpy(char * dest,const char * src)72 char *stpcpy(char *dest, const char *src)
73 {
74 	size_t len = strlen(src);
75 	dest[len] = 0;
76 	return mempcpy(dest, src, len);
77 }
78 #endif
79 
80 #ifndef HAVE_STRCHRNUL
strchrnul(const char * s,int c)81 char *strchrnul(const char *s, int c)
82 {
83 	char *p = strchr(s, c);
84 	if (!p)
85 		p = (char *)s + strlen(s);
86 	return p;
87 }
88 #endif
89 
90 #ifndef HAVE_STRSIGNAL
strsignal(int sig)91 char *strsignal(int sig)
92 {
93 	static char buf[19];
94 
95 	if ((unsigned)sig < NSIG && sys_siglist[sig])
96 		return (char *)sys_siglist[sig];
97 	fmtstr(buf, sizeof(buf), "Signal %d", sig);
98 	return buf;
99 }
100 #endif
101 
102 #ifndef HAVE_BSEARCH
bsearch(const void * key,const void * base,size_t nmemb,size_t size,int (* cmp)(const void *,const void *))103 void *bsearch(const void *key, const void *base, size_t nmemb,
104 	      size_t size, int (*cmp)(const void *, const void *))
105 {
106 	while (nmemb) {
107 		size_t mididx = nmemb / 2;
108 		const void *midobj = base + mididx * size;
109 		int diff = cmp(key, midobj);
110 
111 		if (diff == 0)
112 			return (void *)midobj;
113 
114 		if (diff > 0) {
115 			base = midobj + size;
116 			nmemb -= mididx + 1;
117 		} else
118 			nmemb = mididx;
119 	}
120 
121 	return 0;
122 }
123 #endif
124 
125 #ifndef HAVE_SYSCONF
sysconf(int name)126 long sysconf(int name)
127 {
128 	sh_error("no sysconf for: %d", name);
129 }
130 #endif
131 
132 #ifndef HAVE_ISALPHA
isalnum(int c)133 int isalnum(int c) {
134 	return _isalnum(c);
135 }
136 
137 
iscntrl(int c)138 int iscntrl(int c) {
139 	return _iscntrl(c);
140 }
141 
142 
islower(int c)143 int islower(int c) {
144 	return _islower(c);
145 }
146 
147 
isspace(int c)148 int isspace(int c) {
149 	return _isspace(c);
150 }
151 
152 
isalpha(int c)153 int isalpha(int c) {
154 	return _isalpha(c);
155 }
156 
157 
isdigit(int c)158 int isdigit(int c) {
159 	return _isdigit(c);
160 }
161 
162 
isprint(int c)163 int isprint(int c) {
164 	return _isprint(c);
165 }
166 
167 
isupper(int c)168 int isupper(int c) {
169 	return _isupper(c);
170 }
171 
172 
173 #if HAVE_DECL_ISBLANK
isblank(int c)174 int isblank(int c) {
175 	return _isblank(c);
176 }
177 #endif
178 
179 
isgraph(int c)180 int isgraph(int c) {
181 	return _isgraph(c);
182 }
183 
184 
ispunct(int c)185 int ispunct(int c) {
186 	return _ispunct(c);
187 }
188 
189 
isxdigit(int c)190 int isxdigit(int c) {
191 	return _isxdigit(c);
192 }
193 #endif
194 
195 #if !HAVE_DECL_ISBLANK
isblank(int c)196 int isblank(int c) {
197 	return c == ' ' || c == '\t';
198 }
199 #endif
200