From c711d6371a4eb068e120b0aeca635be153edaa22 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Tue, 3 Jan 2023 17:04:42 -0600 Subject: [PATCH] version 0.2.1 --- Makefile | 21 ++++++++++----------- README | 15 +++++++++------ actions.c | 14 +++++++------- database.c | 4 ++-- function.c | 27 +++++++++++++++------------ odot.c | 8 ++++++-- odot.h | 5 +++-- 7 files changed, 52 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index f50d060..07cc474 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SHELL = /bin/bash PREFIX = /usr DESTDIR = $(PREFIX)/bin -MANDIR = $(PREFIX)/share/man +MANDIR = $(PREFIX)/share/man/man1 PROG = odot @@ -11,36 +11,35 @@ HEADER = $(PROG).h sqlite3.h OBJECTS = $(PROG).o database.o actions.o function.o sqlite3.o LDFLAGS = -L . LDLIBS = -lpthread -CFLAGS = -O2 -WARNINGS = -Werror -Wall -Wextra -Wpedantic -Wconversion -Wformat=2 -Wformat-signedness -Wstrict-prototypes -Wshadow -Wno-unused +CFLAGS = -O2 -v +WARNINGS = -Werror -Wall -Wextra -Wpedantic -Wno-unused CPPFLAGS = -I . ALL_CFLAGS = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LDLIBS) $(WARNINGS) sql: sqlite3.c - $(CC) sqlite3.c $(CFLAGS) -c + $(CC) sqlite3.c $(ALL_CFLAGS) -c header: $(HEADER) - $(CC) $(HEADER) $(CFLAGS) -c + $(CC) $(HEADER) $(ALL_CFLAGS) -c compile: $(CFILE) - $(CC) $(CFILE) $(CFLAGS) -c + $(CC) $(CFILE) $(ALL_CFLAGS) -c link: $(OBJECTS) - $(CC) $(OBJECTS) $(CFLAGS) -o $(PROG) + $(CC) $(OBJECTS) $(ALL_CFLAGS) -o $(PROG) build: $(CFILE) $(HEADER) sqlite3.c - $(CC) $(CFILE) sqlite3.c $(CFLAGS) -o $(PROG) + $(CC) $(CFILE) sqlite3.c $(ALL_CFLAGS) -o $(PROG) install: $(CFILE) $(HEADER) sqlite3.c - $(CC) $(CFILE) sqlite3.c $(CFLAGS) -o $(PROG) + $(CC) $(CFILE) sqlite3.c $(ALL_CFLAGS) -o $(PROG) install -CDTm 755 $(PROG) $(DESTDIR)/$(PROG) - install -CDTm 644 $(PROG).1 $(MANDIR)/$(PROG).1 + install -CDTm 644 $(PROG).1.gz $(MANDIR)/$(PROG).1 debug: $(CFILE) $(CC) $(CFILE) sqlite3.o $(CFLAGS) -ggdb3 -Og -o $(PROG) clean: - rm *.gch *.o [[ -f $(PROG) ]] && rm $(PROG) [[ -f $(DESTDIR)/$(PROG) ]] && sudo rm $(DESTDIR)/$(PROG) diff --git a/README b/README index 6534ff7..df8098a 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -odot +odot v0.2.1 Task manager written in C @@ -12,16 +12,16 @@ Installation: $ mkdir odot && cd odot # Download source files - $ curl "https://download.huck.website/odot-0.2.0.tar.gz" > odot-0.2.0.tar.gz + $ curl "https://download.huck.website/odot-[VERSION].tar.gz" > odot-[VERSION].tar.gz # Optional: Download and check pgp signature - $ curl "https://download.huck.website/odot-0.2.0.tar.gz.sig" > odot-0.2.0.tar.gz.sig + $ curl "https://download.huck.website/odot-[VERSION].tar.gz.sig" > odot-[VERSION].tar.gz.sig $ curl "https://download.huck.website/pub.asc" > huck.asc $ gpg --import huck.asc - $ gpg --verify odot-0.2.0.tar.gz.sig odot-0.2.0.tar.gz + $ gpg --verify odot-[VERSION].tar.gz.sig odot-[VERSION].tar.gz # Unpack source files - $ tar -xzvf odot-0.2.0.tar.gz + $ tar -xzvf odot-[VERSION].tar.gz # Build odot Global install: @@ -40,12 +40,15 @@ Usage: new Adds a new task to the list done Marks a task on the list as complete show Shows current tasks in database + update Change group for a task remove Remove task from database Available options: - -g (group) Specify group for task + -g [group] Specify group for task -a Show all groups -d Also show completed tasks + -q Don't show tasks + -V Display version Examples: diff --git a/actions.c b/actions.c index 2ee8431..2fc6254 100644 --- a/actions.c +++ b/actions.c @@ -4,7 +4,7 @@ extern char *task, *group, *newgroup; -extern u_long hash; +extern int hash; extern int exists, showall, @@ -14,7 +14,7 @@ void newtask(sqlite3 *db){ char *cmd = malloc(MAXLINE*sizeof(char)); if (exists == 0){ - sprintf(cmd,"%s (%lu, '%s', '%s', 0);",INSERT,hash,task,group); + sprintf(cmd,"%s (%i, '%s', '%s', 0);",INSERT,hash,task,group); sqlcmd(db,cmd,'q'); } else { printf("Task already exists\nMark as done? [y/(n)] "); @@ -28,14 +28,14 @@ void done(sqlite3 *db){ char *cmd = malloc(MAXLINE*sizeof(char)); if (exists == 1) { - sprintf(cmd,"%s %lu;",DONE,hash); + sprintf(cmd,"%s %i;",DONE,hash); sqlcmd(db,cmd,'q'); - sprintf(cmd,"%s %lu;", GETTASK,hash); + sprintf(cmd,"%s %i;", GETTASK,hash); sqlcmd(db,cmd,'t'); printf("\n"); - sprintf(cmd,"SELECT Done, Task FROM Tasks WHERE Hash = %lu;",hash); + sprintf(cmd,"SELECT Done, Task FROM Tasks WHERE Hash = %i;",hash); sqlcmd(db,cmd,'p'); } else { printf("Task does not exist\nAdd task [y/n] "); @@ -52,7 +52,7 @@ void update(sqlite3 *db){ char *cmd = malloc(MAXLINE*sizeof(char)); if (exists == 1) { - sprintf(cmd,"%s '%s' WHERE Hash = %lu;",CHANGEGROUP,newgroup,hash); + sprintf(cmd,"%s '%s' WHERE Hash = %i;",CHANGEGROUP,newgroup,hash); sqlcmd(db,cmd,'q'); strcpy(group,newgroup); } else { @@ -67,7 +67,7 @@ void removetask(sqlite3 *db){ char *cmd = malloc(MAXLINE*sizeof(char)); if (exists == 1){ - sprintf(cmd,"%s %lu;",DELETE,hash); + sprintf(cmd,"%s %i;",DELETE,hash); sqlcmd(db,cmd,'c'); } free(cmd); diff --git a/database.c b/database.c index a2f737f..31d4d96 100644 --- a/database.c +++ b/database.c @@ -4,7 +4,7 @@ extern char *group, *newgroup, *task; -extern u_long hash; +extern int hash; extern int exists; void sqlcmd(sqlite3 *db, char *cmd, char action){ @@ -61,7 +61,7 @@ int taskcallback(void *unused,int argc, char **argv, char **name){ int checkcallback(void *unused,int argc, char **argv, char **name){ if (exists == 1) return 0; - if (hash == strtoul(argv[2],NULL,10)) { + if (hash == (int) strtoul(argv[2],NULL,10)) { exists = 1; } else if (strcmp(task,argv[0]) == 0) { checksame(task,argv[1]); diff --git a/function.c b/function.c index e2635bd..0421ebb 100644 --- a/function.c +++ b/function.c @@ -5,20 +5,19 @@ extern char *group, *task, *action; -extern u_long hash; +extern int hash; extern int exists; -u_long genhash(void){ - char *tmp = calloc((strlen(task)+strlen(group)+1), sizeof(char)); - strcat(tmp,task); - strcat(tmp,group); - int h = 11235813; - - for (int i = 0; i < strlen(tmp);i++) - h = ~((h << 5) ^ tmp[i]); - - return h; +/* dgb hash */ +int genhash(char *str) { + u_int h=5381; + + for (int i = 0; i < (int) strlen(str); i++) { + h = ((h << 5) + h) + *str++; + } + + return h % TABLE_SIZE; } char *filepath(void){ @@ -54,7 +53,11 @@ void checksame(char *task,char *oldgroup){ if (getchar() == 'y'){ exists = 1; strcpy(group,oldgroup); - hash = genhash(); + char *hashstring = malloc((strlen(task)+strlen(group)+1)*sizeof(char)); + strcpy(hashstring, task); + strcat(hashstring,group); + hash = genhash(hashstring); + free(hashstring); } return; diff --git a/odot.c b/odot.c index 406121b..f95fedb 100644 --- a/odot.c +++ b/odot.c @@ -6,7 +6,7 @@ int showdone = 0, quiet = 0, exists = 0; -u_long hash; +int hash; char *group, *newgroup, @@ -36,7 +36,11 @@ int main(int argc, char *argv[]){ newgroup = calloc(MAXLINE,sizeof(char)); parseopt(argc,argv); - hash = genhash(); + char *hashstring = malloc((strlen(task)+strlen(group)+1)*sizeof(char)); + strcpy(hashstring, task); + strcat(hashstring,group); + hash = genhash(hashstring); + free(hashstring); /* checks if task already exists in db */ dbcheck(db); diff --git a/odot.h b/odot.h index 397b0d0..99d46ab 100644 --- a/odot.h +++ b/odot.h @@ -11,9 +11,10 @@ #define VERSION "0.2.1" #define MAXLINE 10000 +#define TABLE_SIZE 65535 /* sql commands */ -#define BUILDTABLE "CREATE TABLE IF NOT EXISTS Tasks (Hash int NOT NULL PRIMARY KEY, Task varchar(10000) NOT NULL, Type varchar(32), Done int NOT NULL DEFAULT 0, Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, Duedate DATETIME);" +#define BUILDTABLE "CREATE TABLE IF NOT EXISTS Tasks (Hash int NOT NULL PRIMARY KEY, Task varchar(10000) NOT NULL, Type varchar(32), Done int NOT NULL DEFAULT 0);" #define INSERT "INSERT INTO Tasks VALUES" #define DELETE "DELETE FROM Tasks WHERE Hash =" @@ -43,7 +44,7 @@ void show(sqlite3 *); /* function.c */ char *filepath(void); -u_long genhash(void); +int genhash(char *); void checksame(char *, char *); /* functions for interfacing with sqlite database: database.c */ -- 2.44.2