kern

changeset 6:7e40f14617ed

forgot to add stdarg.h and stdlib.c
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 13 Jan 2011 20:24:10 +0200
parents 633e35c64772
children 611b2d66420b
files src/klibc/stdarg.h src/klibc/stdlib.c
diffstat 2 files changed, 98 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/klibc/stdarg.h	Thu Jan 13 20:24:10 2011 +0200
     1.3 @@ -0,0 +1,12 @@
     1.4 +#ifndef STDARG_H_
     1.5 +#define STDARG_H_
     1.6 +
     1.7 +/* Assumes that arguments are passed on the stack 4-byte aligned */
     1.8 +
     1.9 +typedef int* va_list;
    1.10 +
    1.11 +#define va_start(ap, last)	((ap) = (int*)&(last) + 1)
    1.12 +#define va_arg(ap, type)	(*(type*)(ap)++)
    1.13 +#define va_end(ap)
    1.14 +
    1.15 +#endif	/* STDARG_H_ */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/klibc/stdlib.c	Thu Jan 13 20:24:10 2011 +0200
     2.3 @@ -0,0 +1,86 @@
     2.4 +#include <stdlib.h>
     2.5 +#include <ctype.h>
     2.6 +
     2.7 +int atoi(const char *str)
     2.8 +{
     2.9 +	return atol(str);
    2.10 +}
    2.11 +
    2.12 +long atol(const char *str)
    2.13 +{
    2.14 +	long acc = 0;
    2.15 +	int sign = 1;
    2.16 +
    2.17 +	while(isspace(*str)) str++;
    2.18 +
    2.19 +	if(*str == '+') {
    2.20 +		str++;
    2.21 +	} else if(*str == '-') {
    2.22 +		sign = -1;
    2.23 +		str++;
    2.24 +	}
    2.25 +
    2.26 +	while(*str && isdigit(*str)) {
    2.27 +		acc = acc * 10 + (*str - '0');
    2.28 +		str++;
    2.29 +	}
    2.30 +
    2.31 +	return sign > 0 ? acc : -acc;
    2.32 +}
    2.33 +
    2.34 +void itoa(int val, char *buf, int base)
    2.35 +{
    2.36 +	static char rbuf[16];
    2.37 +	char *ptr = rbuf;
    2.38 +	int neg = 0;
    2.39 +
    2.40 +	if(val < 0) {
    2.41 +		neg = 1;
    2.42 +		val = -val;
    2.43 +	}
    2.44 +
    2.45 +	if(val == 0) {
    2.46 +		*ptr++ = '0';
    2.47 +	}
    2.48 +
    2.49 +	while(val) {
    2.50 +		int digit = val % base;
    2.51 +		*ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a');
    2.52 +		val /= base;
    2.53 +	}
    2.54 +
    2.55 +	if(neg) {
    2.56 +		*ptr++ = '-';
    2.57 +	}
    2.58 +
    2.59 +	ptr--;
    2.60 +
    2.61 +	while(ptr >= rbuf) {
    2.62 +		*buf++ = *ptr--;
    2.63 +	}
    2.64 +	*buf = 0;
    2.65 +}
    2.66 +
    2.67 +void utoa(unsigned int val, char *buf, int base)
    2.68 +{
    2.69 +	static char rbuf[16];
    2.70 +	char *ptr = rbuf;
    2.71 +
    2.72 +	if(val == 0) {
    2.73 +		*ptr++ = '0';
    2.74 +	}
    2.75 +
    2.76 +	while(val) {
    2.77 +		unsigned int digit = val % base;
    2.78 +		*ptr++ = digit < 10 ? (digit + '0') : (digit - 10 + 'a');
    2.79 +		val /= base;
    2.80 +	}
    2.81 +
    2.82 +	ptr--;
    2.83 +
    2.84 +	while(ptr >= rbuf) {
    2.85 +		*buf++ = *ptr--;
    2.86 +	}
    2.87 +	*buf = 0;
    2.88 +}
    2.89 +