insert_event #1

Merged
bjoernf merged 16 commits from insert_event into master 2023-09-09 08:26:50 +02:00
6 changed files with 91 additions and 34 deletions
Showing only changes of commit 3cab08dbf3 - Show all commits

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
a.out a.out
.gitconfig .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 .PHONY:all
all: all:
gcc -Wall *.c gcc -Wall *.c
@ -10,15 +29,15 @@ debug:
.PHONY:run .PHONY:run
run: run:
gcc -Wall *.c gcc -Wall *.c
./a.out ./ics_analyzer
.PHONY:install .PHONY:install
install: a.out install: a.out
cp a.out /usr/local/bin/ics_analyzer cp ics_analyzer /usr/local/bin/ics_analyzer
.PHONY:clean .PHONY:clean
clean: clean:
-rm a.out -rm ics_analyzer *.o
.PHONY:uninstall .PHONY:uninstall
uninstall: uninstall:
@ -26,8 +45,8 @@ uninstall:
.PHONY:test .PHONY:test
test: test:
./a.out ./ics_analyzer
@echo @echo
./a.out -h ./ics_analyzer -h
@echo @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 "cli_arg_parsing.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -5,37 +6,37 @@
#include <string.h> #include <string.h>
void usage() { void usage() {
printf ("-h\tprint this help\n"); printf ("-h\tprint this help\n");
printf ("-f\tspecify ics file path\n"); printf ("-f\tspecify ics file path\n");
exit(0); exit(0);
} }
void get_cli_args(int argc, char **argv, char **file_name) { 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");
char *home = getenv("HOME"); *file_name = home;
*file_name = home;
if (home != NULL) { if (home != NULL) {
strcat(*file_name, "/.local/share/evolution/calendar/system/calendar.ics"); strcat(*file_name, "/.local/share/evolution/calendar/system/calendar.ics");
} else { } else {
printf ("Environment variable HOME is not set.\n"); printf ("Environment variable HOME is not set.\n");
exit(1); exit(1);
} }
return;
}
while ((opt = getopt(argc, argv, "f:h")) != -1) { while ((opt = getopt(argc, argv, "f:hi")) != -1) {
switch(opt) { switch(opt) {
case 'f': case 'f':
*file_name = optarg; *file_name = optarg;
break; break;
case 'h': case 'h':
usage(); 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> #include <limits.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
char *ics_path = ""; char *ics_path = "";
get_cli_args(argc, argv, &ics_path); get_cli_args(argc, argv, &ics_path);
char my_line[4096] = ""; char my_line[4096] = "";