kern

view src/klibc/stdlib.c @ 86:379332fc1667

implementing ata read/write
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 07 Dec 2011 14:03:11 +0200
parents
children f83f50c17c3b
line source
1 #include <stdlib.h>
2 #include <ctype.h>
4 int atoi(const char *str)
5 {
6 return atol(str);
7 }
9 long atol(const char *str)
10 {
11 long acc = 0;
12 int sign = 1;
14 while(isspace(*str)) str++;
16 if(*str == '+') {
17 str++;
18 } else if(*str == '-') {
19 sign = -1;
20 str++;
21 }
23 while(*str && isdigit(*str)) {
24 acc = acc * 10 + (*str - '0');
25 str++;
26 }
28 return sign > 0 ? acc : -acc;
29 }
31 void itoa(int val, char *buf, int base)
32 {
33 static char rbuf[16];
34 char *ptr = rbuf;
35 int neg = 0;
37 if(val < 0) {
38 neg = 1;
39 val = -val;
40 }
42 if(val == 0) {
43 *ptr++ = '0';
44 }
46 while(val) {
47 int digit = val % base;
48 *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a');
49 val /= base;
50 }
52 if(neg) {
53 *ptr++ = '-';
54 }
56 ptr--;
58 while(ptr >= rbuf) {
59 *buf++ = *ptr--;
60 }
61 *buf = 0;
62 }
64 void utoa(unsigned int val, char *buf, int base)
65 {
66 static char rbuf[16];
67 char *ptr = rbuf;
69 if(val == 0) {
70 *ptr++ = '0';
71 }
73 while(val) {
74 unsigned int digit = val % base;
75 *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a');
76 val /= base;
77 }
79 ptr--;
81 while(ptr >= rbuf) {
82 *buf++ = *ptr--;
83 }
84 *buf = 0;
85 }