fix argument parsing

change cmd line args
add config path HOME/.config/systemd-gotify/config
add makefile
add build and install to makefile
This commit is contained in:
Jonathan Santis 2024-09-16 11:30:50 +02:00
parent 5ee37810ee
commit 1d0870f893
3 changed files with 31 additions and 20 deletions

View File

@ -3,14 +3,14 @@
#include<stdlib.h> #include<stdlib.h>
#include<string.h> #include<string.h>
int loadConfig(char *file, char **str_entry,int size) int loadConfig(char *str_file, char **str_entry,int size)
{ {
int i=0; int i=0;
FILE *f_config; FILE *f_config;
char temp_str[256]; char temp_str[256];
printf("\nopening config file\n"); printf("\nopening config file\n");
f_config = fopen("config","r"); f_config = fopen(str_file,"r");
if(f_config != NULL) if(f_config != NULL)
{ {
for(i=0;i<size;i++) for(i=0;i<size;i++)
@ -32,7 +32,7 @@ int loadConfig(char *file, char **str_entry,int size)
} }
else else
{ {
fprintf(stderr,"error opening file"); fprintf(stderr,"error opening file[%s]\n",str_file);
} }
return 0; return 0;
} }

38
main.c
View File

@ -6,10 +6,11 @@
#include<stdbool.h> #include<stdbool.h>
#include<time.h> #include<time.h>
#include<unistd.h> #include<unistd.h>
#include<pwd.h>
#include"config.h" #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 typedef struct service
{ {
@ -26,7 +27,7 @@ int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *p
int GetSendStatus(DBusConnection *connection,char *objectpath); int GetSendStatus(DBusConnection *connection,char *objectpath);
int ServiceGetSendStatus(DBusConnection *connection,Service *service); int ServiceGetSendStatus(DBusConnection *connection,Service *service);
int checkState(char *ServiceName,char *result,Service *service); int checkState(char *ServiceName,char *result,Service *service);
int getPath(char *configPath,int size);
/* /*
*sytemctl show gotify-server | grep PID *sytemctl show gotify-server | grep PID
*sudo busctl tree org.freedesktop.systemd1 > get object paths *sudo busctl tree org.freedesktop.systemd1 > get object paths
@ -40,7 +41,8 @@ int main(int argc,char *argv[])
DBusConnection *connection=NULL; DBusConnection *connection=NULL;
DBusError error; DBusError error;
clock_t time; clock_t time;
time = clock(); time = clock();
printf("time:%f\n",(float)time / CLOCKS_PER_SEC); 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) else if(strcmp(argv[1],"config")==0)
{ {
if(argc==3) if(argc==3 && strcmp(argv[2],"-d")!=0)
{ {
char *str_entry[64]={'\0'}; char *str_entry[64]={'\0'};
int i=0; int i=0;
@ -94,15 +96,18 @@ int main(int argc,char *argv[])
GetSendStatus(connection,str_entry[i]); 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"); printf("Running as a daemon\n");
char *str_entry[64]={'\0'}; char *str_entry[64]={'\0'};
int i=0; int i=0;
Service services[64]={'\0'}; Service services[64]={'\0'};
char *ptr_name=NULL; 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++) for(i=0;str_entry[i];i++)
{ {
services[i].targetPath = strdup(str_entry[i]); services[i].targetPath = strdup(str_entry[i]);
@ -146,17 +151,16 @@ int main(int argc,char *argv[])
} }
return 0; return 0;
} }
/*interactive int getPath(char *configPath,int size)
char userinput[128]; {
while(true) uid_t myid = getuid();
{ printf("My uid:%d\n",myid);
scanf("%s",userinput); struct passwd *info = getpwuid(myid);
printf("\nuserinput: %s",userinput); printf("My dir:%s\n",info->pw_dir);
systemdUnitCall(connection,userinput,"Get","SubState",&result); snprintf(configPath,size,"%s/.config/systemd-gotify/config",info->pw_dir);
checkState(userinput,result); printf("ConfigPath:%s\n",configPath);
return 0;
} }
*/
int DBusOptions(DBusConnection *connection,DBusError **error) //not tested check pointer of error!! int DBusOptions(DBusConnection *connection,DBusError **error) //not tested check pointer of error!!
{ {
int ret=0,i=0; int ret=0,i=0;

7
makefile Normal file
View File

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