From: khamidou Date: Thu, 8 Jan 2009 20:15:21 +0000 (+0100) Subject: first commit X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=1b925148eedd181abd592e40e82ff703a45a5634;p=minilibc.git first commit --- 1b925148eedd181abd592e40e82ff703a45a5634 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e80ec67 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +OBJS=string.o +CC=gcc +CFLAGS=-c +minilibc: $(OBJS) + ar rcs libminic.a $(OBJS) + ranlib libminic.a + cp libminic.a tests + +string.o: string.c string.h + $(CC) $(CFLAGS) string.c + +tests: minilibc + make -f tests/Makefile + \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..4017e0d --- /dev/null +++ b/README @@ -0,0 +1 @@ +Minilibc is a simple C implementation. For the moment it doesn't implement memory allocation, therefore it requires a working malloc() implementation. diff --git a/ctype.h b/ctype.h new file mode 100644 index 0000000..128c15a --- /dev/null +++ b/ctype.h @@ -0,0 +1,7 @@ +#ifndef __CTYPE_H__ +#define __CTYPE_H__ + +#define toupper(c) (c >= 97 && c <= 122) ? (c - 32) : c +#define tolower(c) (c >= 65 && c <= 90) ? (c + 32) : c + +#endif diff --git a/libminic.a b/libminic.a new file mode 100644 index 0000000..db66667 Binary files /dev/null and b/libminic.a differ diff --git a/stddef.h b/stddef.h new file mode 100644 index 0000000..09b17e8 --- /dev/null +++ b/stddef.h @@ -0,0 +1,10 @@ +#ifndef __STDDEF_H__ +#define __STDDEF_H__ + +#define max(a,b) (a > b) ? a : b + +#define NULL 0 + +typedef long size_t; + +#endif diff --git a/stdio.h b/stdio.h new file mode 100644 index 0000000..1b1ad95 --- /dev/null +++ b/stdio.h @@ -0,0 +1,6 @@ +#ifndef __STDIO_H__ +#define __STDIO_H__ + +#define NULL 0 + +#endif diff --git a/stdlib.c b/stdlib.c new file mode 100644 index 0000000..114044f --- /dev/null +++ b/stdlib.c @@ -0,0 +1,13 @@ +#include "stdlib.h" + +static int *malloc_base = 0x804a000; /* It seems to be the value choosen by glibc on ia32 */ + +void *malloc(size_t size) +{ + size_t retval = malloc_base; + malloc_base += size; + if (brk(malloc_base) == -1) + return NULL; + + return (void *) retval; +} diff --git a/stdlib.h b/stdlib.h new file mode 100644 index 0000000..546d9b1 --- /dev/null +++ b/stdlib.h @@ -0,0 +1,8 @@ +#ifndef __STDLIB_H__ +#define __STDLIB_H__ + +#include "stddef.h" + +void *malloc(size_t size); + +#endif diff --git a/string.c b/string.c new file mode 100644 index 0000000..88a9508 --- /dev/null +++ b/string.c @@ -0,0 +1,122 @@ +/* + Note : most of theses routines assume that strings end with '\0' +*/ + +#include "string.h" + +size_t strlen(char *str) +{ + int i = 0; + for (i = 0; *(str + i) != '\0'; i++) + ; + + return i; +} + + +void *memcpy(char *dest, char *src, size_t n) +{ + char *ptr = dest; + int i = 0; + for(i = 0; i < n; i++) + *dest++ = *src++; + return (void *) ptr; +} + + +char *strcat(char *dest, char *src) +{ + memcpy(dest + strlen(dest) - 1, src, strlen(src)); + return dest; +} + +char *strncat(char * dest, char *src, size_t n) +{ + memcpy(dest + max(n, strlen(src)) - 1, src, max(n, strlen(src))); + return dest; +} + +size_t strcmp(char *s1, char *s2) +{ + size_t ret = 0; + while(*s1 != '\0' && *s2 != '\0') + ret = (*s1++ - *s2++); + + return ret; +} + +size_t strncmp(char *s1, char *s2, size_t n) +{ + size_t i = 0; + size_t ret = 0; + for (i = 0; i < n && *s1 != '\0' && *s2 != '\0'; i++) + ret = (*s1++ - *s2++); + + return ret; +} + +char *strstr(char *haystack, char *needle) +{ + int i; + + for (i = 0; i < max(strlen(needle),strlen(haystack)); i++) { + /* Skip the first elements of the string that are not in needle */ + for(; *haystack != *needle; haystack++); + ptr = haystack; + if (strlen(haystack) < strlen(needle)) + return NULL; + + if (strncmp(needle, haystack, strlen(needle)) == 0) + return haystack; + } +} + +void swap(void *a, void *b) +{ + void *c = b; + b = a; + a = c; +} + +char *strrev(char *s) +{ + int i = 0; + for (i = 0; i < strlen(s); i++) + swap(s + i, s + strlen(s) - i); +} + +char *strchr(char *s, int c) +{ + size_t i = 0; + for (i = 0; i < strlen(s); i++) + if (*(s + i) == c) + return (s + i); + return NULL; +} + +char *strtok(char *str, const char *delim) +{ + static char **strtok_arr; + static int strtok_called; /* We have to know if strtok was previously called with this string or not */ + if (str != NULL) { + strtok_arr = NULL; + strtok_called = 1; + } + if (strtok_called != 1 && str == NULL) { + return NULL; /* This is the first time the user calls strtok and he calls it with a NULL argument*/ + } + + int occur = 0; + while(strstr(str, delim) != NULL) + /* First compute the number of occurences :*/ + occur++; + strtok_arr = malloc(occur * sizeof(char *)); + + if (strtok_arr == NULL) + return NULL; + + char *p = strstr(str, delim);; + while (p != NULL) { + malloc(strlen(p) * sizeof(char)); + } +} diff --git a/string.h b/string.h new file mode 100644 index 0000000..e15f065 --- /dev/null +++ b/string.h @@ -0,0 +1,17 @@ +#ifndef __STRING_H__ +#define __STRING_H__ + +#include "stddef.h" +#include "ctype.h" + +size_t strlen(char *str); +void *memcpy(char *dest, char *src, size_t n); +char *strcat(char *dest, char *src); +char *strncat(char * dest, char *src, size_t n); +size_t strcmp(char *s1, char *s2); +size_t strncmp(char *s1, char *s2, size_t n); +char *strstr(char *haystack, char *needle); +char *strrev(char *s); + + +#endif diff --git a/strings.c b/strings.c new file mode 100644 index 0000000..5763084 --- /dev/null +++ b/strings.c @@ -0,0 +1,39 @@ +#include "strings.h" + +size_t strcasecmp(char *s1, char *s2) +{ + size_t ret = 0; + while(*s1 != '\0' && *s2 != '\0') + ret = (toupper(*s1++) - toupper(*s2++)); + + return ret; +} + +size_t strncasecmp(char *s1, char *s2, size_t n) +{ + size_t i = 0; + for (i = 0; i < n && *s1 != '\0' && *s2 != '\0'; i++) + ret = (toupper(*s1++) - toupper(*s2++)); + + return ret; +} + +char *index(const char *s, int c) +{ + int i; + for (i = 0; i < strlen(s) && s[i] != c; i++) + ; + + return (s + i); +} + +char *rindex(const char *s, int c) +{ + int i; + for (i = strlen(s); i > 0 && s[i] != c; i--) + ; + + return (s + i); +} + + diff --git a/strings.h b/strings.h new file mode 100644 index 0000000..6e962ed --- /dev/null +++ b/strings.h @@ -0,0 +1,9 @@ +#ifndef __STRINGS_H__ +#define __STRINGS_H__ + +size_t strcasecmp(char *s1, char *s2); +size_t strncasecmp(char *s1, char *s2, size_t n); +char *index(const char *s, int c); +char *rindex(const char *s, int c); + +#endif diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..920043f --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,5 @@ +CC=gcc +CFLAGS=-c + +tests: + $(CC) -o tests/test_string tests/test_string.c \ No newline at end of file diff --git a/tests/libminic.a b/tests/libminic.a new file mode 100644 index 0000000..db66667 Binary files /dev/null and b/tests/libminic.a differ diff --git a/tests/test_string b/tests/test_string new file mode 100755 index 0000000..43b7254 Binary files /dev/null and b/tests/test_string differ diff --git a/tests/test_string.c b/tests/test_string.c new file mode 100644 index 0000000..8b4e8c6 --- /dev/null +++ b/tests/test_string.c @@ -0,0 +1,16 @@ +/* + Simple unit test for the functions defined by string.h +*/ + +#include "string.h" +#include + + +main(void) +{ + if (strcmp("cadabra", "cadabra") == 0) + puts("cadabra = cadabra"); + + + +} diff --git a/tests/test_string.c~ b/tests/test_string.c~ new file mode 100644 index 0000000..1733296 --- /dev/null +++ b/tests/test_string.c~ @@ -0,0 +1,3 @@ +#include "string.h" +#include +