Update file_stat to match the existing documentation.

Previously, writing to the stat port would produce a line of text
similar to the listing used when reading a directory. This did not
match the documentation around the stat port.

After the change, writing an address to the stat port will fill that
address with bytes equal to the length parameter. These will either be
a hexadecimal file size, or else the same character repeated N times
according to the following logic:

 - path is a directory
 ? file size of path does not fit in N hexadecimal characters
 ! path is not readable

Note that unlike when reading a directory, the string will not include
the requested path, a newline, or a null terminator.
This commit is contained in:
~d6 2024-09-18 23:11:03 -04:00 committed by Devine Lu Linvega
parent 4ff6a5e862
commit 93125457a6
1 changed files with 29 additions and 6 deletions

View File

@ -267,16 +267,39 @@ file_write(UxnFile *c, void *src, Uint16 len, Uint8 flags)
return ret; return ret;
} }
static Uint16
stat_fill(Uint8 *dest, Uint16 len, char c)
{
Uint16 i;
for (i = 0; i < len; i++)
*(dest++) = c;
return len;
}
static Uint16
stat_size(Uint8 *dest, Uint16 len, off_t size)
{
Uint16 i;
dest += len - 1;
for (i = 0; i < len; i++) {
*(dest--) = '0' + (Uint8)(size & 0xf);
size = size >> 4;
}
return size == 0 ? len : stat_fill(dest, len, '?');
}
static Uint16 static Uint16
file_stat(UxnFile *c, void *dest, Uint16 len) file_stat(UxnFile *c, void *dest, Uint16 len)
{ {
char *basename = strrchr(c->current_filename, DIR_SEP_CHAR); struct stat st;
if(c->outside_sandbox) return 0; if(c->outside_sandbox)
if(basename != NULL) return 0;
basename++; else if(stat(c->current_filename, &st))
return stat_fill(dest, len, '!');
else if(S_ISDIR(st.st_mode))
return stat_fill(dest, len, '-');
else else
basename = c->current_filename; return stat_size(dest, len, st.st_size);
return get_entry(dest, len, c->current_filename, basename, 0);
} }
static Uint16 static Uint16