瀏覽代碼

[libc] Allow strtoul() to interpret negative numbers

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 年之前
父節點
當前提交
1d33649516
共有 4 個檔案被更改,包括 25 行新增4 行删除
  1. 1
    0
      src/arch/i386/core/runtime.c
  2. 12
    0
      src/core/misc.c
  3. 12
    0
      src/core/strtoull.c
  4. 0
    4
      src/include/stdlib.h

+ 1
- 0
src/arch/i386/core/runtime.c 查看文件

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 #include <stddef.h>
28 28
 #include <stdint.h>
29 29
 #include <stdlib.h>
30
+#include <ctype.h>
30 31
 #include <errno.h>
31 32
 #include <assert.h>
32 33
 #include <ipxe/init.h>

+ 12
- 0
src/core/misc.c 查看文件

@@ -35,8 +35,17 @@ int inet_aton ( const char *cp, struct in_addr *inp ) {
35 35
 
36 36
 unsigned long strtoul ( const char *p, char **endp, int base ) {
37 37
 	unsigned long ret = 0;
38
+	int negative = 0;
38 39
 	unsigned int charval;
39 40
 
41
+	while ( isspace ( *p ) )
42
+		p++;
43
+
44
+	if ( *p == '-' ) {
45
+		negative = 1;
46
+		p++;
47
+	}
48
+
40 49
 	base = strtoul_base ( &p, base );
41 50
 
42 51
 	while ( 1 ) {
@@ -47,6 +56,9 @@ unsigned long strtoul ( const char *p, char **endp, int base ) {
47 56
 		p++;
48 57
 	}
49 58
 
59
+	if ( negative )
60
+		ret = -ret;
61
+
50 62
 	if ( endp )
51 63
 		*endp = ( char * ) p;
52 64
 

+ 12
- 0
src/core/strtoull.c 查看文件

@@ -29,8 +29,17 @@ FILE_LICENCE ( GPL2_OR_LATER );
29 29
  */
30 30
 unsigned long long strtoull ( const char *p, char **endp, int base ) {
31 31
 	unsigned long long ret = 0;
32
+	int negative = 0;
32 33
 	unsigned int charval;
33 34
 
35
+	while ( isspace ( *p ) )
36
+		p++;
37
+
38
+	if ( *p == '-' ) {
39
+		negative = 1;
40
+		p++;
41
+	}
42
+
34 43
 	base = strtoul_base ( &p, base );
35 44
 
36 45
 	while ( 1 ) {
@@ -41,6 +50,9 @@ unsigned long long strtoull ( const char *p, char **endp, int base ) {
41 50
 		p++;
42 51
 	}
43 52
 
53
+	if ( negative )
54
+		ret = -ret;
55
+
44 56
 	if ( endp )
45 57
 		*endp = ( char * ) p;
46 58
 

+ 0
- 4
src/include/stdlib.h 查看文件

@@ -5,7 +5,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
5 5
 
6 6
 #include <stdint.h>
7 7
 #include <assert.h>
8
-#include <ctype.h>
9 8
 
10 9
 /*****************************************************************************
11 10
  *
@@ -18,9 +17,6 @@ static inline int strtoul_base ( const char **pp, int base )
18 17
 {
19 18
 	const char *p = *pp;
20 19
 
21
-	while ( isspace ( *p ) )
22
-		p++;
23
-
24 20
 	if ( base == 0 ) {
25 21
 		base = 10;
26 22
 		if ( *p == '0' ) {

Loading…
取消
儲存