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,9 @@
struct __DIR_s
{
int lock;
int fd;
off_t tell;
int buf_pos;
int buf_end;
char buf[2048];
};

View file

@ -0,0 +1,12 @@
#include <dirent.h>
#include "syscall.h"
#include "libc.h"
int __getdents(int fd, struct dirent *buf, size_t len)
{
return syscall3(__NR_getdents, fd, (long)buf, len);
}
weak_alias(__getdents, getdents);
LFS64(getdents);

View file

@ -0,0 +1,10 @@
#include <string.h>
#include <dirent.h>
#include "libc.h"
int alphasort(const struct dirent **a, const struct dirent **b)
{
return strcoll((*a)->d_name, (*b)->d_name);
}
LFS64(alphasort);

View file

@ -0,0 +1,11 @@
#include <dirent.h>
#include <unistd.h>
#include "__dirent.h"
#include "libc.h"
int closedir(DIR *dir)
{
int ret = close(dir->fd);
free(dir);
return ret;
}

View file

@ -0,0 +1,7 @@
#include <dirent.h>
#include "__dirent.h"
int dirfd(DIR *d)
{
return d->fd;
}

View file

@ -0,0 +1,26 @@
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "__dirent.h"
DIR *fdopendir(int fd)
{
DIR *dir;
struct stat st;
if (fstat(fd, &st) < 0 || !S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return 0;
}
if (!(dir = calloc(1, sizeof *dir))) {
return 0;
}
fcntl(fd, F_SETFD, FD_CLOEXEC);
dir->fd = fd;
return dir;
}

View file

@ -0,0 +1,25 @@
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "__dirent.h"
DIR *opendir(const char *name)
{
int fd;
DIR *dir;
if ((fd = open(name, O_RDONLY|O_DIRECTORY)) < 0)
return 0;
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (!(dir = calloc(1, sizeof *dir))) {
close(fd);
return 0;
}
dir->fd = fd;
return dir;
}

View file

@ -0,0 +1,32 @@
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include "__dirent.h"
#include "syscall.h"
#include "libc.h"
int __getdents(int, struct dirent *, size_t);
struct dirent *readdir(DIR *dir)
{
struct dirent *de;
if (dir->buf_pos >= dir->buf_end) {
int len = __getdents(dir->fd, (void *)dir->buf, sizeof dir->buf);
if (len < 0) {
dir->lock = 0;
return NULL;
} else if (len == 0) return 0;
dir->buf_end = len;
dir->buf_pos = 0;
}
de = (void *)(dir->buf + dir->buf_pos);
dir->buf_pos += de->d_reclen;
dir->tell = de->d_off;
return de;
}
LFS64(readdir);

View file

@ -0,0 +1,30 @@
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "__dirent.h"
#include "libc.h"
int readdir_r(DIR *dir, struct dirent *buf, struct dirent **result)
{
struct dirent *de;
int errno_save = errno;
int ret;
LOCK(&dir->lock);
errno = 0;
de = readdir(dir);
if ((ret = errno)) {
UNLOCK(&dir->lock);
return ret;
}
errno = errno_save;
if (de) memcpy(buf, de, de->d_reclen);
else buf = NULL;
UNLOCK(&dir->lock);
*result = buf;
return 0;
}
LFS64_2(readdir_r, readdir64_r);

View file

@ -0,0 +1,13 @@
#include <dirent.h>
#include <unistd.h>
#include "__dirent.h"
#include "libc.h"
void rewinddir(DIR *dir)
{
LOCK(&dir->lock);
lseek(dir->fd, 0, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
dir->tell = 0;
UNLOCK(&dir->lock);
}

View file

@ -0,0 +1,50 @@
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <stddef.h>
#include <libc.h>
int scandir(const char *path, struct dirent ***res,
int (*sel)(const struct dirent *),
int (*cmp)(const struct dirent **, const struct dirent **))
{
DIR *d = opendir(path);
struct dirent *de, **names=0, **tmp;
size_t cnt=0, len=0, size;
int old_errno = errno;
if (!d) return -1;
while ((errno=0), (de = readdir(d))) {
if (sel && !sel(de)) continue;
if (cnt >= len) {
len = 2*len+1;
if (len > SIZE_MAX/sizeof *names) break;
tmp = realloc(names, len * sizeof *names);
if (!tmp) break;
names = tmp;
}
size = offsetof(struct dirent,d_name) + strlen(de->d_name) + 1;
names[cnt] = malloc(size);
if (!names[cnt]) break;
memcpy(names[cnt++], de, size);
}
closedir(d);
if (errno) {
old_errno = errno;
if (names) while (cnt-->0) free(names[cnt]);
free(names);
errno = old_errno;
return -1;
}
if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp);
*res = names;
return cnt;
}
LFS64(scandir);

View file

@ -0,0 +1,12 @@
#include <dirent.h>
#include <unistd.h>
#include "__dirent.h"
#include "libc.h"
void seekdir(DIR *dir, long off)
{
LOCK(&dir->lock);
dir->tell = lseek(dir->fd, off, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
UNLOCK(&dir->lock);
}

View file

@ -0,0 +1,7 @@
#include <dirent.h>
#include "__dirent.h"
long telldir(DIR *dir)
{
return dir->tell;
}