commit 5b0cd9a2453ffcb5c7a0a6277e64b3380a3f743b parent bd867f64d1284bc597e91c0df6c28fecd298e28a Author: Roberto E. Vargas Caballero <k0ga@shike2.com> Date: Thu, 8 Nov 2018 16:54:32 +0000 [libc] Rewrite build system of libc Diffstat:
301 files changed, 2285 insertions(+), 1988 deletions(-)
diff --git a/lib/c/.gitignore b/lib/c/.gitignore @@ -0,0 +1 @@ +objlst diff --git a/lib/c/Makefile b/lib/c/Makefile @@ -1,9 +1,29 @@ .POSIX: +PROJECTDIR =../.. +include $(PROJECTDIR)/scripts/rules.mk -PROJECTDIR = ../.. -include $(PROJECTDIR)/rules.mk +TARGET = $(PROJECTDIR)/lib/libc.a +DIRS = arch\ + assert\ + ctype\ + locale\ + stdio\ + stdlib\ + string\ + time\ -DIRS = target +all: $(DIRS) + +@$(MAKE) $(TARGET) -all dep clean distclean: +$(DIRS): FORCE + +@cd $@ && $(MAKE) + +objlst: FORCE + ./mklst $(TARGET) + +$(TARGET): objlst + xargs $(AR) $(ARFLAGS) $@ < objlst + +clean: $(FORALL) + rm -f objlst diff --git a/lib/c/__abs.c b/lib/c/__abs.c @@ -1,4 +0,0 @@ -#define __USE_MACROS -#include <stdlib.h> - -int __abs; diff --git a/lib/c/__getc.c b/lib/c/__getc.c @@ -1,45 +0,0 @@ -#include <errno.h> -#include <stdio.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/lib/c/__labs.c b/lib/c/__labs.c @@ -1,4 +0,0 @@ -#define __USE_MACROS -#include <stdlib.h> - -long __labs; diff --git a/lib/c/__llabs.c b/lib/c/__llabs.c @@ -1,4 +0,0 @@ -#define __USE_MACROS -#include <stdlib.h> - -long long __llabs; diff --git a/lib/c/__putc.c b/lib/c/__putc.c @@ -1,85 +0,0 @@ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> - -extern int _flsbuf(FILE *fp); - -int -fflush(FILE *fp) -{ - int err; - - if (fp) - return _flsbuf(fp); - - err = 0; - for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) { - if ((fp->flags & _IOWRITE) == 0 && _flsbuf(fp)) - err = EOF; - } - return err; -} - -static void -cleanup(void) -{ - fflush(NULL); -} - -int -__putc(int ch, FILE *fp) -{ - static int first = 1; - - if (fp->flags & _IOERR) - return EOF; - - if (fp->flags & _IOREAD) { - fp->flags |= _IOERR; - errno = EBADF; - return EOF; - } - - if (fp->flags & _IOSTRG) { - fp->flags |= _IOERR; - 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 (first) { - if (atexit(cleanup)) { - fp->flags |= _IOERR; - errno = ENOMEM; - return EOF; - } - first = 0; - } - - if (fp->flags & _IOLBF) { - if (fp->lp == fp->rp && _flsbuf(fp)) - return EOF; - *fp->lp++ = ch; - if (ch == '\n' && _flsbuf(fp)) - return EOF; - } else if (fp->flags & _IOFBF) { - if (fp->wp == fp->rp && _flsbuf(fp)) - return EOF; - *fp->wp++ = ch; - } else { - *fp->wp++ = ch; - if (_flsbuf(fp)) - return EOF; - } - - fp->flags |= _IOWRITE; - return ch & 0xFF; -} diff --git a/lib/c/_daysyear.c b/lib/c/_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/lib/c/_flsbuf.c b/lib/c/_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/lib/c/_fpopen.c b/lib/c/_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/lib/c/arch/.gitignore b/lib/c/arch/.gitignore @@ -0,0 +1 @@ +syscall diff --git a/lib/c/arch/Makefile b/lib/c/arch/Makefile @@ -0,0 +1,11 @@ +.POSIX: +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +DIRS = amd64 arm64 + +all: + +@cd $(ARCH) && $(MAKE) + +clean: + $(FORALL) diff --git a/lib/c/arch/amd64/Makefile b/lib/c/arch/amd64/Makefile @@ -0,0 +1,14 @@ +.POSIX: +PROJECTDIR =../../../.. +include $(PROJECTDIR)/scripts/rules.mk + +OBJS = longjmp.o setjmp.o +DIRS = netbsd openbsd dragonfly linux + +all: $(OBJS) $(SYS) + +$(SYS): FORCE + +@cd $@ && $(MAKE) + +clean: + $(FORALL) diff --git a/lib/c/arch/amd64/dragonfly/.gitignore b/lib/c/arch/amd64/dragonfly/.gitignore @@ -0,0 +1,9 @@ +_Exit.s +_close.s +_brk.s +_getpid.s +_kill.s +_lseek.s +_open.s +_read.s +_write.s diff --git a/lib/c/arch/amd64/dragonfly/Makefile b/lib/c/arch/amd64/dragonfly/Makefile @@ -0,0 +1,33 @@ +.POSIX: +PROJECTDIR =../../../../.. +include $(PROJECTDIR)/scripts/rules.mk + +OBJS = _Exit.o \ + _close.o \ + _getpid.o \ + _kill.o \ + _lseek.o \ + _open.o \ + _read.o \ + _write.o \ + _brk.o \ + _getheap.o \ + _sigaction.o\ + _tzone.o\ + getenv.o\ + raise.o\ + signal.o\ + time.o\ + +all: syscall + $(MAKE) objs + +objs: $(OBJS) + +syscall: syscall.lst + ./gensys.sh syscall.lst + touch syscall + +clean: + rm -f `awk '/[0-9]* _/ {print $$2".s"}' syscall.lst` + rm -f syscall diff --git a/lib/c/arch/amd64/dragonfly/_getheap.s b/lib/c/arch/amd64/dragonfly/_getheap.s @@ -0,0 +1,6 @@ + .file "_getheap.s" + + .globl _getheap +_getheap: + movq $end,%rax + retq diff --git a/lib/c/target/amd64-sysv-netbsd/_sigaction.c b/lib/c/arch/amd64/dragonfly/_sigaction.c diff --git a/lib/c/arch/amd64/dragonfly/_tzone.c b/lib/c/arch/amd64/dragonfly/_tzone.c @@ -0,0 +1 @@ +#include "../../posix/_tzone.c" diff --git a/lib/c/arch/amd64/dragonfly/gensys.sh b/lib/c/arch/amd64/dragonfly/gensys.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +sed 's/[ ]*#.*// + /^$/d' syscall.lst | +while read num name +do +cat <<EOF > $name.s + .file "$name.s" + + .globl $name +$name: + movq \$$num,%rax + syscall + jb 1f + retq + +1: movl %eax,(errno) + movl \$-1,%eax + retq +EOF +done diff --git a/lib/c/arch/amd64/dragonfly/getenv.c b/lib/c/arch/amd64/dragonfly/getenv.c @@ -0,0 +1 @@ +#include "../../posix/getenv.c" diff --git a/lib/c/arch/amd64/dragonfly/raise.c b/lib/c/arch/amd64/dragonfly/raise.c @@ -0,0 +1 @@ +#include "../../posix/raise.c" diff --git a/lib/c/arch/amd64/dragonfly/signal.c b/lib/c/arch/amd64/dragonfly/signal.c @@ -0,0 +1 @@ +#include "../../posix/signal.c" diff --git a/lib/c/arch/amd64/dragonfly/syscall.lst b/lib/c/arch/amd64/dragonfly/syscall.lst @@ -0,0 +1,10 @@ +#number name +1 _Exit +3 _read +4 _write +5 _open +6 _close +17 _brk +20 _getpid +37 _kill +199 _lseek diff --git a/lib/c/arch/amd64/dragonfly/time.c b/lib/c/arch/amd64/dragonfly/time.c @@ -0,0 +1 @@ +#include "../../posix/time.c" diff --git a/lib/c/arch/amd64/linux/.gitignore b/lib/c/arch/amd64/linux/.gitignore @@ -0,0 +1,11 @@ +_Exit.s +_close.s +_getpid.s +_kill.s +_lseek.s +_open.s +_read.s +_write.s +_brk.s +_sigaction.s +_sys_errlist.c diff --git a/lib/c/arch/amd64/linux/Makefile b/lib/c/arch/amd64/linux/Makefile @@ -0,0 +1,37 @@ +.POSIX: +PROJECTDIR =../../../../.. +include $(PROJECTDIR)/scripts/rules.mk +include ../../rules.mk + +OBJS = _Exit.o \ + _close.o \ + _getpid.o \ + _kill.o \ + _lseek.o \ + _open.o \ + _sigaction.o \ + _read.o \ + _write.o \ + _brk.o \ + _getheap.o \ + _cerrno.o \ + _sigaction.o \ + _tzone.o \ + getenv.o \ + raise.o \ + signal.o \ + time.o \ + _sys_errlist.o \ + +all: syscall + $(MAKE) objs + +objs: $(OBJS) + +syscall: syscall.lst + ./gensys.sh syscall.lst + touch syscall + +clean: + rm -f `awk '/[0-9]* _/ {print $$2".s"}' syscall.lst` + rm -f syscall _sys_errlist.c diff --git a/lib/c/arch/amd64/linux/_cerrno.s b/lib/c/arch/amd64/linux/_cerrno.s @@ -0,0 +1,12 @@ + .file "_cerrno.s" + .globl _cerrno + +_cerrno: + cmpq $0,%rax + js 1f + retq + +1: neg %rax + mov %eax,(errno) + mov $-1,%eax + retq diff --git a/lib/c/arch/amd64/linux/_getheap.s b/lib/c/arch/amd64/linux/_getheap.s @@ -0,0 +1,6 @@ + .file "_getheap.s" + + .globl _getheap +_getheap: + movq $0,%rax + jmp _brk diff --git a/lib/c/target/amd64-sysv-netbsd/_sigaction.c b/lib/c/arch/amd64/linux/_sigaction.c diff --git a/lib/c/arch/amd64/linux/_tzone.c b/lib/c/arch/amd64/linux/_tzone.c @@ -0,0 +1 @@ +#include "../../posix/_tzone.c" diff --git a/lib/c/target/posix/linux.e b/lib/c/arch/amd64/linux/errno.lst diff --git a/lib/c/arch/amd64/linux/gensys.sh b/lib/c/arch/amd64/linux/gensys.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +sed 's/[ ]*#.*// + /^$/d' syscall.lst | +while read num name +do +cat <<EOF > $name.s + .file "$name.s" + + .globl $name +$name: + movq \$$num,%rax + syscall + jmp _cerrno +EOF +done diff --git a/lib/c/arch/amd64/linux/getenv.c b/lib/c/arch/amd64/linux/getenv.c @@ -0,0 +1 @@ +#include "../../posix/getenv.c" diff --git a/lib/c/arch/amd64/linux/raise.c b/lib/c/arch/amd64/linux/raise.c @@ -0,0 +1 @@ +#include "../../posix/raise.c" diff --git a/lib/c/arch/amd64/linux/signal.c b/lib/c/arch/amd64/linux/signal.c @@ -0,0 +1 @@ +#include "../../posix/signal.c" diff --git a/lib/c/arch/amd64/linux/syscall.lst b/lib/c/arch/amd64/linux/syscall.lst @@ -0,0 +1,11 @@ +#number name +0 _read +1 _write +2 _open +3 _close +8 _lseek +12 _brk +13 _sigaction +39 _getpid +60 _Exit +62 _kill diff --git a/lib/c/arch/amd64/linux/time.c b/lib/c/arch/amd64/linux/time.c @@ -0,0 +1 @@ +#include "../../posix/time.c" diff --git a/lib/c/arch/amd64/longjmp.s b/lib/c/arch/amd64/longjmp.s @@ -0,0 +1,20 @@ + .file "longjmp" + + .text + .globl longjmp +longjmp: + mov %rsi,%rax + test %rax,%rax + jnz 1f + inc %rax +1: + mov (%rdi),%rbx + mov 8(%rdi),%rbp + mov 16(%rdi),%r12 + mov 24(%rdi),%r13 + mov 32(%rdi),%r14 + mov 40(%rdi),%r15 + mov 48(%rdi),%rdx + mov %rdx,%rsp + mov 56(%rdi),%rdx + jmp *%rdx diff --git a/lib/c/arch/amd64/netbsd/.gitignore b/lib/c/arch/amd64/netbsd/.gitignore @@ -0,0 +1,9 @@ +_Exit.s +_close.s +_brk.s +_getpid.s +_kill.s +_lseek.s +_open.s +_read.s +_write.s diff --git a/lib/c/arch/amd64/netbsd/Makefile b/lib/c/arch/amd64/netbsd/Makefile @@ -0,0 +1,36 @@ +.POSIX: +PROJECTDIR =../../../../.. +include $(PROJECTDIR)/scripts/rules.mk + +OBJS = _Exit.o \ + _close.o \ + _getpid.o \ + _kill.o \ + _lseek.o \ + _open.o \ + _read.o \ + _write.o \ + _brk.o \ + _getheap.o \ + _setcontext.o \ + _sigaction.o \ + _sigaction2.o \ + _sigaction.o\ + _tzone.o\ + getenv.o\ + raise.o\ + signal.o\ + time.o\ + +all: syscall + $(MAKE) objs + +objs: $(OBJS) + +syscall: syscall.lst + ./gensys.sh syscall.lst + touch syscall + +clean: + rm -f `awk '/[0-9]* _/ {print $$2".s"}' syscall.lst` + rm -f syscall diff --git a/lib/c/arch/amd64/netbsd/_getheap.s b/lib/c/arch/amd64/netbsd/_getheap.s @@ -0,0 +1,6 @@ + .file "_getheap.s" + + .globl _getheap +_getheap: + movq $end,%rax + retq diff --git a/lib/c/target/amd64-sysv-netbsd/_setcontext.s b/lib/c/arch/amd64/netbsd/_setcontext.s diff --git a/lib/c/target/amd64-sysv-netbsd/_sigaction.c b/lib/c/arch/amd64/netbsd/_sigaction.c diff --git a/lib/c/target/amd64-sysv-netbsd/_sigaction2.s b/lib/c/arch/amd64/netbsd/_sigaction2.s diff --git a/lib/c/arch/amd64/netbsd/_tzone.c b/lib/c/arch/amd64/netbsd/_tzone.c @@ -0,0 +1 @@ +#include "../../posix/_tzone.c" diff --git a/lib/c/arch/amd64/netbsd/gensys.sh b/lib/c/arch/amd64/netbsd/gensys.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +sed 's/[ ]*#.*// + /^$/d' syscall.lst | +while read num name +do +cat <<EOF > $name.s + .file "$name.s" + + .globl $name +$name: + movq \$$num,%rax + syscall + jb 1f + retq + +1: movl %eax,(errno) + movl \$-1,%eax + retq +EOF +done diff --git a/lib/c/arch/amd64/netbsd/getenv.c b/lib/c/arch/amd64/netbsd/getenv.c @@ -0,0 +1 @@ +#include "../../posix/getenv.c" diff --git a/lib/c/arch/amd64/netbsd/raise.c b/lib/c/arch/amd64/netbsd/raise.c @@ -0,0 +1 @@ +#include "../../posix/raise.c" diff --git a/lib/c/arch/amd64/netbsd/signal.c b/lib/c/arch/amd64/netbsd/signal.c @@ -0,0 +1 @@ +#include "../../posix/signal.c" diff --git a/lib/c/arch/amd64/netbsd/syscall.lst b/lib/c/arch/amd64/netbsd/syscall.lst @@ -0,0 +1,11 @@ +#number name +1 _Exit +3 _read +4 _write +5 _open +6 _close +17 _brk +20 _getpid +37 _kill +199 _lseek +418 _gettimeofday diff --git a/lib/c/arch/amd64/netbsd/time.c b/lib/c/arch/amd64/netbsd/time.c @@ -0,0 +1 @@ +#include "../../posix/time.c" diff --git a/lib/c/arch/amd64/openbsd/.gitignore b/lib/c/arch/amd64/openbsd/.gitignore @@ -0,0 +1,9 @@ +_Exit.s +_close.s +_brk.s +_getpid.s +_kill.s +_lseek.s +_open.s +_read.s +_write.s diff --git a/lib/c/arch/amd64/openbsd/Makefile b/lib/c/arch/amd64/openbsd/Makefile @@ -0,0 +1,35 @@ +.POSIX: +PROJECTDIR =../../../../.. +include $(PROJECTDIR)/scripts/rules.mk +include ../../rules.mk + +OBJS = _Exit.o \ + _close.o \ + _getpid.o \ + _kill.o \ + _lseek.o \ + _open.o \ + _read.o \ + _write.o \ + _brk.o \ + _getheap.o \ + _sigaction.o\ + _tzone.o \ + getenv.o \ + raise.o \ + signal.o \ + time.o \ + _sys_errlist.o \ + +all: syscall + $(MAKE) objs + +objs: $(OBJS) + +syscall: syscall.lst + ./gensys.sh syscall.lst + touch syscall + +clean: + rm -f `awk '/[0-9]* _/ {print $$2".s"}' syscall.lst` + rm -f syscall _sys_errlist.c diff --git a/lib/c/arch/amd64/openbsd/_getheap.s b/lib/c/arch/amd64/openbsd/_getheap.s @@ -0,0 +1,6 @@ + .file "_getheap.s" + + .globl _getheap +_getheap: + movq $end,%rax + retq diff --git a/lib/c/target/amd64-sysv-netbsd/_sigaction.c b/lib/c/arch/amd64/openbsd/_sigaction.c diff --git a/lib/c/arch/amd64/openbsd/_tzone.c b/lib/c/arch/amd64/openbsd/_tzone.c @@ -0,0 +1 @@ +#include "../../posix/_tzone.c" diff --git a/lib/c/target/posix/openbsd.e b/lib/c/arch/amd64/openbsd/errno.lst diff --git a/lib/c/arch/amd64/openbsd/gensys.sh b/lib/c/arch/amd64/openbsd/gensys.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +sed 's/[ ]*#.*// + /^$/d' syscall.lst | +while read num name +do +cat <<EOF > $name.s + .file "$name.s" + + .globl $name +$name: + movq \$$num,%rax + syscall + jb 1f + retq + +1: movl %eax,(errno) + movl \$-1,%eax + retq +EOF +done diff --git a/lib/c/arch/amd64/openbsd/getenv.c b/lib/c/arch/amd64/openbsd/getenv.c @@ -0,0 +1 @@ +#include "../../posix/getenv.c" diff --git a/lib/c/arch/amd64/openbsd/raise.c b/lib/c/arch/amd64/openbsd/raise.c @@ -0,0 +1 @@ +#include "../../posix/raise.c" diff --git a/lib/c/arch/amd64/openbsd/signal.c b/lib/c/arch/amd64/openbsd/signal.c @@ -0,0 +1 @@ +#include "../../posix/signal.c" diff --git a/lib/c/arch/amd64/openbsd/syscall.lst b/lib/c/arch/amd64/openbsd/syscall.lst @@ -0,0 +1,11 @@ +#number name +1 _Exit +3 _read +4 _write +5 _open +6 _close +17 _brk +20 _getpid +46 _sigaction +122 _kill +198 _lseek diff --git a/lib/c/arch/amd64/openbsd/time.c b/lib/c/arch/amd64/openbsd/time.c @@ -0,0 +1 @@ +#include "../../posix/time.c" diff --git a/lib/c/arch/amd64/setjmp.s b/lib/c/arch/amd64/setjmp.s @@ -0,0 +1,17 @@ + .file "setjmp.s" + + .text + .globl setjmp +setjmp: + mov %rbx,(%rdi) + mov %rbp,8(%rdi) + mov %r12,16(%rdi) + mov %r13,24(%rdi) + mov %r14,32(%rdi) + mov %r15,40(%rdi) + lea 8(%rsp),%rdx + mov %rdx,48(%rdi) + mov (%rsp),%rdx + mov %rdx,56(%rdi) + xor %rax,%rax + ret diff --git a/lib/c/arch/arm64/Makefile b/lib/c/arch/arm64/Makefile @@ -0,0 +1,14 @@ +.POSIX: +PROJECTDIR =../../../.. +include $(PROJECTDIR)/scripts/rules.mk + +OBJS = longjmp.o setjmp.o +DIRS = linux + +all: $(OBJS) $(SYS) + +$(SYS): FORCE + +@cd $@ && $(MAKE) + +clean: + $(FORALL) diff --git a/lib/c/arch/arm64/linux/.gitignore b/lib/c/arch/arm64/linux/.gitignore @@ -0,0 +1,9 @@ +_Exit.s +_close.s +_brk.s +_getpid.s +_kill.s +_lseek.s +_openat.s +_read.s +_write.s diff --git a/lib/c/arch/arm64/linux/Makefile b/lib/c/arch/arm64/linux/Makefile @@ -0,0 +1,35 @@ +.POSIX: +PROJECTDIR =../../../../.. +include $(PROJECTDIR)/scripts/rules.mk + +OBJS = _Exit.o \ + _close.o \ + _brk.o \ + _getpid.o \ + _kill.o \ + _lseek.o \ + _openat.o \ + _read.o \ + _write.o \ + _getheap.o \ + _cerrno.o \ + _open.o \ + _sigaction.o \ + _tzone.o \ + getenv.o \ + raise.o \ + signal.o \ + time.o \ + +all: syscall + $(MAKE) objs + +objs: $(OBJS) + +syscall: syscall.lst + ./gensys.sh syscall.lst + touch syscall + +clean: + rm -f `awk '/[0-9]* _/ {print $$2".s"}' syscall.lst` + rm -f syscall diff --git a/lib/c/arch/arm64/linux/_cerrno.s b/lib/c/arch/arm64/linux/_cerrno.s @@ -0,0 +1,13 @@ + .file "_cerrno.s" + .globl _cerrno + +_cerrno: + cmp x0,#0 + b.lt 1f + ret + +1: neg x0,x0 + adr x1,errno + str w1,[x0] + mov x0,#-1 + ret diff --git a/lib/c/arch/arm64/linux/_getheap.s b/lib/c/arch/arm64/linux/_getheap.s @@ -0,0 +1,6 @@ + .file "_getheap.s" + + .globl _getheap +_getheap: + mov x0,#0 + b _brk diff --git a/lib/c/arch/arm64/linux/_open.c b/lib/c/arch/arm64/linux/_open.c @@ -0,0 +1,13 @@ +#include <stddef.h> + +#include "../../../syscall.h" + +#define AT_FDCWD -100 + +extern int _openat(int fd, const char *fname, int flags); + +int +_open(const char *fname, int flags) +{ + return _openat(AT_FDCWD, fname, flags); +} diff --git a/lib/c/target/amd64-sysv-netbsd/_sigaction.c b/lib/c/arch/arm64/linux/_sigaction.c diff --git a/lib/c/arch/arm64/linux/_tzone.c b/lib/c/arch/arm64/linux/_tzone.c @@ -0,0 +1 @@ +#include "../../posix/_tzone.c" diff --git a/lib/c/arch/arm64/linux/gensys.sh b/lib/c/arch/arm64/linux/gensys.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +sed 's/[ ]*#.*// + /^$/d' syscall.lst | +while read num name +do +cat <<EOF > $name.s + .file "$name.s" + + .globl $name +$name: + mov x8,#$num + svc 0 + b _cerrno +EOF +done diff --git a/lib/c/arch/arm64/linux/getenv.c b/lib/c/arch/arm64/linux/getenv.c @@ -0,0 +1 @@ +#include "../../posix/getenv.c" diff --git a/lib/c/arch/arm64/linux/raise.c b/lib/c/arch/arm64/linux/raise.c @@ -0,0 +1 @@ +#include "../../posix/raise.c" diff --git a/lib/c/arch/arm64/linux/signal.c b/lib/c/arch/arm64/linux/signal.c @@ -0,0 +1 @@ +#include "../../posix/signal.c" diff --git a/lib/c/arch/arm64/linux/syscall.lst b/lib/c/arch/arm64/linux/syscall.lst @@ -0,0 +1,10 @@ +#number name +56 _openat +57 _close +63 _read +64 _write +93 _Exit +172 _getpid +129 _kill +62 _lseek +214 _brk diff --git a/lib/c/arch/arm64/linux/time.c b/lib/c/arch/arm64/linux/time.c @@ -0,0 +1 @@ +#include "../../posix/time.c" diff --git a/lib/c/arch/arm64/longjmp.s b/lib/c/arch/arm64/longjmp.s @@ -0,0 +1,22 @@ + .file "longjmp.s" + + .text + .globl longjmp +longjmp: + ldp x19, x20, [x0,#0] + ldp x21, x22, [x0,#16] + ldp x23, x24, [x0,#32] + ldp x25, x26, [x0,#48] + ldp x27, x28, [x0,#64] + ldp x29, x30, [x0,#80] + ldr x2, [x0,#104] + mov sp, x2 + ldp d8 , d9, [x0,#112] + ldp d10, d11, [x0,#128] + ldp d12, d13, [x0,#144] + ldp d14, d15, [x0,#160] + + mov x0, x1 + cbnz x1, 1f + mov x0, #1 +1: br x30 diff --git a/lib/c/arch/arm64/setjmp.s b/lib/c/arch/arm64/setjmp.s @@ -0,0 +1,20 @@ + .file "setjmp.s" + + .text + .globl setjmp +setjmp: + // IHI0055B_aapcs64.pdf 5.1.1, 5.1.2 callee saved registers + stp x19, x20, [x0,#0] + stp x21, x22, [x0,#16] + stp x23, x24, [x0,#32] + stp x25, x26, [x0,#48] + stp x27, x28, [x0,#64] + stp x29, x30, [x0,#80] + mov x2, sp + str x2, [x0,#104] + stp d8, d9, [x0,#112] + stp d10, d11, [x0,#128] + stp d12, d13, [x0,#144] + stp d14, d15, [x0,#160] + mov x0, #0 + ret diff --git a/lib/c/arch/generrno.sh b/lib/c/arch/generrno.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +trap 'r=$?; rm -f $$.tmp; exit $r' EXIT HUP QUIT INT TERM + +for i +do + case $i in + -o) + out=$2 + shift 2 + ;; + --) + shift + break + ;; + -*) + echo usage: generrno.sh [-o output] file ... + exit 1 + ;; + *) + break + ;; + esac +done + +awk ' +/^E/ && $2 > 0 { + errno[$1] = $2 +} + +END { + for (i in errno) + print "#define", i, errno[i] | "sort -n -k3" + close("sort -n -k3") +}' $@ > $$.tmp && mv $$.tmp ${out:-errno.h} diff --git a/lib/c/arch/generrstr.sh b/lib/c/arch/generrstr.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +trap 'r=$?; rm -f $$.tmp; exit $r' EXIT HUP INT QUIT TERM + +awk ' +/^E/ && $2 > 0 { + str = "" + for (i = 3; i <= NF; i++) + str = str " " $i + sub(/^ /, "", str) + errstr[$1] = str + if ($2 > max) + max = $2; +} + +END { + print "#include <errno.h>\n" + print "char *_sys_errlist[] = {" + for (i in errstr) + printf "\t%-20.20s = \"%s\",\n", "[" i "]", errstr[i] + print "};" + print "int _sys_nerr =", $2 + 1 ";" +}' $@ > $$.tmp && mv $$.tmp _sys_errlist.c diff --git a/lib/c/target/posix/_tzone.c b/lib/c/arch/posix/_tzone.c diff --git a/lib/c/target/posix/getenv.c b/lib/c/arch/posix/getenv.c diff --git a/lib/c/target/posix/geterrno.sh b/lib/c/arch/posix/geterrno.sh diff --git a/lib/c/target/posix/raise.c b/lib/c/arch/posix/raise.c diff --git a/lib/c/target/posix/signal.c b/lib/c/arch/posix/signal.c diff --git a/lib/c/target/posix/time.c b/lib/c/arch/posix/time.c diff --git a/lib/c/arch/rules.mk b/lib/c/arch/rules.mk @@ -0,0 +1,7 @@ +SYSERRNO = $(INCDIR)/bits/$(SYS)/sys/errno.h + +$(SYSERRNO): errno.lst + ../../generrno.sh -o $@ errno.lst + +_sys_errlist.c: errno.lst $(SYSERRNO) + ../../generrstr.sh errno.lst diff --git a/lib/c/assert/Makefile b/lib/c/assert/Makefile @@ -0,0 +1,10 @@ +.POSIX: +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = __assert.o\ + assert.o\ + +all: $(OBJS) diff --git a/lib/c/__assert.c b/lib/c/assert/__assert.c diff --git a/lib/c/assert/assert.c b/lib/c/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/lib/c/ctype.c b/lib/c/ctype.c @@ -1,25 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> - -int __ctmp; - -/* __ctype is shifted by one to match EOF */ -unsigned char __ctype[257] = { - 0, /* EOF */ - _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ - _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ - _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ - _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ - _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ - _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ - _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ - _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ - _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ - _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ - _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ - _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ - _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ - _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ - _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ - _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ -}; diff --git a/lib/c/ctype/Makefile b/lib/c/ctype/Makefile @@ -0,0 +1,24 @@ +.POSIX: +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/lib/c/ctype/ctype.c b/lib/c/ctype/ctype.c @@ -0,0 +1,24 @@ +#include <ctype.h> + +int __ctmp; + +/* __ctype is shifted by one to match EOF */ +unsigned char __ctype[257] = { + 0, /* EOF */ + _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ + _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ + _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ + _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */ + _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */ + _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */ + _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */ + _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */ + _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */ + _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */ + _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */ + _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */ + _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */ + _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */ + _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */ + _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */ +}; diff --git a/lib/c/ctype/isalnum.c b/lib/c/ctype/isalnum.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isalnum + +int +isalnum(int c) +{ + return (__ctype+1)[c] & (_U|_L|_D); +} diff --git a/lib/c/ctype/isalpha.c b/lib/c/ctype/isalpha.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isalpha + +int +isalpha(int c) +{ + return (__ctype+1)[c] & (_U|_L); +} diff --git a/lib/c/isascii.c b/lib/c/ctype/isascii.c diff --git a/lib/c/isblank.c b/lib/c/ctype/isblank.c diff --git a/lib/c/ctype/iscntrl.c b/lib/c/ctype/iscntrl.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef iscntrl + +int +iscntrl(int c) +{ + return (__ctype+1)[c] & (_C); +} diff --git a/lib/c/ctype/isdigit.c b/lib/c/ctype/isdigit.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isdigit + +int +isdigit(int c) +{ + return (__ctype+1)[c] & (_D); +} diff --git a/lib/c/ctype/isgraph.c b/lib/c/ctype/isgraph.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isgraph + +int +isgraph(int c) +{ + return (__ctype+1)[c] & (_P|_U|_L|_D); +} diff --git a/lib/c/ctype/islower.c b/lib/c/ctype/islower.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef islower + +int +islower(int c) +{ + return (__ctype+1)[c] & _L; +} diff --git a/lib/c/ctype/isprint.c b/lib/c/ctype/isprint.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isprint + +int +isprint(int c) +{ + return (__ctype+1)[c] & (_P|_U|_L|_D|_SP); +} diff --git a/lib/c/ctype/ispunct.c b/lib/c/ctype/ispunct.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef ispunct + +int +ispunct(int c) +{ + return (__ctype+1)[c] & (_P); +} diff --git a/lib/c/ctype/isspace.c b/lib/c/ctype/isspace.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isspace + +int +isspace(int c) +{ + return (__ctype+1)[c] & _S; +} diff --git a/lib/c/ctype/isupper.c b/lib/c/ctype/isupper.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isupper + +int +isupper(int c) +{ + return (__ctype+1)[c] & _U; +} diff --git a/lib/c/ctype/isxdigit.c b/lib/c/ctype/isxdigit.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef isxdigit + +int +isxdigit(int c) +{ + return (__ctype+1)[c] & (_D|_X); +} diff --git a/lib/c/tolower.c b/lib/c/ctype/tolower.c diff --git a/lib/c/ctype/toupper.c b/lib/c/ctype/toupper.c @@ -0,0 +1,8 @@ +#include <ctype.h> +#undef toupper + +int +toupper(int c) +{ + return (islower(c)) ? c & ~0x20 : c; +} diff --git a/lib/c/fclose.c b/lib/c/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/lib/c/fopen.c b/lib/c/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/lib/c/freopen.c b/lib/c/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/lib/c/fseek.c b/lib/c/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/lib/c/ftell.c b/lib/c/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/lib/c/isalnum.c b/lib/c/isalnum.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isalnum - -int -isalnum(int c) -{ - return (__ctype+1)[c] & (_U|_L|_D); -} diff --git a/lib/c/isalpha.c b/lib/c/isalpha.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isalpha - -int -isalpha(int c) -{ - return (__ctype+1)[c] & (_U|_L); -} diff --git a/lib/c/iscntrl.c b/lib/c/iscntrl.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef iscntrl - -int -iscntrl(int c) -{ - return (__ctype+1)[c] & (_C); -} diff --git a/lib/c/isdigit.c b/lib/c/isdigit.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isdigit - -int -isdigit(int c) -{ - return (__ctype+1)[c] & (_D); -} diff --git a/lib/c/isgraph.c b/lib/c/isgraph.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isgraph - -int -isgraph(int c) -{ - return (__ctype+1)[c] & (_P|_U|_L|_D); -} diff --git a/lib/c/islower.c b/lib/c/islower.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef islower - -int -islower(int c) -{ - return (__ctype+1)[c] & _L; -} diff --git a/lib/c/isprint.c b/lib/c/isprint.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isprint - -int -isprint(int c) -{ - return (__ctype+1)[c] & (_P|_U|_L|_D|_SP); -} diff --git a/lib/c/ispunct.c b/lib/c/ispunct.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef ispunct - -int -ispunct(int c) -{ - return (__ctype+1)[c] & (_P); -} diff --git a/lib/c/isspace.c b/lib/c/isspace.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isspace - -int -isspace(int c) -{ - return (__ctype+1)[c] & _S; -} diff --git a/lib/c/isupper.c b/lib/c/isupper.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isupper - -int -isupper(int c) -{ - return (__ctype+1)[c] & _U; -} diff --git a/lib/c/isxdigit.c b/lib/c/isxdigit.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef isxdigit - -int -isxdigit(int c) -{ - return (__ctype+1)[c] & (_D|_X); -} diff --git a/lib/c/libc.h b/lib/c/libc.h @@ -33,5 +33,10 @@ struct tm; extern struct tzone *_tzone(struct tm *tm); extern int _daysyear(int year); extern int _newyear(int year); +extern void *_getheap(void); +#ifdef FILE +extern int _flsbuf(FILE *fp); +extern void _allocbuf(FILE *fp); +#endif extern int _daysmon[12]; diff --git a/lib/c/locale/Makefile b/lib/c/locale/Makefile @@ -0,0 +1,10 @@ +.POSIX: +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = localeconv.o\ + setlocale.o\ + +all: $(OBJS) diff --git a/lib/c/localeconv.c b/lib/c/locale/localeconv.c diff --git a/lib/c/setlocale.c b/lib/c/locale/setlocale.c diff --git a/lib/c/malloc.c b/lib/c/malloc.c @@ -1,152 +0,0 @@ -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#include "malloc.h" -#include "syscall.h" - -#define MAXADDR ((char *)-1) -#define ERRADDR ((char *)-1) - -extern char end[]; -static Header base = { .h.next = &base }; -static Header *freep = &base; -static char *heap = end; - -/* - * 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 = heap; - - if (old >= MAXADDR - inc) - return ERRADDR; - - new = old + inc; - if (_brk(new) < 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) - return NULL; - } - } -} diff --git a/lib/c/mklst b/lib/c/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/lib/c/setvbuf.c b/lib/c/setvbuf.c @@ -1,46 +0,0 @@ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#undef setvbuf - -extern int _flsbuf(FILE *fp); - -int -setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size) -{ - int flags; - - if (_flsbuf(fp) == EOF) - return EOF; - - switch (mode) { - case _IONBF: - size = sizeof(fp->unbuf); - buf = fp->unbuf; - break; - case _IOLBF: - case _IOFBF: - if (size == 0) { - if ((buf = malloc(BUFSIZ)) == NULL) { - errno = ENOMEM; - return EOF; - } - size = BUFSIZ; - } - break; - default: - errno = EINVAL; - return EOF; - } - - flags = fp->flags; - if (flags & _IOALLOC) - free(fp->buf); - flags &= ~(_IONBF | _IOLBF | _IOFBF | _IOALLOC | _IOALLOC); - flags |= mode; - fp->flags = flags; - fp->buf = buf; - fp->len = size; - - return 0; -} diff --git a/lib/c/snprintf.c b/lib/c/snprintf.c @@ -1,17 +0,0 @@ -#include <stdarg.h> -#include <stdio.h> -#undef snprintf - -int -snprintf(char * restrict s, size_t siz, const char * restrict fmt, ...) -{ - int r; - va_list va; - - va_list va_arg; - va_start(va, fmt); - r = vsnprintf(s, siz, fmt, va); - va_end(va); - - return r; -} diff --git a/lib/c/stdio/Makefile b/lib/c/stdio/Makefile @@ -0,0 +1,47 @@ +.POSIX: +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\ + putc.o\ + putchar.o\ + puts.o\ + rewind.o\ + setbuf.o\ + setvbuf.o\ + snprintf.o\ + sprintf.o\ + __iob.o\ + tmpnam.o\ + vfprintf.o\ + vsnprintf.o\ + vsprintf.o\ + vprintf.o\ + _allocbuf.o\ + +all: $(OBJS) diff --git a/lib/c/stdio/__getc.c b/lib/c/stdio/__getc.c @@ -0,0 +1,39 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include "../libc.h" +#include "../syscall.h" + +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 && _allocbuf(fp)) + return EOF; + + 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/lib/c/stdio.c b/lib/c/stdio/__iob.c diff --git a/lib/c/stdio/__putc.c b/lib/c/stdio/__putc.c @@ -0,0 +1,78 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include "../libc.h" + +int +fflush(FILE *fp) +{ + int err; + + if (fp) + return _flsbuf(fp); + + err = 0; + for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) { + if ((fp->flags & _IOWRITE) == 0 && _flsbuf(fp)) + err = EOF; + } + return err; +} + +static void +cleanup(void) +{ + fflush(NULL); +} + +int +__putc(int ch, FILE *fp) +{ + static int first = 1; + + if (fp->flags & _IOERR) + return EOF; + + if (fp->flags & _IOREAD) { + fp->flags |= _IOERR; + errno = EBADF; + return EOF; + } + + if (fp->flags & _IOSTRG) { + fp->flags |= _IOERR; + return EOF; + } + + if (fp->buf == NULL && _allocbuf(fp)) + return EOF; + + if (first) { + if (atexit(cleanup)) { + fp->flags |= _IOERR; + errno = ENOMEM; + return EOF; + } + first = 0; + } + + if (fp->flags & _IOLBF) { + if (fp->wp == fp->lp && _flsbuf(fp)) + return EOF; + *fp->wp++ = ch; + if (ch == '\n' && _flsbuf(fp)) + return EOF; + } else if (fp->flags & _IOFBF) { + if (_flsbuf(fp)) + return EOF; + *fp->wp++ = ch; + fp->rp = fp->buf + fp->len; + } else { + *fp->wp++ = ch; + if (_flsbuf(fp)) + return EOF; + } + + fp->flags |= _IOWRITE; + return ch & 0xFF; +} diff --git a/lib/c/stdio/_allocbuf.c b/lib/c/stdio/_allocbuf.c @@ -0,0 +1,21 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include "../libc.h" + +int +_allocbuf(FILE *fp) +{ + char *bp; + + if ((bp = malloc(BUFSIZ)) == NULL) { + fp->flags |= _IOERR; + errno = ENOMEM; + return EOF; + } + fp->len = BUFSIZ; + fp->rp = fp->wp = fp->buf = bp; + fp->lp = bp + BUFSIZ; + + return 0; +} diff --git a/lib/c/stdio/_flsbuf.c b/lib/c/stdio/_flsbuf.c @@ -0,0 +1,23 @@ +#include <errno.h> +#include <stdio.h> + +#include "../libc.h" +#include "../syscall.h" + +int +_flsbuf(FILE *fp) +{ + size_t cnt; + + if (fp->flags&_IOREAD) + return 0; + + cnt = fp->wp - fp->buf; + if (cnt > 0 && _write(fp->fd, fp->buf, cnt) != cnt) { + fp->flags |= _IOERR; + return EOF; + } + fp->wp = fp->buf; + + return 0; +} diff --git a/lib/c/stdio/_fpopen.c b/lib/c/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/lib/c/clearerr.c b/lib/c/stdio/clearerr.c diff --git a/lib/c/stdio/fclose.c b/lib/c/stdio/fclose.c @@ -0,0 +1,32 @@ +#include <stdlib.h> +#include <stdio.h> +#include "../syscall.h" +#undef fclose + +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/lib/c/feof.c b/lib/c/stdio/feof.c diff --git a/lib/c/ferror.c b/lib/c/stdio/ferror.c diff --git a/lib/c/fgetc.c b/lib/c/stdio/fgetc.c diff --git a/lib/c/fgets.c b/lib/c/stdio/fgets.c diff --git a/lib/c/stdio/fopen.c b/lib/c/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/lib/c/fprintf.c b/lib/c/stdio/fprintf.c diff --git a/lib/c/fputc.c b/lib/c/stdio/fputc.c diff --git a/lib/c/fputs.c b/lib/c/stdio/fputs.c diff --git a/lib/c/fread.c b/lib/c/stdio/fread.c diff --git a/lib/c/stdio/freopen.c b/lib/c/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/lib/c/stdio/fseek.c b/lib/c/stdio/fseek.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include "../syscall.h" +#undef fseek + +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/lib/c/stdio/ftell.c b/lib/c/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/lib/c/fwrite.c b/lib/c/stdio/fwrite.c diff --git a/lib/c/getc.c b/lib/c/stdio/getc.c diff --git a/lib/c/getchar.c b/lib/c/stdio/getchar.c diff --git a/lib/c/gets.c b/lib/c/stdio/gets.c diff --git a/lib/c/perror.c b/lib/c/stdio/perror.c diff --git a/lib/c/stdio/print.c b/lib/c/stdio/print.c diff --git a/lib/c/printf.c b/lib/c/stdio/printf.c diff --git a/lib/c/putc.c b/lib/c/stdio/putc.c diff --git a/lib/c/putchar.c b/lib/c/stdio/putchar.c diff --git a/lib/c/puts.c b/lib/c/stdio/puts.c diff --git a/lib/c/rewind.c b/lib/c/stdio/rewind.c diff --git a/lib/c/setbuf.c b/lib/c/stdio/setbuf.c diff --git a/lib/c/stdio/setvbuf.c b/lib/c/stdio/setvbuf.c @@ -0,0 +1,48 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#undef setvbuf + +int +setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size) +{ + int flags; + char *p; + size_t l; + + if (_flsbuf(fp) == EOF) + return EOF; + + if (buf) + p = buf, l = size; + else + p = fp->buf, l = fp->len; + + switch (mode) { + case _IONBF: + l = sizeof(fp->unbuf); + p = fp->unbuf; + case _IOLBF: + case _IOFBF: + fp->rp = fp->wp = p; + fp->lp = p + l; + break; + default: + errno = EINVAL; + return EOF; + } + + flags = fp->flags; + if (flags&_IOALLOC && (buf || mode == _IONBF)) { + free(fp->buf); + flags &= ~_IOALLOC; + } + + fp->buf = p; + fp->len = l; + flags &= ~(_IONBF | _IOLBF | _IOFBF); + flags |= mode; + fp->flags = flags; + + return 0; +} diff --git a/lib/c/stdio/snprintf.c b/lib/c/stdio/snprintf.c @@ -0,0 +1,16 @@ +#include <stdarg.h> +#include <stdio.h> +#undef snprintf + +int +snprintf(char * restrict s, size_t siz, const char * restrict fmt, ...) +{ + int r; + va_list va; + + va_start(va, fmt); + r = vsnprintf(s, siz, fmt, va); + va_end(va); + + return r; +} diff --git a/lib/c/sprintf.c b/lib/c/stdio/sprintf.c diff --git a/lib/c/stdio/tmpnam.c b/lib/c/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/lib/c/stdio/vfprintf.c b/lib/c/stdio/vfprintf.c @@ -0,0 +1,362 @@ +#include <ctype.h> +#include <limits.h> +#include <stdarg.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <wchar.h> +#undef vfprintf + +enum { + LONG = 1 << 0, + LLONG = 1 << 1, + SHORT = 1 << 2, + CHAR = 1 << 3, + SIZET = 1 << 4, + PTRDIFF = 1 << 5, + INTMAX = 1 << 6, + VOIDPTR = 1 << 7, + UNSIGNED = 1 << 8, + ALTFORM = 1 << 9, +}; + +#define MAXPREC 50 + +struct conv { + int sign; + int prec; + char *digs; + int base; +}; + +static uintmax_t +getnum(va_list *va, int flags, int *sign) +{ + uintmax_t uval; + intmax_t val; + + if (flags & CHAR) { + val = va_arg(*va, int); + uval = (unsigned char) val; + } else if (flags & SHORT) { + val = va_arg(*va, int); + uval = (unsigned short) val; + } else if (flags & LONG) { + val = va_arg(*va, long); + uval = (unsigned long) val; + } else if (flags & LLONG) { + val = va_arg(*va, long long); + uval = (unsigned long long) val; + } else if (flags & SIZET) { + uval = va_arg(*va, size_t); + } else if (flags & INTMAX) { + val = va_arg(*va, intmax_t); + uval = (uintmax_t) val; + } else if (flags & VOIDPTR) { + uval = (uintmax_t) va_arg(*va, void *); + } else { + val = va_arg(*va, int); + uval = (unsigned) val; + } + + if ((flags & UNSIGNED) == 0 && val < 0) { + *sign = '-'; + uval = -uval; + } + return uval; +} + +static char * +numtostr(uintmax_t val, int flags, struct conv *conv, char *buf) +{ + char *buf0 = buf; + int base = conv->base, prec = conv->prec; + uintmax_t oval = val; + + if (prec == -1) + prec = 1; + + for (*buf = '\0'; val > 0; val /= base) + *--buf = conv->digs[val % base]; + while (buf0 - buf < prec) + *--buf = '0'; + + if (flags & ALTFORM) { + if (base == 8 && *buf != '0') { + *--buf = '0'; + } else if (base == 16 && oval != 0) { + *--buf = conv->digs[16]; + *--buf = '0'; + } + } + if (conv->sign) + *--buf = conv->sign; + + return buf; +} + +static void +savecnt(va_list *va, int flags, int cnt) +{ + if (flags & CHAR) + *va_arg(*va, char*) = cnt; + else if (flags & SHORT) + *va_arg(*va, short*) = cnt; + else if (flags & LONG) + *va_arg(*va, long*) = cnt; + else if (flags & LLONG) + *va_arg(*va, long long*) = cnt; + else if (flags & SIZET) + *va_arg(*va, size_t*) = cnt; + else if (flags & INTMAX) + *va_arg(*va, intmax_t*) = cnt; + else + *va_arg(*va, int*) = cnt; +} + +static size_t +wstrout(wchar_t *ws, size_t len, int width, int fill, FILE * restrict fp) +{ + int left = 0, adjust; + size_t cnt = 0; + wchar_t wc; +#if 0 + + if (width < 0) { + left = 1; + width = -width; + } + + len *= sizeof(wchar_t); + adjust = (len < width) ? width - len : 0; + cnt = adjust + len; + if (left) + adjust = -adjust; + + for ( ; adjust > 0; adjust++) + putc(fill, fp); + + while (wc = *ws++) + putwc(wc, fp); + + for ( ; adjust < 0; adjust--) + putc(' ', fp); +#endif + return cnt; +} + +static size_t +strout(char *s, size_t len, int width, int fill, FILE * restrict fp) +{ + int left = 0, adjust, ch, prefix; + size_t cnt = 0; + + if (width < 0) { + left = 1; + width = -width; + } + + adjust = (len < width) ? width - len : 0; + cnt = adjust + len; + if (left) + adjust = -adjust; + + if (fill == '0') { + if (*s == '-' || *s == '+') + prefix = 1; + else if (*s == '0' && toupper(s[1]) == 'X') + prefix = 2; + else + prefix = 0; + while (prefix--) { + putc(*s++, fp); + --len; + } + } + + for ( ; adjust > 0; adjust--) + putc(fill, fp); + + while (ch = *s++) + putc(ch, fp); + + for ( ; adjust < 0; adjust++) + putc(' ', fp); + + return cnt; +} + +int +vfprintf(FILE * restrict fp, const char *fmt, va_list va) +{ + int ch, n, flags, width, left, fill, cnt = 0; + size_t inc, len; + char *s; + wchar_t *ws; + struct conv conv; + char buf[MAXPREC+1]; + wchar_t wbuf[2]; + va_list va2; + + va_copy(va2, va); + for (cnt = 0; ch = *fmt++; cnt += inc) { + if (ch != '%') { + putc(ch, fp); + inc = 1; + continue; + } + + fill = ' '; + left = flags = width = 0; + conv.prec = -1; + conv.base = 10; + conv.sign = '\0'; + conv.digs = "0123456789ABCDEFX"; + +flags: + switch (*fmt++) { + case ' ': + if (conv.sign == '\0') + conv.sign = ' '; + goto flags; + case '+': + conv.sign = '+'; + goto flags; + case '#': + flags |= ALTFORM; + goto flags; + case '.': + if (*fmt == '*') { + fmt++; + n = va_arg(va2, int); + } else { + for (n = 0; isdigit(ch = *fmt); fmt++) + n = n * 10 + ch - '0'; + } + if (n > MAXPREC) + n = MAXPREC; + if (n > 0) + conv.prec = n; + goto flags; + case '*': + width = va_arg(va2, int); + goto flags; + case '-': + left = 1; + ++fmt; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + --fmt; + for (n = 0; isdigit(ch = *fmt); ++fmt) + n = n * 10 + ch - '0'; + if (left) + n = -n; + width = n; + goto flags; + case '0': + fill = '0'; + goto flags; + case 'l': + flags += LONG; + goto flags; + case 'h': + flags += SHORT; + goto flags; + case '%': + ch = '%'; + goto cout; + case 'c': + if (flags & LONG) { + wbuf[0] = va_arg(va2, wint_t); + wbuf[1] = L'\0'; + ws = wbuf; + len = 1; + goto wstrout; + } + ch = va_arg(va2, int); + cout: + buf[0] = ch; + buf[1] = '\0'; + s = buf; + len = 1; + goto strout; + case 'j': + flags |= INTMAX; + goto flags; + case 't': + flags |= PTRDIFF; + goto flags; + case 'z': + flags |= SIZET; + goto flags; + case 'u': + flags |= UNSIGNED; + case 'i': + case 'd': + conv.base = 10; + goto numeric; + case 'p': + flags |= VOIDPTR | ALTFORM; + goto numeric16; + case 'x': + conv.digs = "0123456789abcdefx"; + case 'X': + numeric16: + conv.base = 16; + flags |= UNSIGNED; + goto numeric; + case 'o': + conv.base = 8; + flags |= UNSIGNED; + numeric: + if (conv.prec != -1) + fill = ' '; + s = numtostr(getnum(&va2, flags, &conv.sign), + flags, + &conv, + &buf[MAXPREC]); + len = &buf[MAXPREC] - s; + goto strout; + case 'L': + case 'a': + case 'A': + case 'e': + case 'E': + case 'f': + case 'g': + case 'G': + /* TODO */ + case 's': + if (flags & LONG) { + ws = va_arg(va2, wchar_t *); + /* len = wcsnlen(ws, conv.prec); */ + goto wstrout; + } else { + s = va_arg(va2, char *); + len = strnlen(s, conv.prec); + goto strout; + } + wstrout: + inc = wstrout(ws, len, width, fill, fp); + break; + strout: + inc = strout(s, len, width, fill, fp); + break; + case 'n': + savecnt(&va2, flags, cnt); + break; + case '\0': + goto out_loop; + } + } + +out_loop: + return (ferror(fp)) ? EOF : cnt; +} diff --git a/lib/c/stdio/vprintf.c b/lib/c/stdio/vprintf.c @@ -0,0 +1,12 @@ +#include <stdarg.h> +#include <stdio.h> +#undef vprintf + +int +vprintf(const char *fmt, va_list ap) +{ + va_list ap2; + + va_copy(ap2, ap); + return vfprintf(stdout, fmt, ap2); +} diff --git a/lib/c/vsnprintf.c b/lib/c/stdio/vsnprintf.c diff --git a/lib/c/vsprintf.c b/lib/c/stdio/vsprintf.c diff --git a/lib/c/stdlib/Makefile b/lib/c/stdlib/Makefile @@ -0,0 +1,25 @@ +.POSIX: +PROJECTDIR =../../.. +include $(PROJECTDIR)/scripts/rules.mk + +MORECFLAGS = -w + +OBJS = 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\ + rand.o\ + realloc.o\ + strtoull.o\ + +all: $(OBJS) diff --git a/lib/c/abort.c b/lib/c/stdlib/abort.c diff --git a/lib/c/abs.c b/lib/c/stdlib/abs.c diff --git a/lib/c/atexit.c b/lib/c/stdlib/atexit.c diff --git a/lib/c/atoi.c b/lib/c/stdlib/atoi.c diff --git a/lib/c/atol.c b/lib/c/stdlib/atol.c diff --git a/lib/c/atoll.c b/lib/c/stdlib/atoll.c diff --git a/lib/c/bsearch.c b/lib/c/stdlib/bsearch.c diff --git a/lib/c/calloc.c b/lib/c/stdlib/calloc.c diff --git a/lib/c/errno.c b/lib/c/stdlib/errno.c diff --git a/lib/c/exit.c b/lib/c/stdlib/exit.c diff --git a/lib/c/labs.c b/lib/c/stdlib/labs.c diff --git a/lib/c/llabs.c b/lib/c/stdlib/llabs.c diff --git a/lib/c/stdlib/malloc.c b/lib/c/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/lib/c/malloc.h b/lib/c/stdlib/malloc.h diff --git a/lib/c/qsort.c b/lib/c/stdlib/qsort.c diff --git a/lib/c/rand.c b/lib/c/stdlib/rand.c diff --git a/lib/c/realloc.c b/lib/c/stdlib/realloc.c diff --git a/lib/c/stdlib/strtoull.c b/lib/c/stdlib/strtoull.c @@ -0,0 +1,64 @@ +#include <ctype.h> +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> + +#undef strtoull + +unsigned long long +strtoull(const char *s, char **end, int base) +{ + int d, sign = 1; + unsigned long long n; + static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const char *t, *p; + + while (isspace(*s)) + ++s; + + switch (*s) { + case '-': + sign = -1; + case '+': + ++s; + } + + if (base == 0) { + if (*s == '0' && toupper(s[1]) == 'X') + base = 16; + else if (*s == '0') + base = 8; + else + base = 10; + } + + if (base == 16 && *s == '0' && toupper(s[1]) == 'X') + s += 2; + + n = 0; + for (t = s; p = strchr(digits, toupper(*t)); ++t) { + if ((d = p - digits) >= base) + break; + if (n > ULLONG_MAX/base) + goto overflow; + n *= base; + if (d > ULLONG_MAX - n) + goto overflow; + n += d; + } + + + if (end) + *end = t; + if (n == 0 && s == t) + errno = EINVAL; + return n*sign; + +overflow: + if (end) + *end = t; + errno = ERANGE; + + return ULLONG_MAX; +} diff --git a/lib/c/string/Makefile b/lib/c/string/Makefile @@ -0,0 +1,31 @@ +.POSIX: +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/lib/c/memchr.c b/lib/c/string/memchr.c diff --git a/lib/c/memcmp.c b/lib/c/string/memcmp.c diff --git a/lib/c/memcpy.c b/lib/c/string/memcpy.c diff --git a/lib/c/memmove.c b/lib/c/string/memmove.c diff --git a/lib/c/memset.c b/lib/c/string/memset.c diff --git a/lib/c/strcat.c b/lib/c/string/strcat.c diff --git a/lib/c/strchr.c b/lib/c/string/strchr.c diff --git a/lib/c/strcmp.c b/lib/c/string/strcmp.c diff --git a/lib/c/strcoll.c b/lib/c/string/strcoll.c diff --git a/lib/c/strcpy.c b/lib/c/string/strcpy.c diff --git a/lib/c/strcspn.c b/lib/c/string/strcspn.c diff --git a/lib/c/strerror.c b/lib/c/string/strerror.c diff --git a/lib/c/strlen.c b/lib/c/string/strlen.c diff --git a/lib/c/strncat.c b/lib/c/string/strncat.c diff --git a/lib/c/strncmp.c b/lib/c/string/strncmp.c diff --git a/lib/c/strncpy.c b/lib/c/string/strncpy.c diff --git a/lib/c/strnlen.c b/lib/c/string/strnlen.c diff --git a/lib/c/strpbrk.c b/lib/c/string/strpbrk.c diff --git a/lib/c/strrchr.c b/lib/c/string/strrchr.c diff --git a/lib/c/strspn.c b/lib/c/string/strspn.c diff --git a/lib/c/strstr.c b/lib/c/string/strstr.c diff --git a/lib/c/strtok.c b/lib/c/string/strtok.c diff --git a/lib/c/strxfrm.c b/lib/c/string/strxfrm.c diff --git a/lib/c/target/.gitignore b/lib/c/target/.gitignore @@ -1 +0,0 @@ -_sys_errlist.c diff --git a/lib/c/target/Makefile b/lib/c/target/Makefile @@ -1,15 +0,0 @@ - -PROJECTDIR = ../../.. - -include $(PROJECTDIR)/rules.mk - -DIRS = amd64-sysv-linux \ - amd64-sysv-openbsd \ - amd64-sysv-netbsd \ - -all clean distclean: - $(FORALL) - -dep: - ./script/objlst.sh - $(FORALL) diff --git a/lib/c/target/amd64-sysv-linux/.gitignore b/lib/c/target/amd64-sysv-linux/.gitignore @@ -1,10 +0,0 @@ -_Exit.s -_brk.s -_close.s -_getpid.s -_kill.s -_lseek.s -_open.s -_read.s -_sigaction.s -_write.s diff --git a/lib/c/target/amd64-sysv-linux/Makefile b/lib/c/target/amd64-sysv-linux/Makefile @@ -1,17 +0,0 @@ -.POSIX: - -PROJECTDIR = ../../../.. -include $(PROJECTDIR)/rules.mk - -SYS = linux -ARCH = amd64 -ABI = sysv -SYSERRTBL = ../posix/linux.e -MORECFLAGS = -std=c99 -g -static -nostdinc -ffreestanding -fno-stack-protector -SYSOBJ = raise.o signal.o - -include syscall.mk -include ../amd64-sysv/objlst.mk -include ../script/objlst.mk -include ../posix/objlst.mk -include ../script/common.mk diff --git a/lib/c/target/amd64-sysv-linux/syscall.lst b/lib/c/target/amd64-sysv-linux/syscall.lst @@ -1,11 +0,0 @@ -#number name -0 _read -1 _write -2 _open -3 _close -3 _lseek -12 _brk -13 _sigaction -38 _getpid -60 _Exit -32 _kill diff --git a/lib/c/target/amd64-sysv-linux/syscall.mk b/lib/c/target/amd64-sysv-linux/syscall.mk @@ -1 +0,0 @@ -SYSCALL = _read.o _write.o _open.o _close.o _lseek.o _brk.o _sigaction.o _getpid.o _Exit.o _kill.o diff --git a/lib/c/target/amd64-sysv-netbsd/.gitignore b/lib/c/target/amd64-sysv-netbsd/.gitignore @@ -1,11 +0,0 @@ -_Exit.s -_brk.s -_close.s -_getpid.s -_kill.s -_lseek.s -_open.s -_read.s -_write.s -_sigaction.s -_gettimeofday.s diff --git a/lib/c/target/amd64-sysv-netbsd/Makefile b/lib/c/target/amd64-sysv-netbsd/Makefile @@ -1,19 +0,0 @@ -.POSIX: - -PROJECTDIR = ../../../.. -include $(PROJECTDIR)/rules.mk - -SYS = netbsd -ARCH = amd64 -ABI = sysv -SYSERRTBL = ../posix/netbsd.e -MORECFLAGS = -std=c99 -g -static -nostdinc -SYSOBJ = _tzone.o getenv.o raise.o signal.o \ - _sigaction.o _sigaction2.o _setcontext.o \ - time.o - -include syscall.mk -include ../amd64-sysv/objlst.mk -include ../script/objlst.mk -include ../posix/objlst.mk -include ../script/common.mk diff --git a/lib/c/target/amd64-sysv-netbsd/syscall.lst b/lib/c/target/amd64-sysv-netbsd/syscall.lst @@ -1,11 +0,0 @@ -#number name -1 _Exit -3 _read -4 _write -5 _open -6 _close -17 _brk -20 _getpid -37 _kill -199 _lseek -418 _gettimeofday diff --git a/lib/c/target/amd64-sysv-netbsd/syscall.mk b/lib/c/target/amd64-sysv-netbsd/syscall.mk @@ -1 +0,0 @@ -SYSCALL = _Exit.o _read.o _write.o _open.o _close.o _brk.o _getpid.o _kill.o _lseek.o _gettimeofday.o diff --git a/lib/c/target/amd64-sysv-openbsd/.gitignore b/lib/c/target/amd64-sysv-openbsd/.gitignore @@ -1,10 +0,0 @@ -_Exit.s -_brk.s -_close.s -_getpid.s -_kill.s -_lseek.s -_open.s -_read.s -_sigaction.s -_write.s diff --git a/lib/c/target/amd64-sysv-openbsd/Makefile b/lib/c/target/amd64-sysv-openbsd/Makefile @@ -1,17 +0,0 @@ -.POSIX: - -PROJECTDIR = ../../../.. -include $(PROJECTDIR)/rules.mk - -SYS = openbsd -ARCH = amd64 -ABI = sysv -SYSERRTBL = ../posix/netbsd.e -MORECFLAGS = -std=c99 -g -static -nostdinc -fno-stack-protector --freestanding -SYSOBJ = raise.o signal.o _sigaction.o - -include syscall.mk -include ../amd64-sysv/objlst.mk -include ../script/objlst.mk -include ../posix/objlst.mk -include ../script/common.mk diff --git a/lib/c/target/amd64-sysv-openbsd/syscall.lst b/lib/c/target/amd64-sysv-openbsd/syscall.lst @@ -1,11 +0,0 @@ -#number name -1 _Exit -3 _read -4 _write -5 _open -6 _close -17 _brk -20 _getpid -46 _sigaction -122 _kill -198 _lseek diff --git a/lib/c/target/amd64-sysv-openbsd/syscall.mk b/lib/c/target/amd64-sysv-openbsd/syscall.mk @@ -1 +0,0 @@ -SYSCALL = _Exit.o _read.o _write.o _open.o _close.o _brk.o _getpid.o _sigaction.o _kill.o _lseek.o diff --git a/lib/c/target/amd64-sysv/longjmp.s b/lib/c/target/amd64-sysv/longjmp.s @@ -1,20 +0,0 @@ -# Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license - .file "longjmp" - .global longjmp - -longjmp: - mov %rsi,%rax # val will be longjmp return - test %rax,%rax - jnz 1f - inc %rax # if val==0, val=1 per longjmp semantics -1: - mov (%rdi),%rbx # rdi is the jmp_buf, restore regs from it - mov 8(%rdi),%rbp - mov 16(%rdi),%r12 - mov 24(%rdi),%r13 - mov 32(%rdi),%r14 - mov 40(%rdi),%r15 - mov 48(%rdi),%rdx # this ends up being the stack pointer - mov %rdx,%rsp - mov 56(%rdi),%rdx # this is the instruction pointer - jmp *%rdx # goto saved address without altering rsp diff --git a/lib/c/target/amd64-sysv/objlst.mk b/lib/c/target/amd64-sysv/objlst.mk @@ -1,7 +0,0 @@ -ARCHOBJ = setjmp.o longjmp.o - -setjmp.o: ../amd64-sysv/setjmp.s - $(AS) $(ASFLAGS) -o $@ ../amd64-sysv/setjmp.s - -longjmp.o: ../amd64-sysv/longjmp.s - $(AS) $(ASFLAGS) -o $@ ../amd64-sysv/longjmp.s diff --git a/lib/c/target/amd64-sysv/setjmp.s b/lib/c/target/amd64-sysv/setjmp.s @@ -1,17 +0,0 @@ -/* Copyright 2011-2012 Nicholas J. Kain, licensed under standard MIT license */ - - .file "setjmp.s" - .global setjmp -setjmp: - mov %rbx,(%rdi) # rdi is jmp_buf, move registers onto it - mov %rbp,8(%rdi) - mov %r12,16(%rdi) - mov %r13,24(%rdi) - mov %r14,32(%rdi) - mov %r15,40(%rdi) - lea 8(%rsp),%rdx # this is our rsp WITHOUT current ret addr - mov %rdx,48(%rdi) - mov (%rsp),%rdx # save return addr ptr for new rip - mov %rdx,56(%rdi) - xor %rax,%rax # always return 0 - ret diff --git a/lib/c/target/i386-sysv-linux/Makefile b/lib/c/target/i386-sysv-linux/Makefile @@ -1,9 +0,0 @@ -.POSIX: - -PROJECTDIR = ../../../.. - -include $(PROJECTDIR)/rules.mk -include ../objlst.mk -include ../common.mk - -SCC_CFLAGS = -nostdinc -I../../include -I../../include/bits/i386-sysv/ diff --git a/lib/c/target/posix/netbsd.e b/lib/c/target/posix/netbsd.e @@ -1,98 +0,0 @@ -EPERM 1 Operation not permitted -ENOENT 2 No such file or directory -ESRCH 3 No such process -EINTR 4 Interrupted system call -EIO 5 Input/output error -ENXIO 6 Device not configured -E2BIG 7 Argument list too long -ENOEXEC 8 Exec format error -EBADF 9 Bad file descriptor -ECHILD 10 No child processes -EDEADLK 11 Resource deadlock avoided -ENOMEM 12 Cannot allocate memory -EACCES 13 Permission denied -EFAULT 14 Bad address -ENOTBLK 15 Block device required -EBUSY 16 Device busy -EEXIST 17 File exists -EXDEV 18 Cross-device link -ENODEV 19 Operation not supported by device -ENOTDIR 20 Not a directory -EISDIR 21 Is a directory -EINVAL 22 Invalid argument -ENFILE 23 Too many open files in system -EMFILE 24 Too many open files -ENOTTY 25 Inappropriate ioctl for device -ETXTBSY 26 Text file busy -EFBIG 27 File too large -ENOSPC 28 No space left on device -ESPIPE 29 Illegal seek -EROFS 30 Read-only file system -EMLINK 31 Too many links -EPIPE 32 Broken pipe -EDOM 33 Numerical argument out of domain -ERANGE 34 Result too large or too small -EAGAIN 35 Resource temporarily unavailable -EWOULDBLOCK EAGAIN Operation would block -EINPROGRESS 36 Operation now in progress -EALREADY 37 Operation already in progress -ENOTSOCK 38 Socket operation on non-socket -EDESTADDRREQ 39 Destination address required -EMSGSIZE 40 Message too long -EPROTOTYPE 41 Protocol wrong type for socket -ENOPROTOOPT 42 Protocol option not available -EPROTONOSUPPORT 43 Protocol not supported -ESOCKTNOSUPPORT 44 Socket type not supported -EOPNOTSUPP 45 Operation not supported -EPFNOSUPPORT 46 Protocol family not supported -EAFNOSUPPORT 47 Address family not supported by protocol family -EADDRINUSE 48 Address already in use -EADDRNOTAVAIL 49 Can't assign requested address -ENETDOWN 50 Network is down -ENETUNREACH 51 Network is unreachable -ENETRESET 52 Network dropped connection on reset -ECONNABORTED 53 Software caused connection abort -ECONNRESET 54 Connection reset by peer -ENOBUFS 55 No buffer space available -EISCONN 56 Socket is already connected -ENOTCONN 57 Socket is not connected -ESHUTDOWN 58 Can't send after socket shutdown -ETOOMANYREFS 59 Too many references: can't splice -ETIMEDOUT 60 Operation timed out -ECONNREFUSED 61 Connection refused -ELOOP 62 Too many levels of symbolic links -ENAMETOOLONG 63 File name too long -EHOSTDOWN 64 Host is down -EHOSTUNREACH 65 No route to host -ENOTEMPTY 66 Directory not empty -EPROCLIM 67 Too many processes -EUSERS 68 Too many users -EDQUOT 69 Disc quota exceeded -ESTALE 70 Stale NFS file handle -EREMOTE 71 Too many levels of remote in path -EBADRPC 72 RPC struct is bad -ERPCMISMATCH 73 RPC version wrong -EPROGUNAVAIL 74 RPC prog. not avail -EPROGMISMATCH 75 Program version wrong -EPROCUNAVAIL 76 Bad procedure for program -ENOLCK 77 No locks available -ENOSYS 78 Function not implemented -EFTYPE 79 Inappropriate file type or format -EAUTH 80 Authentication error -ENEEDAUTH 81 Need authenticator -EIDRM 82 Identifier removed -ENOMSG 83 No message of desired type -EOVERFLOW 84 Value too large to be stored in data type -EILSEQ 85 Illegal byte sequence -ENOTSUP 86 Not supported -ECANCELED 87 Operation canceled -EBADMSG 88 Bad or Corrupt message -ENODATA 89 No message available -ENOSR 90 No STREAM resources -ENOSTR 91 Not a STREAM -ETIME 92 STREAM ioctl timeout -ENOATTR 93 Attribute not found -EMULTIHOP 94 Multihop attempted -ENOLINK 95 Link has been severed -EPROTO 96 Protocol error -ELAST 96 Must equal largest errno diff --git a/lib/c/target/posix/objlst.mk b/lib/c/target/posix/objlst.mk @@ -1,15 +0,0 @@ - -raise.o: ../posix/raise.c - $(CC) $(SCC_CFLAGS) ../posix/raise.c -c - -signal.o: ../posix/signal.c - $(CC) $(SCC_CFLAGS) ../posix/signal.c -c - -getenv.o: ../posix/getenv.c - $(CC) $(SCC_CFLAGS) ../posix/getenv.c -c - -time.o: ../posix/time.c - $(CC) $(SCC_CFLAGS) ../posix/time.c -c - -_tzone.o: ../posix/_tzone.c ../../libc.h - $(CC) $(SCC_CFLAGS) ../posix/_tzone.c -c diff --git a/lib/c/target/script/amd64-sysv.sh b/lib/c/target/script/amd64-sysv.sh @@ -1,15 +0,0 @@ -#!/bin/sh - -# -# This job is very easy because app and kernel ABI are identical -# until the 4th parameter, so we only have to set the syscall -# number in rax - -awk '/^#/ {next} - {name=$2 ".s" - printf ".global %s\n" \ - "%s:\n" \ - "\tmovq\t$%d,%%rax\n" \ - "\tsyscall\n" \ - "\tret\n", $2, $2, $1 > name - close(name)}' syscall.lst diff --git a/lib/c/target/script/common.mk b/lib/c/target/script/common.mk @@ -1,38 +0,0 @@ -SYSNAME = $(ARCH)-$(ABI)-$(SYS) -SYSASM = $(SYSCALL:.o=.s) -TARGET = $(LIBDIR)/$(SYSNAME)/libc.a -INCLUDE = -I$(INCDIR) \ - -I$(INCDIR)/bits/$(ARCH)-$(ABI) \ - -I$(INCDIR)/bits/$(SYS) \ - -I. -SYSERRNO = $(INCDIR)/bits/$(SYS)/sys/errno.h -OBJ = $(LIBOBJ) $(SYSOBJ) $(SYSCALL) $(ARCHOBJ) - -SCC_CFLAGS = $(MORECFLAGS) $(INCLUDE) - -all: $(TARGET) - -$(OBJ): $(SYSERRNO) - -$(SYSERRNO): $(SYSERRTBL) - trap "rm -f $$$$.tmp" 0 2 3 4; \ - ../script/generrno.sh $(SYSERRTBL) > $$$$.tmp && mv $$$$.tmp $@ - -_sys_errlist.c: $(SYSERRTBL) $(SYSERRNO) - trap "rm -f $$$$.tmp" 0 2 3 4; \ - ../script/generrstr.sh $(SYSERRTBL) > $$$$.tmp && mv $$$$.tmp $@ - -$(TARGET): $(OBJ) - $(AR) $(ARFLAGS) $@ $? - ranlib $@ - -clean: - rm -f *.o *.a _sys_errlist.c $(SYSERRNO) - rm -f $(SYSASM) - rm -f $(TARGET) - -$(SYSASM): syscall.lst - ../script/amd64-sysv.sh - -dep: - ../script/syscall.sh diff --git a/lib/c/target/script/generrno.sh b/lib/c/target/script/generrno.sh @@ -1,12 +0,0 @@ -#!/bin/sh - -awk ' -/^E/ && $2 > 0 { - errno[$1] = $2 -} - -END { - for (i in errno) - print "#define", i, errno[i] | "sort -n -k3" - close("sort -n -k3") -}' $@ diff --git a/lib/c/target/script/generrstr.sh b/lib/c/target/script/generrstr.sh @@ -1,21 +0,0 @@ -#!/bin/sh - -awk ' -/^E/ && $2 > 0 { - str = "" - for (i = 3; i <= NF; i++) - str = str " " $i - sub(/^ /, "", str) - errstr[$1] = str - if ($2 > max) - max = $2; -} - -END { - print "#include <errno.h>\n" - print "char *_sys_errstr[] = {" - for (i in errstr) - printf "\t%-20.20s = \"%s\",\n", "[" i "]", errstr[i] - print "};" - print "int _sys_nerr =", $2 + 1 ";" -}' $@ diff --git a/lib/c/target/script/objlst.mk b/lib/c/target/script/objlst.mk @@ -1,345 +0,0 @@ - -LIBOBJ = bsearch.o qsort.o \ - abs.o __abs.o labs.o __labs.o llabs.o __llabs.o \ - perror.o strerror.o \ - rand.o tmpnam.o \ - sprintf.o snprintf.o vsprintf.o vsnprintf.o \ - printf.o fprintf.o vfprintf.o \ - fgets.o gets.o fgetc.o fputc.o getchar.o putchar.o \ - fputs.o puts.o fread.o fwrite.o \ - getc.o putc.o __putc.o __getc.o \ - ftell.o rewind.o fseek.o ferror.o feof.o clearerr.o \ - setbuf.o setvbuf.o \ - fclose.o fopen.o freopen.o _fpopen.o _flsbuf.o stdio.o \ - realloc.o calloc.o malloc.o \ - __assert.o strcpy.o strcmp.o strlen.o strchr.o \ - strrchr.o strcat.o strncmp.o strncpy.o strncat.o strcoll.o \ - strxfrm.o strstr.o strspn.o strcspn.o strpbrk.o strtok.o \ - memset.o memcpy.o memmove.o memcmp.o memchr.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 toupper.o tolower.o ctype.o setlocale.o \ - localeconv.o atoi.o atol.o atoll.o atexit.o abort.o exit.o \ - mktime.o localtime.o gmtime.o difftime.o \ - _daysyear.o ctime.o asctime.o strftime.o \ - errno.o _sys_errlist.o strnlen.o - -#rules -__abs.o: ../../__abs.c - $(CC) $(SCC_CFLAGS) ../../__abs.c -c - -__assert.o: ../../__assert.c - $(CC) $(SCC_CFLAGS) ../../__assert.c -c - -__getc.o: ../../__getc.c - $(CC) $(SCC_CFLAGS) ../../__getc.c -c - -__labs.o: ../../__labs.c - $(CC) $(SCC_CFLAGS) ../../__labs.c -c - -__llabs.o: ../../__llabs.c - $(CC) $(SCC_CFLAGS) ../../__llabs.c -c - -__putc.o: ../../__putc.c - $(CC) $(SCC_CFLAGS) ../../__putc.c -c - -_daysyear.o: ../../_daysyear.c - $(CC) $(SCC_CFLAGS) ../../_daysyear.c -c - -_flsbuf.o: ../../_flsbuf.c - $(CC) $(SCC_CFLAGS) ../../_flsbuf.c -c - -_fpopen.o: ../../_fpopen.c - $(CC) $(SCC_CFLAGS) ../../_fpopen.c -c - -abort.o: ../../abort.c - $(CC) $(SCC_CFLAGS) ../../abort.c -c - -abs.o: ../../abs.c - $(CC) $(SCC_CFLAGS) ../../abs.c -c - -asctime.o: ../../asctime.c - $(CC) $(SCC_CFLAGS) ../../asctime.c -c - -atexit.o: ../../atexit.c - $(CC) $(SCC_CFLAGS) ../../atexit.c -c - -atoi.o: ../../atoi.c - $(CC) $(SCC_CFLAGS) ../../atoi.c -c - -atol.o: ../../atol.c - $(CC) $(SCC_CFLAGS) ../../atol.c -c - -atoll.o: ../../atoll.c - $(CC) $(SCC_CFLAGS) ../../atoll.c -c - -bsearch.o: ../../bsearch.c - $(CC) $(SCC_CFLAGS) ../../bsearch.c -c - -calloc.o: ../../calloc.c - $(CC) $(SCC_CFLAGS) ../../calloc.c -c - -clearerr.o: ../../clearerr.c - $(CC) $(SCC_CFLAGS) ../../clearerr.c -c - -ctime.o: ../../ctime.c - $(CC) $(SCC_CFLAGS) ../../ctime.c -c - -ctype.o: ../../ctype.c - $(CC) $(SCC_CFLAGS) ../../ctype.c -c - -difftime.o: ../../difftime.c - $(CC) $(SCC_CFLAGS) ../../difftime.c -c - -errno.o: ../../errno.c - $(CC) $(SCC_CFLAGS) ../../errno.c -c - -exit.o: ../../exit.c - $(CC) $(SCC_CFLAGS) ../../exit.c -c - -fclose.o: ../../fclose.c - $(CC) $(SCC_CFLAGS) ../../fclose.c -c - -feof.o: ../../feof.c - $(CC) $(SCC_CFLAGS) ../../feof.c -c - -ferror.o: ../../ferror.c - $(CC) $(SCC_CFLAGS) ../../ferror.c -c - -fgetc.o: ../../fgetc.c - $(CC) $(SCC_CFLAGS) ../../fgetc.c -c - -fgets.o: ../../fgets.c - $(CC) $(SCC_CFLAGS) ../../fgets.c -c - -fopen.o: ../../fopen.c - $(CC) $(SCC_CFLAGS) ../../fopen.c -c - -fprintf.o: ../../fprintf.c - $(CC) $(SCC_CFLAGS) ../../fprintf.c -c - -fputc.o: ../../fputc.c - $(CC) $(SCC_CFLAGS) ../../fputc.c -c - -fputs.o: ../../fputs.c - $(CC) $(SCC_CFLAGS) ../../fputs.c -c - -fread.o: ../../fread.c - $(CC) $(SCC_CFLAGS) ../../fread.c -c - -freopen.o: ../../freopen.c - $(CC) $(SCC_CFLAGS) ../../freopen.c -c - -fseek.o: ../../fseek.c - $(CC) $(SCC_CFLAGS) ../../fseek.c -c - -ftell.o: ../../ftell.c - $(CC) $(SCC_CFLAGS) ../../ftell.c -c - -fwrite.o: ../../fwrite.c - $(CC) $(SCC_CFLAGS) ../../fwrite.c -c - -getc.o: ../../getc.c - $(CC) $(SCC_CFLAGS) ../../getc.c -c - -getchar.o: ../../getchar.c - $(CC) $(SCC_CFLAGS) ../../getchar.c -c - -gets.o: ../../gets.c - $(CC) $(SCC_CFLAGS) ../../gets.c -c - -gmtime.o: ../../gmtime.c - $(CC) $(SCC_CFLAGS) ../../gmtime.c -c - -isalnum.o: ../../isalnum.c - $(CC) $(SCC_CFLAGS) ../../isalnum.c -c - -isalpha.o: ../../isalpha.c - $(CC) $(SCC_CFLAGS) ../../isalpha.c -c - -isascii.o: ../../isascii.c - $(CC) $(SCC_CFLAGS) ../../isascii.c -c - -isblank.o: ../../isblank.c - $(CC) $(SCC_CFLAGS) ../../isblank.c -c - -iscntrl.o: ../../iscntrl.c - $(CC) $(SCC_CFLAGS) ../../iscntrl.c -c - -isdigit.o: ../../isdigit.c - $(CC) $(SCC_CFLAGS) ../../isdigit.c -c - -isgraph.o: ../../isgraph.c - $(CC) $(SCC_CFLAGS) ../../isgraph.c -c - -islower.o: ../../islower.c - $(CC) $(SCC_CFLAGS) ../../islower.c -c - -isprint.o: ../../isprint.c - $(CC) $(SCC_CFLAGS) ../../isprint.c -c - -ispunct.o: ../../ispunct.c - $(CC) $(SCC_CFLAGS) ../../ispunct.c -c - -isspace.o: ../../isspace.c - $(CC) $(SCC_CFLAGS) ../../isspace.c -c - -isupper.o: ../../isupper.c - $(CC) $(SCC_CFLAGS) ../../isupper.c -c - -isxdigit.o: ../../isxdigit.c - $(CC) $(SCC_CFLAGS) ../../isxdigit.c -c - -labs.o: ../../labs.c - $(CC) $(SCC_CFLAGS) ../../labs.c -c - -llabs.o: ../../llabs.c - $(CC) $(SCC_CFLAGS) ../../llabs.c -c - -localeconv.o: ../../localeconv.c - $(CC) $(SCC_CFLAGS) ../../localeconv.c -c - -localtime.o: ../../localtime.c - $(CC) $(SCC_CFLAGS) ../../localtime.c -c - -malloc.o: ../../malloc.c - $(CC) $(SCC_CFLAGS) ../../malloc.c -c - -memchr.o: ../../memchr.c - $(CC) $(SCC_CFLAGS) ../../memchr.c -c - -memcmp.o: ../../memcmp.c - $(CC) $(SCC_CFLAGS) ../../memcmp.c -c - -memcpy.o: ../../memcpy.c - $(CC) $(SCC_CFLAGS) ../../memcpy.c -c - -memmove.o: ../../memmove.c - $(CC) $(SCC_CFLAGS) ../../memmove.c -c - -memset.o: ../../memset.c - $(CC) $(SCC_CFLAGS) ../../memset.c -c - -mktime.o: ../../mktime.c - $(CC) $(SCC_CFLAGS) ../../mktime.c -c - -perror.o: ../../perror.c - $(CC) $(SCC_CFLAGS) ../../perror.c -c - -printf.o: ../../printf.c - $(CC) $(SCC_CFLAGS) ../../printf.c -c - -putc.o: ../../putc.c - $(CC) $(SCC_CFLAGS) ../../putc.c -c - -putchar.o: ../../putchar.c - $(CC) $(SCC_CFLAGS) ../../putchar.c -c - -puts.o: ../../puts.c - $(CC) $(SCC_CFLAGS) ../../puts.c -c - -qsort.o: ../../qsort.c - $(CC) $(SCC_CFLAGS) ../../qsort.c -c - -rand.o: ../../rand.c - $(CC) $(SCC_CFLAGS) ../../rand.c -c - -realloc.o: ../../realloc.c - $(CC) $(SCC_CFLAGS) ../../realloc.c -c - -rewind.o: ../../rewind.c - $(CC) $(SCC_CFLAGS) ../../rewind.c -c - -setbuf.o: ../../setbuf.c - $(CC) $(SCC_CFLAGS) ../../setbuf.c -c - -setlocale.o: ../../setlocale.c - $(CC) $(SCC_CFLAGS) ../../setlocale.c -c - -setvbuf.o: ../../setvbuf.c - $(CC) $(SCC_CFLAGS) ../../setvbuf.c -c - -snprintf.o: ../../snprintf.c - $(CC) $(SCC_CFLAGS) ../../snprintf.c -c - -sprintf.o: ../../sprintf.c - $(CC) $(SCC_CFLAGS) ../../sprintf.c -c - -stdio.o: ../../stdio.c - $(CC) $(SCC_CFLAGS) ../../stdio.c -c - -strcat.o: ../../strcat.c - $(CC) $(SCC_CFLAGS) ../../strcat.c -c - -strchr.o: ../../strchr.c - $(CC) $(SCC_CFLAGS) ../../strchr.c -c - -strcmp.o: ../../strcmp.c - $(CC) $(SCC_CFLAGS) ../../strcmp.c -c - -strcoll.o: ../../strcoll.c - $(CC) $(SCC_CFLAGS) ../../strcoll.c -c - -strcpy.o: ../../strcpy.c - $(CC) $(SCC_CFLAGS) ../../strcpy.c -c - -strcspn.o: ../../strcspn.c - $(CC) $(SCC_CFLAGS) ../../strcspn.c -c - -strerror.o: ../../strerror.c - $(CC) $(SCC_CFLAGS) ../../strerror.c -c - -strftime.o: ../../strftime.c - $(CC) $(SCC_CFLAGS) ../../strftime.c -c - -strlen.o: ../../strlen.c - $(CC) $(SCC_CFLAGS) ../../strlen.c -c - -strncat.o: ../../strncat.c - $(CC) $(SCC_CFLAGS) ../../strncat.c -c - -strncmp.o: ../../strncmp.c - $(CC) $(SCC_CFLAGS) ../../strncmp.c -c - -strncpy.o: ../../strncpy.c - $(CC) $(SCC_CFLAGS) ../../strncpy.c -c - -strnlen.o: ../../strnlen.c - $(CC) $(SCC_CFLAGS) ../../strnlen.c -c - -strpbrk.o: ../../strpbrk.c - $(CC) $(SCC_CFLAGS) ../../strpbrk.c -c - -strrchr.o: ../../strrchr.c - $(CC) $(SCC_CFLAGS) ../../strrchr.c -c - -strspn.o: ../../strspn.c - $(CC) $(SCC_CFLAGS) ../../strspn.c -c - -strstr.o: ../../strstr.c - $(CC) $(SCC_CFLAGS) ../../strstr.c -c - -strtok.o: ../../strtok.c - $(CC) $(SCC_CFLAGS) ../../strtok.c -c - -strxfrm.o: ../../strxfrm.c - $(CC) $(SCC_CFLAGS) ../../strxfrm.c -c - -tmpnam.o: ../../tmpnam.c - $(CC) $(SCC_CFLAGS) ../../tmpnam.c -c - -tolower.o: ../../tolower.c - $(CC) $(SCC_CFLAGS) ../../tolower.c -c - -toupper.o: ../../toupper.c - $(CC) $(SCC_CFLAGS) ../../toupper.c -c - -vfprintf.o: ../../vfprintf.c - $(CC) $(SCC_CFLAGS) ../../vfprintf.c -c - -vsnprintf.o: ../../vsnprintf.c - $(CC) $(SCC_CFLAGS) ../../vsnprintf.c -c - -vsprintf.o: ../../vsprintf.c - $(CC) $(SCC_CFLAGS) ../../vsprintf.c -c - diff --git a/lib/c/target/script/objlst.sh b/lib/c/target/script/objlst.sh @@ -1,20 +0,0 @@ -#!/bin/sh - -set -e - - -(cd .. -echo H -echo '/^#rules/+;$c' - -for i in *.c -do - cat <<EOF -${i%.c}.o: ../../$i - \$(CC) \$(SCC_CFLAGS) ../../$i -c - -EOF -done - -echo . -echo w) | ed -s script/objlst.mk diff --git a/lib/c/target/script/syscall.sh b/lib/c/target/script/syscall.sh @@ -1,8 +0,0 @@ -#!/bin/sh - -(echo '/SYS/c' - awk 'BEGIN {printf "SYSCALL = "} - ! /^#/ {printf "%s.o ", $2} - END {print ""}' syscall.lst - echo . - echo w) | ed -s syscall.mk diff --git a/lib/c/target/z80-scc-none/Makefile b/lib/c/target/z80-scc-none/Makefile @@ -1,9 +0,0 @@ -.POSIX: - -PROJECTDIR = ../../../.. - -include $(PROJECTDIR)/rules.mk -include ../objlst.mk -include ../common.mk - -SCC_CFLAGS = -nostdinc -I../../include -I../../include/bits/z80-none/ diff --git a/lib/c/time/Makefile b/lib/c/time/Makefile @@ -0,0 +1,16 @@ +.POSIX: +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/lib/c/time/_daysyear.c b/lib/c/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/lib/c/asctime.c b/lib/c/time/asctime.c diff --git a/lib/c/ctime.c b/lib/c/time/ctime.c diff --git a/lib/c/difftime.c b/lib/c/time/difftime.c diff --git a/lib/c/gmtime.c b/lib/c/time/gmtime.c diff --git a/lib/c/localtime.c b/lib/c/time/localtime.c diff --git a/lib/c/mktime.c b/lib/c/time/mktime.c diff --git a/lib/c/strftime.c b/lib/c/time/strftime.c diff --git a/lib/c/tmpnam.c b/lib/c/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; -} diff --git a/lib/c/toupper.c b/lib/c/toupper.c @@ -1,9 +0,0 @@ -#define __USE_MACROS -#include <ctype.h> -#undef toupper - -int -toupper(int c) -{ - return (islower(c)) ? c & ~0x20 : c; -} diff --git a/lib/c/vfprintf.c b/lib/c/vfprintf.c @@ -1,361 +0,0 @@ -#include <ctype.h> -#include <limits.h> -#include <stdarg.h> -#include <stdint.h> -#include <stdio.h> -#include <string.h> -#include <wchar.h> -#undef vfprintf - -enum { - LONG = 1 << 0, - LLONG = 1 << 1, - SHORT = 1 << 2, - CHAR = 1 << 3, - SIZET = 1 << 4, - PTRDIFF = 1 << 5, - INTMAX = 1 << 6, - VOIDPTR = 1 << 7, - UNSIGNED = 1 << 8, - ALTFORM = 1 << 9, -}; - -#define MAXPREC 50 - -struct conv { - int sign; - int prec; - char *digs; - int base; -}; - -static uintmax_t -getnum(va_list *va, int flags, int *sign) -{ - uintmax_t uval; - intmax_t val; - - if (flags & CHAR) { - val = va_arg(*va, int); - uval = (unsigned char) val; - } else if (flags & SHORT) { - val = va_arg(*va, int); - uval = (unsigned short) val; - } else if (flags & LONG) { - val = va_arg(*va, long); - uval = (unsigned long) val; - } else if (flags & LLONG) { - val = va_arg(*va, long long); - uval = (unsigned long long) val; - } else if (flags & SIZET) { - uval = va_arg(*va, size_t); - } else if (flags & INTMAX) { - val = va_arg(*va, intmax_t); - uval = (uintmax_t) val; - } else if (flags & VOIDPTR) { - uval = (uintmax_t) va_arg(*va, void *); - } else { - val = va_arg(*va, int); - uval = (unsigned) val; - } - - if ((flags & UNSIGNED) == 0 && val < 0) { - *sign = '-'; - uval = -uval; - } - return uval; -} - -static char * -numtostr(uintmax_t val, int flags, struct conv *conv, char *buf) -{ - char *buf0 = buf; - int base = conv->base, prec = conv->prec; - uintmax_t oval = val; - - if (prec == -1) - prec = 1; - - for (*buf = '\0'; val > 0; val /= base) - *--buf = conv->digs[val % base]; - while (buf0 - buf < prec) - *--buf = '0'; - - if (flags & ALTFORM) { - if (base == 8 && *buf != '0') { - *--buf = '0'; - } else if (base == 16 && oval != 0) { - *--buf = conv->digs[16]; - *--buf = '0'; - } - } - if (conv->sign) - *--buf = conv->sign; - - return buf; -} - -static void -savecnt(va_list *va, int flags, int cnt) -{ - if (flags & CHAR) - *va_arg(*va, char*) = cnt; - else if (flags & SHORT) - *va_arg(*va, short*) = cnt; - else if (flags & LONG) - *va_arg(*va, long*) = cnt; - else if (flags & LLONG) - *va_arg(*va, long long*) = cnt; - else if (flags & SIZET) - *va_arg(*va, size_t*) = cnt; - else if (flags & INTMAX) - *va_arg(*va, intmax_t*) = cnt; - else - *va_arg(*va, int*) = cnt; -} - -static size_t -wstrout(wchar_t *ws, size_t len, int width, int fill, FILE * restrict fp) -{ - int left = 0, adjust; - size_t cnt = 0; - wchar_t wc; -#if 0 - - if (width < 0) { - left = 1; - width = -width; - } - - len *= sizeof(wchar_t); - adjust = (len < width) ? width - len : 0; - cnt = adjust + len; - if (left) - adjust = -adjust; - - for ( ; adjust > 0; adjust++) - putc(fill, fp); - - while (wc = *ws++) - putwc(wc, fp); - - for ( ; adjust < 0; adjust--) - putc(' ', fp); -#endif - return cnt; -} - -static size_t -strout(char *s, size_t len, int width, int fill, FILE * restrict fp) -{ - int left = 0, adjust, ch, prefix; - size_t cnt = 0; - - if (width < 0) { - left = 1; - width = -width; - } - - adjust = (len < width) ? width - len : 0; - cnt = adjust + len; - if (left) - adjust = -adjust; - - if (fill == '0') { - if (*s == '-' || *s == '+') - prefix = 1; - else if (*s == '0' && toupper(s[1]) == 'X') - prefix = 2; - else - prefix = 0; - while (prefix--) { - putc(*s++, fp); - --len; - } - } - - for ( ; adjust > 0; adjust--) - putc(fill, fp); - - while (ch = *s++) - putc(ch, fp); - - for ( ; adjust < 0; adjust++) - putc(' ', fp); - - return cnt; -} - -int -vfprintf(FILE * restrict fp, const char *fmt, va_list va) -{ - int ch, n, flags, width, left, fill, cnt = 0; - size_t inc, len; - char *s; - wchar_t *ws; - struct conv conv; - char buf[MAXPREC+1]; - wchar_t wbuf[2]; - - for (cnt = 0; ch = *fmt++; cnt += inc) { - if (ch != '%') { - putc(ch, fp); - inc = 1; - continue; - } - - fill = ' '; - left = flags = width = 0; - conv.prec = -1; - conv.base = 10; - conv.sign = '\0'; - conv.digs = "0123456789ABCDEFX"; - -flags: - switch (*fmt++) { - case ' ': - if (conv.sign == '\0') - conv.sign = ' '; - goto flags; - case '+': - conv.sign = '+'; - goto flags; - case '#': - flags |= ALTFORM; - goto flags; - case '.': - if (*fmt == '*') { - fmt++; - n = va_arg(va, int); - } else { - for (n = 0; isdigit(ch = *fmt); fmt++) - n = n * 10 + ch - '0'; - } - if (n > MAXPREC) - n = MAXPREC; - if (n > 0) - conv.prec = n; - goto flags; - case '*': - width = va_arg(va, int); - goto flags; - case '-': - left = 1; - ++fmt; - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - --fmt; - for (n = 0; isdigit(ch = *fmt); ++fmt) - n = n * 10 + ch - '0'; - if (left) - n = -n; - width = n; - goto flags; - case '0': - fill = '0'; - goto flags; - case 'l': - flags += LONG; - goto flags; - case 'h': - flags += SHORT; - goto flags; - case '%': - ch = '%'; - goto cout; - case 'c': - if (flags & LONG) { - wbuf[0] = va_arg(va, wint_t); - wbuf[1] = L'\0'; - ws = wbuf; - len = 1; - goto wstrout; - } - ch = va_arg(va, int); - cout: - buf[0] = ch; - buf[1] = '\0'; - s = buf; - len = 1; - goto strout; - case 'j': - flags |= INTMAX; - goto flags; - case 't': - flags |= PTRDIFF; - goto flags; - case 'z': - flags |= SIZET; - goto flags; - case 'u': - flags |= UNSIGNED; - case 'i': - case 'd': - numeric10: - conv.base = 10; - goto numeric; - case 'p': - flags |= VOIDPTR | ALTFORM; - goto numeric16; - case 'x': - conv.digs = "0123456789abcdefx"; - case 'X': - numeric16: - conv.base = 16; - flags |= UNSIGNED; - goto numeric; - case 'o': - conv.base = 8; - flags |= UNSIGNED; - numeric: - if (conv.prec != -1) - fill = ' '; - s = numtostr(getnum(&va, flags, &conv.sign), - flags, - &conv, - &buf[MAXPREC]); - len = &buf[MAXPREC] - s; - goto strout; - case 'L': - case 'a': - case 'A': - case 'e': - case 'E': - case 'f': - case 'g': - case 'G': - /* TODO */ - case 's': - if (flags & LONG) { - ws = va_arg(va, wchar_t *); - /* len = wcsnlen(ws, conv.prec); */ - goto wstrout; - } else { - s = va_arg(va, char *); - len = strnlen(s, conv.prec); - goto strout; - } - wstrout: - inc = wstrout(ws, len, width, fill, fp); - break; - strout: - inc = strout(s, len, width, fill, fp); - break; - case 'n': - savecnt(&va, flags, cnt); - break; - case '\0': - goto out_loop; - } - } - -out_loop: - return (ferror(fp)) ? EOF : cnt; -} diff --git a/root/include/scc/bits/amd64-sysv/arch/limits.h b/root/include/scc/bits/amd64-sysv/arch/limits.h @@ -1,18 +0,0 @@ -#define CHAR_BIT 8 -#define SCHAR_MAX 0x7F -#define SCHAR_MIN (-SCHAR_MAX-1) -#define CHAR_MAX 0x7F -#define CHAR_MIN (-CHAR_MAX-1) -#define UCHAR_MAX 0xFF -#define SHRT_MAX 0x7FFF -#define SHRT_MIN (-SHRT_MAX-1) -#define USHRT_MAX 0xFFFF -#define INT_MAX 0x7FFFFFFF -#define INT_MIN (-INT_MAX-1) -#define UINT_MAX 0xFFFFFFFF -#define LONG_MAX 0x7FFFFFFFFFFFFFFF -#define LONG_MIN (-LONG_MAX-1) -#define ULONG_MAX 0xFFFFFFFFFFFFFFFF -#define LLONG_MAX 0x7FFFFFFFFFFFFFFF -#define LLONG_MIN (-LLONG_MAX-1) -#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/root/include/scc/bits/amd64/arch/limits.h b/root/include/scc/bits/amd64/arch/limits.h @@ -0,0 +1,18 @@ +#define CHAR_BIT 8 +#define SCHAR_MAX 0x7F +#define SCHAR_MIN (-SCHAR_MIN-1) +#define CHAR_MAX 0x7F +#define CHAR_MIN (-CHAR_MAX-1) +#define UCHAR_MAX 0xFF +#define SHRT_MAX 0x7FFF +#define SHRT_MIN (-SHRT_MAX-1) +#define USHRT_MAX 0xFFFF +#define INT_MAX 0x7FFFFFFF +#define INT_MIN (-INT_MAX-1) +#define UINT_MAX 0xFFFFFFFF +#define LONG_MAX 0x7FFFFFFFFFFFFFFF +#define LONG_MIN (-LONG_MAX-1) +#define ULONG_MAX 0xFFFFFFFFFFFFFFFF +#define LLONG_MAX 0x7FFFFFFFFFFFFFFF +#define LLONG_MIN (-LLONG_MAX-1) +#define ULLONG_MAX 0xFFFFFFFFFFFFFFFF diff --git a/root/include/scc/bits/amd64-sysv/arch/setjmp.h b/root/include/scc/bits/amd64/arch/setjmp.h diff --git a/root/include/scc/bits/amd64-sysv/arch/stddef.h b/root/include/scc/bits/amd64/arch/stddef.h diff --git a/root/include/scc/bits/amd64-sysv/arch/stdint.h b/root/include/scc/bits/amd64/arch/stdint.h diff --git a/root/include/scc/bits/amd64-sysv/arch/stdio.h b/root/include/scc/bits/amd64/arch/stdio.h diff --git a/root/include/scc/bits/amd64-sysv/arch/stdlib.h b/root/include/scc/bits/amd64/arch/stdlib.h diff --git a/root/include/scc/bits/amd64/arch/string.h b/root/include/scc/bits/amd64/arch/string.h @@ -0,0 +1,5 @@ +#ifndef _SIZET +typedef unsigned long size_t; +#endif + +#define __NUMCHARS 128 +\ No newline at end of file diff --git a/root/include/scc/bits/amd64-sysv/arch/time.h b/root/include/scc/bits/amd64/arch/time.h diff --git a/root/include/scc/bits/arm64/arch/limits.h b/root/include/scc/bits/arm64/arch/limits.h @@ -0,0 +1,18 @@ +#define CHAR_BIT 8 +#define SCHAR_MAX 0x7F +#define SCHAR_MIN (-SCHAR_MIN - 1) +#define CHAR_MAX 0x7F +#define CHAR_MIN (-CHAR_MAX - 1) +#define UCHAR_MAX 0xFFU +#define SHRT_MAX 0x7FFF +#define SHRT_MIN (-SHRT_MAX - 1) +#define USHRT_MAX 0xFFFFU +#define INT_MAX 0x7FFFFFFF +#define INT_MIN (-INT_MAX - 1) +#define UINT_MAX 0xFFFFFFFFU +#define LONG_MAX 0x7FFFFFFFFFFFFFFFL +#define LONG_MIN (-LONG_MAX - 1L) +#define ULONG_MAX 0xFFFFFFFFFFFFFFFFUL +#define LLONG_MAX 0x7FFFFFFFFFFFFFFFLL +#define LLONG_MIN (-LLONG_MAX - 1LL) +#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL diff --git a/root/include/scc/bits/arm64/arch/setjmp.h b/root/include/scc/bits/arm64/arch/setjmp.h @@ -0,0 +1 @@ +typedef unsigned long long jmp_buf[22]; diff --git a/root/include/scc/bits/arm64/arch/stddef.h b/root/include/scc/bits/arm64/arch/stddef.h @@ -0,0 +1,9 @@ +#ifndef SIZET_ +typedef unsigned long size_t; +#define SIZET_ +#endif + +#ifndef _PTRDIFF_T +typedef long ptrdiff_t; +#define _PTRDIFF_T +#endif diff --git a/root/include/scc/bits/arm64/arch/stdint.h b/root/include/scc/bits/arm64/arch/stdint.h @@ -0,0 +1,109 @@ +#define INT8_MAX 0x7F +#define INT8_MIN (-INT8_MAX - 1) +#define UINT8_MAX 0xFFU + +#define INT16_MAX 0x7FFF +#define INT16_MIN (-INT16_MAX - 1) +#define UINT16_MAX 0xFFFFU + +#define INT32_MAX 0x7FFFFFFF +#define INT32_MIN (-INT32_MAX - 1) +#define UINT32_MAX 0xFFFFFFFFU + +#define INT64_MAX 0x7FFFFFFFFFFFFFFFLL +#define INT64_MIN (-INT64_MAX - 1LL) +#define UINT64_MAX 0xFFFFFFFFFFFFFFFFULL + +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define UINT_LEAST8_MAX UINT8_MAX + +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define UINT_LEAST16_MAX UINT16_MAX + +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define UINT_LEAST32_MAX UINT32_MAX + +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +#define INT_FAST8_MIN INT32_MIN +#define INT_FAST8_MAX INT32_MAX +#define UINT_FAST8_MAX UINT32_MAX + +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST16_MAX INT32_MAX +#define UINT_FAST16_MAX UINT32_MAX + +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define UINT_FAST32_MAX UINT32_MAX + +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST64_MAX UINT64_MAX + +#define INTPTR_MIN INT64_MIN +#define INTPTR_MAX INT64_MAX +#define UINTPTR_MAX UINT64_MAX + +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +#define PTRDIFF_MIN INT64_MIN +#define PTRDIFF_MAX INT64_MAX + +#define SIZE_MAX UINT64_MAX + +#define INT8_C(x) x +#define INT16_C(x) x +#define INT32_C(x) x +#define INT64_C(x) x ## LL + +#define UINT8_C(x) x +#define UINT16_C(x) x +#define UINT32_C(x) x ## U +#define UINT64_C(x) x ## ULL + +#define INTMAX_C(x) x ## L +#define UINTMAX_C(x) x ## ULL + +typedef signed char int8_t; +typedef short int16_t; +typedef int int32_t; +typedef long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; + +typedef signed char int8_least_t; +typedef short int16_least_t; +typedef int int32_least_t; +typedef long long int64_least_t; + +typedef unsigned char uint8_least_t; +typedef unsigned short uint16_least_t; +typedef unsigned int uint32_least_t; +typedef unsigned long long uint64_least_t; + +typedef int int8_fast_t; +typedef int int16_fast_t; +typedef int int32_fast_t; +typedef long long int64_fast_t; + +typedef unsigned int uint8_fast_t; +typedef unsigned int uint16_fast_t; +typedef unsigned int uint32_fast_t; +typedef unsigned long long uint64_fast_t; + +typedef long intptr_t; +typedef unsigned long uintptr_t; + +typedef long intmax_t; +typedef unsigned long uintmax_t; diff --git a/root/include/scc/bits/amd64-sysv/arch/stdio.h b/root/include/scc/bits/arm64/arch/stdio.h diff --git a/root/include/scc/bits/arm64/arch/stdlib.h b/root/include/scc/bits/arm64/arch/stdlib.h @@ -0,0 +1,14 @@ +#ifndef SIZET_ +typedef unsigned long size_t; +#define SIZET_ +#endif + +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + +#ifndef _WCHAR_T +typedef int wchar_t; +#define _WCHAR_T +#endif + +#define _ALIGNTYPE long double diff --git a/root/include/scc/bits/arm64/arch/string.h b/root/include/scc/bits/arm64/arch/string.h @@ -0,0 +1,6 @@ +#ifndef SIZET_ +typedef unsigned long size_t; +#define SIZET_ +#endif + +#define __NUMCHARS 128 diff --git a/root/include/scc/bits/amd64-sysv/arch/time.h b/root/include/scc/bits/arm64/arch/time.h diff --git a/lib/c/target/amd64-sysv-netbsd/sys.h b/root/include/scc/bits/dragonfly/sys.h diff --git a/root/include/scc/bits/dragonfly/sys/signal.h b/root/include/scc/bits/dragonfly/sys/signal.h @@ -0,0 +1,12 @@ +typedef int sig_atomic_t; + +#define SIG_ERR ((void (*)(int))-1) +#define SIG_DFL ((void (*)(int)) 0) +#define SIG_IGN ((void (*)(int)) 1) + +#define SIGINT 2 +#define SIGILL 4 +#define SIGABRT 6 +#define SIGFPE 8 +#define SIGSEGV 11 +#define SIGTERM 15 diff --git a/root/include/scc/bits/i386-sysv/arch/string.h b/root/include/scc/bits/i386-sysv/arch/string.h @@ -1,5 +0,0 @@ -#ifndef _SIZET -typedef unsigned long size_t; -#endif - -#define __NUMCHARS 128 diff --git a/root/include/scc/bits/i386-sysv/arch/limits.h b/root/include/scc/bits/i386/arch/limits.h diff --git a/root/include/scc/bits/i386-sysv/arch/stddef.h b/root/include/scc/bits/i386/arch/stddef.h diff --git a/root/include/scc/bits/i386-sysv/arch/stdint.h b/root/include/scc/bits/i386/arch/stdint.h diff --git a/root/include/scc/bits/i386-sysv/arch/stdio.h b/root/include/scc/bits/i386/arch/stdio.h diff --git a/root/include/scc/bits/i386-sysv/arch/stdlib.h b/root/include/scc/bits/i386/arch/stdlib.h diff --git a/root/include/scc/bits/amd64-sysv/arch/string.h b/root/include/scc/bits/i386/arch/string.h diff --git a/root/include/scc/bits/i386-sysv/arch/time.h b/root/include/scc/bits/i386/arch/time.h diff --git a/lib/c/target/amd64-sysv-linux/sys.h b/root/include/scc/bits/linux/sys.h diff --git a/lib/c/target/amd64-sysv-netbsd/sys.h b/root/include/scc/bits/netbsd/sys.h diff --git a/root/include/scc/bits/netbsd/sys/signal.h b/root/include/scc/bits/netbsd/sys/signal.h @@ -4,24 +4,9 @@ typedef int sig_atomic_t; #define SIG_DFL ((void (*)(int)) 0) #define SIG_IGN ((void (*)(int)) 1) -#define SIGHUP 1 #define SIGINT 2 -#define SIGQUIT 3 #define SIGILL 4 #define SIGABRT 6 #define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 #define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 #define SIGTERM 15 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGSSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 - -#define __NR_SIGNALS 23 diff --git a/lib/c/target/amd64-sysv-openbsd/sys.h b/root/include/scc/bits/openbsd/sys.h diff --git a/root/include/scc/bits/openbsd/sys/signal.h b/root/include/scc/bits/openbsd/sys/signal.h @@ -4,24 +4,9 @@ typedef int sig_atomic_t; #define SIG_DFL ((void (*)(int)) 0) #define SIG_IGN ((void (*)(int)) 1) -#define SIGHUP 1 #define SIGINT 2 -#define SIGQUIT 3 #define SIGILL 4 #define SIGABRT 6 #define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 #define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 #define SIGTERM 15 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGSSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 - -#define __NR_SIGNALS 23 diff --git a/root/include/scc/bits/z80-dos/arch/limits.h b/root/include/scc/bits/z80/arch/limits.h diff --git a/root/include/scc/bits/z80-dos/arch/stddef.h b/root/include/scc/bits/z80/arch/stddef.h diff --git a/root/include/scc/bits/z80-dos/arch/stdint.h b/root/include/scc/bits/z80/arch/stdint.h diff --git a/root/include/scc/bits/z80-dos/arch/stdio.h b/root/include/scc/bits/z80/arch/stdio.h diff --git a/root/include/scc/bits/z80-dos/arch/stdlib.h b/root/include/scc/bits/z80/arch/stdlib.h diff --git a/root/include/scc/bits/z80-dos/arch/string.h b/root/include/scc/bits/z80/arch/string.h diff --git a/root/include/scc/bits/z80-dos/arch/time.h b/root/include/scc/bits/z80/arch/time.h diff --git a/root/include/scc/ctype.h b/root/include/scc/ctype.h @@ -16,7 +16,6 @@ extern int ispunct(int c); extern int tolower(int c); extern int toupper(int c); -#ifdef __USE_MACROS #define _U 0x01 /* upper */ #define _L 0x02 /* lower */ @@ -28,7 +27,6 @@ extern int toupper(int c); #define _SP 0x80 /* hard space (0x20) */ extern unsigned char __ctype[]; -extern int __ctmp; #define isalnum(c) ((__ctype+1)[c] & (_U|_L|_D)) #define isalpha(c) ((__ctype+1)[c] & (_U|_L)) @@ -42,11 +40,6 @@ extern int __ctmp; #define isupper(c) ((__ctype+1)[c] & (_U)) #define isxdigit(c) ((__ctype+1)[c] & (_D|_X)) -#define tolower(c) ((__ctmp=c, isupper(__ctmp) ? __ctmp | 0x20 : __ctmp)) -#define toupper(c) ((__ctmp=c, islower(__ctmp) ? __ctmp & ~0x20 : __ctmp)) - #define isascii(c) ((unsigned)(c)<=0x7f) #endif - -#endif diff --git a/root/include/scc/float.h b/root/include/scc/float.h @@ -1,4 +0,0 @@ -#ifndef _FLOAT_H -#define _FLOAT_H -#error not supported yet -#endif diff --git a/root/include/scc/stdio.h b/root/include/scc/stdio.h @@ -110,11 +110,8 @@ extern void perror(const char *s); extern int __getc(FILE *fp); extern int __putc(int, FILE *fp); -#ifdef __USE_MACROS -#ifdef __UNIX_FILES -#define getc(fp) ((fp)->rp >= (fp)->wp ? __getc(fp) : *(fp)->rp++) -#define putc(c, fp) ((fp)->wp >= (fp)->rp ? __putc(c,fp) : (*(fp)->wp++ = c)) -#endif +#define getc(fp) ((fp)->rp >= (fp)->wp ? __getc(fp) : *(fp)->rp++) +#define putc(c, fp) ((fp)->wp >= (fp)->rp ? __putc(c,fp) : (*(fp)->wp++ = c)) #define ferror(fp) ((fp)->flags & _IOERR) #define feof(fp) ((fp)->flags & _IOEOF) @@ -122,6 +119,5 @@ extern int __putc(int, FILE *fp); #define getchar() getc(stdin) #define putchar(c) putc((c), stdout) #define setbuf(fp, b) (void) setvbuf(fp, b, b ? _IOFBF:_IONBF, BUFSIZ) -#endif #endif diff --git a/root/include/scc/stdlib.h b/root/include/scc/stdlib.h @@ -66,14 +66,4 @@ extern int wctomb(char *s, wchar_t wchar); extern size_t mbstowcs(wchar_t * restrict pwcs, const char * restrict s, size_t n); extern size_t wcstombs(char * restrict s, const wchar_t * restrict pwcs, size_t n); -#ifdef __USE_MACROS -extern int __abs; -extern long __labs; -extern long long __llabs; - -#define abs(x) (__abs = (x), (__abs) < 0 ? -__abs : __abs) -#define labs(x) (__labs = (x), (__labs) < 0 ? -__labs : __labs) -#define llabs(x) (__llabs = (x), (__llabs) < 0 ? -__llabs : __llabs) -#endif - #endif