From abf38fd2a065daa79ce392e7601008ab544dc59f Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Fri, 21 Oct 2022 17:26:44 -0500 Subject: [PATCH 1/1] creation --- Makefile | 22 ++++++++++++++++++ file.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ input.c | 47 ++++++++++++++++++++++++++++++++++++++ main.c | 45 ++++++++++++++++++++++++++++++++++++ odot.h | 14 ++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 Makefile create mode 100644 file.c create mode 100644 input.c create mode 100644 main.c create mode 100644 odot.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..687fbd5 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +SHELL = /bin/zsh +PROG = todo +PREFIX ?= /home/huck/.local/bin +TODOLIST = /home/huck/info/notes/todo + +install : main.c input.c file.c + gcc *.c -o '$(PREFIX)/$(PROG)' + +header : todo.h + gcc *.h + +clean : + rm *.gch + rm "$(PREFIX)/$(PROG)" + cp $(TODOLIST).md $(TODOLIST) + +test : + todo + todo -n something + todo -d stuff to do now + todo -ns stuff to do + todo -ds stuff to do diff --git a/file.c b/file.c new file mode 100644 index 0000000..09d621d --- /dev/null +++ b/file.c @@ -0,0 +1,69 @@ +#include "todo.h" + +#define TODOLIST "/home/huck/info/notes/todo" + +extern char *note; + +enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE}; + +FILE *fp; + +void add(void){ + fp = fopen(TODOLIST, "a"); + if (fp != NULL){ + fputs(strcat(note,"\n"),fp); + fclose(fp); + } +} + +void rem(void){ + char *s = malloc(MAXLINE * sizeof(char)); + FILE *tmp = fopen("temp", "w"); + + fp = fopen(TODOLIST, "r"); + + + while (fgets(s, MAXLINE, fp) != NULL){ + s[strlen(s) - 1] = 0; + if (strcmp(note, s) != 0){ + fputs(strcat(s,"\n"),tmp); + } + } + + fclose(fp); + fclose(tmp); + + remove (TODOLIST); + rename("temp", TODOLIST); + + free(s); +} + +void show(void){ + char *c; + + c = (char *) malloc(MAXLINE * sizeof(int)); + + fp = fopen(TODOLIST,"r"); + while (fgets(c, MAXLINE, fp) != NULL ) + printf("\t\t\033[1;35m*\033[0m %s", c); + fclose(fp); + free(c); +} + +int listcheck(void){ + char *s; + + fp = fopen(TODOLIST, "r"); + s = (char *) malloc(MAXLINE * sizeof(char)); + + while (fgets(s, MAXLINE, fp) != NULL){ + s[strlen(s) - 1] = 0; + if (strcmp(note, s) == 0){ + free(s); + return 1; + } + } + free(s); + return 0; +} diff --git a/input.c b/input.c new file mode 100644 index 0000000..786e28d --- /dev/null +++ b/input.c @@ -0,0 +1,47 @@ +#include "todo.h" + +extern char *note, *o; + +void getnote(int n, char *arg[]){ + char *s = malloc(MAXLINE * sizeof(char)); + + while(--n > 0){ + if ((*++arg)[0] != '-'){ + strcat(s, *arg); + strcat(s, (n > 1) ? " " : ""); + } + } + strcpy(note, s); + free(s); +} + +void getopt(int n, char *arg[]){ + char *c; + + if (n == 1){ + o = "s"; + } + + while (--n > 0 && (*++arg)[0] == '-'){ + if (strlen(*arg) <= 3){ + c = malloc(strlen(*arg) * sizeof(char)); + c = *arg; + strcat(o,(strchr(c, 'd')) ? "d" : "n"); + if (strchr(c,'s') != NULL){ + strcat(o,"s"); + } + free(c); + } else { + printf("\033[31mToo many options\033[0m: %s\n\tUse -h for help", *arg); + } + } + + if (strlen(o) == 0){ + if (listcheck() == 0) + o = "n"; + else + o = "d"; + } +} + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..ce634b2 --- /dev/null +++ b/main.c @@ -0,0 +1,45 @@ +#include "todo.h" + +char *note, *o; + +int main(int argc, char *argv[]){ + char op; + int length,i; + note = (char *) malloc(MAXLINE * sizeof(char)); + o = (char *) malloc (3 * sizeof(char)); + + getnote(argc, argv); + length = strlen(note)+1; + getopt(argc, argv); + + for (i = 0; i < strlen(o); i++) + switch (o[i]) { + case 'n': + if (listcheck() == 0){ + add(); + printf("\033[32mAdded to list\033[0m: %s\n", note); + } else { + printf("\033[33mAlready on list\033[0m: %s\nRemove from list? (y/\033[1mn\033[0m): ", note); + if (getchar() == 'y') + rem(); + } + break; + case 'd': + if (listcheck() == 1) { + rem(); + printf("\033[36mRemoved from list\033[0m: %s\n", note); + } else { + printf("\033[31mNot on list\033[0m: %s\nAdd to list? (y/\033[1mn\033[0m): ", note); + if (getchar() == 'y') + add(); + } + break; + case 's': + printf("\n\tTODO LIST:\n"); + show(); + break; + } + free(note); + return 0; +} + diff --git a/odot.h b/odot.h new file mode 100644 index 0000000..97a172c --- /dev/null +++ b/odot.h @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +#define MAXLINE 1000 + +void getnote(int, char *[]); +void getopt(int, char *[]); +void add(void); +void rem(void); +void show(void); +int listcheck(void); + -- 2.45.2