Browse Source

[libc] Allow strtoul() to interpret negative numbers

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 years ago
parent
commit
1d33649516
4 changed files with 25 additions and 4 deletions
  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 View File

27
 #include <stddef.h>
27
 #include <stddef.h>
28
 #include <stdint.h>
28
 #include <stdint.h>
29
 #include <stdlib.h>
29
 #include <stdlib.h>
30
+#include <ctype.h>
30
 #include <errno.h>
31
 #include <errno.h>
31
 #include <assert.h>
32
 #include <assert.h>
32
 #include <ipxe/init.h>
33
 #include <ipxe/init.h>

+ 12
- 0
src/core/misc.c View File

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

+ 12
- 0
src/core/strtoull.c View File

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

+ 0
- 4
src/include/stdlib.h View File

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

Loading…
Cancel
Save