Drop fstatat for building on MSYS2

This commit is contained in:
Andrew Alderwick 2021-11-07 00:45:55 +00:00
parent 0465b68ab5
commit 3acaaa2874
1 changed files with 19 additions and 17 deletions

View File

@ -20,6 +20,7 @@ WITH REGARD TO THIS SOFTWARE.
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h>
static FILE *f; static FILE *f;
static DIR *d; static DIR *d;
@ -56,19 +57,22 @@ file_prepare(void *filename)
} }
static Uint16 static Uint16
write_entry(char *p, Uint16 len, const char *filename, struct stat *st) get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero)
{ {
if(len < strlen(filename) + 7) return 0; struct stat st;
if(len < strlen(basename) + 7) return 0;
memcpy(p, "???? ", 5); memcpy(p, "???? ", 5);
strcpy(p + 5, filename); strcpy(p + 5, basename);
strcat(p, "\n"); strcat(p, "\n");
if(S_ISDIR(st->st_mode)) { if(stat(pathname, &st))
return fail_nonzero ? strlen(p) : 0;
if(S_ISDIR(st.st_mode)) {
memcpy(p, "---- ", 5); memcpy(p, "---- ", 5);
} else if(st->st_size < 0x10000) { } else if(st.st_size < 0x10000) {
p[0] = hex[(st->st_size >> 12) & 0xf]; p[0] = hex[(st.st_size >> 12) & 0xf];
p[1] = hex[(st->st_size >> 8) & 0xf]; p[1] = hex[(st.st_size >> 8) & 0xf];
p[2] = hex[(st->st_size >> 4) & 0xf]; p[2] = hex[(st.st_size >> 4) & 0xf];
p[3] = hex[(st->st_size >> 0) & 0xf]; p[3] = hex[(st.st_size >> 0) & 0xf];
} }
return strlen(p); return strlen(p);
} }
@ -87,16 +91,17 @@ file_read(void *dest, Uint16 len)
if(state == FILE_READ) if(state == FILE_READ)
return fread(dest, 1, len, f); return fread(dest, 1, len, f);
if(state == DIR_READ) { if(state == DIR_READ) {
static char pathname[PATH_MAX];
char *p = dest; char *p = dest;
if(de == NULL) de = readdir(d); if(de == NULL) de = readdir(d);
for(; de != NULL; de = readdir(d)) { for(; de != NULL; de = readdir(d)) {
struct stat st;
Uint16 n; Uint16 n;
if(de->d_name[0] == '.' && de->d_name[1] == '\0') if(de->d_name[0] == '.' && de->d_name[1] == '\0')
continue; continue;
if(fstatat(dir_fd, de->d_name, &st, 0)) strncpy(pathname, current_filename, sizeof(pathname) - 1);
continue; strncat(pathname, "/", sizeof(pathname) - 1);
n = write_entry(p, len, de->d_name, &st); strncat(pathname, de->d_name, sizeof(pathname) - 1);
n = get_entry(p, len, pathname, de->d_name, 1);
if(!n) break; if(!n) break;
p += n; p += n;
len -= n; len -= n;
@ -125,10 +130,7 @@ file_write(void *src, Uint16 len, Uint8 flags)
Uint16 Uint16
file_stat(void *dest, Uint16 len) file_stat(void *dest, Uint16 len)
{ {
struct stat st; return get_entry(dest, len, current_filename, current_filename, 0);
if(stat(current_filename, &st))
return 0;
return write_entry((char *)dest, len, current_filename, &st);
} }
Uint16 Uint16