OSDN Git Service

チケット #30493 , チケット #30494 , チケット #30502
[uzume/uzume_bfin.git] / uzumeapp / kernel / uzume / ntshell / ntlibc.c
1 /**
2  * @file ntlibc.c
3  * @author Shinichiro Nakamura
4  * @brief 小規模組み込みシステム向けのlibcライクライブラリの実装。
5  */
6
7 /*
8  * ===============================================================
9  *  Natural Tiny Shell (NT-Shell)
10  * ===============================================================
11  * Copyright (c) 2010-2012 Shinichiro Nakamura
12  *
13  * Permission is hereby granted, free of charge, to any person
14  * obtaining a copy of this software and associated documentation
15  * files (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use,
17  * copy, modify, merge, publish, distribute, sublicense, and/or
18  * sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following
20  * conditions:
21  *
22  * The above copyright notice and this permission notice shall be
23  * included in all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
27  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
29  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32  * OTHER DEALINGS IN THE SOFTWARE.
33  * ===============================================================
34  */
35
36 #include "ntlibc.h"
37
38 int ntlibc_strlen(const char *s)
39 {
40     const char *p = s;
41     int cnt = 0;
42     while (*p) {
43         cnt++;
44         p++;
45     }
46     return cnt;
47 }
48
49 char *ntlibc_strcpy(char *des, const char *src)
50 {
51     char *d = des;
52     const char *s = src;
53     while (*s) {
54         *d = *s;
55         d++;
56         s++;
57     }
58     *d = '\0';
59     return des;
60 }
61
62 char *ntlibc_strcat(char *des, const char *src)
63 {
64     char *d = des;
65     const char *s = src;
66     while (*d) {
67         d++;
68     }
69     while (*s) {
70         *d = *s;
71         d++;
72         s++;
73     }
74     *d = '\0';
75     return des;
76 }
77
78 int ntlibc_strcmp(const char *s1, const char *s2)
79 {
80     char *p1 = (char *)s1;
81     char *p2 = (char *)s2;
82     while (*p1 || *p2) {
83         if (*p1 != *p2) {
84             return (*p1 < *p2) ? -1 : 1;
85         }
86         p1++;
87         p2++;
88     }
89     if (*p1 == *p2) {
90         return 0;
91     } else {
92         return (*p1 < *p2) ? -1 : 1;
93     }
94 }
95
96 int ntlibc_strncmp(const char *s1, const char *s2, int n)
97 {
98     char *p1 = (char *)s1;
99     char *p2 = (char *)s2;
100     int len = 0;
101     while (*p1 || *p2) {
102         if (n <= len) {
103             break;
104         }
105         if (*p1 != *p2) {
106             return (*p1 < *p2) ? -1 : 1;
107         }
108         p1++;
109         p2++;
110         len++;
111     }
112     return 0;
113 }
114
115 int ntlibc_isdigit(int c)
116 {
117   if (('0' <= c) && (c <= '9')) {
118     return 1;
119   }
120   return 0;
121 }
122
123 int ntlibc_isalpha(int c)
124 {
125   if (('A' <= c) && (c <= 'Z')) {
126     return 1;
127   }
128   if (('a' <= c) && (c <= 'z')) {
129     return 1;
130   }
131   return 0;
132 }
133
134 int ntlibc_iscntrl(int c)
135 {
136   if (c == 0x07) { return 0; }
137   if (c == 0x08) { return 0; }
138   if (c == 0x09) { return 0; }
139   if (c == 0x0a) { return 0; }
140   if (c == 0x0b) { return 0; }
141   if (c == 0x0c) { return 0; }
142   if (c == 0x0d) { return 0; }
143   if ((0x00 <= c) && (c <= 0x1f)) {
144     return 1;
145   }
146   return 0;
147 }
148
149 int ntlibc_toupper(int c)
150 {
151   if (('a' <= c) && (c <= 'z')) {
152     int diff = 'a' - 'A';
153     return c - diff;
154   }
155   return c;
156 }
157
158 int ntlibc_tolower(int c)
159 {
160   if (('A' <= c) && (c <= 'Z')) {
161     int diff = 'a' - 'A';
162     return c + diff;
163   }
164   return c;
165 }
166
167 int ntlibc_atoi(const char *nptr)
168 {
169   int cnt;
170   int num = 0;
171   int ofs = 0;
172   int sign = 0;
173   int scnt = 0;
174   char *p = (char *)nptr;
175   while (*p != '\0') {
176     if (!ntlibc_isdigit(*p)) {
177       if (*p == ' ') {
178         ofs++;
179       }
180       if (*p == '+') {
181         sign = 0;
182         ofs++;
183         if (scnt++ > 0) {
184           return 0;
185         }
186       }
187       if (*p == '-') {
188         sign = 1;
189         ofs++;
190         if (scnt++ > 0) {
191           return 0;
192         }
193       }
194     }
195     p++;
196   }
197   for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
198     num = 10 * num + (nptr[cnt] - '0');
199   }
200   if (sign) {
201     return -num;
202   } else {
203     return num;
204   }
205 }
206
207 char *ntlibc_strchr(const char *s, int c)
208 {
209   char *p = (char *)s;
210   while (*p) {
211     if (*p == c) {
212       return p;
213     }
214     p++;
215   }
216   return 0;
217 }
218
219 char *ntlibc_utoa(unsigned int value, char *s, int radix)
220 {
221   char *s1 = s;
222   char *s2 = s;
223
224   do {
225     *s2++ = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
226     value /= radix;
227   } while (value > 0);
228
229   *s2-- = '\0';
230
231   while (s1 < s2) {
232     char c = *s1;
233     *s1++ = *s2;
234     *s2-- = c;
235   }
236
237   return s;
238 }
239