summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--7ed.c19
-rw-r--r--Makefile2
-rw-r--r--editmode.c198
-rw-r--r--functions.c6
-rw-r--r--startmode.c4
-rw-r--r--test/endnewlinetest1
-rw-r--r--test/noline1
7 files changed, 133 insertions, 98 deletions
diff --git a/7ed.c b/7ed.c
index a2dd4e1..25f254f 100644
--- a/7ed.c
+++ b/7ed.c
@@ -10,30 +10,29 @@
int main (int argc, char *argv[]) {
-int opt;
-int returnval;
-while ((opt = getopt(argc, argv, "i:")) != -1) {
+ int opt;
+ int returnval;
+ while ((opt = getopt(argc, argv, "i:")) != -1) {
switch (opt) {
-
- case 'i':
+ case 'i':
returnval = startmode(optarg);
if (returnval == 1) {
return EXIT_FAILURE;
}
- break;
+ 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_FAILURE;
}
-return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
} \ No newline at end of file
diff --git a/Makefile b/Makefile
index 01eaee4..216c2f7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
all: 7ed
7ed:
- gcc -Wfatal-errors -Wall -Werror -Wextra -g 7ed.c functions.c startmode.c editmode.c -o 7ed
+ gcc -Wfatal-errors -Wall -Werror -Wextra -g -O2 7ed.c functions.c startmode.c editmode.c -o 7ed
clean:
rm -f 7ed
diff --git a/editmode.c b/editmode.c
index 24156dd..d89a0d5 100644
--- a/editmode.c
+++ b/editmode.c
@@ -8,77 +8,94 @@
int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer_size) {
-char *line;
-size_t start;
-int ret = GET_LINE(filename, focus, &line, &start); // get start position of focus
-if (ret == 1) {
- return EXIT_FAILURE;
-}
+ char *line;
+ size_t start;
+ int ret = GET_LINE(filename, focus, &line, &start); // get start position of focus
+ if (ret == 1) {
+ return EXIT_FAILURE;
+ }
-FILE *file;
-FILE *temp;
-file = fopen(filename,"r"); // Open file
-temp = fopen("temp.7ed","w+");
-if (file == NULL || temp == NULL) { // Check if you can open file
- fprintf(stderr, "Cannot open file. write_line\n");
- return 1;
-}
+ FILE *file;
+ FILE *temp;
+ file = fopen(filename,"r"); // Open file
+ temp = fopen("temp.7ed","w+");
+ if (file == NULL || temp == NULL) { // Check if you can open file
+ fprintf(stderr, "Cannot open file. write_line\n");
+ return 1;
+ }
-fseek(file, start, SEEK_SET);
-size_t count = 0;
-while (1) {
- char c = fgetc(file);
- if (c == '\n') {
- break; // count the length of the specific line to be replaced
- } else if (c == EOF) {
- break;
- } else {
- count++;
+ fseek(file, start, SEEK_SET);
+ size_t count = 0;
+ while (1) {
+ char c = fgetc(file);
+ if (c == '\n') {
+ break; // count the length of the specific line to be replaced
+ } else if (c == EOF) {
+ break;
+ } else {
+ count++;
+ }
}
-}
-fseek(file, 0, SEEK_SET); // go to start of file
-size_t counter = 0;
-char ch;
-for (;; counter++) { // for loop that puts contents of file in to temp
- // when counter is equal to start, the buffer with the edited content is written to temp
- if (start == counter) { // after that it uses fseek to point temp right after the written content
- fprintf(temp, "%s", editbuffer); // and fseek on file will use count variable to skip the content that is supposed to be replaced
- fseek(temp, start+editbuffer_size-1, SEEK_SET); // After that the function keeps writing everything else to temp
- fseek(file, start+count, SEEK_SET);
+ fseek(file, 0, SEEK_SET); // go to start of file
+ size_t counter = 0;
+ char ch;
+ for (;; counter++) { // for loop that puts contents of file in to temp
+ // when counter is equal to start, the buffer with the edited content is written to temp
+ if (start == counter) { // after that it uses fseek to point temp right after the written content
+ fprintf(temp, "%s", editbuffer); // and fseek on file will use count variable to skip the content that is supposed to be replaced
+ fseek(temp, start+editbuffer_size-1, SEEK_SET); // After that the function keeps writing everything else to temp
+ fseek(file, start+count, SEEK_SET);
+ }
+ ch = fgetc(file);
+ if (ch == EOF) {
+ break;
+ }
+ fputc(ch, temp);
+
+ }
+ fseek(temp, 0, SEEK_SET); // reset both files to start at 0 so it doesnt mess with fread and fwrite
+ fclose(file);
+ file = fopen(filename, "w");
+ if (file == NULL) { // Check if you can open file
+ fprintf(stderr, "Cannot open file for writing.\n");
+ return 1;
}
- ch = fgetc(file);
- if (ch == EOF) {
- break;
+ char buffer[BUF_SZ_2];
+ size_t bytes_read;
+ while ((bytes_read = fread(buffer, 1, BUF_SZ_2, temp)) > 0) { // Write contents of temp to file
+ fwrite(buffer, 1, bytes_read, file);
}
- fputc(ch, temp);
+ free(line);
+ fclose(file);
+ fclose(temp);
+ remove("temp.7ed");
+ return 0;
}
-fseek(temp, 0, SEEK_SET); // reset both files to start at 0 so it doesnt mess with fread and fwrite
-fclose(file);
-file = fopen(filename, "w");
-if (file == NULL) { // Check if you can open file
- fprintf(stderr, "Cannot open file for writing.\n");
- return 1;
-}
-char buffer[BUF_SZ_2];
-size_t bytes_read;
-while ((bytes_read = fread(buffer, 1, BUF_SZ_2, temp)) > 0) { // Write contents of temp to file
- fwrite(buffer, 1, bytes_read, file);
-}
-free(line);
-fclose(file);
-fclose(temp);
-remove("temp.7ed");
-return 0;
-}
+int check_end_newline(char filename[]) {
+ FILE *file;
+ file = fopen(filename, "r");
+ if (file == NULL) {
+ fprintf(stderr, "Cannot open file. check_end_newline()\n");
+ return 1;
+ }
+
+ fseek(file, -1, SEEK_END);
+ if (fgetc(file) != '\n') {
+ return -1; // returns -1 if there is not newline at the end
+ }
+ fclose(file);
+ return 0; // returns 0 if there is one
+
+}
int NEW_LINE(char filename[]) { // doin this test again
long new_line_pos; // The "focus" that the newline will be inserted afterwards
- long new_line_pos_temp; // temp
+ long new_line_pos_temp = 0; // temp
char buf[1024];
int success;
@@ -116,6 +133,23 @@ int NEW_LINE(char filename[]) { // doin this test again
} else {
new_line_pos = new_line_pos_temp;
+ int cen = check_end_newline(filename);
+ if (cen == -1) {
+ size_t linecount;
+ int clif = COUNT_LINES_IN_FILE(filename, &linecount);
+ if (clif == 1) {
+ return 1;
+ }
+ if (linecount == (size_t)new_line_pos) {
+ FILE *file;
+ file = fopen(filename, "a");
+ fputc('\n', file);
+ fclose(file);
+ return 0;
+ }
+
+ }
+ printf("continuing\n");
char *line; // line is the line before the newline
// line2 is the line right after line
size_t start; // start is the position at the start of line
@@ -141,39 +175,39 @@ int NEW_LINE(char filename[]) { // doin this test again
int editmode(char filename[], long focus) {
-char *line;
-size_t start;
-int ret = GET_LINE(filename, focus, &line, &start);
-if (ret == 1) {
- return EXIT_FAILURE;
-}
+ char *line;
+ size_t start;
+ int ret = GET_LINE(filename, focus, &line, &start);
+ if (ret == 1) {
+ return EXIT_FAILURE;
+ }
-fprintf(stdout, "%s", line); // print line to be edited
-free(line);
+ fprintf(stdout, "%s", line); // print line to be edited
+ free(line);
-while (1) {
- char editbuffer[BUF_SZ_2];
- fprintf(stdout, "(%ld EDIT): ", focus);
+ while (1) {
+ char editbuffer[BUF_SZ_2];
+ fprintf(stdout, "(%ld EDIT): ", focus);
- fgets(editbuffer, BUF_SZ_2, stdin);
+ fgets(editbuffer, BUF_SZ_2, stdin);
- if (editbuffer[0] == '\n') { continue; }
+ if (editbuffer[0] == '\n') { continue; }
- fprintf(stdout, "Do you want to write the changes?\n");
- int yesno = CHOICE();
+ fprintf(stdout, "Do you want to write the changes?\n");
+ int yesno = CHOICE();
- if (yesno == 1) {
- printf("N\n");
- return 0;
- }
-
- size_t editbuffer_size = strlen(editbuffer);
- write_line(filename, focus, editbuffer, editbuffer_size);
+ if (yesno == 1) {
+ printf("N\n");
+ return 0;
+ }
+
+ size_t editbuffer_size = strlen(editbuffer);
+ write_line(filename, focus, editbuffer, editbuffer_size);
- return 0;
+ return 0;
-}
+ }
-return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
} \ No newline at end of file
diff --git a/functions.c b/functions.c
index c114efd..a03195d 100644
--- a/functions.c
+++ b/functions.c
@@ -168,7 +168,7 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Makin
focus--;
size_t line_count = 0; // Counter starting at 0
- size_t save_i;
+ size_t save_i = 0;
for (size_t i = 0; ; i++) {
char c = fgetc(file);
if (feof(file)) { // If end of file is encountered then break
@@ -213,6 +213,6 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Makin
}
-fclose(file);
-return 0;
+ fclose(file);
+ return 0;
} \ No newline at end of file
diff --git a/startmode.c b/startmode.c
index 1ecaa94..d9779ba 100644
--- a/startmode.c
+++ b/startmode.c
@@ -48,7 +48,7 @@ int startmode(char filename[]) {
switch (command) {
case 'L':
case 'l':
- uint64_t Lfocus;
+ uint64_t Lfocus = 0;
char buf[1024];
int success;
@@ -130,7 +130,7 @@ int startmode(char filename[]) {
break;
case 'n':
case 'N':
-
+
NEW_LINE(filename);
break;
diff --git a/test/endnewlinetest b/test/endnewlinetest
new file mode 100644
index 0000000..4f02b0d
--- /dev/null
+++ b/test/endnewlinetest
@@ -0,0 +1 @@
+And thus, the new line bug was fixed... I think.
diff --git a/test/noline b/test/noline
new file mode 100644
index 0000000..08a1db1
--- /dev/null
+++ b/test/noline
@@ -0,0 +1 @@
+asdasdasdasdasd \ No newline at end of file