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 |
|
---|
35 | int 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 |
|
---|
46 | char *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 |
|
---|
59 | char *ntlibc_strcat(char *des, const char *src)
|
---|
60 | {
|
---|
61 | char *d = des;
|
---|
62 | const char *s = src;
|
---|
63 | while (*d) {
|
---|
64 | d++;
|
---|
65 | }
|
---|
66 | while (*s) {
|
---|
67 | *d = *s;
|
---|
68 | d++;
|
---|
69 | s++;
|
---|
70 | }
|
---|
71 | *d = '\0';
|
---|
72 | return des;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int ntlibc_strcmp(const char *s1, const char *s2)
|
---|
76 | {
|
---|
77 | char *p1 = (char *)s1;
|
---|
78 | char *p2 = (char *)s2;
|
---|
79 | while (*p1 || *p2) {
|
---|
80 | if (*p1 != *p2) {
|
---|
81 | return (*p1 < *p2) ? -1 : 1;
|
---|
82 | }
|
---|
83 | p1++;
|
---|
84 | p2++;
|
---|
85 | }
|
---|
86 | if (*p1 == *p2) {
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | return (*p1 < *p2) ? -1 : 1;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | int ntlibc_stricmp(const char *s1, const char *s2)
|
---|
95 | {
|
---|
96 | char *p1 = (char *)s1;
|
---|
97 | char *p2 = (char *)s2;
|
---|
98 | while (*p1 || *p2) {
|
---|
99 | if (ntlibc_toupper(*p1) != ntlibc_toupper(*p2)) {
|
---|
100 | return (*p1 < *p2) ? -1 : 1;
|
---|
101 | }
|
---|
102 | p1++;
|
---|
103 | p2++;
|
---|
104 | }
|
---|
105 | if (*p1 == *p2) {
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 | else {
|
---|
109 | return (*p1 < *p2) ? -1 : 1;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | int ntlibc_strncmp(const char *s1, const char *s2, int n)
|
---|
114 | {
|
---|
115 | char *p1 = (char *)s1;
|
---|
116 | char *p2 = (char *)s2;
|
---|
117 | int len = 0;
|
---|
118 | while (*p1 || *p2) {
|
---|
119 | if (n <= len) {
|
---|
120 | break;
|
---|
121 | }
|
---|
122 | if (*p1 != *p2) {
|
---|
123 | return (*p1 < *p2) ? -1 : 1;
|
---|
124 | }
|
---|
125 | p1++;
|
---|
126 | p2++;
|
---|
127 | len++;
|
---|
128 | }
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | int ntlibc_isdigit(int c)
|
---|
133 | {
|
---|
134 | if (('0' <= c) && (c <= '9')) {
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 | return 0;
|
---|
138 | }
|
---|
139 |
|
---|
140 | int ntlibc_isalpha(int c)
|
---|
141 | {
|
---|
142 | if (('A' <= c) && (c <= 'Z')) {
|
---|
143 | return 1;
|
---|
144 | }
|
---|
145 | if (('a' <= c) && (c <= 'z')) {
|
---|
146 | return 1;
|
---|
147 | }
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | int ntlibc_iscntrl(int c)
|
---|
152 | {
|
---|
153 | if (c == 0x07) { return 0; }
|
---|
154 | if (c == 0x08) { return 0; }
|
---|
155 | if (c == 0x09) { return 0; }
|
---|
156 | if (c == 0x0a) { return 0; }
|
---|
157 | if (c == 0x0b) { return 0; }
|
---|
158 | if (c == 0x0c) { return 0; }
|
---|
159 | if (c == 0x0d) { return 0; }
|
---|
160 | if ((0x00 <= c) && (c <= 0x1f)) {
|
---|
161 | return 1;
|
---|
162 | }
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | int ntlibc_toupper(int c)
|
---|
167 | {
|
---|
168 | if (('a' <= c) && (c <= 'z')) {
|
---|
169 | int diff = 'a' - 'A';
|
---|
170 | return c - diff;
|
---|
171 | }
|
---|
172 | return c;
|
---|
173 | }
|
---|
174 |
|
---|
175 | int ntlibc_tolower(int c)
|
---|
176 | {
|
---|
177 | if (('A' <= c) && (c <= 'Z')) {
|
---|
178 | int diff = 'a' - 'A';
|
---|
179 | return c + diff;
|
---|
180 | }
|
---|
181 | return c;
|
---|
182 | }
|
---|
183 |
|
---|
184 | int ntlibc_atoi(const char *nptr)
|
---|
185 | {
|
---|
186 | int cnt;
|
---|
187 | int num = 0;
|
---|
188 | int ofs = 0;
|
---|
189 | int sign = 0;
|
---|
190 | int scnt = 0;
|
---|
191 | char *p = (char *)nptr;
|
---|
192 | while (*p != '\0') {
|
---|
193 | if (!ntlibc_isdigit(*p)) {
|
---|
194 | if (*p == ' ') {
|
---|
195 | ofs++;
|
---|
196 | }
|
---|
197 | if (*p == '+') {
|
---|
198 | sign = 0;
|
---|
199 | ofs++;
|
---|
200 | if (scnt++ > 0) {
|
---|
201 | return 0;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | if (*p == '-') {
|
---|
205 | sign = 1;
|
---|
206 | ofs++;
|
---|
207 | if (scnt++ > 0) {
|
---|
208 | return 0;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | p++;
|
---|
213 | }
|
---|
214 | for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
|
---|
215 | num = 10 * num + (nptr[cnt] - '0');
|
---|
216 | }
|
---|
217 | if (sign) {
|
---|
218 | return -num;
|
---|
219 | }
|
---|
220 | else {
|
---|
221 | return num;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | char *ntlibc_strchr(const char *s, int c)
|
---|
226 | {
|
---|
227 | char *p = (char *)s;
|
---|
228 | for (;;) {
|
---|
229 | if (*p == c) {
|
---|
230 | return p;
|
---|
231 | }
|
---|
232 | if (!*p)
|
---|
233 | break;
|
---|
234 | p++;
|
---|
235 | }
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | char *ntlibc_utoa(unsigned int value, char *s, int radix)
|
---|
240 | {
|
---|
241 | char *s1 = s;
|
---|
242 | char *s2 = s;
|
---|
243 |
|
---|
244 | do {
|
---|
245 | *s2++ = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
|
---|
246 | value /= radix;
|
---|
247 | } while (value > 0);
|
---|
248 |
|
---|
249 | *s2-- = '\0';
|
---|
250 |
|
---|
251 | while (s1 < s2) {
|
---|
252 | char c = *s1;
|
---|
253 | *s1++ = *s2;
|
---|
254 | *s2-- = c;
|
---|
255 | }
|
---|
256 |
|
---|
257 | return s;
|
---|
258 | }
|
---|