From 1d0870f8932d430fd61095654395a5a27bfa5249 Mon Sep 17 00:00:00 2001 From: Jonathan Santis Date: Mon, 16 Sep 2024 11:30:50 +0200 Subject: [PATCH] fix argument parsing change cmd line args add config path HOME/.config/systemd-gotify/config add makefile add build and install to makefile --- config.c | 6 +++--- main.c | 38 +++++++++++++++++++++----------------- makefile | 7 +++++++ 3 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 makefile diff --git a/config.c b/config.c index 372776b..7b83aae 100644 --- a/config.c +++ b/config.c @@ -3,14 +3,14 @@ #include #include -int loadConfig(char *file, char **str_entry,int size) +int loadConfig(char *str_file, char **str_entry,int size) { int i=0; FILE *f_config; char temp_str[256]; printf("\nopening config file\n"); - f_config = fopen("config","r"); + f_config = fopen(str_file,"r"); if(f_config != NULL) { for(i=0;i #include #include +#include #include"config.h" -#define HELP_TEXT "requires a command\n--\ngetSendStatus [objectpath]\nlistUnits\nconfig [configfile]\nset token with GOTIFY_TOKEN=\"sjdfoiasfd\"\n--\n" +#define HELP_TEXT "requires a command\n--\ngetSendStatus [objectpath]\nlistUnits\nconfig [configfile / -d]\nset token with GOTIFY_TOKEN=\"sjdfoiasfd\"\n--\n" typedef struct service { @@ -26,7 +27,7 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p int GetSendStatus(DBusConnection *connection,char *objectpath); int ServiceGetSendStatus(DBusConnection *connection,Service *service); int checkState(char *ServiceName,char *result,Service *service); - +int getPath(char *configPath,int size); /* *sytemctl show gotify-server | grep PID *sudo busctl tree org.freedesktop.systemd1 > get object paths @@ -40,7 +41,8 @@ int main(int argc,char *argv[]) DBusConnection *connection=NULL; DBusError error; clock_t time; - + + time = clock(); printf("time:%f\n",(float)time / CLOCKS_PER_SEC); @@ -82,7 +84,7 @@ int main(int argc,char *argv[]) } else if(strcmp(argv[1],"config")==0) { - if(argc==3) + if(argc==3 && strcmp(argv[2],"-d")!=0) { char *str_entry[64]={'\0'}; int i=0; @@ -94,15 +96,18 @@ int main(int argc,char *argv[]) GetSendStatus(connection,str_entry[i]); } } - else if(argc==4 && strcmp(argv[3],"-d")==0) + else if(argc==3 && strcmp(argv[2],"-d")==0) { printf("Running as a daemon\n"); char *str_entry[64]={'\0'}; int i=0; Service services[64]={'\0'}; char *ptr_name=NULL; + char path[256]; + getPath(path,256); + printf("[main]configPath:%s\n",path); - loadConfig(argv[2],str_entry,64); + loadConfig(path,str_entry,64); for(i=0;str_entry[i];i++) { services[i].targetPath = strdup(str_entry[i]); @@ -146,17 +151,16 @@ int main(int argc,char *argv[]) } return 0; } -/*interactive - char userinput[128]; - while(true) - { - scanf("%s",userinput); - printf("\nuserinput: %s",userinput); - systemdUnitCall(connection,userinput,"Get","SubState",&result); - checkState(userinput,result); - - } - */ +int getPath(char *configPath,int size) +{ + uid_t myid = getuid(); + printf("My uid:%d\n",myid); + struct passwd *info = getpwuid(myid); + printf("My dir:%s\n",info->pw_dir); + snprintf(configPath,size,"%s/.config/systemd-gotify/config",info->pw_dir); + printf("ConfigPath:%s\n",configPath); + return 0; +} int DBusOptions(DBusConnection *connection,DBusError **error) //not tested check pointer of error!! { int ret=0,i=0; diff --git a/makefile b/makefile new file mode 100644 index 0000000..3a58405 --- /dev/null +++ b/makefile @@ -0,0 +1,7 @@ +default: + gcc main.c config.c -lcurl -Wall -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -ldbus-1 -o systemd-gotify.bin +install: + cp ./systemd-gotify.bin /usr/local/bin/systemd-gotify + mkdir $${HOME}/.config/systemd-gotify/ + cp config $${HOME}/.config/systemd-gotify/config +