Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 5 commits
  • 10 files changed
  • 0 comments
  • 1 contributor
1  software/bios/main.c
... ...
@@ -1,4 +1,5 @@
1 1
 #include <stdio.h>
  2
+#include <stdlib.h>
2 3
 #include <console.h>
3 4
 #include <string.h>
4 5
 #include <uart.h>
72  software/include/base/ctype.h
... ...
@@ -1,51 +1,55 @@
1 1
 #ifndef __CTYPE_H
2 2
 #define __CTYPE_H
3 3
 
4  
-static inline int isdigit(char c)
5  
-{
6  
-	return (c >= '0') && (c <= '9');
7  
-}
8  
-
9  
-static inline int isxdigit(char c)
10  
-{
11  
-	return isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
12  
-}
13  
-
14  
-static inline int isupper(char c)
15  
-{
16  
-	return (c >= 'A') && (c <= 'Z');
17  
-}
18  
-
19  
-static inline int islower(char c)
20  
-{
21  
-	return (c >= 'a') && (c <= 'z');
22  
-}
23  
-
24  
-static inline unsigned char tolower(unsigned char c)
  4
+/*
  5
+ * NOTE! This ctype does not handle EOF like the standard C
  6
+ * library is required to.
  7
+ */
  8
+
  9
+#define _U	0x01	/* upper */
  10
+#define _L	0x02	/* lower */
  11
+#define _D	0x04	/* digit */
  12
+#define _C	0x08	/* cntrl */
  13
+#define _P	0x10	/* punct */
  14
+#define _S	0x20	/* white space (space/lf/tab) */
  15
+#define _X	0x40	/* hex digit */
  16
+#define _SP	0x80	/* hard space (0x20) */
  17
+
  18
+extern const unsigned char _ctype[];
  19
+
  20
+#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
  21
+
  22
+#define isalnum(c)	((__ismask(c)&(_U|_L|_D)) != 0)
  23
+#define isalpha(c)	((__ismask(c)&(_U|_L)) != 0)
  24
+#define iscntrl(c)	((__ismask(c)&(_C)) != 0)
  25
+#define isdigit(c)	((__ismask(c)&(_D)) != 0)
  26
+#define isgraph(c)	((__ismask(c)&(_P|_U|_L|_D)) != 0)
  27
+#define islower(c)	((__ismask(c)&(_L)) != 0)
  28
+#define isprint(c)	((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
  29
+#define ispunct(c)	((__ismask(c)&(_P)) != 0)
  30
+/* Note: isspace() must return false for %NUL-terminator */
  31
+#define isspace(c)	((__ismask(c)&(_S)) != 0)
  32
+#define isupper(c)	((__ismask(c)&(_U)) != 0)
  33
+#define isxdigit(c)	((__ismask(c)&(_D|_X)) != 0)
  34
+
  35
+#define isascii(c) (((unsigned char)(c))<=0x7f)
  36
+#define toascii(c) (((unsigned char)(c))&0x7f)
  37
+
  38
+static inline unsigned char __tolower(unsigned char c)
25 39
 {
26 40
 	if (isupper(c))
27 41
 		c -= 'A'-'a';
28 42
 	return c;
29 43
 }
30 44
 
31  
-static inline unsigned char toupper(unsigned char c)
  45
+static inline unsigned char __toupper(unsigned char c)
32 46
 {
33 47
 	if (islower(c))
34 48
 		c -= 'a'-'A';
35 49
 	return c;
36 50
 }
37 51
 
38  
-static inline char isspace(unsigned char c)
39  
-{
40  
-	if(c == ' '
41  
-		|| c == '\f'
42  
-		|| c == '\n'
43  
-		|| c == '\r'
44  
-		|| c == '\t'
45  
-		|| c == '\v')
46  
-		return 1;
47  
-
48  
-	return 0;
49  
-}
  52
+#define tolower(c) __tolower(c)
  53
+#define toupper(c) __toupper(c)
50 54
 
51 55
 #endif /* __CTYPE_H */
253  software/include/base/errno.h
... ...
@@ -0,0 +1,253 @@
  1
+#ifndef __ERRNO_H
  2
+#define __ERRNO_H
  3
+
  4
+extern int errno;
  5
+
  6
+#define EPERM               1
  7
+#define EPERM_STR           "Operation not permitted"
  8
+#define ENOENT              2
  9
+#define ENOENT_STR          "No such file or directory"
  10
+#define ESRCH               3
  11
+#define ESRCH_STR           "No such process"
  12
+#define EINTR               4
  13
+#define EINTR_STR           "Interrupted system call"
  14
+#define EIO                 5
  15
+#define EIO_STR             "I/O error"
  16
+#define ENXIO               6
  17
+#define ENXIO_STR           "No such device or address"
  18
+#define E2BIG               7
  19
+#define E2BIG_STR           "Arg list too long"
  20
+#define ENOEXEC             8
  21
+#define ENOEXEC_STR         "Exec format error"
  22
+#define EBADF               9
  23
+#define EBADF_STR           "Bad file number"
  24
+#define ECHILD              10
  25
+#define ECHILD_STR          "No child processes"
  26
+#define EAGAIN              11
  27
+#define EWOULDBLOCK         EAGAIN
  28
+#define EAGAIN_STR          "Try again"
  29
+#define ENOMEM              12
  30
+#define ENOMEM_STR          "Out of memory"
  31
+#define EACCES              13
  32
+#define EACCES_STR          "Permission denied"
  33
+#define EFAULT              14
  34
+#define EFAULT_STR          "Bad address"
  35
+#define ENOTBLK             15
  36
+#define ENOTBLK_STR         "Block device required"
  37
+#define EBUSY               16
  38
+#define EBUSY_STR           "Device or resource busy"
  39
+#define EEXIST              17
  40
+#define EEXIST_STR          "File exists"
  41
+#define EXDEV               18
  42
+#define EXDEV_STR           "Cross-device link"
  43
+#define ENODEV              19
  44
+#define ENODEV_STR          "No such device"
  45
+#define ENOTDIR             20
  46
+#define ENOTDIR_STR         "Not a directory"
  47
+#define EISDIR              21
  48
+#define EISDIR_STR          "Is a directory"
  49
+#define EINVAL              22
  50
+#define EINVAL_STR          "Invalid argument"
  51
+#define ENFILE              23
  52
+#define ENFILE_STR          "File table overflow"
  53
+#define EMFILE              24
  54
+#define EMFILE_STR          "Too many open files"
  55
+#define ENOTTY              25
  56
+#define ENOTTY_STR          "Not a typewriter"
  57
+#define ETXTBSY             26
  58
+#define ETXTBSY_STR         "Text file busy"
  59
+#define EFBIG               27
  60
+#define EFBIG_STR           "File too large"
  61
+#define ENOSPC              28
  62
+#define ENOSPC_STR          "No space left on device"
  63
+#define ESPIPE              29
  64
+#define ESPIPE_STR          "Illegal seek"
  65
+#define EROFS               30
  66
+#define EROFS_STR           "Read-only file system"
  67
+#define EMLINK              31
  68
+#define EMLINK_STR          "Too many links"
  69
+#define EPIPE               32
  70
+#define EPIPE_STR           "Broken pipe"
  71
+#define EDOM                33
  72
+#define EDOM_STR            "Math argument out of domain of func"
  73
+#define ERANGE              34
  74
+#define ERANGE_STR          "Math result not representable"
  75
+#define EDEADLK             35
  76
+#define EDEADLOCK           EDEADLK
  77
+#define EDEADLK_STR         "Resource deadlock would occur"
  78
+#define ENAMETOOLONG        36
  79
+#define ENAMETOOLONG_STR    "File name too long"
  80
+#define ENOLCK              37
  81
+#define ENOLCK_STR          "No record locks available"
  82
+#define ENOSYS              38
  83
+#define ENOSYS_STR          "Function not implemented"
  84
+#define ENOTEMPTY           39
  85
+#define ENOTEMPTY_STR       "Directory not empty"
  86
+#define ELOOP               40
  87
+#define ELOOP_STR           "Too many symbolic links encountered"
  88
+#define ENOMSG              42
  89
+#define ENOMSG_STR          "No message of desired type"
  90
+#define EIDRM               43
  91
+#define EIDRM_STR           "Identifier removed"
  92
+#define ECHRNG              44
  93
+#define ECHRNG_STR          "Channel number out of range"
  94
+#define EL2NSYNC            45
  95
+#define EL2NSYNC_STR        "Level 2 not synchronized"
  96
+#define EL3HLT              46
  97
+#define EL3HLT_STR          "Level 3 halted"
  98
+#define EL3RST              47
  99
+#define EL3RST_STR          "Level 3 reset"
  100
+#define ELNRNG              48
  101
+#define ELNRNG_STR          "Link number out of range"
  102
+#define EUNATCH             49
  103
+#define EUNATCH_STR         "Protocol driver not attached"
  104
+#define ENOCSI              50
  105
+#define ENOCSI_STR          "No CSI structure available"
  106
+#define EL2HLT              51
  107
+#define EL2HLT_STR          "Level 2 halted"
  108
+#define EBADE               52
  109
+#define EBADE_STR           "Invalid exchange"
  110
+#define EBADR               53
  111
+#define EBADR_STR           "Invalid request descriptor"
  112
+#define EXFULL              54
  113
+#define EXFULL_STR          "Exchange full"
  114
+#define ENOANO              55
  115
+#define ENOANO_STR          "No anode"
  116
+#define EBADRQC             56
  117
+#define EBADRQC_STR         "Invalid request code"
  118
+#define EBADSLT             57
  119
+#define EBADSLT_STR         "Invalid slot"
  120
+#define EBFONT              59
  121
+#define EBFONT_STR          "Bad font file format"
  122
+#define ENOSTR              60
  123
+#define ENOSTR_STR          "Device not a stream"
  124
+#define ENODATA             61
  125
+#define ENODATA_STR         "No data available"
  126
+#define ETIME               62
  127
+#define ETIME_STR           "Timer expired"
  128
+#define ENOSR               63
  129
+#define ENOSR_STR           "Out of streams resources"
  130
+#define ENONET              64
  131
+#define ENONET_STR          "Machine is not on the network"
  132
+#define ENOPKG              65
  133
+#define ENOPKG_STR          "Package not installed"
  134
+#define EREMOTE             66
  135
+#define EREMOTE_STR         "Object is remote"
  136
+#define ENOLINK             67
  137
+#define ENOLINK_STR         "Link has been severed"
  138
+#define EADV                68
  139
+#define EADV_STR            "Advertise error"
  140
+#define ESRMNT              69
  141
+#define ESRMNT_STR          "Srmount error"
  142
+#define ECOMM               70
  143
+#define ECOMM_STR           "Communication error on send"
  144
+#define EPROTO              71
  145
+#define EPROTO_STR          "Protocol error"
  146
+#define EMULTIHOP           72
  147
+#define EMULTIHOP_STR       "Multihop attempted"
  148
+#define EDOTDOT             73
  149
+#define EDOTDOT_STR         "RFS specific error"
  150
+#define EBADMSG             74
  151
+#define EBADMSG_STR         "Not a data message"
  152
+#define EOVERFLOW           75
  153
+#define EOVERFLOW_STR       "Value too large for defined data type"
  154
+#define ENOTUNIQ            76
  155
+#define ENOTUNIQ_STR        "Name not unique on network"
  156
+#define EBADFD              77
  157
+#define EBADFD_STR          "File descriptor in bad state"
  158
+#define EREMCHG             78
  159
+#define EREMCHG_STR         "Remote address changed"
  160
+#define ELIBACC             79
  161
+#define ELIBACC_STR         "Can not access a needed shared library"
  162
+#define ELIBBAD             80
  163
+#define ELIBBAD_STR         "Accessing a corrupted shared library"
  164
+#define ELIBSCN             81
  165
+#define ELIBSCN_STR         ".lib section in a.out corrupted"
  166
+#define ELIBMAX             82
  167
+#define ELIBMAX_STR         "Attempting to link in too many shared libraries"
  168
+#define ELIBEXEC            83
  169
+#define ELIBEXEC_STR        "Cannot exec a shared library directly"
  170
+#define EILSEQ              84
  171
+#define EILSEQ_STR          "Illegal byte sequence"
  172
+#define ERESTART            85
  173
+#define ERESTART_STR        "Interrupted system call should be restarted"
  174
+#define ESTRPIPE            86
  175
+#define ESTRPIPE_STR        "Streams pipe error"
  176
+#define EUSERS              87
  177
+#define EUSERS_STR          "Too many users"
  178
+#define ENOTSOCK            88
  179
+#define ENOTSOCK_STR        "Socket operation on non-socket"
  180
+#define EDESTADDRREQ        89
  181
+#define EDESTADDRREQ_STR    "Destination address required"
  182
+#define EMSGSIZE            90
  183
+#define EMSGSIZE_STR        "Message too long"
  184
+#define EPROTOTYPE          91
  185
+#define EPROTOTYPE_STR      "Protocol wrong type for socket"
  186
+#define ENOPROTOOPT         92
  187
+#define ENOPROTOOPT_STR     "Protocol not available"
  188
+#define EPROTONOSUPPORT      93
  189
+#define EPROTONOSUPPORT_STR "Protocol not supported"
  190
+#define ESOCKTNOSUPPORT     94
  191
+#define ESOCKTNOSUPPORT_STR "Socket type not supported"
  192
+#define EOPNOTSUPP          95
  193
+#define EOPNOTSUPP_STR      "Operation not supported on transport endpoint"
  194
+#define EPFNOSUPPORT        96
  195
+#define EPFNOSUPPORT_STR    "Protocol family not supported"
  196
+#define EAFNOSUPPORT        97
  197
+#define EAFNOSUPPORT_STR    "Address family not supported by protocol"
  198
+#define EADDRINUSE          98
  199
+#define EADDRINUSE_STR      "Address already in use"
  200
+#define EADDRNOTAVAIL       99
  201
+#define EADDRNOTAVAIL_STR   "Cannot assign requested address"
  202
+#define ENETDOWN            100
  203
+#define ENETDOWN_STR        "Network is down"
  204
+#define ENETUNREACH         101
  205
+#define ENETUNREACH_STR     "Network is unreachable"
  206
+#define ENETRESET           102
  207
+#define ENETRESET_STR       "Network dropped connection because of reset"
  208
+#define ECONNABORTED        103
  209
+#define ECONNABORTED_STR    "Software caused connection abort"
  210
+#define ECONNRESET          104
  211
+#define ECONNRESET_STR      "Connection reset by peer"
  212
+#define ENOBUFS             105
  213
+#define ENOBUFS_STR         "No buffer space available"
  214
+#define EISCONN             106
  215
+#define EISCONN_STR         "Transport endpoint is already connected"
  216
+#define ENOTCONN            107
  217
+#define ENOTCONN_STR        "Transport endpoint is not connected"
  218
+#define ESHUTDOWN           108
  219
+#define ESHUTDOWN_STR       "Cannot send after transport endpoint shutdown"
  220
+#define ETOOMANYREFS        109
  221
+#define ETOOMANYREFS_STR    "Too many references: cannot splice"
  222
+#define ETIMEDOUT           110
  223
+#define ETIMEDOUT_STR       "Connection timed out"
  224
+#define ECONNREFUSED        111
  225
+#define ECONNREFUSED_STR    "Connection refused"
  226
+#define EHOSTDOWN           112
  227
+#define EHOSTDOWN_STR       "Host is down"
  228
+#define EHOSTUNREACH        113
  229
+#define EHOSTUNREACH_STR    "No route to host"
  230
+#define EALREADY            114
  231
+#define EALREADY_STR        "Operation already in progress"
  232
+#define EINPROGRESS         115
  233
+#define EINPROGRESS_STR     "Operation now in progress"
  234
+#define ESTALE              116
  235
+#define ESTALE_STR          "Stale NFS file handle"
  236
+#define EUCLEAN             117
  237
+#define EUCLEAN_STR         "Structure needs cleaning"
  238
+#define ENOTNAM             118
  239
+#define ENOTNAM_STR         "Not a XENIX named type file"
  240
+#define ENAVAIL             119
  241
+#define ENAVAIL_STR         "No XENIX semaphores available"
  242
+#define EISNAM              120
  243
+#define EISNAM_STR          "Is a named type file"
  244
+#define EREMOTEIO           121
  245
+#define EREMOTEIO_STR       "Remote I/O error"
  246
+#define EDQUOT              122
  247
+#define EDQUOT_STR          "Quota exceeded"
  248
+#define ENOMEDIUM           123
  249
+#define ENOMEDIUM_STR       "No medium found"
  250
+#define EMEDIUMTYPE         124
  251
+#define EMEDIUMTYPE_STR     "Wrong medium type"
  252
+
  253
+#endif /* __ERRNO_H */
26  software/include/base/stdio.h
... ...
@@ -1,7 +1,7 @@
1 1
 #ifndef __STDIO_H
2 2
 #define __STDIO_H
3 3
 
4  
-#include <stdlib.h>
  4
+#include <stddef.h>
5 5
 
6 6
 int putchar(int c);
7 7
 int puts(const char *s);
@@ -12,4 +12,28 @@ int sprintf(char *buf, const char *fmt, ...);
12 12
 
13 13
 int printf(const char *fmt, ...);
14 14
 
  15
+/*
  16
+ * Note: this library does not provide FILE operations.
  17
+ * User code must implement them.
  18
+ */
  19
+
  20
+#ifndef BUFSIZ
  21
+#define BUFSIZ 1024
  22
+#endif
  23
+
  24
+typedef int FILE;
  25
+
  26
+extern FILE *stdin;
  27
+extern FILE *stdout;
  28
+extern FILE *stderr;
  29
+
  30
+int fprintf(FILE *stream, const char *format, ...);
  31
+int fflush(FILE *stream);
  32
+
  33
+FILE *fopen(const char *path, const char *mode);
  34
+char *fgets(char *s, int size, FILE *stream);
  35
+size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
  36
+size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
  37
+int fclose(FILE *fp);
  38
+
15 39
 #endif /* __STDIO_H */
8  software/include/base/stdlib.h
@@ -44,7 +44,7 @@ static inline long atol(const char *nptr) {
44 44
 }
45 45
 char *number(char *buf, char *end, unsigned long num, int base, int size, int precision, int type);
46 46
 long strtol(const char *nptr, char **endptr, int base);
47  
-float atof(const char *s);
  47
+double strtod(const char *str, char **endptr);
48 48
 
49 49
 #define   RAND_MAX        2147483647
50 50
 
@@ -52,4 +52,10 @@ unsigned int rand(void);
52 52
 void srand(unsigned int seed);
53 53
 void abort(void);
54 54
 
  55
+/*
  56
+ * The following functions are not provided by this library.
  57
+ */
  58
+
  59
+char *getenv(const char *name);
  60
+
55 61
 #endif /* __STDLIB_H */
5  software/include/base/string.h
@@ -19,9 +19,10 @@
19 19
 #ifndef __STRING_H
20 20
 #define __STRING_H
21 21
 
22  
-#include <stdlib.h>
  22
+#include <stddef.h>
23 23
 
24 24
 char *strchr(const char *s, int c);
  25
+char *strpbrk(const char *,const char *);
25 26
 char *strrchr(const char *s, int c);
26 27
 char *strnchr(const char *s, size_t count, int c);
27 28
 char *strcpy(char *dest, const char *src);
@@ -30,10 +31,12 @@ int strcmp(const char *cs, const char *ct);
30 31
 int strncmp(const char *cs, const char *ct, size_t count);
31 32
 size_t strlen(const char *s);
32 33
 size_t strnlen(const char *s, size_t count);
  34
+size_t strspn(const char *s, const char *accept);
33 35
 int memcmp(const void *cs, const void *ct, size_t count);
34 36
 void *memset(void *s, int c, size_t count);
35 37
 void *memcpy(void *to, const void *from, size_t n);
36 38
 void *memmove(void *dest, const void *src, size_t count);
37 39
 char *strstr(const char *s1, const char *s2);
  40
+void *memchr(const void *s, int c, size_t n);
38 41
 
39 42
 #endif /* __STRING_H */
2  software/libbase/Makefile
... ...
@@ -1,7 +1,7 @@
1 1
 M2DIR=../..
2 2
 include $(M2DIR)/software/common.mak
3 3
 
4  
-OBJECTS=divsi3.o setjmp.o libc.o crc16.o crc32.o console.o timer.o system.o board.o uart.o softfloat.o softfloat-glue.o vsnprintf.o atof.o
  4
+OBJECTS=divsi3.o setjmp.o libc.o crc16.o crc32.o console.o timer.o system.o board.o uart.o softfloat.o softfloat-glue.o vsnprintf.o strtod.o
5 5
 
6 6
 all: libbase.a
7 7
 
84  software/libbase/atof.c
... ...
@@ -1,84 +0,0 @@
1  
-/*  atof.c: converts an ASCII string to float
2  
-
3  
-    Copyright (C) 2003  Jesus Calvino-Fraga, jesusc@ieee.org
4  
-
5  
-    This library is free software; you can redistribute it and/or
6  
-    modify it under the terms of the GNU Lesser General Public
7  
-    License as published by the Free Software Foundation; either
8  
-    version 2.1 of the License, or (at your option) any later version.
9  
-
10  
-    This library is distributed in the hope that it will be useful,
11  
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  
-    Lesser General Public License for more details.
14  
-
15  
-    You should have received a copy of the GNU Lesser General Public
16  
-    License along with this library; if not, write to the Free Software
17  
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA */
18  
-
19  
-#include <stdlib.h>
20  
-#include <ctype.h>
21  
-
22  
-float atof(const char * s)
23  
-{
24  
-	float value, fraction;
25  
-	char iexp;
26  
-	char sign;
27  
-
28  
-	//Skip leading blanks
29  
-	while (isspace(*s)) s++;
30  
-
31  
-	//Get the sign
32  
-	if (*s == '-')
33  
-	{
34  
-		sign=1;
35  
-		s++;
36  
-	}
37  
-	else
38  
-	{
39  
-		sign=0;
40  
-		if (*s == '+') s++;
41  
-	}
42  
-
43  
-	//Get the integer part
44  
-	for (value=0.0f; isdigit(*s); s++)
45  
-	{
46  
-		value=10.0f*value+(*s-'0');
47  
-	}
48  
-
49  
-	//Get the fraction
50  
-	if (*s == '.')
51  
-	{
52  
-		s++;
53  
-		for (fraction=0.1f; isdigit(*s); s++)
54  
-		{
55  
-			value+=(*s-'0')*fraction;
56  
-			fraction*=0.1f;
57  
-		}
58  
-	}
59  
-
60  
-	//Finally, the exponent (not very efficient, but enough for now)
61  
-	if (toupper(*s)=='E')
62  
-	{
63  
-		s++;
64  
-		iexp=(char)atoi(s);
65  
-		{
66  
-			while(iexp!=0)
67  
-			{
68  
-				if(iexp<0)
69  
-				{
70  
-					value*=0.1f;
71  
-					iexp++;
72  
-				}
73  
-				else
74  
-				{
75  
-					value*=10.0f;
76  
-					iexp--;
77  
-				}
78  
-			}
79  
-		}
80  
-	}
81  
-
82  
-	if(sign) value*=-1.0f;
83  
-	return (value);
84  
-}
90  software/libbase/libc.c
@@ -23,6 +23,8 @@
23 23
 #include <string.h>
24 24
 #include <limits.h>
25 25
 
  26
+int errno;
  27
+
26 28
 /**
27 29
  * strchr - Find the first occurrence of a character in a string
28 30
  * @s: The string to be searched
@@ -37,6 +39,24 @@ char *strchr(const char *s, int c)
37 39
 }
38 40
 
39 41
 /**
  42
+ * strpbrk - Find the first occurrence of a set of characters
  43
+ * @cs: The string to be searched
  44
+ * @ct: The characters to search for
  45
+ */
  46
+char *strpbrk(const char *cs, const char *ct)
  47
+{
  48
+	const char *sc1, *sc2;
  49
+
  50
+	for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  51
+		for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  52
+			if (*sc1 == *sc2)
  53
+				return (char *)sc1;
  54
+		}
  55
+	}
  56
+	return NULL;
  57
+}
  58
+
  59
+/**
40 60
  * strrchr - Find the last occurrence of a character in a string
41 61
  * @s: The string to be searched
42 62
  * @c: The character to search for
@@ -170,6 +190,29 @@ size_t strnlen(const char *s, size_t count)
170 190
 }
171 191
 
172 192
 /**
  193
+ * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
  194
+ * @s: The string to be searched
  195
+ * @accept: The string to search for
  196
+ */
  197
+size_t strspn(const char *s, const char *accept)
  198
+{
  199
+	const char *p;
  200
+	const char *a;
  201
+	size_t count = 0;
  202
+
  203
+	for (p = s; *p != '\0'; ++p) {
  204
+		for (a = accept; *a != '\0'; ++a) {
  205
+			if (*p == *a)
  206
+				break;
  207
+		}
  208
+		if (*a == '\0')
  209
+			return count;
  210
+		++count;
  211
+	}
  212
+	return count;
  213
+}
  214
+
  215
+/**
173 216
  * memcmp - Compare two areas of memory
174 217
  * @cs: One area of memory
175 218
  * @ct: Another area of memory
@@ -302,6 +345,26 @@ char *strstr(const char *s1, const char *s2)
302 345
 }
303 346
 
304 347
 /**
  348
+ * memchr - Find a character in an area of memory.
  349
+ * @s: The memory area
  350
+ * @c: The byte to search for
  351
+ * @n: The size of the area.
  352
+ *
  353
+ * returns the address of the first occurrence of @c, or %NULL
  354
+ * if @c is not found
  355
+ */
  356
+void *memchr(const void *s, int c, size_t n)
  357
+{
  358
+	const unsigned char *p = s;
  359
+	while (n-- != 0) {
  360
+        	if ((unsigned char)c == *p++) {
  361
+			return (void *)(p - 1);
  362
+		}
  363
+	}
  364
+	return NULL;
  365
+}
  366
+
  367
+/**
305 368
  * strtoul - convert a string to an unsigned long
306 369
  * @nptr: The start of the string
307 370
  * @endptr: A pointer to the end of the parsed string will be placed here
@@ -559,6 +622,33 @@ int sprintf(char * buf, const char *fmt, ...)
559 622
 	return i;
560 623
 }
561 624
 
  625
+/* From linux/lib/ctype.c, Copyright (C) 1991, 1992  Linus Torvalds */
  626
+const unsigned char _ctype[] = {
  627
+_C,_C,_C,_C,_C,_C,_C,_C,				/* 0-7 */
  628
+_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C,			/* 8-15 */
  629
+_C,_C,_C,_C,_C,_C,_C,_C,				/* 16-23 */
  630
+_C,_C,_C,_C,_C,_C,_C,_C,				/* 24-31 */
  631
+_S|_SP,_P,_P,_P,_P,_P,_P,_P,				/* 32-39 */
  632
+_P,_P,_P,_P,_P,_P,_P,_P,				/* 40-47 */
  633
+_D,_D,_D,_D,_D,_D,_D,_D,				/* 48-55 */
  634
+_D,_D,_P,_P,_P,_P,_P,_P,				/* 56-63 */
  635
+_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U,		/* 64-71 */
  636
+_U,_U,_U,_U,_U,_U,_U,_U,				/* 72-79 */
  637
+_U,_U,_U,_U,_U,_U,_U,_U,				/* 80-87 */
  638
+_U,_U,_U,_P,_P,_P,_P,_P,				/* 88-95 */
  639
+_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L,		/* 96-103 */
  640
+_L,_L,_L,_L,_L,_L,_L,_L,				/* 104-111 */
  641
+_L,_L,_L,_L,_L,_L,_L,_L,				/* 112-119 */
  642
+_L,_L,_L,_P,_P,_P,_P,_C,				/* 120-127 */
  643
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,			/* 128-143 */
  644
+0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,			/* 144-159 */
  645
+_S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,	/* 160-175 */
  646
+_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,	/* 176-191 */
  647
+_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,	/* 192-207 */
  648
+_U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L,	/* 208-223 */
  649
+_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,	/* 224-239 */
  650
+_L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L};	/* 240-255 */
  651
+
562 652
 /**
563 653
  * rand - Returns a pseudo random number
564 654
  */
234  software/libbase/strtod.c
... ...
@@ -0,0 +1,234 @@
  1
+/****************************************************************************
  2
+ * lib/string/lib_strtod.c
  3
+ * Convert string to double
  4
+ *
  5
+ *   Copyright (C) 2002 Michael Ringgaard. All rights reserved.
  6
+ *   Copyright (C) 2006-2007 H. Peter Anvin.
  7
+ *
  8
+ * Redistribution and use in source and binary forms, with or without
  9
+ * modification, are permitted provided that the following conditions
  10
+ * are met:
  11
+ *
  12
+ * 1. Redistributions of source code must retain the above copyright
  13
+ *    notice, this list of conditions and the following disclaimer.
  14
+ * 2. Redistributions in binary form must reproduce the above copyright
  15
+ *    notice, this list of conditions and the following disclaimer in the
  16
+ *    documentation and/or other materials provided with the distribution.
  17
+ * 3. Neither the name of the project nor the names of its contributors
  18
+ *    may be used to endorse or promote products derived from this software
  19
+ *    without specific prior written permission.
  20
+ *
  21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
  25
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  30
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  32
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
  33
+ *
  34
+ ****************************************************************************/
  35
+
  36
+/****************************************************************************
  37
+ * Included Files
  38
+ ****************************************************************************/
  39
+
  40
+#include <stdlib.h>
  41
+#include <ctype.h>
  42
+#include <errno.h>
  43
+
  44
+/****************************************************************************
  45
+ * Pre-processor definitions
  46
+ ****************************************************************************/
  47
+
  48
+/* These are predefined with GCC, but could be issues for other compilers. If
  49
+ * not defined, an arbitrary big number is put in for now.  These should be
  50
+ * added to nuttx/compiler for your compiler.
  51
+ */
  52
+
  53
+#if !defined(__DBL_MIN_EXP__) || !defined(__DBL_MAX_EXP__)
  54
+#  ifdef CONFIG_CPP_HAVE_WARNING
  55
+#    warning "Size of exponent is unknown"
  56
+#  endif
  57
+#  undef  __DBL_MIN_EXP__
  58
+#  define __DBL_MIN_EXP__ (-1021)
  59
+#  undef  __DBL_MAX_EXP__
  60
+#  define __DBL_MAX_EXP__ (1024)
  61
+#endif
  62
+
  63
+/****************************************************************************
  64
+ * Private Functions
  65
+ ****************************************************************************/
  66
+
  67
+static inline int is_real(double x)
  68
+{
  69
+  const double infinite = 1.0/0.0;
  70
+  return (x < infinite) && (x >= -infinite);
  71
+}
  72
+
  73
+/****************************************************************************
  74
+ * Public Functions
  75
+ ****************************************************************************/
  76
+
  77
+/***************************************************(************************
  78
+ * Name: strtod
  79
+ *
  80
+ * Description:
  81
+ *   Convert a string to a double value
  82
+ *
  83
+ ****************************************************************************/
  84
+
  85
+double strtod(const char *str, char **endptr)
  86
+{
  87
+  double number;
  88
+  int exponent;
  89
+  int negative;
  90
+  char *p = (char *) str;
  91
+  double p10;
  92
+  int n;
  93
+  int num_digits;
  94
+  int num_decimals;
  95
+  const double infinite = 1.0/0.0;
  96
+
  97
+  /* Skip leading whitespace */
  98
+
  99
+  while (isspace(*p))
  100
+    {
  101
+      p++;
  102
+    }
  103
+
  104
+  /* Handle optional sign */
  105
+
  106
+  negative = 0;
  107
+  switch (*p)
  108
+    {
  109
+    case '-':
  110
+      negative = 1; /* Fall through to increment position */
  111
+    case '+':
  112
+      p++;
  113
+    }
  114
+
  115
+  number       = 0.;
  116
+  exponent     = 0;
  117
+  num_digits   = 0;
  118
+  num_decimals = 0;
  119
+
  120
+  /* Process string of digits */
  121
+
  122
+  while (isdigit(*p))
  123
+    {
  124
+      number = number * 10. + (*p - '0');
  125
+      p++;
  126
+      num_digits++;
  127
+    }
  128
+
  129
+  /* Process decimal part */
  130
+
  131
+  if (*p == '.')
  132
+    {
  133
+      p++;
  134
+
  135
+      while (isdigit(*p))
  136
+      {
  137
+        number = number * 10. + (*p - '0');
  138
+        p++;
  139
+        num_digits++;
  140
+        num_decimals++;
  141
+      }
  142
+
  143
+      exponent -= num_decimals;
  144
+    }
  145
+
  146
+  if (num_digits == 0)
  147
+    {
  148
+      errno = ERANGE;
  149
+      return 0.0;
  150
+    }
  151
+
  152
+  /* Correct for sign */
  153
+
  154
+  if (negative)
  155
+    {
  156
+      number = -number;
  157
+    }
  158
+
  159
+  /* Process an exponent string */
  160
+
  161
+  if (*p == 'e' || *p == 'E')
  162
+    {
  163
+      /* Handle optional sign */
  164
+
  165
+      negative = 0;
  166
+      switch(*++p)
  167
+        {
  168
+        case '-':
  169
+          negative = 1;   /* Fall through to increment pos */
  170
+        case '+':
  171
+          p++;
  172
+        }
  173
+
  174
+      /* Process string of digits */
  175
+
  176
+      n = 0;
  177
+      while (isdigit(*p))
  178
+        {
  179
+          n = n * 10 + (*p - '0');
  180
+          p++;
  181
+        }
  182
+
  183
+      if (negative)
  184
+        {
  185
+          exponent -= n;
  186
+        }
  187
+      else
  188
+        {
  189
+          exponent += n;
  190
+        }
  191
+    }
  192
+
  193
+  if (exponent < __DBL_MIN_EXP__ ||
  194
+      exponent > __DBL_MAX_EXP__)
  195
+    {
  196
+      errno = ERANGE;
  197
+      return infinite;
  198
+    }
  199
+
  200
+  /* Scale the result */
  201
+
  202
+  p10 = 10.;
  203
+  n = exponent;
  204
+  if (n < 0) n = -n;
  205
+  while (n)
  206
+    {
  207
+      if (n & 1)
  208
+        {
  209
+          if (exponent < 0)
  210
+            {
  211
+              number /= p10;
  212
+            }
  213
+          else
  214
+            {
  215
+              number *= p10;
  216
+            }
  217
+        }
  218
+      n >>= 1;
  219
+      p10 *= p10;
  220
+    }
  221
+
  222
+  if (!is_real(number))
  223
+    {
  224
+      errno = ERANGE;
  225
+    }
  226
+
  227
+  if (endptr)
  228
+    {
  229
+      *endptr = p;
  230
+    }
  231
+
  232
+  return number;
  233
+}
  234
+

No commit comments for this range

Something went wrong with that request. Please try again.