diff options
author | Oskar <[email protected]> | 2023-10-26 15:59:28 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2023-10-26 15:59:28 +0200 |
commit | 45795ba6e39027982e215d1ea4c37ad67c987c04 (patch) | |
tree | 6b36b9d8fe5d0708df7096ef1a2f4e31cb0f7c75 |
First commit
-rw-r--r-- | 7ed.c | 38 | ||||
-rw-r--r-- | 7ed.h | 57 | ||||
-rw-r--r-- | LICENSE | 21 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | README | 0 | ||||
-rw-r--r-- | editmode.c | 0 | ||||
-rw-r--r-- | functions.c | 129 | ||||
-rw-r--r-- | startmode.c | 94 | ||||
-rw-r--r-- | writemode.c | 0 |
9 files changed, 344 insertions, 0 deletions
@@ -0,0 +1,38 @@ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include "7ed.h" + +#define USAGE "" +#define PROGRAM_NAME "7ed" + +int main (int argc, char *argv[]) { + +int opt; +int returnval; +while ((opt = getopt(argc, argv, "i:")) != -1) { + switch (opt) { + + case 'i': + + returnval = startmode(optarg); + if (returnval == 1) { + return EXIT_FAILURE; + } + break; + + default: + fprintf(stderr, "%s", USAGE); + return EXIT_FAILURE; + } +} + + if (argc == 1) { + fprintf(stderr, "%s: Please provide a file.\n%s", argv[0], USAGE); + return EXIT_FAILURE; + } + +return EXIT_SUCCESS; + +}
\ No newline at end of file @@ -0,0 +1,57 @@ +#include <stddef.h> + +#define BUF_SZ_256 256 +#define BUF_SZ_512 512 +#define BUF_SZ_1 1024 +#define BUF_SZ_2 2048 +#define BUF_SZ_4 4096 +#define BUF_SZ_8 8192 + +#ifndef COUNT_LINES_IN_FILE_H +#define COUNT_LINES_IN_FILE_H + +int COUNT_LINES_IN_FILE (char filename[], size_t *lines); + +#endif /* COUNT_LINES_IN_FILE_H */ + +#ifndef COUNT_LINES_IN_FILE_POSIX_H +#define COUNT_LINES_IN_FILE_POSIX_H + +int COUNT_LINES_IN_FILE_POSIX (char filename[], size_t *lines); + +#endif /* COUNT_LINES_IN_FILE_POSIX_H */ + +#ifndef CONFIRM_H +#define CONFIRM_H + +void CONFIRM(); + +#endif /* CONFIRM_H */ + +#ifndef CHOICE_H +#define CHOICE_H + +int CHOICE(); + +#endif /* CHOICE_H */ + +#ifndef SHUFFLE_H +#define SHUFFLE_H + +void shuffle(char arr[], int n); + +#endif /* SHUFFLE_H */ + +#ifndef STARTMODE_H +#define STARTMODE_H +int startmode(char filename[]); + +#endif /* STARTMODE_H */ + + +#ifndef PRINT_7ED_H +#define PRINT_7ED_H + +int print_7ed(); + +#endif /* PRINT_7ED_H */ @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 734j + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5e2326b --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: 7ed +7ed: + gcc -Wfatal-errors -Wall -Werror -Wextra -g 7ed.c functions.c startmode.c -o 7ed +clean: + rm -f 7ed
\ No newline at end of file diff --git a/editmode.c b/editmode.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/editmode.c diff --git a/functions.c b/functions.c new file mode 100644 index 0000000..f381dc8 --- /dev/null +++ b/functions.c @@ -0,0 +1,129 @@ +#include <stdio.h> +#include <termios.h> +#include <stdlib.h> +#include "7ed.h" +#include <time.h> + +void CONFIRM() { + struct termios old,new; + + tcgetattr(fileno(stdin),&old); // gets something? + tcgetattr(fileno(stdin),&new); // gets something else? + cfmakeraw(&new); // makes new terminal settings + tcsetattr(fileno(stdin),TCSANOW,&new); // sets those settings immediately (TCSANOW) to &new + fputs("Press any key to continue...",stdout); + fflush(NULL); + fgetc(stdin); + tcsetattr(fileno(stdin),TCSANOW,&old); // goes back to old settings + puts(""); // newline +} + +int CHOICE() { + int choice; + + do { + + fputs("[Y / N] ? ", stdout); + + choice = getchar(); + if (choice == '\n') { continue; } + + while ('\n' != getchar()); + + } while ( (choice != 'Y') && (choice != 'y') && (choice != 'N') && (choice != 'n') ); + + if ( (choice == 'Y') || (choice == 'y') ) + { + return 0; + } + + if ((choice == 'N') || (choice == 'n') ) + { + return 1; + } + + return EXIT_FAILURE; +} + +int COUNT_LINES_IN_FILE (char filename[], size_t *lines) { + + // Does not follow posix because this function accounts for if the last line does not end with a newline. + + size_t line_count = 0; // Counter starting at 0 + FILE *file; + file = fopen(filename,"rb"); // Open file + + if (file == NULL) { // Check if you can open file + fprintf(stderr, "Cannot open file.\n"); + return 1; + } + fseek(file, -1, SEEK_END); + int last = fgetc(file); + if (last != '\n') { // Checks if the file ends with a newline or not. If not then it adds 1 to the count to account for the last line + line_count++; + } + fseek(file, 0, SEEK_SET); + + char buffer[BUF_SZ_4]; // Creates buffer with size of BUF_SZ_4 + while (1) { + size_t bytes_read = fread(buffer, 1, BUF_SZ_4, file); // puts chars from file in to buffer and returns the amount of bytes. + for (size_t i = 0 ; i < bytes_read; i++) { + if (buffer[i] == '\n') { // Searches through the buffer for newlines. + line_count++; + } + } + if (feof(file)) { // If end of file is encountered then break + break; + } + } + fclose(file); + *lines = line_count; + return 0; +} + +int COUNT_LINES_IN_FILE_POSIX (char filename[], size_t *lines) { + + // Same function as before but posix + + size_t line_count = 0; // Counter starting at 0 + FILE *file; + file = fopen(filename,"rb"); // Open file + + if (file == NULL) { // Check if you can open file + fprintf(stderr, "Cannot open file.\n"); + return 1; + } + + char buffer[BUF_SZ_4]; // Creates buffer with size of BUF_SZ_4 + while (1) { + size_t bytes_read = fread(buffer, 1, BUF_SZ_4, file); // puts chars from file in to buffer and returns the amount of bytes. + for (size_t i = 0 ; i < bytes_read; i++) { + if (buffer[i] == '\n') { // Searches through the buffer for newlines. + line_count++; + } + } + if (feof(file)) { // If end of file is encountered then break + break; + } + } + fclose(file); + *lines = line_count; + return 0; +} + +void shuffle(char arr[], int n) { + for (int i = n -1 ; i > 0 ; i--) { + int j = rand() % (i + 1); + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +int print_7ed() { + + printf("print_7ed()\n"); + + return 0; + +}
\ No newline at end of file diff --git a/startmode.c b/startmode.c new file mode 100644 index 0000000..7c1e5fd --- /dev/null +++ b/startmode.c @@ -0,0 +1,94 @@ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include "7ed.h" + +int startmode(char filename[]) { + + size_t Flines; + int returnval = COUNT_LINES_IN_FILE(filename, &Flines); + if (returnval == 1) { + return EXIT_FAILURE; + } + fprintf(stdout,"%s %ld lines\n", filename, Flines); + + long focus = 1; + + while(1) { + + fprintf(stdout, "(%ld): ", focus); + char command = getchar(); + if (command == '\n') { + continue; + } + while ('\n' != getchar()); + + switch (command) { + case 'L': + case 'l': + long Lfocus; + char buf[1024]; + int success; + + do { + fprintf(stdout, "(L): "); + if (!fgets(buf, 1024, stdin)) { + fprintf(stderr, "Too many characters\n"); + break; + } + char *endptr; + + Lfocus = strtol(buf, &endptr, 10); + errno = 0; + if (errno == ERANGE) { + printf("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 > (long)Flines) { + fprintf(stdout, "L is either less than 1 or more than %ld\n", Flines); + } else { + focus = Lfocus; + } + + break; + case 'P': + case 'p': + print_7ed(); + + break; + case 'E': + case 'e': + printf("EDIT MODE\n"); + break; + case 'Q': + case 'q': + return EXIT_SUCCESS; + break; + default: + printf("?\n"); + } + + + + } + + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/writemode.c b/writemode.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/writemode.c |