diff options
author | Oskar <[email protected]> | 2023-11-07 23:12:31 +0100 |
---|---|---|
committer | Oskar <[email protected]> | 2023-11-07 23:12:31 +0100 |
commit | e2965f7ea63a5426e018c4e8c7f557fcce4c69c0 (patch) | |
tree | c6c351a729b64a8a0112de6f1e3849746f629695 /startmode.c | |
parent | c09957c3026aa7f25d7bc7a0379d961990bc9e34 (diff) |
Added edit mode and function to write the changes to file. Write function is not completely done yet because the temp file created by it is not removed when it is done. But other than that the editor actually works now. There are still some quirks that i need to sort out, especially the fact that you cant actually create a new line beyond the amount in the file you are editing
Diffstat (limited to 'startmode.c')
-rw-r--r-- | startmode.c | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/startmode.c b/startmode.c index 9744b0b..a6f1395 100644 --- a/startmode.c +++ b/startmode.c @@ -5,6 +5,25 @@ #include <errno.h> #include "7ed.h" +int NCAT(char filename[]) { + + FILE *file; + file = fopen(filename,"r"); + + if (file == NULL) { + fprintf(stderr, "Cannot open file.\n"); + return 1; + } + + char buffer[BUF_SZ_2]; + size_t bytes_read; + while ((bytes_read = fread(buffer, 1, BUF_SZ_2, file)) > 0) { + fwrite(buffer, 1, bytes_read, stdout); + } + fclose(file); + return 0; +} + int startmode(char filename[]) { size_t Flines; @@ -43,7 +62,7 @@ int startmode(char filename[]) { Lfocus = strtol(buf, &endptr, 10); errno = 0; if (errno == ERANGE) { - printf("Sorry, this number is too small or too large.\n"); + fprintf(stderr, "Sorry, this number is too small or too large.\n"); success = 0; } else if (endptr == buf) { @@ -63,7 +82,7 @@ int startmode(char filename[]) { } while (!success); if (Lfocus < 1 || Lfocus > (long)Flines) { - fprintf(stdout, "L is either less than 1 or more than %ld\n", Flines); + fprintf(stderr, "L is either less than 1 or more than %ld\n", Flines); } else { focus = Lfocus; } @@ -78,32 +97,38 @@ int startmode(char filename[]) { if (ret == 1) { return EXIT_FAILURE; } - printf("%s", line); + fprintf(stdout, "%s", line); //printf("%ld", start); free(line); break; case 'E': case 'e': - printf("EDIT MODE\n"); + + editmode(filename, focus); + break; case 'C': case 'c': - size_t Flines; - int returnval = COUNT_LINES_IN_FILE(filename, &Flines); + size_t CFlines; + int returnval = COUNT_LINES_IN_FILE(filename, &CFlines); if (returnval == 1) { return EXIT_FAILURE; } - fprintf(stdout,"%s %ld lines\n", filename, Flines); + fprintf(stdout,"%s %ld lines\n", filename, CFlines); break; case 'Q': case 'q': return EXIT_SUCCESS; break; + case 'a': + case 'A': + NCAT(filename); + break; default: - printf("?\n"); + fprintf(stdout, "?\n"); } |