diff --git a/src/Makefile b/src/Makefile index d8b836f..be7b47e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -21,11 +21,6 @@ main.o: main.c all: gcc -Wall *.c -.PHONY:debug -debug: - gcc -Wall -g *.c - gdb a.out - .PHONY:run run: gcc -Wall *.c diff --git a/src/insert_event.c b/src/insert_event.c index 75bbace..03ce9ea 100644 --- a/src/insert_event.c +++ b/src/insert_event.c @@ -6,7 +6,7 @@ #include void insert_event(char *file_name) { - int myfd = open(file_name, O_APPEND | O_WRONLY); + int myfd = open(file_name, O_RDWR); char summary_buf[256] = "SUMMARY:"; @@ -24,10 +24,34 @@ void insert_event(char *file_name) { summary_buf[strlen(summary_buf)-1] = '\r'; summary_buf[strlen(summary_buf)] = '\n'; + seek_cal_end(myfd); write(myfd, "BEGIN:VEVENT\r\n", strlen("BEGIN:VEVENT\r\n")); write(myfd, summary_buf, strlen(summary_buf)); + write(myfd, "END:VCALENDAR\r\n", strlen("END:VCALENDAR\r\n")); close(myfd); exit(0); } + +void seek_cal_end(int fd) { + char search_string[] = "END:VCALENDAR"; + int j = 0; + char char_reader = '\0'; + + lseek(fd, -1, SEEK_END); + + while(read(fd, &char_reader, 1)) { + // no need to compare to the null terminator of the search_string + if (char_reader == search_string[strlen(search_string)-j-1]) { + j++; + } else { + j = 0; + } + if (j == (strlen(search_string))) { + lseek(fd, -1, SEEK_CUR); + break; + } + lseek(fd, -2, SEEK_CUR); + } +} diff --git a/src/insert_event.h b/src/insert_event.h index 563dec0..4719351 100644 --- a/src/insert_event.h +++ b/src/insert_event.h @@ -1,3 +1,4 @@ #pragma once void insert_event(char *file_name); +void seek_cal_end(int fd);