25
25
26
26
#include <stddef.h>
27
27
#include <stdarg.h>
28
+ #include <stdio.h>
28
29
#include <math.h>
30
+ #include <limits.h>
31
+ #include <errno.h>
29
32
30
33
#include "ets_sys.h"
31
34
#include "os_type.h"
@@ -46,6 +49,10 @@ void* realloc(void* ptr, size_t size) {
46
49
return os_realloc (ptr , size );
47
50
}
48
51
52
+ int puts (const char * str ){
53
+ return os_printf ("%s" , str );
54
+ }
55
+
49
56
int printf (const char * format , ...) {
50
57
int ret ;
51
58
va_list arglist ;
@@ -113,7 +120,7 @@ char *ets_strstr(const char *haystack, const char *needle) {
113
120
return strstr (haystack , needle );
114
121
}
115
122
116
- char * strchr (const char * str , int character ) {
123
+ char * strchr (const char * str , int character ) {
117
124
while (1 ) {
118
125
if (* str == 0x00 ) {
119
126
return NULL ;
@@ -125,7 +132,7 @@ char *strchr(const char * str, int character) {
125
132
}
126
133
}
127
134
128
- char * strrchr (const char * str , int character ) {
135
+ char * strrchr (const char * str , int character ) {
129
136
char * ret = NULL ;
130
137
while (1 ) {
131
138
if (* str == 0x00 ) {
@@ -138,11 +145,11 @@ char *strrchr(const char * str, int character) {
138
145
}
139
146
}
140
147
141
- char * strcat (char * dest , const char * src ) {
148
+ char * strcat (char * dest , const char * src ) {
142
149
return strncat (dest , src , strlen (src ));
143
150
}
144
151
145
- char * strncat (char * dest , const char * src , size_t n ) {
152
+ char * strncat (char * dest , const char * src , size_t n ) {
146
153
uint32_t offset = strlen (dest );
147
154
for (uint32_t i = 0 ; i < n ; i ++ ) {
148
155
* (dest + i + offset ) = * (src + i );
@@ -153,7 +160,7 @@ char *strncat(char * dest, const char * src, size_t n) {
153
160
return dest ;
154
161
}
155
162
156
- char * strtok_r (char * str , const char * delimiters , char * * temp ) {
163
+ char * strtok_r (char * str , const char * delimiters , char * * temp ) {
157
164
static char * ret = NULL ;
158
165
char * start = NULL ;
159
166
char * end = NULL ;
@@ -195,6 +202,128 @@ char *strtok_r(char * str, const char * delimiters, char ** temp) {
195
202
return ret ;
196
203
}
197
204
205
+ int strcasecmp (const char * str1 , const char * str2 ) {
206
+ int d = 0 ;
207
+ while (1 ) {
208
+ int c1 = tolower (* str1 ++ );
209
+ int c2 = tolower (* str2 ++ );
210
+ if (((d = c1 - c2 ) != 0 ) || (c2 == '\0' )) {
211
+ break ;
212
+ }
213
+ }
214
+ return d ;
215
+ }
216
+
217
+ char * strdup (const char * str ) {
218
+ size_t len = strlen (str ) + 1 ;
219
+ char * cstr = malloc (len );
220
+ if (cstr ) {
221
+ memcpy (cstr , str , len );
222
+ }
223
+ return cstr ;
224
+ }
225
+
226
+
227
+ long int ICACHE_FLASH_ATTR strtol (const char * str , char * * endptr , int base ) {
228
+ long int result = 0 ;
229
+ int sign = 1 ;
230
+
231
+ while (isspace (* str )) {
232
+ str ++ ;
233
+ }
234
+
235
+ if (* str == 0x00 ) {
236
+ // only space in str?
237
+ * endptr = (char * ) str ;
238
+ return result ;
239
+ }
240
+
241
+ switch (base ) {
242
+ case 10 :
243
+
244
+ if (* str == '-' ) {
245
+ sign = -1 ;
246
+ str ++ ;
247
+ } else if (* str == '+' ) {
248
+ str ++ ;
249
+ }
250
+
251
+ for (uint8_t i = 0 ; * str ; i ++ , str ++ ) {
252
+ int x = * str - '0' ;
253
+ if (x < 0 || x > 9 ) {
254
+ break ;
255
+ }
256
+ result = result * 10 + x ;
257
+ }
258
+ break ;
259
+ case 2 :
260
+ for (uint8_t i = 0 ; * str ; i ++ , str ++ ) {
261
+ int x = * str - '0' ;
262
+ if (x < 0 || x > 1 ) {
263
+ break ;
264
+ }
265
+ result = result * 2 + x ;
266
+ }
267
+ break ;
268
+ case 16 :
269
+ default :
270
+ os_printf ("fnk: strtol() only supports base 10 and 2 ATM!\n" );
271
+ break ;
272
+
273
+ }
274
+ * endptr = (char * ) str ;
275
+ return sign * result ;
276
+ }
277
+
278
+
279
+ // based on Source:
280
+ // https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93
281
+ double ICACHE_FLASH_ATTR strtod (const char * str , char * * endptr ) {
282
+ double result = 0.0 ;
283
+ double factor = 1.0 ;
284
+ bool decimals = false;
285
+ char c ;
286
+
287
+ while (isspace (* str )) {
288
+ str ++ ;
289
+ }
290
+
291
+ if (* str == 0x00 ) {
292
+ // only space in str?
293
+ * endptr = (char * ) str ;
294
+ return result ;
295
+ }
296
+
297
+ if (* str == '-' ) {
298
+ factor = -1 ;
299
+ str ++ ;
300
+ } else if (* str == '+' ) {
301
+ str ++ ;
302
+ }
303
+
304
+ while ((c = * str )) {
305
+ if (c == '.' ) {
306
+ decimals = true;
307
+ str ++ ;
308
+ continue ;
309
+ }
310
+
311
+ int d = c - '0' ;
312
+ if (d < 0 || d > 9 ) {
313
+ break ;
314
+ }
315
+
316
+ result = 10.0 * result + d ;
317
+ if (decimals ) {
318
+ factor *= 0.1 ;
319
+ }
320
+
321
+ str ++ ;
322
+ }
323
+ * endptr = (char * ) str ;
324
+ return result * factor ;
325
+ }
326
+
198
327
// ##########################################################################
199
328
// ctype functions
200
329
// ##########################################################################
@@ -226,6 +355,7 @@ int isdigit(int c) {
226
355
}
227
356
return 0 ;
228
357
}
358
+
229
359
int isgraph (int c ) {
230
360
if (isprint (c ) && c != ' ' ) {
231
361
return 1 ;
@@ -312,14 +442,13 @@ int isblank(int c) {
312
442
313
443
// ##########################################################################
314
444
315
- static int errno = 0 ;
445
+ static int errno_var = 0 ;
316
446
317
447
int * __errno (void ) {
318
- printf ("__errno is called last error: %d (not current)\n" , errno );
319
- return & errno ;
448
+ os_printf ("__errno is called last error: %d (not current)\n" , errno_var );
449
+ return & errno_var ;
320
450
}
321
451
322
-
323
452
// ##########################################################################
324
453
// __ieee754 functions
325
454
// ##########################################################################
0 commit comments