shithub: npe

Download patch

ref: c892d8d9aeb18a0872652cde8dd301821dd0c397
parent: 1f24e2a0c85f9250d71225db08f05b126c320a0c
author: qwx <qwx@sciops.net>
date: Mon Mar 2 18:59:21 EST 2026

import and define some missing functions from ape for schockolate

required by npe port of schockolate [1].

[1] https://github.com/Interrupt/systemshock

--- a/include/npe/math.h
+++ b/include/npe/math.h
@@ -32,6 +32,8 @@
 #define powf pow
 #define sqrtf sqrt
 
+#define	sqrtl sqrt
+
 double exp2(double);
 #define exp2f exp2
 
--- a/include/npe/stdlib.h
+++ b/include/npe/stdlib.h
@@ -22,6 +22,8 @@
 int mkstemp(char *t);
 div_t div(int n, int d);
 
+long long llabs(long long);
+
 void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
 		int (*compar)(const void*, const void*));
 
--- a/include/npe/string.h
+++ b/include/npe/string.h
@@ -6,5 +6,7 @@
 char *strerror(int errnum);
 int strerror_r(int errnum, char *buf, size_t buflen);
 size_t strlcpy(char *dst, char *src, size_t sz);
+size_t strnlen(char *s, size_t maxlen);
+char *strtok_r(char *s, char *b, char **last);
 
 #endif
--- a/include/npe/unistd.h
+++ b/include/npe/unistd.h
@@ -61,6 +61,7 @@
 int npe_stat(char *filename, struct stat *buf);
 int npe_fstat(int fd, struct stat *buf);
 int fstatat(int dirfd, char *path, struct stat *buf, int flags);
+int ftruncate(int fd, off_t length);
 int unlink(char *path);
 int access(char *name, int mode);
 void usleep(useconds_t us);
--- /dev/null
+++ b/libnpe/abs.c
@@ -1,0 +1,9 @@
+#include <stdlib.h>
+
+long long
+llabs(long long a)
+{
+	if(a < 0)
+		return -a;
+	return a;
+}
--- /dev/null
+++ b/libnpe/ftruncate.c
@@ -1,0 +1,20 @@
+#include <npe.h>
+#include <errno.h>
+
+int
+ftruncate(int fd, off_t length)
+{
+	Dir d;
+
+	if(length < 0){
+		errno = EINVAL;
+		return -1;
+	}
+	nulldir(&d);
+	d.length = length;
+	if(dirfwstat(fd, &d) < 0){
+		errno = ENOENT;
+		return -1;
+	}
+	return 0;
+}
--- a/libnpe/mkfile
+++ b/libnpe/mkfile
@@ -13,6 +13,7 @@
 	_main.$O\
 	_npe.$O\
 	_parg.$O\
+	abs.$O\
 	acosh.$O\
 	basename.$O\
 	bsearch.$O\
@@ -28,6 +29,7 @@
 	exp2.$O\
 	fmax.$O\
 	fstatat.$O\
+	ftruncate.$O\
 	fts_children.$O\
 	fts_close.$O\
 	fts_open.$O\
@@ -69,6 +71,8 @@
 	strerror_r.$O\
 	strftime.$O\
 	strlcpy.$O\
+	strnlen.$O\
+	strtok.$O\
 	system.$O\
 	trunc.$O\
 	unlink.$O\
--- /dev/null
+++ b/libnpe/strnlen.c
@@ -1,0 +1,11 @@
+#include <string.h>
+
+size_t
+strnlen(char *s, size_t maxlen)
+{
+	size_t i;
+
+	for (i = 0; i < maxlen && s[i] != '\0'; i++)
+		;
+	return i;
+}
--- /dev/null
+++ b/libnpe/strtok.c
@@ -1,0 +1,28 @@
+#include <string.h>
+
+#define	N	256
+
+char*
+strtok_r(char *s, char *b, char **last)
+{
+	char map[N], *os;
+
+	memset(map, 0, N);
+	while(*b)
+		map[*(unsigned char*)b++] = 1;
+	if(s == 0)
+		s = *last;
+	while(map[*(unsigned char*)s++])
+		;
+	if(*--s == 0)
+		return 0;
+	os = s;
+	while(map[*(unsigned char*)s] == 0)
+		if(*s++ == 0) {
+			*last = s-1;
+			return os;
+		}
+	*s++ = 0;
+	*last = s;
+	return os;
+}
--