Files
my-os-project2/user/fs/fetch.c
2025-10-03 20:03:03 +02:00

68 lines
1.7 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <ulib.h>
void fs_fetch(void) {
if (argslen() < 2) {
uprintf("fs: Not enough arguments\n");
return;
}
char *path = *(args()+1);
IoctlStat statbuf; ZERO(&statbuf);
if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)path, 0) != E_OK) {
uprintf("fs: could not stat %s\n", path);
return;
}
if (statbuf.type == IOCTLSTAT_FILE) {
IOH ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)path, IOCTL_F_READ, 0);
if (ioh < 0) {
uprintf("fs: could not open %s\n", path);
return;
}
uint8_t *buf = umalloc(statbuf.size+1);
string_memset(buf, 0, statbuf.size+1);
if (ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0) < 0) {
uprintf("fs: coult not read %s\n", path);
goto donefile;
return;
}
uprintf("%s", buf);
donefile:
ufree(buf);
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
} else if (statbuf.type == IOCTLSTAT_DIR) {
IoctlDirent *dirents = (IoctlDirent *)umalloc(statbuf.size * sizeof(*dirents));
for (size_t i = 0; i < statbuf.size; i++) {
ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)path, (uint64_t)&dirents[i], i);
IoctlDirent *dirent = &dirents[i];
if (dirent->stat.type == IOCTLSTAT_FILE) {
char *membuf = umalloc(20);
uprintf("%-30s %-15s %-1s\n",
dirent->name,
human_size(dirent->stat.size, membuf, 20),
"F"
);
ufree(membuf);
} else if (dirent->stat.type == IOCTLSTAT_DIR) {
uprintf("%-30s %-15s %-1s\n",
dirent->name,
"-",
"D"
);
}
}
ufree(dirents);
}
}