0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-10-31 19:42:39 +00:00
uxn/src/devices/file.c
2021-11-07 10:30:56 -05:00

149 lines
3 KiB
C

#include "../uxn.h"
#include "file.h"
/*
Copyright (c) 2021 Devine Lu Linvega
Copyright (c) 2021 Andrew Alderwick
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
static FILE *f;
static DIR *d;
static char *current_filename = "";
static enum { IDLE,
FILE_READ,
FILE_WRITE,
DIR_READ } state;
static struct dirent *de;
static char hex[] = "0123456789abcdef";
static void
reset(void)
{
if(f != NULL) {
fclose(f);
f = NULL;
}
if(d != NULL) {
closedir(d);
d = NULL;
}
de = NULL;
state = IDLE;
}
static Uint16
get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero)
{
struct stat st;
if(len < strlen(basename) + 7) return 0;
memcpy(p, "???? ", 5);
strcpy(p + 5, basename);
strcat(p, "\n");
if(stat(pathname, &st))
return fail_nonzero ? strlen(p) : 0;
if(S_ISDIR(st.st_mode)) {
memcpy(p, "---- ", 5);
} else if(st.st_size < 0x10000) {
p[0] = hex[(st.st_size >> 12) & 0xf];
p[1] = hex[(st.st_size >> 8) & 0xf];
p[2] = hex[(st.st_size >> 4) & 0xf];
p[3] = hex[(st.st_size >> 0) & 0xf];
}
return strlen(p);
}
static Uint16
file_read_dir(void *dest, Uint16 len)
{
static char pathname[PATH_MAX];
char *p = dest;
if(de == NULL) de = readdir(d);
for(; de != NULL; de = readdir(d)) {
Uint16 n;
if(de->d_name[0] == '.' && de->d_name[1] == '\0')
continue;
strncpy(pathname, current_filename, sizeof(pathname) - 1);
strncat(pathname, "/", sizeof(pathname) - 1);
strncat(pathname, de->d_name, sizeof(pathname) - 1);
n = get_entry(p, len, pathname, de->d_name, 1);
if(!n) break;
p += n;
len -= n;
}
return p - (char *)dest;
}
Uint16
file_init(void *filename)
{
reset();
current_filename = (char *)filename;
return 0;
}
Uint16
file_read(void *dest, Uint16 len)
{
if(state != FILE_READ && state != DIR_READ) {
reset();
if((d = opendir(current_filename)) != NULL)
state = DIR_READ;
else if((f = fopen(current_filename, "rb")) != NULL)
state = FILE_READ;
}
if(state == FILE_READ)
return fread(dest, 1, len, f);
if(state == DIR_READ)
return file_read_dir(dest, len);
return 0;
}
Uint16
file_write(void *src, Uint16 len, Uint8 flags)
{
if(state != FILE_WRITE) {
reset();
if((f = fopen(current_filename, (flags & 0x01) ? "ab" : "wb")) != NULL)
state = FILE_WRITE;
}
if(state == FILE_WRITE) {
Uint16 ret = fwrite(src, 1, len, f);
fflush(f);
return ret;
}
return 0;
}
Uint16
file_stat(void *dest, Uint16 len)
{
char *basename = strrchr(current_filename, '/');
if(basename != NULL)
++basename;
else
basename = current_filename;
return get_entry(dest, len, current_filename, basename, 0);
}
Uint16
file_delete(void)
{
return unlink(current_filename);
}