kern

changeset 91:f83f50c17c3b

continuing with the fs added strtol and strstr to klibc
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 15:29:54 +0200
parents 7ff2b4971216
children b4264d173aae 083849df660b
files src/bdev.c src/bdev.h src/fs.h src/klibc/stdlib.c src/klibc/stdlib.h src/klibc/string.c src/klibc/string.h
diffstat 7 files changed, 96 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/src/bdev.c	Fri Dec 09 13:44:15 2011 +0200
     1.2 +++ b/src/bdev.c	Fri Dec 09 15:29:54 2011 +0200
     1.3 @@ -1,11 +1,13 @@
     1.4  #include <stdlib.h>
     1.5 +#include <string.h>
     1.6  #include <assert.h>
     1.7  #include "bdev.h"
     1.8  #include "ata.h"
     1.9  #include "part.h"
    1.10  
    1.11 -#define MINOR_DISK(x)	(((x) >> 4) & 0xf)
    1.12 -#define MINOR_PART(x)	((x) & 0xf)
    1.13 +#define MKMINOR(disk, part)	((((disk) & 0xf) << 4) | ((part) & 0xf))
    1.14 +#define MINOR_DISK(x)		(((x) >> 4) & 0xf)
    1.15 +#define MINOR_PART(x)		((x) & 0xf)
    1.16  
    1.17  struct block_device *blk_open(dev_t dev)
    1.18  {
    1.19 @@ -84,3 +86,35 @@
    1.20  	}
    1.21  	return 0;
    1.22  }
    1.23 +
    1.24 +dev_t bdev_by_name(const char *name)
    1.25 +{
    1.26 +	int minor;
    1.27 +	int atadev, part = 0;
    1.28 +
    1.29 +	char *tmp = strrchr(name, '/');
    1.30 +	if(tmp) {
    1.31 +		name = tmp + 1;
    1.32 +	}
    1.33 +
    1.34 +	if(strstr(name, "ata") != name) {
    1.35 +		return 0;
    1.36 +	}
    1.37 +	name += 3;
    1.38 +
    1.39 +	atadev = strtol(name, &tmp, 10);
    1.40 +	if(tmp == name) {
    1.41 +		return 0;
    1.42 +	}
    1.43 +	name = tmp;
    1.44 +
    1.45 +	if(*name++ == 'p') {
    1.46 +		part = strtol(name, &tmp, 10) + 1;
    1.47 +		if(tmp == name) {
    1.48 +			return 0;
    1.49 +		}
    1.50 +	}
    1.51 +
    1.52 +	minor = MKMINOR(atadev, part);
    1.53 +	return DEVNO(0, minor);
    1.54 +}
     2.1 --- a/src/bdev.h	Fri Dec 09 13:44:15 2011 +0200
     2.2 +++ b/src/bdev.h	Fri Dec 09 15:29:54 2011 +0200
     2.3 @@ -16,4 +16,6 @@
     2.4  int blk_read(struct block_device *bdev, uint32_t blk, void *buf);
     2.5  int blk_write(struct block_device *bdev, uint32_t blk, void *buf);
     2.6  
     2.7 +dev_t bdev_by_name(const char *name);
     2.8 +
     2.9  #endif	/* BDEV_H_ */
     3.1 --- a/src/fs.h	Fri Dec 09 13:44:15 2011 +0200
     3.2 +++ b/src/fs.h	Fri Dec 09 15:29:54 2011 +0200
     3.3 @@ -8,6 +8,7 @@
     3.4  
     3.5  #define SECT_TO_BLK(x)	((x) / (BLKSZ / 512))
     3.6  
     3.7 +#define DEVNO(maj, min)	((((maj) & 0xff) << 8) | ((min) & 0xff))
     3.8  #define DEV_MAJOR(dev)	(((dev) >> 8) & 0xff)
     3.9  #define DEV_MINOR(dev)	((dev) & 0xff)
    3.10  
     4.1 --- a/src/klibc/stdlib.c	Fri Dec 09 13:44:15 2011 +0200
     4.2 +++ b/src/klibc/stdlib.c	Fri Dec 09 15:29:54 2011 +0200
     4.3 @@ -3,16 +3,33 @@
     4.4  
     4.5  int atoi(const char *str)
     4.6  {
     4.7 -	return atol(str);
     4.8 +	return strtol(str, 0, 10);
     4.9  }
    4.10  
    4.11  long atol(const char *str)
    4.12  {
    4.13 +	return strtol(str, 0, 10);
    4.14 +}
    4.15 +
    4.16 +long strtol(const char *str, char **endp, int base)
    4.17 +{
    4.18  	long acc = 0;
    4.19  	int sign = 1;
    4.20  
    4.21  	while(isspace(*str)) str++;
    4.22  
    4.23 +	if(base == 0) {
    4.24 +		if(str[0] == '0') {
    4.25 +			if(str[1] == 'x' || str[1] == 'X') {
    4.26 +				base = 16;
    4.27 +			} else {
    4.28 +				base = 8;
    4.29 +			}
    4.30 +		} else {
    4.31 +			base = 10;
    4.32 +		}
    4.33 +	}
    4.34 +
    4.35  	if(*str == '+') {
    4.36  		str++;
    4.37  	} else if(*str == '-') {
    4.38 @@ -20,11 +37,27 @@
    4.39  		str++;
    4.40  	}
    4.41  
    4.42 -	while(*str && isdigit(*str)) {
    4.43 -		acc = acc * 10 + (*str - '0');
    4.44 +	while(*str) {
    4.45 +		long val;
    4.46 +		char c = tolower(*str);
    4.47 +
    4.48 +		if(isdigit(c)) {
    4.49 +			val = *str - '0';
    4.50 +		} else if(c >= 'a' || c <= 'f') {
    4.51 +			val = 10 + c - 'a';
    4.52 +		}
    4.53 +		if(val >= base) {
    4.54 +			break;
    4.55 +		}
    4.56 +
    4.57 +		acc = acc * base + val;
    4.58  		str++;
    4.59  	}
    4.60  
    4.61 +	if(endp) {
    4.62 +		*endp = (char*)str;
    4.63 +	}
    4.64 +
    4.65  	return sign > 0 ? acc : -acc;
    4.66  }
    4.67  
     5.1 --- a/src/klibc/stdlib.h	Fri Dec 09 13:44:15 2011 +0200
     5.2 +++ b/src/klibc/stdlib.h	Fri Dec 09 15:29:54 2011 +0200
     5.3 @@ -8,6 +8,7 @@
     5.4  
     5.5  int atoi(const char *str);
     5.6  long atol(const char *str);
     5.7 +long strtol(const char *str, char **endp, int base);
     5.8  
     5.9  void itoa(int val, char *buf, int base);
    5.10  void utoa(unsigned int val, char *buf, int base);
     6.1 --- a/src/klibc/string.c	Fri Dec 09 13:44:15 2011 +0200
     6.2 +++ b/src/klibc/string.c	Fri Dec 09 15:29:54 2011 +0200
     6.3 @@ -89,6 +89,24 @@
     6.4  	return 0;
     6.5  }
     6.6  
     6.7 +char *strstr(const char *str, const char *substr)
     6.8 +{
     6.9 +	while(*str) {
    6.10 +		const char *s1 = str;
    6.11 +		const char *s2 = substr;
    6.12 +
    6.13 +		while(*s1 && *s1 == *s2) {
    6.14 +			s1++;
    6.15 +			s2++;
    6.16 +		}
    6.17 +		if(!*s2) {
    6.18 +			return (char*)str;
    6.19 +		}
    6.20 +		str++;
    6.21 +	}
    6.22 +	return 0;
    6.23 +}
    6.24 +
    6.25  int strcmp(const char *s1, const char *s2)
    6.26  {
    6.27  	while(*s1 && *s1 == *s2) {
     7.1 --- a/src/klibc/string.h	Fri Dec 09 13:44:15 2011 +0200
     7.2 +++ b/src/klibc/string.h	Fri Dec 09 15:29:54 2011 +0200
     7.3 @@ -14,6 +14,8 @@
     7.4  char *strchr(const char *s, int c);
     7.5  char *strrchr(const char *s, int c);
     7.6  
     7.7 +char *strstr(const char *str, const char *substr);
     7.8 +
     7.9  int strcmp(const char *s1, const char *s2);
    7.10  
    7.11  #endif	/* STRING_H_ */