Browse code

Rewrote get_entry

Devine Lu Linvega authored on 07/11/2021 17:26:37
Showing 1 changed files
... ...
@@ -31,8 +31,6 @@ static enum { IDLE,
31 31
 	DIR_READ } state;
32 32
 static struct dirent *de;
33 33
 
34
-static char hex[] = "0123456789abcdef";
35
-
36 34
 static void
37 35
 reset(void)
38 36
 {
... ...
@@ -52,21 +50,16 @@ static Uint16
52 50
 get_entry(char *p, Uint16 len, const char *pathname, const char *basename, int fail_nonzero)
53 51
 {
54 52
 	struct stat st;
55
-	if(len < strlen(basename) + 7) return 0;
56
-	memcpy(p, "???? ", 5);
57
-	strcpy(p + 5, basename);
58
-	strcat(p, "\n");
53
+	if(len < strlen(basename) + 7)
54
+		return 0;
59 55
 	if(stat(pathname, &st))
60
-		return fail_nonzero ? strlen(p) : 0;
61
-	if(S_ISDIR(st.st_mode)) {
62
-		memcpy(p, "---- ", 5);
63
-	} else if(st.st_size < 0x10000) {
64
-		p[0] = hex[(st.st_size >> 12) & 0xf];
65
-		p[1] = hex[(st.st_size >> 8) & 0xf];
66
-		p[2] = hex[(st.st_size >> 4) & 0xf];
67
-		p[3] = hex[(st.st_size >> 0) & 0xf];
68
-	}
69
-	return strlen(p);
56
+		return fail_nonzero ? snprintf(p, len, "!!!! %s\n", basename) : 0;
57
+	else if(S_ISDIR(st.st_mode))
58
+		return snprintf(p, len, "---- %s\n", basename);
59
+	else if(st.st_size < 0x10000)
60
+		return snprintf(p, len, "%04x %s\n", (Uint16)st.st_size, basename);
61
+	else
62
+		return snprintf(p, len, "???? %s\n", basename);
70 63
 }
71 64
 
72 65
 static Uint16