commit cda5d55
Devine Lu Linvega
·
2026-01-07 23:27:54 +0000 UTC
parent 437834c
Removed directory buffer
1 files changed,
+39,
-38
+39,
-38
1@@ -806,7 +806,6 @@ typedef struct {
2 } UxnFile;
3
4 static UxnFile ufs[2];
5-static Uint8 dirbuf[0x10000], *_dirbuf = dirbuf;
6 static unsigned int rL1, rL2;
7
8 static void
9@@ -825,9 +824,7 @@ make_pathfile(char *pathbuf, const char *filepath, const char *basename)
10 static unsigned int
11 put_fill(Uint8 *dest, unsigned int len, char c)
12 {
13- unsigned int i;
14- for(i = 0; i < len; i++)
15- *dest = c, dest++;
16+ memset(dest, c, len);
17 return len;
18 }
19
20@@ -879,28 +876,24 @@ put_statfile(Uint8 *dest, const char *filepath, const char *basename)
21 static unsigned int
22 put_fdir(Uint8 *dest, unsigned int len, const char *filepath, DIR *dir)
23 {
24- unsigned int i;
25- struct dirent *de = readdir(dir);
26- for(_dirbuf = dirbuf; de != NULL; de = readdir(dir)) {
27+ Uint8 *p = dest, *end = dest + len - 1;
28+ struct dirent *de;
29+ while((de = readdir(dir)) && p < end) {
30 const char *name = de->d_name;
31- if(name[0] == '.' && (name[1] == '.' || name[1] == '\0'))
32+ if(name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
33 continue;
34- else
35- _dirbuf += put_statfile(_dirbuf, filepath, name);
36+ p += put_statfile(p, filepath, name);
37 }
38- for(i = 0; i < len && dirbuf[i]; i++)
39- dest[i] = dirbuf[i];
40- dest[i] = 0;
41- return i;
42+ *p = 0;
43+ return p - dest;
44 }
45
46 static unsigned int
47-is_dir_path(char *p)
48+is_dir_path(const char *p)
49 {
50- char c;
51- unsigned int saw_slash = 0;
52- while((c = *p++)) saw_slash = c == '/';
53- return saw_slash;
54+ const char *end = p;
55+ while(*end) end++;
56+ return end > p && end[-1] == '/';
57 }
58
59 static unsigned int
60@@ -911,15 +904,17 @@ is_dir_real(char *p)
61 }
62
63 static unsigned int
64-file_write_dir(char *p)
65+file_write_dir(const char *p)
66 {
67+ char buf[0x1000];
68+ char *s = buf;
69 unsigned int ok = 1;
70- char c, *s = p;
71- for(; ok && (c = *p); p++) {
72- if(c == '/') {
73- *p = '\0';
74- ok = is_dir_real(s) || (mkdir(s, 0755) == 0);
75- *p = c;
76+ while(*p && ok) {
77+ *s++ = *p;
78+ if(*p++ == '/') {
79+ s[-1] = '\0';
80+ ok = is_dir_real(buf) || mkdir(buf, 0755) == 0;
81+ s[-1] = '/';
82 }
83 }
84 return ok;
85@@ -944,32 +939,38 @@ file_init(unsigned int id, Uint16 addr)
86 static unsigned int
87 file_not_ready(unsigned int id)
88 {
89- if(ufs[id].filepath == 0) {
90- fprintf(stderr, "File %d is uninitialized\n", id);
91- return 1;
92- } else
93- return 0;
94+ return ufs[id].filepath == 0;
95 }
96
97 static unsigned int
98 file_read(unsigned int id, Uint16 addr, unsigned int len)
99 {
100 void *dest = &ram[addr];
101+ struct stat st;
102+ unsigned int n;
103 if(file_not_ready(id))
104 return 0;
105 if(addr + len > 0x10000)
106 len = 0x10000 - addr;
107 if(ufs[id].state != FILE_READ && ufs[id].state != DIR_READ) {
108 file_reset(id);
109- if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL)
110- ufs[id].state = DIR_READ;
111- else if((ufs[id].f = fopen(ufs[id].filepath, "rb")) != NULL)
112+ if(stat(ufs[id].filepath, &st) == 0 && S_ISDIR(st.st_mode)) {
113+ if((ufs[id].dir = opendir(ufs[id].filepath)) != NULL)
114+ ufs[id].state = DIR_READ;
115+ } else if((ufs[id].f = fopen(ufs[id].filepath, "rb")) != NULL) {
116 ufs[id].state = FILE_READ;
117+ }
118+ }
119+ if(ufs[id].state == FILE_READ) {
120+ n = fread(dest, 1, len, ufs[id].f);
121+ if(n == 0) file_reset(id);
122+ return n;
123+ }
124+ if(ufs[id].state == DIR_READ) {
125+ n = put_fdir(dest, len, ufs[id].filepath, ufs[id].dir);
126+ if(n == 0) file_reset(id);
127+ return n;
128 }
129- if(ufs[id].state == FILE_READ)
130- return fread(dest, 1, len, ufs[id].f);
131- if(ufs[id].state == DIR_READ)
132- return put_fdir(dest, len, ufs[id].filepath, ufs[id].dir);
133 return 0;
134 }
135