#include #include #include #include #include #include "7ed.h" #include int NCAT(char filename[]) { FILE *file; file = fopen(filename,"r"); if (file == NULL) { fprintf(stderr, "Cannot open file. NCAT\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[]) { uint64_t Flines; int returnval = COUNT_LINES_IN_FILE(filename, &Flines); if (returnval == 1) { return EXIT_FAILURE; } fprintf(stdout,"%s %lu lines\n", filename, Flines); uint64_t focus = 1; while(1) { firstwhile: fprintf(stdout, "(%lu): ", focus); char command = getchar(); if (command == '\n') { continue; } while ('\n' != getchar()); switch (command) { case 'L': case 'l': uint64_t Lfocus = 0; char buf[1024]; int success; do { fprintf(stdout, "(L): "); if (!fgets(buf, 1024, stdin)) { fprintf(stderr, "Too many characters\n"); break; } if (buf[0] == '\n') { goto firstwhile; // start the first while loop aka startmode } char *endptr; Lfocus = strtol(buf, &endptr, 10); errno = 0; if (errno == ERANGE) { fprintf(stderr, "Sorry, this number is too small or too large.\n"); success = 0; } else if (endptr == buf) { // no character was read success = 0; } else if (*endptr && *endptr != '\n') { // *endptr is neither end of string nor newline, // so we didn't convert the *whole* input success = 0; } else { success = 1; } } while (!success); if (Lfocus < 1 || Lfocus > Flines) { fprintf(stderr, "L is either less than 1 or more than %lu\n", Flines); } else { focus = Lfocus; } break; case 'P': case 'p': char *line; size_t start; int ret = GET_LINE(filename, focus, &line, &start); if (ret == 1) { return EXIT_FAILURE; } fprintf(stdout, "%s", line); //printf("%ld", start); free(line); break; case 'E': case 'e': editmode(filename, focus); break; case 'C': case 'c': uint64_t CFlines; int returnval = COUNT_LINES_IN_FILE(filename, &CFlines); if (returnval == 1) { return EXIT_FAILURE; } fprintf(stdout,"%s %zu lines\n", filename, CFlines); break; case 'Q': case 'q': return EXIT_SUCCESS; break; case 'a': case 'A': NCAT(filename); break; case 'n': case 'N': { // The "focus" that the newline will be inserted afterwards uint64_t new_line_pos_temp = 0; // temp char buf[1024]; int success; do { fprintf(stdout, "Create a new line after: "); if (!fgets(buf, 1024, stdin)) { // take input from user fprintf(stderr, "Too many characters\n"); break; } char *endptr; new_line_pos_temp = strtol(buf, &endptr, 10); errno = 0; if (errno == ERANGE) { fprintf(stderr, "Sorry, this number is too small or too large.\n"); success = 0; } else if (endptr == buf) { // no character was read success = 0; } else if (*endptr && *endptr != '\n') { success = 0; } else { success = 1; } } while (!success); NEW_LINE(filename, new_line_pos_temp); break; } case 'X': case 'x': /* if (focus == Flines) { size_t Plines; int ret = COUNT_LINES_IN_FILE_POSIX(filename, &Plines); if (Flines != Plines) { NEW_LINE(filename); } } */ int choice = CHOICE(); if (choice == 0) { remove_line_contents(filename, focus); } break; case 'D': case 'd': fprintf(stdout, "delete\n"); break; default: fprintf(stdout, "?\n"); } } return EXIT_SUCCESS; }