Add file descriptors and basic cat
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include "src/user/syscall.h"
|
||||
|
||||
#define BLOCK_SIZE 65536
|
||||
|
||||
#define PRINT_S(s) write(1, s, sizeof(s) - 1);
|
||||
#define PRINT_D(s) write(1, s, string_length(s));
|
||||
|
||||
uint64_t cat(const char *path) {
|
||||
uint64_t fd = open(path);
|
||||
|
||||
if (fd == (uint64_t)-1) {
|
||||
PRINT_S("cat: path does not exist\n");
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
static char buffer[BLOCK_SIZE];
|
||||
uint64_t bytes;
|
||||
while ((bytes = read(fd, BLOCK_SIZE, buffer))) {
|
||||
if (bytes == (uint64_t)-1) {
|
||||
PRINT_S("cat: could not read file\n");
|
||||
close(fd);
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
write(1, buffer, bytes);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t main(uint64_t argc, const char **argv) {
|
||||
if (argc == 1) {
|
||||
return cat(".");
|
||||
}
|
||||
|
||||
uint64_t code = 0;
|
||||
for (uint64_t i = 1; i < argc; i++) {
|
||||
if (cat(argv[i]) == (uint64_t)-1) {
|
||||
code = (uint64_t)-1;
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
Reference in New Issue
Block a user