new branch for option to insert an event

This commit is contained in:
bjoernf 2023-09-02 13:24:12 +02:00
parent c713522736
commit 3cab08dbf3
6 changed files with 91 additions and 34 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
a.out
.gitconfig
*.o

View File

@ -1,3 +1,22 @@
CC = gcc
CFLAGS = -Wall
# List of all source files (assuming they're all in the same directory)
SRC_FILES = $(wildcard *.c)
# Generate a list of object files by replacing the .c extension with .o
OBJ_FILES = $(SRC_FILES:.c=.o)
ics_analyzer: $(OBJ_FILES)
gcc -Wall $(OBJ_FILES) -o ics_analyzer
main.o: main.c
$(CC) $(CFLAGS) -c $<
# use implicit rule to compile C source files to object files
%.o: %.c %.h
$(CC) $(CFLAGS) -c $<
.PHONY:all
all:
gcc -Wall *.c
@ -10,15 +29,15 @@ debug:
.PHONY:run
run:
gcc -Wall *.c
./a.out
./ics_analyzer
.PHONY:install
install: a.out
cp a.out /usr/local/bin/ics_analyzer
cp ics_analyzer /usr/local/bin/ics_analyzer
.PHONY:clean
clean:
-rm a.out
-rm ics_analyzer *.o
.PHONY:uninstall
uninstall:
@ -26,8 +45,8 @@ uninstall:
.PHONY:test
test:
./a.out
./ics_analyzer
@echo
./a.out -h
./ics_analyzer -h
@echo
./a.out -f ../tests/calendar.ics
./ics_analyzer -f ../tests/calendar.ics

View File

@ -1,3 +1,4 @@
#include "insert_event.h"
#include "cli_arg_parsing.h"
#include <stdio.h>
#include <stdlib.h>
@ -5,37 +6,37 @@
#include <string.h>
void usage() {
printf ("-h\tprint this help\n");
printf ("-f\tspecify ics file path\n");
exit(0);
printf ("-h\tprint this help\n");
printf ("-f\tspecify ics file path\n");
exit(0);
}
void get_cli_args(int argc, char **argv, char **file_name) {
int opt = 0;
int opt = 0;
memset(file_name, '\0', strlen(*file_name));
memset(file_name, '\0', strlen(*file_name));
if (argc < 2) {
char *home = getenv("HOME");
*file_name = home;
char *home = getenv("HOME");
*file_name = home;
if (home != NULL) {
strcat(*file_name, "/.local/share/evolution/calendar/system/calendar.ics");
} else {
printf ("Environment variable HOME is not set.\n");
exit(1);
}
return;
}
if (home != NULL) {
strcat(*file_name, "/.local/share/evolution/calendar/system/calendar.ics");
} else {
printf ("Environment variable HOME is not set.\n");
exit(1);
}
while ((opt = getopt(argc, argv, "f:h")) != -1) {
switch(opt) {
case 'f':
*file_name = optarg;
break;
case 'h':
usage();
}
}
while ((opt = getopt(argc, argv, "f:hi")) != -1) {
switch(opt) {
case 'f':
*file_name = optarg;
break;
case 'h':
usage();
break;
case 'i':
insert_event(*file_name);
break;
}
}
}

33
src/insert_event.c Normal file
View File

@ -0,0 +1,33 @@
#include "insert_event.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void insert_event(char *file_name) {
int myfd = open(file_name, O_APPEND | O_WRONLY);
char summary_buf[256] = "SUMMARY:";
char *input_buffer = &summary_buf[8];
printf("Insert a new event\n");
printf("SUMMARY: ");
fgets (input_buffer, (sizeof(summary_buf)-strlen(summary_buf)), stdin);
if (strchr(input_buffer, '\n') == NULL)
printf ("The input has been truncated to:\n%s\n", input_buffer);
// modify the string to have \r\n line ending
summary_buf[strlen(summary_buf)+1] = '\0';
summary_buf[strlen(summary_buf)-1] = '\r';
summary_buf[strlen(summary_buf)] = '\n';
write(myfd, "BEGIN:VEVENT\r\n", strlen("BEGIN:VEVENT\r\n"));
write(myfd, summary_buf, strlen(summary_buf));
close(myfd);
exit(0);
}

3
src/insert_event.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void insert_event(char *file_name);

View File

@ -14,8 +14,8 @@
#include <limits.h>
int main(int argc, char **argv) {
char *ics_path = "";
get_cli_args(argc, argv, &ics_path);
char *ics_path = "";
get_cli_args(argc, argv, &ics_path);
char my_line[4096] = "";