working on it

This commit is contained in:
Dawid Sobczak 2025-04-05 10:55:40 +01:00
parent 56a6e78765
commit 35a88970c2
1094 changed files with 51093 additions and 51 deletions

View file

@ -0,0 +1,12 @@
#include <string.h>
#include <libgen.h>
char *basename(char *s)
{
size_t i;
if (!s || !*s) return ".";
i = strlen(s)-1;
for (; i&&s[i]=='/'; i--) s[i] = 0;
for (; i&&s[i-1]!='/'; i--);
return s+i;
}

View file

@ -0,0 +1,7 @@
#include <endian.h>
#include <stdint.h>
uint32_t bswap_32(uint32_t x)
{
return x>>24 | x>>16&0xff00 | x<<16&0xff0000 | x<<24;
}

View file

@ -0,0 +1,9 @@
#include <endian.h>
#include <stdint.h>
uint32_t bswap_32(uint32_t);
uint64_t bswap_64(uint64_t x)
{
return bswap_32(x)+0LL<<32 | bswap_32(x>>32);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,14 @@
#define _GNU_SOURCE
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
char *cuserid(char *buf)
{
struct passwd pw, *ppw;
long pwb[256];
if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw))
return 0;
snprintf(buf, L_cuserid, "%s", pw.pw_name);
return buf;
}

View file

@ -0,0 +1,15 @@
#include <string.h>
#include <libgen.h>
char *dirname(char *s)
{
size_t i;
if (!s || !*s || !strchr(s, '/')) return ".";
i = strlen(s)-1;
for (; i&&s[i]=='/'; i--);
for (; i&&s[i-1]!='/'; i--);
for (; i&&s[i-1]=='/'; i--);
if (!i && *s=='/') i++;
s[i] = 0;
return s;
}

View file

@ -0,0 +1,9 @@
#include <strings.h>
int ffs(int i)
{
unsigned int j = i;
for (i=1; j && !(j&1); j>>=1, i++);
if (j) return i;
return 0;
}

View file

@ -0,0 +1,9 @@
#include <ftw.h>
#include "libc.h"
int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int fd_limit)
{
return nftw(path, (void *)fn, fd_limit, FTW_PHYS);
}
LFS64(ftw);

View file

@ -0,0 +1,9 @@
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
int getdomainname(char *name, size_t len)
{
*name = 0;
return 0;
}

View file

@ -0,0 +1,11 @@
#include <grp.h>
/* FIXME */
int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
{
if (*ngroups<1) return -1;
*groups = gid;
*ngroups = 1;
return 0;
}

View file

@ -0,0 +1,63 @@
#include <unistd.h>
#include <wchar.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
char *optarg;
int optind=1, opterr=1, optopt;
static int optpos;
int getopt(int argc, char * const argv[], const char *optstring)
{
int i;
wchar_t c, d;
int k, l;
char *optchar;
if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
return -1;
if (argv[optind][1] == '-' && !argv[optind][2])
return optind++, -1;
if (!optpos) optpos++;
if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
k = 1;
c = 0xfffd; /* replacement char */
}
optchar = argv[optind]+optpos;
optopt = c;
optpos += k;
if (!argv[optind][optpos]) {
optind++;
optpos = 0;
}
for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
if (d != c) {
if (optstring[0] != ':' && opterr) {
write(2, argv[0], strlen(argv[0]));
write(2, ": illegal option: ", 18);
write(2, optchar, k);
write(2, "\n", 1);
}
return '?';
}
if (optstring[i+1] == ':') {
if (optind >= argc) {
if (optstring[0] == ':') return ':';
if (opterr) {
write(2, argv[0], strlen(argv[0]));
write(2, ": option requires an argument: ", 31);
write(2, optchar, k);
write(2, "\n", 1);
}
return '?';
}
optarg = argv[optind++] + optpos;
optpos = 0;
}
return c;
}

View file

@ -0,0 +1,9 @@
#include <sys/resource.h>
#include "syscall.h"
int getpriority(int which, id_t who)
{
int ret = syscall2(__NR_getpriority, which, who);
if (ret < 0) return ret;
return 20-ret;
}

View file

@ -0,0 +1,15 @@
#include <sys/resource.h>
#include "syscall.h"
#include "libc.h"
int getrlimit(int resource, struct rlimit *rlim)
{
long k_rlim[2];
if (syscall2(__NR_getrlimit, resource, (long)k_rlim) < 0)
return -1;
rlim->rlim_cur = k_rlim[0] == -1 ? -1 : (unsigned long)k_rlim[0];
rlim->rlim_max = k_rlim[1] == -1 ? -1 : (unsigned long)k_rlim[1];
return 0;
}
LFS64(getrlimit);

View file

@ -0,0 +1,20 @@
#include <sys/resource.h>
#include <string.h>
#include "syscall.h"
/* this is a huge hack to make up for the kernel's stupid 32bit time_t
* without having to recopy the whole rusage structure ourselves.. */
int getrusage(int who, struct rusage *ru)
{
struct { long tv_sec, tv_usec; } ktv[2];
char *fakeaddr = ((char *)ru + sizeof(struct timeval [2]) - sizeof ktv);
if (syscall2(__NR_getrusage, who, (long)fakeaddr) < 0)
return -1;
memcpy(ktv, fakeaddr, sizeof ktv);
ru->ru_utime.tv_sec = ktv[0].tv_sec;
ru->ru_utime.tv_usec = ktv[0].tv_usec;
ru->ru_stime.tv_sec = ktv[1].tv_sec;
ru->ru_stime.tv_usec = ktv[1].tv_usec;
return 0;
}

View file

@ -0,0 +1,23 @@
#include <stdlib.h>
#include <string.h>
int getsubopt(char **opt, char *const *keys, char **val)
{
char *s = *opt;
int i;
*val = NULL;
*opt = strchr(s, ',');
if (*opt) *(*opt)++ = 0;
else *opt = s + strlen(s);
for (i=0; keys[i]; i++) {
size_t l = strlen(keys[i]);
if (strncmp(keys[i], s, l)) continue;
if (s[l] == '=')
*val = s + l;
else if (s[l]) continue;
return i;
}
return -1;
}

View file

@ -0,0 +1,13 @@
#include <sys/ioctl.h>
#include <stdarg.h>
#include "syscall.h"
int ioctl(int fd, int req, ...)
{
void *arg;
va_list ap;
va_start(ap, req);
arg = va_arg(ap, void *);
va_end(ap);
return syscall3(__NR_ioctl, fd, req, (long)arg);
}

View file

@ -0,0 +1,33 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "libc.h"
int lockf(int fd, int op, off_t size)
{
struct flock l = {
.l_type = F_WRLCK,
.l_whence = SEEK_CUR,
.l_len = size,
};
switch (op) {
case F_TEST:
l.l_type = F_RDLCK;
if (fcntl(fd, F_GETLK, &l) < 0)
return -1;
if (l.l_type == F_UNLCK || l.l_pid == getpid())
return 0;
errno = EACCES;
return -1;
case F_ULOCK:
l.l_type = F_UNLCK;
case F_TLOCK:
return fcntl(fd, F_SETLK, &l);
case F_LOCK:
return fcntl(fd, F_SETLKW, &l);
}
errno = EINVAL;
return -1;
}
LFS64(lockf);

View file

@ -0,0 +1,121 @@
#include <ftw.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include "libc.h"
struct history
{
struct history *chain;
dev_t dev;
ino_t ino;
int level;
int base;
};
#undef dirfd
#define dirfd(d) (*(int *)d)
static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
{
size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
struct stat st;
struct history new;
int type;
int r;
struct FTW lev;
char *name;
if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
type = FTW_SLN;
else if (errno != EACCES) return -1;
else type = FTW_NS;
} else if (S_ISDIR(st.st_mode)) {
if (access(path, R_OK) < 0) type = FTW_DNR;
else if (flags & FTW_DEPTH) type = FTW_DP;
else type = FTW_D;
} else if (S_ISLNK(st.st_mode)) {
if (flags & FTW_PHYS) type = FTW_SL;
else type = FTW_SLN;
} else {
type = FTW_F;
}
if ((flags & FTW_MOUNT) && h
&& (st.st_dev != h->dev || st.st_ino != h->ino))
return 0;
new.chain = h;
new.dev = st.st_dev;
new.ino = st.st_ino;
new.level = h ? h->level+1 : 0;
new.base = l+1;
lev.level = new.level;
lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0;
if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
return r;
for (; h; h = h->chain)
if (h->dev == st.st_dev && h->ino == st.st_ino)
return 0;
if ((type == FTW_D || type == FTW_DP) && fd_limit) {
DIR *d = opendir(path);
if (d) {
struct dirent *de;
while ((de = readdir(d))) {
if (de->d_name[0] == '.'
&& (!de->d_name[1]
|| (de->d_name[1]=='.'
&& !de->d_name[2]))) continue;
if (strlen(de->d_name) >= PATH_MAX-l) {
errno = ENAMETOOLONG;
closedir(d);
return -1;
}
path[j]='/';
strcpy(path+j+1, de->d_name);
if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
closedir(d);
return r;
}
}
closedir(d);
} else if (errno != EACCES) {
return -1;
}
}
path[l] = 0;
if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
return r;
return 0;
}
int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
{
size_t l;
char pathbuf[PATH_MAX+1];
if (fd_limit <= 0) return 0;
l = strlen(path);
if (l > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
memcpy(pathbuf, path, l+1);
return do_nftw(pathbuf, fn, fd_limit, flags, NULL);
}
LFS64(nftw);

View file

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <pty.h>
#include <stdio.h>
/* Nonstandard, but vastly superior to the standard functions */
int openpty(int *m, int *s, char *name, const struct termios *tio, const struct winsize *ws)
{
int n=0;
char buf[20];
*m = open("/dev/ptmx", O_RDWR|O_NOCTTY);
if (!*m) return -1;
if (ioctl(*m, TIOCSPTLCK, &n) || ioctl (*m, TIOCGPTN, &n)) {
close(*m);
return -1;
}
if (!name) name = buf;
snprintf(name, sizeof buf, "/dev/pts/%d", n);
if ((*s = open(name, O_RDWR|O_NOCTTY)) < 0) {
close(*m);
return -1;
}
if (tio) tcsetattr(*s, TCSANOW, tio);
if (ws) ioctl(*s, TIOCSWINSZ, ws);
return 0;
}

View file

@ -0,0 +1,35 @@
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
int posix_openpt(int flags)
{
return open("/dev/ptmx", flags);
}
int grantpt(int fd)
{
return 0;
}
int unlockpt(int fd)
{
int unlock = 0;
return ioctl(fd, TIOCSPTLCK, &unlock);
}
char *ptsname(int fd)
{
static char buf[9 + sizeof(int)*3 + 1];
char *s = buf+sizeof(buf)-1;
int pty;
if (ioctl (fd, TIOCGPTN, &pty))
return NULL;
if (pty) for (; pty; pty/=10) *--s = '0' + pty%10;
else *--s = '0';
s -= 9;
s[0] = '/'; s[1] = 'd'; s[2] = 'e'; s[3] = 'v';
s[4] = '/'; s[5] = 'p'; s[6] = 't'; s[7] = 's'; s[8] = '/';
return s;
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
char *realpath(const char *filename, char *resolved)
{
return 0;
}

View file

@ -0,0 +1,10 @@
#include <sched.h>
#include "syscall.h"
#include "libc.h"
int __yield()
{
return syscall0(__NR_sched_yield);
}
weak_alias(__yield, sched_yield);

View file

@ -0,0 +1,7 @@
#include <sys/resource.h>
#include "syscall.h"
int setpriority(int which, id_t who, int prio)
{
return syscall3(__NR_getpriority, which, who, prio);
}

View file

@ -0,0 +1,11 @@
#include <sys/resource.h>
#include "syscall.h"
#include "libc.h"
int setrlimit(int resource, const struct rlimit *rlim)
{
long k_rlim[2] = { rlim->rlim_cur, rlim->rlim_max };
return syscall2(__NR_setrlimit, resource, (long)k_rlim);
}
LFS64(setrlimit);

View file

@ -0,0 +1,115 @@
#include <stdarg.h>
#include <sys/socket.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include "libc.h"
static int lock;
static const char *log_ident;
static int log_opt;
static int log_facility = LOG_USER;
static int log_mask = 0xff;
static FILE *log_f;
int setlogmask(int maskpri)
{
int old = log_mask;
if (maskpri) log_mask = maskpri;
return old;
}
static const struct {
short sun_family;
char sun_path[9];
} log_addr = {
AF_UNIX,
"/dev/log"
};
void closelog(void)
{
LOCK(&lock);
if (log_f) fclose(log_f);
log_f = NULL;
UNLOCK(&lock);
}
static void __openlog(const char *ident, int opt, int facility)
{
int fd;
log_ident = ident;
log_opt = opt;
log_facility = facility;
if (!(opt & LOG_NDELAY) || log_f) return;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (connect(fd, (void *)&log_addr, sizeof(short) + sizeof "/dev/log") < 0)
close(fd);
else log_f = fdopen(fd, "wb");
}
void openlog(const char *ident, int opt, int facility)
{
LOCK(&lock);
__openlog(ident, opt, facility);
UNLOCK(&lock);
}
void syslog(int priority, const char *message, ...)
{
struct sigaction sa;
va_list ap;
char timebuf[16];
time_t now;
struct tm tm;
//const char *fmt, *ident, *sep;
//int i;
if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
LOCK(&lock);
if (!log_f) __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
if (!log_f) {
UNLOCK(&lock);
return;
}
memset(&sa, 0, sizeof sa);
sa.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &sa, &sa) < 0) {
// we must abandon logging or we might cause SIGPIPE
UNLOCK(&lock);
return;
}
now = time(NULL);
gmtime_r(&now, &tm);
strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
fprintf(log_f, "<%d>%s ", priority, timebuf);
if (log_ident) fprintf(log_f, "%s", log_ident);
if (log_opt & LOG_PID) fprintf(log_f, "[%d]", getpid());
if (log_ident) fprintf(log_f, ": ");
va_start(ap, message);
vfprintf(log_f, message, ap);
va_end(ap);
fputc(0, log_f);
fflush(log_f);
// Note: LOG_CONS is not supported because it is annoying!!
// syslogd will send messages to console if it deems them appropriate!
sigaction(SIGPIPE, &sa, NULL);
UNLOCK(&lock);
}

View file

@ -0,0 +1,8 @@
#include <sys/utsname.h>
#include <string.h>
#include "syscall.h"
int uname(struct utsname *uts)
{
return syscall1(__NR_uname, (long)uts);
}