diff --git a/programs/edit.c b/programs/edit.c index f864cb1..4d26e5a 100644 --- a/programs/edit.c +++ b/programs/edit.c @@ -1,11 +1,16 @@ #include +#include +#include #include #include +#include #include #include #include +#include #define CTRL_KEY(k) ((k)&0x1f) +#define BUFFER_SIZE 4096 static struct termios _original_termios, _termios; @@ -30,19 +35,70 @@ static inline void enable_raw_mode() int main(int argc, char *argv[]) { + if (argc != 2) { + printf("edit: missing operand.\n"); + printf("Try 'edit --help' for more information.\n\n"); + return 1; + } + if (strcmp(argv[1], "--help") == 0) { + printf("Prints the content of the given file.\n"); + printf("Usage:\n"); + printf(" edit \n\n"); + return 0; + } + int fd = open(argv[1], O_RDONLY, 42); + if (fd < 0) { + printf("edit: %s: %s\n\n", argv[0], strerror(errno)); + return 1; + } + stat_t statbuf; + if (fstat(fd, &statbuf) == -1) { + printf("edit: %s: %s\n\n", argv[0], strerror(errno)); + // Close the file descriptor. + close(fd); + return 1; + } + if (S_ISDIR(statbuf.st_mode)) { + printf("edit: %s: %s\n\n", argv[0], strerror(EISDIR)); + // Close the file descriptor. + close(fd); + return 1; + } + // Put on the standard output the characters. + char buffer[BUFFER_SIZE]; + while (read(fd, buffer, BUFFER_SIZE) > 0) { + puts(buffer); + } + // Close the file descriptor. + close(fd); + enable_raw_mode(); int c; do { c = getchar(); - if (iscntrl(c)) { - printf("%d\n", c); + // Return Key + if (c == '\n') { + putchar('\n'); + } else if (c == '\033') { + c = getchar(); + if (c == '[') { + c = getchar(); + if (c == 'D') { + puts("\033[1D"); + } else if (c == 'C') { + puts("\033[1C"); + } else if (c == 'H') { + printf("\033[%dD", 0); + } else if (c == 'F') { + printf("\033[%dC", 60); + } + } } else { - printf("%d ('%c')\n", c, c); + putchar(c); } } while (c != 'q'); disable_raw_mode(); - return 0; } \ No newline at end of file