shithub: scc

Download patch

ref: 895c2c914ce5ea912b0f1818bc173d7aadbee32e
parent: 5256bd97e16fe134d0e9b8b072997b4e9df6cbb0
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Sep 14 09:30:05 EDT 2021

libc: Fix arch/posix

As part of the big integration some of the functions located in
arch/posix were broken. This patch solves the problem and makes
a better separation between portable and not portable parts  of
the code.

--- a/include/bits/darwin/sys.h
+++ b/include/bits/darwin/sys.h
@@ -6,6 +6,7 @@
 #define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
 
+#define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 1000000)
 #define RUSAGE_SELF 0
 
--- a/include/bits/dragonfly/sys.h
+++ b/include/bits/dragonfly/sys.h
@@ -6,6 +6,7 @@
 #define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
 
+#define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 128)
 #define RUSAGE_SELF 0
 
--- a/include/bits/linux/sys.h
+++ b/include/bits/linux/sys.h
@@ -6,6 +6,7 @@
 #define O_APPEND  0x00000400
 #define O_CREAT   0x00000040
 
+#define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 1000000)
 #define RUSAGE_SELF 0
 
--- a/include/bits/netbsd/sys.h
+++ b/include/bits/netbsd/sys.h
@@ -6,6 +6,7 @@
 #define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
 
+#define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 100)
 #define RUSAGE_SELF 0
 
--- a/include/bits/openbsd/sys.h
+++ b/include/bits/openbsd/sys.h
@@ -6,6 +6,7 @@
 #define O_APPEND  0x00000008
 #define O_CREAT   0x00000200
 
+#define AT_FDCWD  -100
 #define CLOCKS_PER_SEC ((clock_t) 100)
 #define RUSAGE_SELF 0
 
--- /dev/null
+++ b/src/libc/arch/posix/Makefile
@@ -1,0 +1,20 @@
+.POSIX:
+PROJECTDIR =../../../..
+RULES = user
+include $(PROJECTDIR)/scripts/rules.mk
+include $(SRCDIR)/libc/rules.mk
+
+OBJS=\
+	_getheap.$O\
+	_open.$O\
+	_systime.$O\
+	_tzone.$O\
+	clock.$O\
+	getenv.$O\
+	raise.$O\
+	signal.$O\
+	time.$O\
+
+all: $(OBJS)
+
+include deps.mk
--- a/src/libc/arch/posix/_open.c
+++ b/src/libc/arch/posix/_open.c
@@ -1,10 +1,10 @@
 #include <stddef.h>
 
-#include "../../../syscall.h"
+#include <sys.h>
 
-#define AT_FDCWD  -100
+#include "../../syscall.h"
 
-extern int _openat(int fd, const char *fname, int flags, int mode);
+extern int _openat(int, const char *, int, int);
 
 int
 _open(const char *fname, int flags, int mode)
--- /dev/null
+++ b/src/libc/arch/posix/_systime.c
@@ -1,0 +1,21 @@
+#include <time.h>
+
+#include "../../libc.h"
+
+time_t
+_systime(struct tm *tm)
+{
+	time_t t = 0;
+	int year = tm->tm_year + MINYEAR;
+
+	for (int i = EPOCH; i < year; ++i)
+		t += _daysyear(i) * SECDAY;
+	for (int i = 0; i < tm->tm_mon; ++i)
+		t += _daysmon[i] * SECDAY;
+
+	t += tm->tm_sec;
+	t += tm->tm_min * SECMIN;
+	t += tm->tm_hour * SECHOUR;
+	t += (tm->tm_mday-1) * SECDAY;
+	return t;
+}
--- a/src/libc/arch/posix/clock.c
+++ b/src/libc/arch/posix/clock.c
@@ -36,5 +36,5 @@
 	if (_getrusage(RUSAGE_SELF, &ru))
 		return -1;
 
-	return TOCLOCK(ru.ru_utime) + TOCLOCK(ru_ru_stime);
+	return TOCLOCK(ru.ru_utime) + TOCLOCK(ru.ru_stime);
 }
--- /dev/null
+++ b/src/libc/arch/posix/deps.mk
@@ -1,0 +1,7 @@
+#deps
+./_getheap.o: ./../../libc.h
+./_open.o: ./../../syscall.h
+./_systime.o: ./../../libc.h
+./_tzone.o: ./../../libc.h
+./clock.o: ./time.h
+./time.o: ./time.h
--- a/src/libc/arch/posix/getenv.c
+++ b/src/libc/arch/posix/getenv.c
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <string.h>
+
 #undef getenv
 
 extern char **_environ;
--- a/src/libc/arch/posix/raise.c
+++ b/src/libc/arch/posix/raise.c
@@ -1,5 +1,5 @@
-#include <stddef.h>
 #include <signal.h>
+
 #include <sys.h>
 
 #undef raise
--- a/src/libc/arch/posix/signal.c
+++ b/src/libc/arch/posix/signal.c
@@ -1,17 +1,18 @@
-#include <stddef.h>
 #include <signal.h>
+
 #include <sys.h>
+
 #undef signal
 
 void
 (*signal(int signum, void (*func)(int)))(int)
 {
-	struct sigaction sa = {
+	struct sigaction osa, sa = {
 		.sa_handler = func,
 	};
 
-	if (_sigaction(signum, &sa, &sa) < 0)
+	if (_sigaction(signum, &sa, &osa) < 0)
 		return SIG_ERR;
 
-	return sa.sa_handler;
+	return osa.sa_handler;
 }
--- a/src/libc/arch/posix/time.c
+++ b/src/libc/arch/posix/time.c
@@ -1,7 +1,8 @@
 #include <time.h>
 
-int
-_gettimeofday(struct timeval *restrict, void *tzp);
+#include "time.h"
+
+extern int _gettimeofday(struct timeval *restrict, void *);
 
 time_t
 time(time_t *t)
--- /dev/null
+++ b/src/libc/arch/posix/time.h
@@ -1,0 +1,4 @@
+struct timeval {
+	time_t tv_sec;
+	__suseconds_t tv_usec;
+};