9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit f7bdb10cfc2b035793b190e4824d084187815015
parent f290085ada3f7ae930a9cdc1242bbb757cfb88e2
Author: Roberto Vargas <roberto.vargas@arm.com>
Date:   Thu,  1 Nov 2018 10:00:20 +0000

[libc] Split the files based in the header

There were too many files in a simple directory. With
this new organization it is simpler to locate some of the files.

Diffstat:
Asrc/libc/.gitignore | 1+
Msrc/libc/Makefile | 137++++++++++---------------------------------------------------------------------
Dsrc/libc/__getc.c | 46----------------------------------------------
Dsrc/libc/_daysyear.c | 30------------------------------
Dsrc/libc/_flsbuf.c | 21---------------------
Dsrc/libc/_fpopen.c | 75---------------------------------------------------------------------------
Dsrc/libc/arch/amd64/bsd/objlst.mk | 12------------
Dsrc/libc/arch/amd64/linux/objlst.mk | 14--------------
Dsrc/libc/arch/arm64/linux/objlst.mk | 14--------------
Dsrc/libc/arch/arm64/none/objlst.mk | 6------
Asrc/libc/assert/Makefile | 9+++++++++
Rsrc/libc/__assert.c -> src/libc/assert/__assert.c | 0
Asrc/libc/assert/assert.c | 13+++++++++++++
Asrc/libc/ctype/Makefile | 23+++++++++++++++++++++++
Rsrc/libc/ctype.c -> src/libc/ctype/ctype.c | 0
Rsrc/libc/isalnum.c -> src/libc/ctype/isalnum.c | 0
Rsrc/libc/isalpha.c -> src/libc/ctype/isalpha.c | 0
Rsrc/libc/isascii.c -> src/libc/ctype/isascii.c | 0
Rsrc/libc/isblank.c -> src/libc/ctype/isblank.c | 0
Rsrc/libc/iscntrl.c -> src/libc/ctype/iscntrl.c | 0
Rsrc/libc/isdigit.c -> src/libc/ctype/isdigit.c | 0
Rsrc/libc/isgraph.c -> src/libc/ctype/isgraph.c | 0
Rsrc/libc/islower.c -> src/libc/ctype/islower.c | 0
Rsrc/libc/isprint.c -> src/libc/ctype/isprint.c | 0
Rsrc/libc/ispunct.c -> src/libc/ctype/ispunct.c | 0
Rsrc/libc/isspace.c -> src/libc/ctype/isspace.c | 0
Rsrc/libc/isupper.c -> src/libc/ctype/isupper.c | 0
Rsrc/libc/isxdigit.c -> src/libc/ctype/isxdigit.c | 0
Rsrc/libc/tolower.c -> src/libc/ctype/tolower.c | 0
Rsrc/libc/toupper.c -> src/libc/ctype/toupper.c | 0
Dsrc/libc/fclose.c | 34----------------------------------
Dsrc/libc/fopen.c | 23-----------------------
Dsrc/libc/freopen.c | 14--------------
Dsrc/libc/fseek.c | 28----------------------------
Dsrc/libc/ftell.c | 27---------------------------
Asrc/libc/locale/Makefile | 9+++++++++
Rsrc/libc/localeconv.c -> src/libc/locale/localeconv.c | 0
Rsrc/libc/setlocale.c -> src/libc/locale/setlocale.c | 0
Dsrc/libc/malloc.c | 158-------------------------------------------------------------------------------
Asrc/libc/mklst | 12++++++++++++
Dsrc/libc/printk.c | 118-------------------------------------------------------------------------------
Asrc/libc/stdio/Makefile | 45+++++++++++++++++++++++++++++++++++++++++++++
Asrc/libc/stdio/__getc.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/libc/__putc.c -> src/libc/stdio/__putc.c | 0
Asrc/libc/stdio/_flsbuf.c | 21+++++++++++++++++++++
Asrc/libc/stdio/_fpopen.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/libc/clearerr.c -> src/libc/stdio/clearerr.c | 0
Asrc/libc/stdio/fclose.c | 34++++++++++++++++++++++++++++++++++
Rsrc/libc/feof.c -> src/libc/stdio/feof.c | 0
Rsrc/libc/ferror.c -> src/libc/stdio/ferror.c | 0
Rsrc/libc/fgetc.c -> src/libc/stdio/fgetc.c | 0
Rsrc/libc/fgets.c -> src/libc/stdio/fgets.c | 0
Asrc/libc/stdio/fopen.c | 23+++++++++++++++++++++++
Rsrc/libc/fprintf.c -> src/libc/stdio/fprintf.c | 0
Rsrc/libc/fputc.c -> src/libc/stdio/fputc.c | 0
Rsrc/libc/fputs.c -> src/libc/stdio/fputs.c | 0
Rsrc/libc/fread.c -> src/libc/stdio/fread.c | 0
Asrc/libc/stdio/freopen.c | 14++++++++++++++
Asrc/libc/stdio/fseek.c | 28++++++++++++++++++++++++++++
Asrc/libc/stdio/ftell.c | 27+++++++++++++++++++++++++++
Rsrc/libc/fwrite.c -> src/libc/stdio/fwrite.c | 0
Rsrc/libc/getc.c -> src/libc/stdio/getc.c | 0
Rsrc/libc/getchar.c -> src/libc/stdio/getchar.c | 0
Rsrc/libc/gets.c -> src/libc/stdio/gets.c | 0
Rsrc/libc/perror.c -> src/libc/stdio/perror.c | 0
Asrc/libc/stdio/print.c | 0
Rsrc/libc/printf.c -> src/libc/stdio/printf.c | 0
Asrc/libc/stdio/printk.c | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/libc/putc.c -> src/libc/stdio/putc.c | 0
Rsrc/libc/putchar.c -> src/libc/stdio/putchar.c | 0
Rsrc/libc/puts.c -> src/libc/stdio/puts.c | 0
Rsrc/libc/rewind.c -> src/libc/stdio/rewind.c | 0
Rsrc/libc/setbuf.c -> src/libc/stdio/setbuf.c | 0
Rsrc/libc/setvbuf.c -> src/libc/stdio/setvbuf.c | 0
Rsrc/libc/snprintf.c -> src/libc/stdio/snprintf.c | 0
Rsrc/libc/sprintf.c -> src/libc/stdio/sprintf.c | 0
Rsrc/libc/stdio.c -> src/libc/stdio/stdio.c | 0
Asrc/libc/stdio/tmpnam.c | 31+++++++++++++++++++++++++++++++
Rsrc/libc/vfprintf.c -> src/libc/stdio/vfprintf.c | 0
Rsrc/libc/vsnprintf.c -> src/libc/stdio/vsnprintf.c | 0
Rsrc/libc/vsprintf.c -> src/libc/stdio/vsprintf.c | 0
Asrc/libc/stdlib/Makefile | 27+++++++++++++++++++++++++++
Rsrc/libc/__abs.c -> src/libc/stdlib/__abs.c | 0
Rsrc/libc/__labs.c -> src/libc/stdlib/__labs.c | 0
Rsrc/libc/__llabs.c -> src/libc/stdlib/__llabs.c | 0
Rsrc/libc/abort.c -> src/libc/stdlib/abort.c | 0
Rsrc/libc/abs.c -> src/libc/stdlib/abs.c | 0
Rsrc/libc/atexit.c -> src/libc/stdlib/atexit.c | 0
Rsrc/libc/atoi.c -> src/libc/stdlib/atoi.c | 0
Rsrc/libc/atol.c -> src/libc/stdlib/atol.c | 0
Rsrc/libc/atoll.c -> src/libc/stdlib/atoll.c | 0
Rsrc/libc/bsearch.c -> src/libc/stdlib/bsearch.c | 0
Rsrc/libc/calloc.c -> src/libc/stdlib/calloc.c | 0
Rsrc/libc/errno.c -> src/libc/stdlib/errno.c | 0
Rsrc/libc/exit.c -> src/libc/stdlib/exit.c | 0
Rsrc/libc/labs.c -> src/libc/stdlib/labs.c | 0
Rsrc/libc/llabs.c -> src/libc/stdlib/llabs.c | 0
Asrc/libc/stdlib/malloc.c | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/libc/malloc.h -> src/libc/stdlib/malloc.h | 0
Rsrc/libc/qsort.c -> src/libc/stdlib/qsort.c | 0
Rsrc/libc/raise.c -> src/libc/stdlib/raise.c | 0
Rsrc/libc/rand.c -> src/libc/stdlib/rand.c | 0
Rsrc/libc/realloc.c -> src/libc/stdlib/realloc.c | 0
Asrc/libc/string/Makefile | 30++++++++++++++++++++++++++++++
Rsrc/libc/memchr.c -> src/libc/string/memchr.c | 0
Rsrc/libc/memcmp.c -> src/libc/string/memcmp.c | 0
Rsrc/libc/memcpy.c -> src/libc/string/memcpy.c | 0
Rsrc/libc/memmove.c -> src/libc/string/memmove.c | 0
Rsrc/libc/memset.c -> src/libc/string/memset.c | 0
Rsrc/libc/strcat.c -> src/libc/string/strcat.c | 0
Rsrc/libc/strchr.c -> src/libc/string/strchr.c | 0
Rsrc/libc/strcmp.c -> src/libc/string/strcmp.c | 0
Rsrc/libc/strcoll.c -> src/libc/string/strcoll.c | 0
Rsrc/libc/strcpy.c -> src/libc/string/strcpy.c | 0
Rsrc/libc/strcspn.c -> src/libc/string/strcspn.c | 0
Rsrc/libc/strerror.c -> src/libc/string/strerror.c | 0
Rsrc/libc/strlen.c -> src/libc/string/strlen.c | 0
Rsrc/libc/strncat.c -> src/libc/string/strncat.c | 0
Rsrc/libc/strncmp.c -> src/libc/string/strncmp.c | 0
Rsrc/libc/strncpy.c -> src/libc/string/strncpy.c | 0
Rsrc/libc/strnlen.c -> src/libc/string/strnlen.c | 0
Rsrc/libc/strpbrk.c -> src/libc/string/strpbrk.c | 0
Rsrc/libc/strrchr.c -> src/libc/string/strrchr.c | 0
Rsrc/libc/strspn.c -> src/libc/string/strspn.c | 0
Rsrc/libc/strstr.c -> src/libc/string/strstr.c | 0
Rsrc/libc/strtok.c -> src/libc/string/strtok.c | 0
Rsrc/libc/strxfrm.c -> src/libc/string/strxfrm.c | 0
Asrc/libc/time/Makefile | 15+++++++++++++++
Asrc/libc/time/_daysyear.c | 30++++++++++++++++++++++++++++++
Rsrc/libc/asctime.c -> src/libc/time/asctime.c | 0
Rsrc/libc/ctime.c -> src/libc/time/ctime.c | 0
Rsrc/libc/difftime.c -> src/libc/time/difftime.c | 0
Rsrc/libc/gmtime.c -> src/libc/time/gmtime.c | 0
Rsrc/libc/localtime.c -> src/libc/time/localtime.c | 0
Rsrc/libc/mktime.c -> src/libc/time/mktime.c | 0
Rsrc/libc/strftime.c -> src/libc/time/strftime.c | 0
Dsrc/libc/tmpnam.c | 31-------------------------------
137 files changed, 806 insertions(+), 771 deletions(-)

diff --git a/src/libc/.gitignore b/src/libc/.gitignore @@ -0,0 +1 @@ +objlst diff --git a/src/libc/Makefile b/src/libc/Makefile @@ -1,131 +1,28 @@ PROJECTDIR =../.. include $(PROJECTDIR)/scripts/rules.mk -include arch/$(ARCH)/$(SYS)/objlst.mk -LIBSYS = arch/$(ARCH)/$(SYS) -LIBARCH = arch/$(ARCH) -MORECFLAGS = -I. -w - -COBJ = __assert.o \ - __getc.o \ - __putc.o \ - _flsbuf.o \ - _fpopen.o \ - abort.o \ - atexit.o \ - calloc.o \ - clearerr.o \ - ctype.o \ - errno.o \ - exit.o \ - fclose.o \ - feof.o \ - ferror.o \ - fopen.o \ - fprintf.o \ - fputc.o \ - fputs.o \ - fread.o \ - fseek.o \ - ftell.o \ - fwrite.o \ - isalnum.o \ - isalpha.o \ - isascii.o \ - isblank.o \ - iscntrl.o \ - isdigit.o \ - isgraph.o \ - islower.o \ - isprint.o \ - ispunct.o \ - isspace.o \ - isupper.o \ - isxdigit.o \ - malloc.o \ - memchr.o \ - memcmp.o \ - memcpy.o \ - memmove.o \ - memset.o \ - printf.o \ - printk.o \ - putc.o \ - putchar.o \ - puts.o \ - raise.o \ - rand.o \ - setbuf.o \ - setvbuf.o \ - snprintf.o \ - sprintf.o \ - stdio.o \ - strcat.o \ - strchr.o \ - strcmp.o \ - strcoll.o \ - strcpy.o \ - strcspn.o \ - strerror.o \ - strlen.o \ - strncat.o \ - strncmp.o \ - strncpy.o \ - strnlen.o \ - strpbrk.o \ - strrchr.o \ - strspn.o \ - strstr.o \ - strtok.o \ - strxfrm.o \ - toupper.o \ - vfprintf.o \ - vsnprintf.o \ - vsprintf.o \ - __abs.o\ - __labs.o\ - __llabs.o\ - _daysyear.o\ - abs.o\ - asctime.o\ - atoi.o\ - atol.o\ - atoll.o\ - bsearch.o\ - ctime.o\ - fgetc.o\ - fgets.o\ - freopen.o\ - getc.o\ - getchar.o\ - gets.o\ - gmtime.o\ - labs.o\ - llabs.o\ - localeconv.o\ - localtime.o\ - mktime.o\ - perror.o\ - qsort.o\ - realloc.o\ - rewind.o\ - setlocale.o\ - strftime.o\ - tmpnam.o\ - tolower.o\ - -OBJS = $(COBJ) $(SYSOBJ) TARGET = $(PROJECTDIR)/lib/libc.a +DIRS = arch\ + assert\ + ctype\ + locale\ + stdio\ + stdlib\ + string\ + time\ -all: $(COBJS) arch/$(ARCH)/$(SYS) +all: $(DIRS) +@$(MAKE) $(TARGET) -arch/$(ARCH)/$(SYS): FORCE +$(DIRS): FORCE +@cd $@ && $(MAKE) -$(TARGET): $(OBJS) - $(AR) $(ARFLAGS) $@ $? - ranlib $@ +objlst: FORCE + mklst $(TARGET) + +$(TARGET): objlst + xargs $(AR) $(ARFLAGS) $@ < objlst clean: - +@cd arch && $(MAKE) clean + $(FORALL) + rm -f objlst diff --git a/src/libc/__getc.c b/src/libc/__getc.c @@ -1,46 +0,0 @@ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include "syscall.h" -#undef getc - -int -__getc(FILE *fp) -{ - int cnt; - - if (fp->flags & (_IOEOF | _IOERR)) - return EOF; - - if ((fp->flags & (_IOREAD | _IORW)) == 0) { - fp->flags |= _IOERR; - errno = EBADF; - return EOF; - } - - if (fp->flags & _IOSTRG) { - fp->flags |= _IOEOF; - return EOF; - } - - if (fp->buf == NULL) { - if ((fp->buf = malloc(BUFSIZ)) == NULL) { - errno = ENOMEM; - return EOF; - } - fp->len = BUFSIZ; - fp->flags |= _IOALLOC; - fp->lp = fp->rp = fp->wp = fp->buf; - } - - if ((cnt = _read(fp->fd, fp->buf, fp->len)) <= 0) { - fp->flags |= (cnt == 0) ? _IOEOF : _IOERR; - return EOF; - } - - fp->flags |= _IOREAD; - fp->rp = fp->buf; - fp->wp = fp->buf + cnt; - - return *fp->rp++; -} diff --git a/src/libc/_daysyear.c b/src/libc/_daysyear.c @@ -1,30 +0,0 @@ -#include <time.h> -#include "libc.h" - -int _daysmon[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - -int -_daysyear(int year) -{ - if (year%4 != 0) - return 365; - if (year%100 == 0 && year%400 != 0) - return 365; - return 366; -} - -/* - * Happy New Year!!!! - */ -int -_newyear(int year) -{ - int day; - - year += 1900 - 1; - day = 1 + year + year/4; - day -= year/100; - day += year/400; - - return day % 7; -} diff --git a/src/libc/_flsbuf.c b/src/libc/_flsbuf.c @@ -1,21 +0,0 @@ -#include <errno.h> -#include <stdio.h> -#include "syscall.h" - -int -_flsbuf(FILE *fp) -{ - unsigned char *p; - size_t cnt; - - p = (fp->flags & _IOLBF) ? fp->lp : fp->wp; - cnt = p - fp->buf; - - if (_write(fp->fd, fp->buf, cnt) != cnt) { - fp->flags |= _IOERR; - return EOF; - } - fp->lp = fp->rp = fp->wp = fp->buf; - - return 0; -} diff --git a/src/libc/_fpopen.c b/src/libc/_fpopen.c @@ -1,75 +0,0 @@ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <sys.h> -#include "syscall.h" -#include "libc.h" -#undef fopen - -FILE * -_fpopen(const char * restrict fname, - const char * restrict mode, - FILE * restrict fp) -{ - int i, flags, fd, rw, bin; - - flags = rw = bin = 0; - - if (mode[0] == '\0') - goto einval; - - for (i = 1; mode[i]; ++i) { - switch (mode[i]) { - case '+': - if (rw) - goto einval; - rw = 1; - break; - case 'b': - if (bin) - goto einval; - bin = 1; - break; - default: - goto einval; - } - } - - switch (mode[0]) { - case 'a': - flags |= O_APPEND | O_CREAT; - goto wrflags; - case 'w': - flags |= O_TRUNC | O_CREAT; - wrflags: - flags |= (rw) ? O_RDWR : O_WRONLY; - break; - case 'r': - flags = (rw) ? O_RDWR : O_RDONLY; - break; - default: - einval: - errno = EINVAL; - return NULL; - } - - if ((fd = _open(fname, flags)) < 0) - return NULL; - - fp->buf = NULL; - fp->fd = fd; - - if (!bin) - fp->flags |= _IOTXT; - - if (flags & O_RDWR) - fp->flags |= _IORW; - else if (flags & O_RDONLY) - fp->flags |= _IOREAD; - else - fp->flags |= _IOWRITE; - - fp->lp = fp->rp = fp->wp = NULL; - - return fp; -} diff --git a/src/libc/arch/amd64/bsd/objlst.mk b/src/libc/arch/amd64/bsd/objlst.mk @@ -1,12 +0,0 @@ -SYSOBJ = $(LIBSYS)/_Exit.o \ - $(LIBSYS)/_close.o \ - $(LIBSYS)/_brk.o \ - $(LIBSYS)/_getpid.o \ - $(LIBSYS)/_kill.o \ - $(LIBSYS)/_lseek.o \ - $(LIBSYS)/_open.o \ - $(LIBSYS)/_read.o \ - $(LIBSYS)/_write.o \ - $(LIBSYS)/_getheap.o \ - $(LIBARCH)/setjmp.o \ - $(LIBARCH)/longjmp.o \ diff --git a/src/libc/arch/amd64/linux/objlst.mk b/src/libc/arch/amd64/linux/objlst.mk @@ -1,14 +0,0 @@ -SYSOBJ = $(LIBSYS)/_Exit.o \ - $(LIBSYS)/_close.o \ - $(LIBSYS)/_getpid.o \ - $(LIBSYS)/_kill.o \ - $(LIBSYS)/_lseek.o \ - $(LIBSYS)/_open.o \ - $(LIBSYS)/_read.o \ - $(LIBSYS)/_write.o \ - $(LIBSYS)/_lseek.o \ - $(LIBSYS)/_cerrno.o \ - $(LIBSYS)/_brk.o \ - $(LIBSYS)/_getheap.o \ - $(LIBARCH)/setjmp.o \ - $(LIBARCH)/longjmp.o \ diff --git a/src/libc/arch/arm64/linux/objlst.mk b/src/libc/arch/arm64/linux/objlst.mk @@ -1,14 +0,0 @@ -SYSOBJ = $(LIBSYS)/_Exit.o \ - $(LIBSYS)/_close.o \ - $(LIBSYS)/_brk.o \ - $(LIBSYS)/_getpid.o \ - $(LIBSYS)/_kill.o \ - $(LIBSYS)/_lseek.o \ - $(LIBSYS)/_open.o \ - $(LIBSYS)/_openat.o \ - $(LIBSYS)/_read.o \ - $(LIBSYS)/_write.o \ - $(LIBSYS)/_cerrno.o \ - $(LIBSYS)/_getheap.o \ - $(LIBARCH)/setjmp.o \ - $(LIBARCH)/longjmp.o \ diff --git a/src/libc/arch/arm64/none/objlst.mk b/src/libc/arch/arm64/none/objlst.mk @@ -1,6 +0,0 @@ -SYSOBJ = $(LIBSYS)/_Exit.o \ - $(LIBSYS)/_getpid.o \ - $(LIBSYS)/_kill.o \ - $(LIBSYS)/_write.o \ - $(LIBARCH)/setjmp.o \ - $(LIBARCH)/longjmp.o \ diff --git a/src/libc/assert/Makefile b/src/libc/assert/Makefile @@ -0,0 +1,9 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = __assert.o\ + assert.o\ + +all: $(OBJS) diff --git a/src/libc/__assert.c b/src/libc/assert/__assert.c diff --git a/src/libc/assert/assert.c b/src/libc/assert/assert.c @@ -0,0 +1,13 @@ +#include <assert.h> +#include <stdlib.h> +#include <stdio.h> +#undef assert + +void +assert(int exp) +{ + if (exp) + return; + fputs("assert failed\n", stderr); + abort(); +} diff --git a/src/libc/ctype/Makefile b/src/libc/ctype/Makefile @@ -0,0 +1,23 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = ctype.o\ + isalnum.o\ + isalpha.o\ + isascii.o\ + isblank.o\ + iscntrl.o\ + isdigit.o\ + isgraph.o\ + islower.o\ + isprint.o\ + ispunct.o\ + isspace.o\ + isupper.o\ + isxdigit.o\ + tolower.o\ + toupper.o\ + +all: $(OBJS) diff --git a/src/libc/ctype.c b/src/libc/ctype/ctype.c diff --git a/src/libc/isalnum.c b/src/libc/ctype/isalnum.c diff --git a/src/libc/isalpha.c b/src/libc/ctype/isalpha.c diff --git a/src/libc/isascii.c b/src/libc/ctype/isascii.c diff --git a/src/libc/isblank.c b/src/libc/ctype/isblank.c diff --git a/src/libc/iscntrl.c b/src/libc/ctype/iscntrl.c diff --git a/src/libc/isdigit.c b/src/libc/ctype/isdigit.c diff --git a/src/libc/isgraph.c b/src/libc/ctype/isgraph.c diff --git a/src/libc/islower.c b/src/libc/ctype/islower.c diff --git a/src/libc/isprint.c b/src/libc/ctype/isprint.c diff --git a/src/libc/ispunct.c b/src/libc/ctype/ispunct.c diff --git a/src/libc/isspace.c b/src/libc/ctype/isspace.c diff --git a/src/libc/isupper.c b/src/libc/ctype/isupper.c diff --git a/src/libc/isxdigit.c b/src/libc/ctype/isxdigit.c diff --git a/src/libc/tolower.c b/src/libc/ctype/tolower.c diff --git a/src/libc/toupper.c b/src/libc/ctype/toupper.c diff --git a/src/libc/fclose.c b/src/libc/fclose.c @@ -1,34 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include "syscall.h" -#undef fclose - -extern int _flsbuf(FILE *fp); - -int -fclose(FILE *fp) -{ - int r = EOF; - - if ((fp->flags & _IOSTRG) == 0 && - fp->flags & (_IOWRITE | _IOREAD | _IORW)) { - r = 0; - if (_flsbuf(fp) == EOF) - r = EOF; - if (_close(fp->fd) < 0) - r = EOF; - } - - if (fp->flags & _IOALLOC) { - free(fp->buf); - fp->buf = NULL; - } - - fp->flags &= ~(_IOWRITE | _IOREAD | _IORW | - _IOERR | _IOEOF | - _IOALLOC | - _IOTXT | - _IOSTRG); - - return r; -} diff --git a/src/libc/fopen.c b/src/libc/fopen.c @@ -1,23 +0,0 @@ -#include <errno.h> -#include <stdio.h> - -#include "syscall.h" -#include "libc.h" -#undef fopen - - -FILE * -fopen(const char * restrict name, const char * restrict mode) -{ - FILE *fp; - - for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) { - if ((fp->flags & (_IOREAD | _IOWRITE | _IORW)) == 0) - break; - } - if (fp == &__iob[FOPEN_MAX]) { - errno = ENOMEM; - return NULL; - } - return _fpopen(name, mode, fp); -} diff --git a/src/libc/freopen.c b/src/libc/freopen.c @@ -1,14 +0,0 @@ -#include <stdio.h> - -#include "syscall.h" -#include "libc.h" -#undef freopen - -FILE * -freopen(const char * restrict name, const char * restrict mode, - FILE * restrict fp) -{ - if (fclose(fp) == EOF) - return NULL; - return _fpopen(name, mode, fp); -} diff --git a/src/libc/fseek.c b/src/libc/fseek.c @@ -1,28 +0,0 @@ -#include <stdio.h> -#include "syscall.h" -#undef fseek - -extern int _flsbuf(FILE *fp); - -int -fseek(FILE *fp, long off, int whence) -{ - if (fp->flags & _IOERR) - return EOF; - - if ((fp->flags & _IOWRITE) && _flsbuf(fp)) - return -1; - else if (whence == SEEK_CUR && (fp->flags & _IOREAD)) - off -= fp->wp - fp->rp; - - if (_lseek(fp->fd, off, whence) < 0) { - fp->flags |= _IOERR; - return EOF; - } - - if (fp->flags & _IORW) - fp->flags &= ~(_IOREAD | _IOWRITE); - fp->flags &= ~_IOEOF; - - return 0; -} diff --git a/src/libc/ftell.c b/src/libc/ftell.c @@ -1,27 +0,0 @@ -#include <stdio.h> -#include "syscall.h" -#undef ftell - -long -ftell(FILE *fp) -{ - long off; - unsigned char *p; - - if (fp->flags & _IOERR) - return EOF; - - if ((off = _lseek(fp->fd, 0, SEEK_CUR)) < 0) { - fp->flags |= _IOERR; - return EOF; - } - - if (fp->flags & _IOREAD) - return off - (fp->wp - fp->rp); - - if (fp->flags & _IOWRITE) { - p = (fp->flags & _IOLBF) ? fp->lp : fp->wp; - return off + (p - fp->buf); - } - return off; -} diff --git a/src/libc/locale/Makefile b/src/libc/locale/Makefile @@ -0,0 +1,9 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = localeconv.o\ + setlocale.o\ + +all: $(OBJS) diff --git a/src/libc/localeconv.c b/src/libc/locale/localeconv.c diff --git a/src/libc/setlocale.c b/src/libc/locale/setlocale.c diff --git a/src/libc/malloc.c b/src/libc/malloc.c @@ -1,158 +0,0 @@ -#include <errno.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#include "malloc.h" -#include "syscall.h" - -#define MAXADDR ((char *)-1) -#define ERRADDR ((char *)-1) - -static Header base = { .h.next = &base }; -static Header *freep = &base; - -/* - * Run over the free list looking for the nearest previous - * block. There are two possible results: end of the list - * or an intermediary block. - */ -void * -_prevchunk(Header *hp) -{ - Header *p; - - for (p = freep; ;p = p->h.next) { - /* hp between p and p->h.next? */ - if (p < hp && hp < p->h.next) - break; - /* p before hp and hp at the end of list? */ - if (p->h.next <= p && (hp < p->h.next || hp > p)) - break; - } - return p; -} - -/* - * Get the previous block and try to merge - * with next and previous blocks - */ -void -free(void *mem) -{ - Header *hp, *prev; - - if (!mem) - return; - - hp = (Header *) mem - 1; - prev = _prevchunk(hp); - - /* join to next */ - if (hp + hp->h.size == prev->h.next) { - hp->h.size += prev->h.next->h.size; - hp->h.next = prev->h.next->h.next; - } else { - hp->h.next = prev->h.next; - } - - /* join to previous */ - if (prev + prev->h.size == hp) { - prev->h.size += hp->h.size; - prev->h.next = hp->h.next; - } else { - prev->h.next = hp; - } - - freep = prev; -} - -static void * -sbrk(uintptr_t inc) -{ - char *new, *old; - void *p; - static void *heap; - - if (!heap) - heap = _getheap(); - old = heap; - if (old >= MAXADDR - inc) - return ERRADDR; - new = old + inc; - p = _brk(new); - if (p == old || p < 0) - return ERRADDR; - heap = new; - - return old; -} - -static Header * -morecore(size_t nunits) -{ - char *rawmem; - Header *hp; - - if (nunits < NALLOC) - nunits = NALLOC; - - rawmem = sbrk(nunits * sizeof(Header)); - if (rawmem == ERRADDR) - return NULL; - - hp = (Header*)rawmem; - hp->h.size = nunits; - - /* integrate new memory into the list */ - free(hp + 1); - - return freep; -} - -/* - * Run over the list of free blocks trying to find a block - * big enough for nbytes. If the block fit perfectly with - * the required size then we only have to unlink - * the block. Otherwise we have to split the block and - * return the right part. If we run over the full list - * without a fit then we have to require more memory - * - * ______________________________________ - * ___________./______________________________________\_____ - * ...| in | | | in | |.....| in | | | |.... - * ...| use | | | use | |.....| use | | | |.... - * ___|______|___|.____|_____|._|_____|______|._|.___|.|____ - * \__/ \_________/ \_____________/ \/ \__/ - */ -void * -malloc(size_t nbytes) -{ - Header *cur, *prev; - size_t nunits; - - /* 1 unit for header plus enough units to fit nbytes */ - nunits = (nbytes+sizeof(Header)-1) / sizeof(Header) + 1; - - for (prev = freep; ; prev = cur) { - cur = prev->h.next; - if (cur->h.size >= nunits) { - if (cur->h.size == nunits) { - prev->h.next = cur->h.next; - } else { - cur->h.size -= nunits; - cur += cur->h.size; - cur->h.size = nunits; - } - freep = prev; - return cur + 1; - } - - if (cur == freep) { - if ((cur = morecore(nunits)) == NULL) { - errno = ENOMEM; - return NULL; - } - } - } -} diff --git a/src/libc/mklst b/src/libc/mklst @@ -0,0 +1,12 @@ +#!/bin/sh + +set -e +trap 'r=$?;rm -f $$.tmp;exit $r' EXIT HUP QUIT INT TERM +archive=${1?'First parameter must be the archive name'} + +if test -f $archive +then + newer="-newer $archive" +fi + +find . -name '*.o' $newer > $$.tmp && mv $$.tmp objlst diff --git a/src/libc/printk.c b/src/libc/printk.c @@ -1,118 +0,0 @@ - -#include <stdarg.h> -#include <stddef.h> - -#include "syscall.h" - -static void -putch(int c) -{ - char ch = c; - _write(1, &ch, 1); -} - -static void -printn(long long n, int base, int sign) -{ - int first, d; - unsigned long long div; - const static char digits[] = "0123456789ABCDEF"; - - switch (base) { - case 8: - div = 01000000000000000000000u; - break; - case 10: - div = 10000000000000000000u; - break; - case 16: - div = 0x1000000000000000u; - break; - } - - if (sign && n < 0) { - n = -n; - putch('-'); - } - - for (first = 1; div > 0; div /= base) { - d = n / div; - if (d == 0 && first && div != 1) - continue; - n -= d * div; - putch(digits[d]); - first = 0; - } -} - -static long long -getnum(va_list *va, int size) -{ - switch (size) { - case 0: - return va_arg(*va, int); - case 1: - return va_arg(*va, long); - case 2: - return va_arg(*va, long long); - case 3: - default: - return (long long) va_arg(*va, void *); - } -} - -void -printk(const char * restrict fmt, ...) -{ - char c; - va_list va; - int base, sign, size; - char *s; - - va_start(va, fmt); - while (( c = *fmt++) != '\0') { - if (c != '%') { - putch(c); - continue; - } - - sign = 0; - size = 0; -flags: - switch (c = *fmt++) { - case 'l': - size++; - goto flags; - case 'o': - base = 8; - goto print_number; - case 'd': - sign = 1; - case 'u': - base = 10; - goto print_number; - case 'p': - size = 3; - case 'X': - case 'x': - base = 16; - print_number: - printn(getnum(&va, size), base, sign); - break; - case 's': - for (s = va_arg(va, char *); *s; s++) - putch(*s); - break; - case 'c': - c = va_arg(va, int); - case '%': - putch(c); - break; - case '\0': - goto out_loop; - } - } - -out_loop: - va_end(va); -} diff --git a/src/libc/stdio/Makefile b/src/libc/stdio/Makefile @@ -0,0 +1,45 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = __getc.o\ + __putc.o\ + _flsbuf.o\ + _fpopen.o\ + clearerr.o\ + fclose.o\ + feof.o\ + ferror.o\ + fgetc.o\ + fgets.o\ + fopen.o\ + fprintf.o\ + fputc.o\ + fputs.o\ + fread.o\ + freopen.o\ + fseek.o\ + ftell.o\ + fwrite.o\ + getc.o\ + getchar.o\ + gets.o\ + perror.o\ + printf.o\ + printk.o\ + putc.o\ + putchar.o\ + puts.o\ + rewind.o\ + setbuf.o\ + setvbuf.o\ + snprintf.o\ + sprintf.o\ + stdio.o\ + tmpnam.o\ + vfprintf.o\ + vsnprintf.o\ + vsprintf.o\ + +all: $(OBJS) diff --git a/src/libc/stdio/__getc.c b/src/libc/stdio/__getc.c @@ -0,0 +1,46 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include "../syscall.h" +#undef getc + +int +__getc(FILE *fp) +{ + int cnt; + + if (fp->flags & (_IOEOF | _IOERR)) + return EOF; + + if ((fp->flags & (_IOREAD | _IORW)) == 0) { + fp->flags |= _IOERR; + errno = EBADF; + return EOF; + } + + if (fp->flags & _IOSTRG) { + fp->flags |= _IOEOF; + return EOF; + } + + if (fp->buf == NULL) { + if ((fp->buf = malloc(BUFSIZ)) == NULL) { + errno = ENOMEM; + return EOF; + } + fp->len = BUFSIZ; + fp->flags |= _IOALLOC; + fp->lp = fp->rp = fp->wp = fp->buf; + } + + if ((cnt = _read(fp->fd, fp->buf, fp->len)) <= 0) { + fp->flags |= (cnt == 0) ? _IOEOF : _IOERR; + return EOF; + } + + fp->flags |= _IOREAD; + fp->rp = fp->buf; + fp->wp = fp->buf + cnt; + + return *fp->rp++; +} diff --git a/src/libc/__putc.c b/src/libc/stdio/__putc.c diff --git a/src/libc/stdio/_flsbuf.c b/src/libc/stdio/_flsbuf.c @@ -0,0 +1,21 @@ +#include <errno.h> +#include <stdio.h> +#include "../syscall.h" + +int +_flsbuf(FILE *fp) +{ + unsigned char *p; + size_t cnt; + + p = (fp->flags & _IOLBF) ? fp->lp : fp->wp; + cnt = p - fp->buf; + + if (_write(fp->fd, fp->buf, cnt) != cnt) { + fp->flags |= _IOERR; + return EOF; + } + fp->lp = fp->rp = fp->wp = fp->buf; + + return 0; +} diff --git a/src/libc/stdio/_fpopen.c b/src/libc/stdio/_fpopen.c @@ -0,0 +1,75 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys.h> +#include "../syscall.h" +#include "../libc.h" +#undef fopen + +FILE * +_fpopen(const char * restrict fname, + const char * restrict mode, + FILE * restrict fp) +{ + int i, flags, fd, rw, bin; + + flags = rw = bin = 0; + + if (mode[0] == '\0') + goto einval; + + for (i = 1; mode[i]; ++i) { + switch (mode[i]) { + case '+': + if (rw) + goto einval; + rw = 1; + break; + case 'b': + if (bin) + goto einval; + bin = 1; + break; + default: + goto einval; + } + } + + switch (mode[0]) { + case 'a': + flags |= O_APPEND | O_CREAT; + goto wrflags; + case 'w': + flags |= O_TRUNC | O_CREAT; + wrflags: + flags |= (rw) ? O_RDWR : O_WRONLY; + break; + case 'r': + flags = (rw) ? O_RDWR : O_RDONLY; + break; + default: + einval: + errno = EINVAL; + return NULL; + } + + if ((fd = _open(fname, flags)) < 0) + return NULL; + + fp->buf = NULL; + fp->fd = fd; + + if (!bin) + fp->flags |= _IOTXT; + + if (flags & O_RDWR) + fp->flags |= _IORW; + else if (flags & O_RDONLY) + fp->flags |= _IOREAD; + else + fp->flags |= _IOWRITE; + + fp->lp = fp->rp = fp->wp = NULL; + + return fp; +} diff --git a/src/libc/clearerr.c b/src/libc/stdio/clearerr.c diff --git a/src/libc/stdio/fclose.c b/src/libc/stdio/fclose.c @@ -0,0 +1,34 @@ +#include <stdlib.h> +#include <stdio.h> +#include "../syscall.h" +#undef fclose + +extern int _flsbuf(FILE *fp); + +int +fclose(FILE *fp) +{ + int r = EOF; + + if ((fp->flags & _IOSTRG) == 0 && + fp->flags & (_IOWRITE | _IOREAD | _IORW)) { + r = 0; + if (_flsbuf(fp) == EOF) + r = EOF; + if (_close(fp->fd) < 0) + r = EOF; + } + + if (fp->flags & _IOALLOC) { + free(fp->buf); + fp->buf = NULL; + } + + fp->flags &= ~(_IOWRITE | _IOREAD | _IORW | + _IOERR | _IOEOF | + _IOALLOC | + _IOTXT | + _IOSTRG); + + return r; +} diff --git a/src/libc/feof.c b/src/libc/stdio/feof.c diff --git a/src/libc/ferror.c b/src/libc/stdio/ferror.c diff --git a/src/libc/fgetc.c b/src/libc/stdio/fgetc.c diff --git a/src/libc/fgets.c b/src/libc/stdio/fgets.c diff --git a/src/libc/stdio/fopen.c b/src/libc/stdio/fopen.c @@ -0,0 +1,23 @@ +#include <errno.h> +#include <stdio.h> + +#include "../syscall.h" +#include "../libc.h" +#undef fopen + + +FILE * +fopen(const char * restrict name, const char * restrict mode) +{ + FILE *fp; + + for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) { + if ((fp->flags & (_IOREAD | _IOWRITE | _IORW)) == 0) + break; + } + if (fp == &__iob[FOPEN_MAX]) { + errno = ENOMEM; + return NULL; + } + return _fpopen(name, mode, fp); +} diff --git a/src/libc/fprintf.c b/src/libc/stdio/fprintf.c diff --git a/src/libc/fputc.c b/src/libc/stdio/fputc.c diff --git a/src/libc/fputs.c b/src/libc/stdio/fputs.c diff --git a/src/libc/fread.c b/src/libc/stdio/fread.c diff --git a/src/libc/stdio/freopen.c b/src/libc/stdio/freopen.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +#include "../syscall.h" +#include "../libc.h" +#undef freopen + +FILE * +freopen(const char * restrict name, const char * restrict mode, + FILE * restrict fp) +{ + if (fclose(fp) == EOF) + return NULL; + return _fpopen(name, mode, fp); +} diff --git a/src/libc/stdio/fseek.c b/src/libc/stdio/fseek.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include "../syscall.h" +#undef fseek + +extern int _flsbuf(FILE *fp); + +int +fseek(FILE *fp, long off, int whence) +{ + if (fp->flags & _IOERR) + return EOF; + + if ((fp->flags & _IOWRITE) && _flsbuf(fp)) + return -1; + else if (whence == SEEK_CUR && (fp->flags & _IOREAD)) + off -= fp->wp - fp->rp; + + if (_lseek(fp->fd, off, whence) < 0) { + fp->flags |= _IOERR; + return EOF; + } + + if (fp->flags & _IORW) + fp->flags &= ~(_IOREAD | _IOWRITE); + fp->flags &= ~_IOEOF; + + return 0; +} diff --git a/src/libc/stdio/ftell.c b/src/libc/stdio/ftell.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include "../syscall.h" +#undef ftell + +long +ftell(FILE *fp) +{ + long off; + unsigned char *p; + + if (fp->flags & _IOERR) + return EOF; + + if ((off = _lseek(fp->fd, 0, SEEK_CUR)) < 0) { + fp->flags |= _IOERR; + return EOF; + } + + if (fp->flags & _IOREAD) + return off - (fp->wp - fp->rp); + + if (fp->flags & _IOWRITE) { + p = (fp->flags & _IOLBF) ? fp->lp : fp->wp; + return off + (p - fp->buf); + } + return off; +} diff --git a/src/libc/fwrite.c b/src/libc/stdio/fwrite.c diff --git a/src/libc/getc.c b/src/libc/stdio/getc.c diff --git a/src/libc/getchar.c b/src/libc/stdio/getchar.c diff --git a/src/libc/gets.c b/src/libc/stdio/gets.c diff --git a/src/libc/perror.c b/src/libc/stdio/perror.c diff --git a/src/libc/stdio/print.c b/src/libc/stdio/print.c diff --git a/src/libc/printf.c b/src/libc/stdio/printf.c diff --git a/src/libc/stdio/printk.c b/src/libc/stdio/printk.c @@ -0,0 +1,118 @@ + +#include <stdarg.h> +#include <stddef.h> + +#include "../syscall.h" + +static void +putch(int c) +{ + char ch = c; + _write(1, &ch, 1); +} + +static void +printn(long long n, int base, int sign) +{ + int first, d; + unsigned long long div; + const static char digits[] = "0123456789ABCDEF"; + + switch (base) { + case 8: + div = 01000000000000000000000u; + break; + case 10: + div = 10000000000000000000u; + break; + case 16: + div = 0x1000000000000000u; + break; + } + + if (sign && n < 0) { + n = -n; + putch('-'); + } + + for (first = 1; div > 0; div /= base) { + d = n / div; + if (d == 0 && first && div != 1) + continue; + n -= d * div; + putch(digits[d]); + first = 0; + } +} + +static long long +getnum(va_list *va, int size) +{ + switch (size) { + case 0: + return va_arg(*va, int); + case 1: + return va_arg(*va, long); + case 2: + return va_arg(*va, long long); + case 3: + default: + return (long long) va_arg(*va, void *); + } +} + +void +printk(const char * restrict fmt, ...) +{ + char c; + va_list va; + int base, sign, size; + char *s; + + va_start(va, fmt); + while (( c = *fmt++) != '\0') { + if (c != '%') { + putch(c); + continue; + } + + sign = 0; + size = 0; +flags: + switch (c = *fmt++) { + case 'l': + size++; + goto flags; + case 'o': + base = 8; + goto print_number; + case 'd': + sign = 1; + case 'u': + base = 10; + goto print_number; + case 'p': + size = 3; + case 'X': + case 'x': + base = 16; + print_number: + printn(getnum(&va, size), base, sign); + break; + case 's': + for (s = va_arg(va, char *); *s; s++) + putch(*s); + break; + case 'c': + c = va_arg(va, int); + case '%': + putch(c); + break; + case '\0': + goto out_loop; + } + } + +out_loop: + va_end(va); +} diff --git a/src/libc/putc.c b/src/libc/stdio/putc.c diff --git a/src/libc/putchar.c b/src/libc/stdio/putchar.c diff --git a/src/libc/puts.c b/src/libc/stdio/puts.c diff --git a/src/libc/rewind.c b/src/libc/stdio/rewind.c diff --git a/src/libc/setbuf.c b/src/libc/stdio/setbuf.c diff --git a/src/libc/setvbuf.c b/src/libc/stdio/setvbuf.c diff --git a/src/libc/snprintf.c b/src/libc/stdio/snprintf.c diff --git a/src/libc/sprintf.c b/src/libc/stdio/sprintf.c diff --git a/src/libc/stdio.c b/src/libc/stdio/stdio.c diff --git a/src/libc/stdio/tmpnam.c b/src/libc/stdio/tmpnam.c @@ -0,0 +1,31 @@ +#include <stdio.h> +#include <string.h> +#include "../syscall.h" +#undef tmpnam + +char * +tmpnam(char *s) +{ + static char *tmpl, buf[L_tmpnam]; + char *p; + + if (*buf == '\0') { + for (tmpl = buf, p = _TMPNAME; *tmpl++ = *p++; ) + ; + for (p = tmpl; p < &buf[L_tmpnam-1]; ++p) + *p = '0'; + *p = '\0'; + } + for (;;) { + for (p = tmpl; *p && *p != '9'; ++p) + ; + if (*p == '\0') + return NULL; + ++*p; + if (_access(buf, 0) != 0) + break; + } + if (s) + strcpy(s, buf); + return buf; +} diff --git a/src/libc/vfprintf.c b/src/libc/stdio/vfprintf.c diff --git a/src/libc/vsnprintf.c b/src/libc/stdio/vsnprintf.c diff --git a/src/libc/vsprintf.c b/src/libc/stdio/vsprintf.c diff --git a/src/libc/stdlib/Makefile b/src/libc/stdlib/Makefile @@ -0,0 +1,27 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = __abs.o\ + __labs.o\ + __llabs.o\ + abort.o\ + abs.o\ + atexit.o\ + atoi.o\ + atol.o\ + atoll.o\ + bsearch.o\ + calloc.o\ + errno.o\ + exit.o\ + labs.o\ + llabs.o\ + malloc.o\ + qsort.o\ + raise.o\ + rand.o\ + realloc.o\ + +all: $(OBJS) diff --git a/src/libc/__abs.c b/src/libc/stdlib/__abs.c diff --git a/src/libc/__labs.c b/src/libc/stdlib/__labs.c diff --git a/src/libc/__llabs.c b/src/libc/stdlib/__llabs.c diff --git a/src/libc/abort.c b/src/libc/stdlib/abort.c diff --git a/src/libc/abs.c b/src/libc/stdlib/abs.c diff --git a/src/libc/atexit.c b/src/libc/stdlib/atexit.c diff --git a/src/libc/atoi.c b/src/libc/stdlib/atoi.c diff --git a/src/libc/atol.c b/src/libc/stdlib/atol.c diff --git a/src/libc/atoll.c b/src/libc/stdlib/atoll.c diff --git a/src/libc/bsearch.c b/src/libc/stdlib/bsearch.c diff --git a/src/libc/calloc.c b/src/libc/stdlib/calloc.c diff --git a/src/libc/errno.c b/src/libc/stdlib/errno.c diff --git a/src/libc/exit.c b/src/libc/stdlib/exit.c diff --git a/src/libc/labs.c b/src/libc/stdlib/labs.c diff --git a/src/libc/llabs.c b/src/libc/stdlib/llabs.c diff --git a/src/libc/stdlib/malloc.c b/src/libc/stdlib/malloc.c @@ -0,0 +1,158 @@ +#include <errno.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "malloc.h" +#include "../syscall.h" + +#define MAXADDR ((char *)-1) +#define ERRADDR ((char *)-1) + +static Header base = { .h.next = &base }; +static Header *freep = &base; + +/* + * Run over the free list looking for the nearest previous + * block. There are two possible results: end of the list + * or an intermediary block. + */ +void * +_prevchunk(Header *hp) +{ + Header *p; + + for (p = freep; ;p = p->h.next) { + /* hp between p and p->h.next? */ + if (p < hp && hp < p->h.next) + break; + /* p before hp and hp at the end of list? */ + if (p->h.next <= p && (hp < p->h.next || hp > p)) + break; + } + return p; +} + +/* + * Get the previous block and try to merge + * with next and previous blocks + */ +void +free(void *mem) +{ + Header *hp, *prev; + + if (!mem) + return; + + hp = (Header *) mem - 1; + prev = _prevchunk(hp); + + /* join to next */ + if (hp + hp->h.size == prev->h.next) { + hp->h.size += prev->h.next->h.size; + hp->h.next = prev->h.next->h.next; + } else { + hp->h.next = prev->h.next; + } + + /* join to previous */ + if (prev + prev->h.size == hp) { + prev->h.size += hp->h.size; + prev->h.next = hp->h.next; + } else { + prev->h.next = hp; + } + + freep = prev; +} + +static void * +sbrk(uintptr_t inc) +{ + char *new, *old; + void *p; + static void *heap; + + if (!heap) + heap = _getheap(); + old = heap; + if (old >= MAXADDR - inc) + return ERRADDR; + new = old + inc; + p = _brk(new); + if (p == old || p < 0) + return ERRADDR; + heap = new; + + return old; +} + +static Header * +morecore(size_t nunits) +{ + char *rawmem; + Header *hp; + + if (nunits < NALLOC) + nunits = NALLOC; + + rawmem = sbrk(nunits * sizeof(Header)); + if (rawmem == ERRADDR) + return NULL; + + hp = (Header*)rawmem; + hp->h.size = nunits; + + /* integrate new memory into the list */ + free(hp + 1); + + return freep; +} + +/* + * Run over the list of free blocks trying to find a block + * big enough for nbytes. If the block fit perfectly with + * the required size then we only have to unlink + * the block. Otherwise we have to split the block and + * return the right part. If we run over the full list + * without a fit then we have to require more memory + * + * ______________________________________ + * ___________./______________________________________\_____ + * ...| in | | | in | |.....| in | | | |.... + * ...| use | | | use | |.....| use | | | |.... + * ___|______|___|.____|_____|._|_____|______|._|.___|.|____ + * \__/ \_________/ \_____________/ \/ \__/ + */ +void * +malloc(size_t nbytes) +{ + Header *cur, *prev; + size_t nunits; + + /* 1 unit for header plus enough units to fit nbytes */ + nunits = (nbytes+sizeof(Header)-1) / sizeof(Header) + 1; + + for (prev = freep; ; prev = cur) { + cur = prev->h.next; + if (cur->h.size >= nunits) { + if (cur->h.size == nunits) { + prev->h.next = cur->h.next; + } else { + cur->h.size -= nunits; + cur += cur->h.size; + cur->h.size = nunits; + } + freep = prev; + return cur + 1; + } + + if (cur == freep) { + if ((cur = morecore(nunits)) == NULL) { + errno = ENOMEM; + return NULL; + } + } + } +} diff --git a/src/libc/malloc.h b/src/libc/stdlib/malloc.h diff --git a/src/libc/qsort.c b/src/libc/stdlib/qsort.c diff --git a/src/libc/raise.c b/src/libc/stdlib/raise.c diff --git a/src/libc/rand.c b/src/libc/stdlib/rand.c diff --git a/src/libc/realloc.c b/src/libc/stdlib/realloc.c diff --git a/src/libc/string/Makefile b/src/libc/string/Makefile @@ -0,0 +1,30 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = memchr.o\ + memcmp.o\ + memcpy.o\ + memmove.o\ + memset.o\ + strcat.o\ + strchr.o\ + strcmp.o\ + strcoll.o\ + strcpy.o\ + strcspn.o\ + strerror.o\ + strlen.o\ + strncat.o\ + strncmp.o\ + strncpy.o\ + strnlen.o\ + strpbrk.o\ + strrchr.o\ + strspn.o\ + strstr.o\ + strtok.o\ + strxfrm.o\ + +all: $(OBJS) diff --git a/src/libc/memchr.c b/src/libc/string/memchr.c diff --git a/src/libc/memcmp.c b/src/libc/string/memcmp.c diff --git a/src/libc/memcpy.c b/src/libc/string/memcpy.c diff --git a/src/libc/memmove.c b/src/libc/string/memmove.c diff --git a/src/libc/memset.c b/src/libc/string/memset.c diff --git a/src/libc/strcat.c b/src/libc/string/strcat.c diff --git a/src/libc/strchr.c b/src/libc/string/strchr.c diff --git a/src/libc/strcmp.c b/src/libc/string/strcmp.c diff --git a/src/libc/strcoll.c b/src/libc/string/strcoll.c diff --git a/src/libc/strcpy.c b/src/libc/string/strcpy.c diff --git a/src/libc/strcspn.c b/src/libc/string/strcspn.c diff --git a/src/libc/strerror.c b/src/libc/string/strerror.c diff --git a/src/libc/strlen.c b/src/libc/string/strlen.c diff --git a/src/libc/strncat.c b/src/libc/string/strncat.c diff --git a/src/libc/strncmp.c b/src/libc/string/strncmp.c diff --git a/src/libc/strncpy.c b/src/libc/string/strncpy.c diff --git a/src/libc/strnlen.c b/src/libc/string/strnlen.c diff --git a/src/libc/strpbrk.c b/src/libc/string/strpbrk.c diff --git a/src/libc/strrchr.c b/src/libc/string/strrchr.c diff --git a/src/libc/strspn.c b/src/libc/string/strspn.c diff --git a/src/libc/strstr.c b/src/libc/string/strstr.c diff --git a/src/libc/strtok.c b/src/libc/string/strtok.c diff --git a/src/libc/strxfrm.c b/src/libc/string/strxfrm.c diff --git a/src/libc/time/Makefile b/src/libc/time/Makefile @@ -0,0 +1,15 @@ +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = _daysyear.o\ + asctime.o\ + ctime.o\ +# difftime.o\ + gmtime.o\ + localtime.o\ + mktime.o\ + strftime.o\ + +all: $(OBJS) diff --git a/src/libc/time/_daysyear.c b/src/libc/time/_daysyear.c @@ -0,0 +1,30 @@ +#include <time.h> +#include "../libc.h" + +int _daysmon[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +int +_daysyear(int year) +{ + if (year%4 != 0) + return 365; + if (year%100 == 0 && year%400 != 0) + return 365; + return 366; +} + +/* + * Happy New Year!!!! + */ +int +_newyear(int year) +{ + int day; + + year += 1900 - 1; + day = 1 + year + year/4; + day -= year/100; + day += year/400; + + return day % 7; +} diff --git a/src/libc/asctime.c b/src/libc/time/asctime.c diff --git a/src/libc/ctime.c b/src/libc/time/ctime.c diff --git a/src/libc/difftime.c b/src/libc/time/difftime.c diff --git a/src/libc/gmtime.c b/src/libc/time/gmtime.c diff --git a/src/libc/localtime.c b/src/libc/time/localtime.c diff --git a/src/libc/mktime.c b/src/libc/time/mktime.c diff --git a/src/libc/strftime.c b/src/libc/time/strftime.c diff --git a/src/libc/tmpnam.c b/src/libc/tmpnam.c @@ -1,31 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include "syscall.h" -#undef tmpnam - -char * -tmpnam(char *s) -{ - static char *tmpl, buf[L_tmpnam]; - char *p; - - if (*buf == '\0') { - for (tmpl = buf, p = _TMPNAME; *tmpl++ = *p++; ) - ; - for (p = tmpl; p < &buf[L_tmpnam-1]; ++p) - *p = '0'; - *p = '\0'; - } - for (;;) { - for (p = tmpl; *p && *p != '9'; ++p) - ; - if (*p == '\0') - return NULL; - ++*p; - if (_access(buf, 0) != 0) - break; - } - if (s) - strcpy(s, buf); - return buf; -}