shithub: scc

Download patch

ref: 914b01afa8422ffa8ab9f125346502235e4d3163
parent: e9c7dc18d0822fb48455be3bcc96b9eca6c8a142
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Aug 20 07:19:02 EDT 2018

[lib/c] Fix libc compilation errors

This is a first step to get a working version of the library.
At this moment the code is only to be compiled using gcc compatible
compilers.

--- a/lib/Makefile
+++ b/lib/Makefile
@@ -4,7 +4,7 @@
 
 include $(PROJECTDIR)/rules.mk
 
-DIRS = scc coff32 crt
+DIRS = scc coff32 crt c
 
 all: $(DIRS)
 
--- a/lib/c/Makefile
+++ b/lib/c/Makefile
@@ -1,6 +1,7 @@
 .POSIX:
 
 PROJECTDIR = ../..
+include $(PROJECTDIR)/rules.mk
 
 DIRS = target
 
--- a/lib/c/__putc.c
+++ b/lib/c/__putc.c
@@ -11,7 +11,7 @@
 	int err;
 
 	if (fp)
-		return flsbuf(fp);
+		return _flsbuf(fp);
 
 	err = 0;
 	for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) {
@@ -56,10 +56,10 @@
 	}
 
 	if (fp->flags & _IOLBF) {
-		if (fp->lp == fp->rp && _flush(fp))
+		if (fp->lp == fp->rp && _flsbuf(fp))
 			return EOF;
 		*fp->lp++ = ch;
-		if (ch == '\n' && flsbuf(fp))
+		if (ch == '\n' && _flsbuf(fp))
 			return EOF;
 	} else {
 		if (fp->wp == fp->rp && _flsbuf(fp))
--- a/lib/c/_fpopen.c
+++ b/lib/c/_fpopen.c
@@ -52,12 +52,12 @@
 		return NULL;
 	}
 
-	if ((fd = _open(name, flags)) < 0)
+	if ((fd = _open(fname, flags)) < 0)
 		return NULL;
 
 	if (fp->buf == NULL) {
 		if ((fp->buf = malloc(BUFSIZ)) == NULL) {
-			close(fd);
+			_close(fd);
 			errno = ENOMEM;
 			return NULL;
 		}
@@ -66,7 +66,7 @@
 	fp->fd = fd;
 
 	if (!bin)
-		fp->flags |= _IOTEXT;
+		fp->flags |= _IOTXT;
 
 	if (flags & O_RDWR)
 		fp->flags |= _IORW;
--- a/lib/c/fclose.c
+++ b/lib/c/fclose.c
@@ -1,4 +1,5 @@
 
+#include <stdlib.h>
 #include <stdio.h>
 #include "syscall.h"
 #undef fclose
@@ -11,7 +12,7 @@
 	int r = EOF;
 
 	if ((fp->flags & _IOSTRG) == 0 &&
-	    fp->flags & (_IOWRITE | _IOREAD | _IOWR)) {
+	    fp->flags & (_IOWRITE | _IOREAD | _IORW)) {
 		r = 0;
 		if (_flsbuf(fp) == EOF)
 			r = EOF;
@@ -24,7 +25,7 @@
 		fp->buf = NULL;
 	}
 
-	fp->flags &= ~(_IOWRITE | _IOREAD | _IOWR |
+	fp->flags &= ~(_IOWRITE | _IOREAD | _IORW |
 	               _IOERR | _IOEOF |
 	               _IOALLOC |
 	               _IOTXT |
--- a/lib/c/fopen.c
+++ b/lib/c/fopen.c
@@ -1,18 +1,21 @@
 
 #include <errno.h>
 #include <stdio.h>
+
+#include "syscall.h"
 #undef fopen
 
+
 FILE *
 fopen(const char * restrict name, const char * restrict mode)
 {
 	FILE *fp;
 
-	for (fp = __iob; fp < &__iob[FILE_MAX]; ++fp) {
+	for (fp = __iob; fp < &__iob[FOPEN_MAX]; ++fp) {
 		if (fp->flags & (_IOREAD | _IOWRITE | _IORW) == 0)
 			break;
 	}
-	if (fp == &__iob[FILE_MAX]) {
+	if (fp == &__iob[FOPEN_MAX]) {
 		errno = ENOMEM;
 		return NULL;
 	}
--- a/lib/c/freopen.c
+++ b/lib/c/freopen.c
@@ -1,5 +1,7 @@
 
 #include <stdio.h>
+
+#include "syscall.h"
 #undef freopen
 
 FILE *
--- a/lib/c/fseek.c
+++ b/lib/c/fseek.c
@@ -14,9 +14,9 @@
 	if ((fp->flags & _IOWRITE) && _flsbuf(fp))
 		return -1;
 	else if (whence == SEEK_CUR && (fp->flags & _IOREAD))
-		off -= fp->wp - fp->rd;
+		off -= fp->wp - fp->rp;
 
-	if (_seek(fp->fd, off, type) < 0) {
+	if (_lseek(fp->fd, off, whence) < 0) {
 		fp->flags |= _IOERR;
 		return EOF;
 	}
--- a/lib/c/include/bits/amd64-sysv/arch/stdio.h
+++ b/lib/c/include/bits/amd64-sysv/arch/stdio.h
@@ -10,4 +10,6 @@
 #define TMP_MAX        25
 #define L_tmpnam      256
 
+#define _TMPNAME      "/tmp/tmp.0000000"
+
 typedef int fpos_t;
--- a/lib/c/include/bits/i386-sysv/arch/stdio.h
+++ b/lib/c/include/bits/i386-sysv/arch/stdio.h
@@ -10,4 +10,6 @@
 #define TMP_MAX        25
 #define L_tmpnam      256
 
+#define _TMPNAME      "/tmp/tmp.0000000"
+
 typedef long fpos_t;
--- a/lib/c/include/bits/z80-dos/arch/stdio.h
+++ b/lib/c/include/bits/z80-dos/arch/stdio.h
@@ -10,4 +10,6 @@
 #define TMP_MAX        25
 #define L_tmpnam      256
 
+#define _TMPNAME      "TMP.000"
+
 typedef long fpos_t;
--- a/lib/c/include/errno.h
+++ b/lib/c/include/errno.h
@@ -6,6 +6,7 @@
 #define ERANGE 3
 #define ENOMEM 4
 #define EBADF  5
+#define EINVAL 6
 
 extern int errno;
 extern char *_sys_errlist[];
--- a/lib/c/include/string.h
+++ b/lib/c/include/string.h
@@ -29,5 +29,6 @@
 extern void *memset(void *s, int c, size_t n);
 extern char *strerror(int errnum);
 extern size_t strlen(const char *s);
+extern size_t strnlen(const char *s, size_t maxlen);
 
 #endif
--- /dev/null
+++ b/lib/c/include/wchar.h
@@ -1,0 +1,12 @@
+#ifndef _WCHAR_H_
+#define _WCHAR_H_
+
+
+/* TODO: This is only a placeholder */
+typedef long wchar_t;
+typedef long wint_t;
+
+extern size_t wcsnlen(const wchar_t *s, size_t maxlen);
+extern wint_t putwc(wchar_t wc, FILE *fp);
+
+#endif
--- a/lib/c/putc.c
+++ b/lib/c/putc.c
@@ -5,5 +5,5 @@
 int
 putc(int ch, FILE *fp)
 {
-	return (fp->wp >= fp->rp) ? __putc(c,fp) : *fp->wp++ = c;
+	return (fp->wp >= fp->rp) ? __putc(ch,fp) : (*fp->wp++ = ch);
 }
--- a/lib/c/setvbuf.c
+++ b/lib/c/setvbuf.c
@@ -1,6 +1,7 @@
 
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #undef setvbuf
 
 extern int _flsbuf(FILE *fp);
@@ -29,7 +30,7 @@
 		}
 		break;
 	default:
-		errno = EIVAL;
+		errno = EINVAL;
 		return EOF;
 	}
 
@@ -36,11 +37,11 @@
 	flags = fp->flags;
 	if (flags & _IOALLOC)
 		free(fp->buf);
-	flag &= ~(_IONBF | _IOLBF | _IOFBF | _IOALLOC | _IOALLOC);
+	flags &= ~(_IONBF | _IOLBF | _IOFBF | _IOALLOC | _IOALLOC);
 	flags |= mode;
 	fp->flags = flags;
 	fp->buf = buf;
-	fp->size = size;
+	fp->len = size;
 
 	return 0;
 }
--- a/lib/c/strpbrk.c
+++ b/lib/c/strpbrk.c
@@ -11,7 +11,7 @@
 		for (p = s2; *p && *p != c; ++p)
 			;
 		if (*p == c)
-			return s1;
+			return (char *) s1;
 	}
 	return NULL;
 }
--- a/lib/c/syscall.h
+++ b/lib/c/syscall.h
@@ -1,14 +1,29 @@
 extern void *_brk(void *addr);
-extern int _open(char *path, int flags, int perm);
+extern int _open(const char *path, int flags);
 extern int _close(int fd);
 extern int _read(int fd, void *buf, size_t n);
 extern int _write(int fd, void *buf, size_t n);
 extern int _lseek(int fd, long off, int whence);
 extern void _Exit(int status);
-extern void _access(char *path, int mode);
+extern int _access(char *path, int mode);
 
 extern int raise(int sig);
 extern void (*signal(int sig, void (*func)(int)))(int);
-extern getenv(const char *var);
+extern char *getenv(const char *var);
 extern int rename(const char *from, const char *to);
 extern int remove(const char *path);
+
+/* auxiliar functions */
+
+#ifdef stdin
+extern FILE *_fpopen(const char * restrict fname,
+                     const char * restrict mode,
+                     FILE * restrict fp);
+#endif
+
+#define O_WRONLY  1
+#define O_RDONLY  2
+#define O_RDWR    4
+#define O_TRUNC   8
+#define O_APPEND 16
+#define O_CREAT  32
--- a/lib/c/target/objlst.mk
+++ b/lib/c/target/objlst.mk
@@ -5,12 +5,12 @@
       tmpnam.o \
       sprintf.o snprintf.o vsprintf.o vsnprintf.o \
       printf.o fprintf.o vfprintf.o \
-      fgets.o gets.of fgetc.o fputc.o getchar.o putchar.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 \
       rewind.o fseek.o ferror.o feof.o clearerr.o \
       setbuf.o setvbuf.o \
-      fclose.o fopen.c freopen.c _fpopen.o _flsbuf.o stdio.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 \
--- a/lib/c/vsnprintf.c
+++ b/lib/c/vsnprintf.c
@@ -9,13 +9,13 @@
 	FILE f;
 	int r;
 
-	f.flag = _IOWRT | _IOSTRG;
-	f.size = siz;
+	f.flags = _IORW | _IOSTRG;
+	f.len = siz;
 	f.buf = s;
 	f.wp = s;
 	f.rp = s + siz;
 
-	r = vfprintf(&f, fmt, va);
+	r = vfprintf(&f, fmt, ap);
 	if (s) {
 		if (f.wp == f.rp)
 			--f.wp;
--- a/lib/c/vsprintf.c
+++ b/lib/c/vsprintf.c
@@ -1,6 +1,7 @@
 
 #include <limits.h>
 #include <stdarg.h>
+#include <stdint.h>
 #include <stdio.h>
 #undef vsprintf