Convert directory reading to generic stream
This commit is contained in:
+18
-11
@@ -6,21 +6,28 @@
|
||||
#define PRINT_D(s) write(1, s, string_length(s));
|
||||
|
||||
exit_code_t ls(const char *path) {
|
||||
uint64_t i = 0;
|
||||
while (1) {
|
||||
char out[100];
|
||||
uint64_t s = readdir(path, i, 100, out);
|
||||
if (!s) {
|
||||
return EXIT_CODE_OK;
|
||||
}
|
||||
if (s == (uint64_t)-1) {
|
||||
PRINT_S("ls: path does not exist\n");
|
||||
uint64_t fd = open(path);
|
||||
|
||||
if (fd == (uint64_t)-1) {
|
||||
PRINT_S("ls: path does not exist\n");
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
|
||||
char buffer[100];
|
||||
uint64_t bytes;
|
||||
while ((bytes = read(fd, 100, buffer))) {
|
||||
if (bytes == (uint64_t)-1) {
|
||||
PRINT_S("ls: could not read directory\n");
|
||||
close(fd);
|
||||
return EXIT_CODE_GENERAL_FAILURE;
|
||||
}
|
||||
PRINT_D(out);
|
||||
write(1, buffer, bytes - 1);
|
||||
PRINT_S("\n");
|
||||
i++;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
exit_code_t main(uint64_t argc, const char **argv) {
|
||||
|
||||
Reference in New Issue
Block a user