Browse code

Drop fstatat for building on MSYS2

Andrew Alderwick authored on 07/11/2021 00:45:55
Showing 1 changed files
... ...
@@ -20,6 +20,7 @@ WITH REGARD TO THIS SOFTWARE.
20 20
 #include <string.h>
21 21
 #include <sys/stat.h>
22 22
 #include <unistd.h>
23
+#include <limits.h>
23 24
 
24 25
 static FILE *f;
25 26
 static DIR *d;
... ...
@@ -56,19 +57,22 @@ file_prepare(void *filename)
56 57
 }
57 58
 
58 59
 static Uint16
59
-write_entry(char *p, Uint16 len, const char *filename, struct stat *st)
60
+get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero)
60 61
 {
61
-	if(len < strlen(filename) + 7) return 0;
62
+	struct stat st;
63
+	if(len < strlen(basename) + 7) return 0;
62 64
 	memcpy(p, "???? ", 5);
63
-	strcpy(p + 5, filename);
65
+	strcpy(p + 5, basename);
64 66
 	strcat(p, "\n");
65
-	if(S_ISDIR(st->st_mode)) {
67
+	if(stat(pathname, &st))
68
+		return fail_nonzero ? strlen(p) : 0;
69
+	if(S_ISDIR(st.st_mode)) {
66 70
 		memcpy(p, "---- ", 5);
67
-	} else if(st->st_size < 0x10000) {
68
-		p[0] = hex[(st->st_size >> 12) & 0xf];
69
-		p[1] = hex[(st->st_size >> 8) & 0xf];
70
-		p[2] = hex[(st->st_size >> 4) & 0xf];
71
-		p[3] = hex[(st->st_size >> 0) & 0xf];
71
+	} else if(st.st_size < 0x10000) {
72
+		p[0] = hex[(st.st_size >> 12) & 0xf];
73
+		p[1] = hex[(st.st_size >> 8) & 0xf];
74
+		p[2] = hex[(st.st_size >> 4) & 0xf];
75
+		p[3] = hex[(st.st_size >> 0) & 0xf];
72 76
 	}
73 77
 	return strlen(p);
74 78
 }
... ...
@@ -87,16 +91,17 @@ file_read(void *dest, Uint16 len)
87 91
 	if(state == FILE_READ)
88 92
 		return fread(dest, 1, len, f);
89 93
 	if(state == DIR_READ) {
94
+		static char pathname[PATH_MAX];
90 95
 		char *p = dest;
91 96
 		if(de == NULL) de = readdir(d);
92 97
 		for(; de != NULL; de = readdir(d)) {
93
-			struct stat st;
94 98
 			Uint16 n;
95 99
 			if(de->d_name[0] == '.' && de->d_name[1] == '\0')
96 100
 				continue;
97
-			if(fstatat(dir_fd, de->d_name, &st, 0))
98
-				continue;
99
-			n = write_entry(p, len, de->d_name, &st);
101
+			strncpy(pathname, current_filename, sizeof(pathname) - 1);
102
+			strncat(pathname, "/", sizeof(pathname) - 1);
103
+			strncat(pathname, de->d_name, sizeof(pathname) - 1);
104
+			n = get_entry(p, len, pathname, de->d_name, 1);
100 105
 			if(!n) break;
101 106
 			p += n;
102 107
 			len -= n;
... ...
@@ -125,10 +130,7 @@ file_write(void *src, Uint16 len, Uint8 flags)
125 130
 Uint16
126 131
 file_stat(void *dest, Uint16 len)
127 132
 {
128
-	struct stat st;
129
-	if(stat(current_filename, &st))
130
-		return 0;
131
-	return write_entry((char *)dest, len, current_filename, &st);
133
+	return get_entry(dest, len, current_filename, current_filename, 0);
132 134
 }
133 135
 
134 136
 Uint16