kern

annotate src/klibc/stdlib.c @ 93:083849df660b

split the system call implementations out of fs.c into fs_sys.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 11 Dec 2011 10:17:58 +0200
parents 7e40f14617ed
children
rev   line source
nuclear@6 1 #include <stdlib.h>
nuclear@6 2 #include <ctype.h>
nuclear@6 3
nuclear@6 4 int atoi(const char *str)
nuclear@6 5 {
nuclear@91 6 return strtol(str, 0, 10);
nuclear@6 7 }
nuclear@6 8
nuclear@6 9 long atol(const char *str)
nuclear@6 10 {
nuclear@91 11 return strtol(str, 0, 10);
nuclear@91 12 }
nuclear@91 13
nuclear@91 14 long strtol(const char *str, char **endp, int base)
nuclear@91 15 {
nuclear@6 16 long acc = 0;
nuclear@6 17 int sign = 1;
nuclear@6 18
nuclear@6 19 while(isspace(*str)) str++;
nuclear@6 20
nuclear@91 21 if(base == 0) {
nuclear@91 22 if(str[0] == '0') {
nuclear@91 23 if(str[1] == 'x' || str[1] == 'X') {
nuclear@91 24 base = 16;
nuclear@91 25 } else {
nuclear@91 26 base = 8;
nuclear@91 27 }
nuclear@91 28 } else {
nuclear@91 29 base = 10;
nuclear@91 30 }
nuclear@91 31 }
nuclear@91 32
nuclear@6 33 if(*str == '+') {
nuclear@6 34 str++;
nuclear@6 35 } else if(*str == '-') {
nuclear@6 36 sign = -1;
nuclear@6 37 str++;
nuclear@6 38 }
nuclear@6 39
nuclear@91 40 while(*str) {
nuclear@91 41 long val;
nuclear@91 42 char c = tolower(*str);
nuclear@91 43
nuclear@91 44 if(isdigit(c)) {
nuclear@91 45 val = *str - '0';
nuclear@91 46 } else if(c >= 'a' || c <= 'f') {
nuclear@91 47 val = 10 + c - 'a';
nuclear@91 48 }
nuclear@91 49 if(val >= base) {
nuclear@91 50 break;
nuclear@91 51 }
nuclear@91 52
nuclear@91 53 acc = acc * base + val;
nuclear@6 54 str++;
nuclear@6 55 }
nuclear@6 56
nuclear@91 57 if(endp) {
nuclear@91 58 *endp = (char*)str;
nuclear@91 59 }
nuclear@91 60
nuclear@6 61 return sign > 0 ? acc : -acc;
nuclear@6 62 }
nuclear@6 63
nuclear@6 64 void itoa(int val, char *buf, int base)
nuclear@6 65 {
nuclear@6 66 static char rbuf[16];
nuclear@6 67 char *ptr = rbuf;
nuclear@6 68 int neg = 0;
nuclear@6 69
nuclear@6 70 if(val < 0) {
nuclear@6 71 neg = 1;
nuclear@6 72 val = -val;
nuclear@6 73 }
nuclear@6 74
nuclear@6 75 if(val == 0) {
nuclear@6 76 *ptr++ = '0';
nuclear@6 77 }
nuclear@6 78
nuclear@6 79 while(val) {
nuclear@6 80 int digit = val % base;
nuclear@6 81 *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a');
nuclear@6 82 val /= base;
nuclear@6 83 }
nuclear@6 84
nuclear@6 85 if(neg) {
nuclear@6 86 *ptr++ = '-';
nuclear@6 87 }
nuclear@6 88
nuclear@6 89 ptr--;
nuclear@6 90
nuclear@6 91 while(ptr >= rbuf) {
nuclear@6 92 *buf++ = *ptr--;
nuclear@6 93 }
nuclear@6 94 *buf = 0;
nuclear@6 95 }
nuclear@6 96
nuclear@6 97 void utoa(unsigned int val, char *buf, int base)
nuclear@6 98 {
nuclear@6 99 static char rbuf[16];
nuclear@6 100 char *ptr = rbuf;
nuclear@6 101
nuclear@6 102 if(val == 0) {
nuclear@6 103 *ptr++ = '0';
nuclear@6 104 }
nuclear@6 105
nuclear@6 106 while(val) {
nuclear@6 107 unsigned int digit = val % base;
nuclear@6 108 *ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a');
nuclear@6 109 val /= base;
nuclear@6 110 }
nuclear@6 111
nuclear@6 112 ptr--;
nuclear@6 113
nuclear@6 114 while(ptr >= rbuf) {
nuclear@6 115 *buf++ = *ptr--;
nuclear@6 116 }
nuclear@6 117 *buf = 0;
nuclear@6 118 }
nuclear@6 119