Sfoglia il codice sorgente

[libc] Allow strtoul() to interpret negative numbers

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 anni fa
parent
commit
1d33649516
4 ha cambiato i file con 25 aggiunte e 4 eliminazioni
  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 Vedi File

@@ -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 Vedi File

@@ -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 Vedi File

@@ -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 Vedi File

@@ -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…
Annulla
Salva