dont append to the end but seek END:VCALENDAR

This commit is contained in:
bjoernf 2023-09-02 15:18:13 +02:00
parent 3cab08dbf3
commit 72dbf969f5
3 changed files with 26 additions and 6 deletions

View File

@ -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

View File

@ -6,7 +6,7 @@
#include <unistd.h>
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);
}
}

View File

@ -1,3 +1,4 @@
#pragma once
void insert_event(char *file_name);
void seek_cal_end(int fd);