source: EcnlProtoTool/trunk/ntshell/ntshell/core/ntlibc.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 5.6 KB
Line 
1/**
2 * @file ntlibc.c
3 * @author CuBeatSystems
4 * @author Shinichiro Nakamura
5 * @copyright
6 * ===============================================================
7 * Natural Tiny Shell (NT-Shell) Version 0.3.1
8 * ===============================================================
9 * Copyright (c) 2010-2016 Shinichiro Nakamura
10 *
11 * Permission is hereby granted, free of charge, to any person
12 * obtaining a copy of this software and associated documentation
13 * files (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use,
15 * copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following
18 * conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33#include "ntlibc.h"
34
35int ntlibc_strlen(const char *s)
36{
37 const char *p = s;
38 int cnt = 0;
39 while (*p) {
40 cnt++;
41 p++;
42 }
43 return cnt;
44}
45
46char *ntlibc_strcpy(char *des, const char *src)
47{
48 char *d = des;
49 const char *s = src;
50 while (*s) {
51 *d = *s;
52 d++;
53 s++;
54 }
55 *d = '\0';
56 return des;
57}
58
59int ntlibc_strlcpy(char *des, const char *src, int n)
60{
61 char *d = des, *e = &des[n];
62 const char *s = src;
63 while (*s && d < e) {
64 *d = *s;
65 d++;
66 s++;
67 }
68 *d++ = '\0';
69 return (int)d - (int)des;
70}
71
72char *ntlibc_strcat(char *des, const char *src)
73{
74 char *d = des;
75 const char *s = src;
76 while (*d) {
77 d++;
78 }
79 while (*s) {
80 *d = *s;
81 d++;
82 s++;
83 }
84 *d = '\0';
85 return des;
86}
87
88int ntlibc_strlcat(char *des, const char *src, int n)
89{
90 char *d = des, *e = &des[n];
91 const char *s = src;
92 while (*d && d < e) {
93 d++;
94 }
95 while (*s && d < e) {
96 *d = *s;
97 d++;
98 s++;
99 }
100 *d++ = '\0';
101 return (int)d - (int)des;
102}
103
104int ntlibc_strcmp(const char *s1, const char *s2)
105{
106 char *p1 = (char *)s1;
107 char *p2 = (char *)s2;
108 while (*p1 || *p2) {
109 if (*p1 != *p2) {
110 return (*p1 < *p2) ? -1 : 1;
111 }
112 p1++;
113 p2++;
114 }
115 if (*p1 == *p2) {
116 return 0;
117 }
118 else {
119 return (*p1 < *p2) ? -1 : 1;
120 }
121}
122
123int ntlibc_stricmp(const char *s1, const char *s2)
124{
125 char *p1 = (char *)s1;
126 char *p2 = (char *)s2;
127 while (*p1 || *p2) {
128 if (ntlibc_toupper(*p1) != ntlibc_toupper(*p2)) {
129 return (*p1 < *p2) ? -1 : 1;
130 }
131 p1++;
132 p2++;
133 }
134 if (*p1 == *p2) {
135 return 0;
136 }
137 else {
138 return (*p1 < *p2) ? -1 : 1;
139 }
140}
141
142int ntlibc_strncmp(const char *s1, const char *s2, int n)
143{
144 char *p1 = (char *)s1;
145 char *p2 = (char *)s2;
146 int len = 0;
147 while (*p1 || *p2) {
148 if (n <= len) {
149 break;
150 }
151 if (*p1 != *p2) {
152 return (*p1 < *p2) ? -1 : 1;
153 }
154 p1++;
155 p2++;
156 len++;
157 }
158 return 0;
159}
160
161int ntlibc_isdigit(int c)
162{
163 if (('0' <= c) && (c <= '9')) {
164 return 1;
165 }
166 return 0;
167}
168
169int ntlibc_isalpha(int c)
170{
171 if (('A' <= c) && (c <= 'Z')) {
172 return 1;
173 }
174 if (('a' <= c) && (c <= 'z')) {
175 return 1;
176 }
177 return 0;
178}
179
180int ntlibc_iscntrl(int c)
181{
182 if (c == 0x07) { return 0; }
183 if (c == 0x08) { return 0; }
184 if (c == 0x09) { return 0; }
185 if (c == 0x0a) { return 0; }
186 if (c == 0x0b) { return 0; }
187 if (c == 0x0c) { return 0; }
188 if (c == 0x0d) { return 0; }
189 if ((0x00 <= c) && (c <= 0x1f)) {
190 return 1;
191 }
192 return 0;
193}
194
195int ntlibc_toupper(int c)
196{
197 if (('a' <= c) && (c <= 'z')) {
198 int diff = 'a' - 'A';
199 return c - diff;
200 }
201 return c;
202}
203
204int ntlibc_tolower(int c)
205{
206 if (('A' <= c) && (c <= 'Z')) {
207 int diff = 'a' - 'A';
208 return c + diff;
209 }
210 return c;
211}
212
213int ntlibc_atoi(const char *nptr)
214{
215 int cnt;
216 int num = 0;
217 int ofs = 0;
218 int sign = 0;
219 int scnt = 0;
220 char *p = (char *)nptr;
221 while (*p != '\0') {
222 if (!ntlibc_isdigit(*p)) {
223 if (*p == ' ') {
224 ofs++;
225 }
226 if (*p == '+') {
227 sign = 0;
228 ofs++;
229 if (scnt++ > 0) {
230 return 0;
231 }
232 }
233 if (*p == '-') {
234 sign = 1;
235 ofs++;
236 if (scnt++ > 0) {
237 return 0;
238 }
239 }
240 }
241 p++;
242 }
243 for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
244 num = 10 * num + (nptr[cnt] - '0');
245 }
246 if (sign) {
247 return -num;
248 }
249 else {
250 return num;
251 }
252}
253
254unsigned long ntlibc_strtoul(const char *restrict nptr, char **restrict endptr, int base)
255{
256 int cnt;
257 int num = 0;
258 int ofs = 0;
259 int sign = 0;
260 int scnt = 0;
261 char *p = (char *)nptr;
262 while (*p != '\0') {
263 if (!ntlibc_isdigit(*p)) {
264 if (*p == ' ') {
265 ofs++;
266 }
267 if (*p == '+') {
268 sign = 0;
269 ofs++;
270 if (scnt++ > 0) {
271 *endptr = p;
272 break;
273 }
274 }
275 if (*p == '-') {
276 sign = 1;
277 ofs++;
278 if (scnt++ > 0) {
279 *endptr = p;
280 break;
281 }
282 }
283 *endptr = p;
284 break;
285 }
286 p++;
287 }
288 for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
289 num = 10 * num + (nptr[cnt] - '0');
290 }
291 if (sign) {
292 return -num;
293 }
294 else {
295 return num;
296 }
297}
298
299char *ntlibc_strchr(const char *s, int c)
300{
301 char *p = (char *)s;
302 for (;;) {
303 if (*p == c) {
304 return p;
305 }
306 if (!*p)
307 break;
308 p++;
309 }
310 return 0;
311}
312
313char *ntlibc_utoa(unsigned int value, char *s, int radix)
314{
315 char *s1 = s;
316 char *s2 = s;
317
318 do {
319 *s2++ = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
320 value /= radix;
321 } while (value > 0);
322
323 *s2-- = '\0';
324
325 while (s1 < s2) {
326 char c = *s1;
327 *s1++ = *s2;
328 *s2-- = c;
329 }
330
331 return s;
332}
Note: See TracBrowser for help on using the repository browser.