void error(char type){
switch (type) {
case 'f':
+ fprintf(stderr,"ERROR: COULD NOT ACCESS FILE: %s\n", TODOLIST);
+ exit(1);
case 't':
- case ''
+ fprintf(stderr,"ERROR: COULD NOT GET CURRENT TIME\n");
+ exit(2);
+ case 'r':
+ fprintf(stderr,"ERROR: ARGUMENT OUT OF RANGE\n");
+ exit(3);
}
}
+
+void dialogue(char *m1, char *m2, int c){
+ printf("\033[1;3%im%s\033[0m: %s\n", c, m1, m2);
+ return;
+}
void add(void){
fp = fopen(TODOLIST, "a");
- if (fp == NULL){
- printf("ERROR: COULD NOT ACCESS FILE: %s\n",TODOLIST);
- return;
- }
+ if (fp == NULL)
+ error('f');
fputs(strcat(note,"\n"),fp);
fclose(fp);
}
FILE *tmp = fopen("temp", "w");
fp = fopen(TODOLIST, "r");
- if (fp == NULL){
- printf("ERROR: COULD NOT ACCESS FILE: %s\n",TODOLIST);
- return;
- }
+ if (fp == NULL)
+ error('f');
while (fgets(s, MAXLINE, fp) != NULL){
void show(void){
char *c = (char *) malloc(MAXLINE * sizeof(int));
- fp = fopen(TODOLIST,"r");
- if (fp == NULL){
- printf("ERROR: COULD NOT ACCESS FILE %s",TODOLIST);
- return;
- }
+ if (fp == NULL)
+ error('f');
while (fgets(c, MAXLINE, fp) != NULL )
printf("\t\t\033[1;3%im*\033[0m %s",geturgency(urgency), c);
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
-int listcheck(void){
- char *s = malloc(MAXLINE * sizeof(char));
+
+int listcheck(struct task t){
+ char *s = malloc(t.length* sizeof(char));
fp = fopen(TODOLIST, "r");
+ if (fp == NULL)
+ error('f');
- while (fgets(s, MAXLINE, fp) != NULL){
+ while (fgets(s, t.length, fp) != NULL){
s[strlen(s) - 1] = 0;
- if (strcmp(note, s) == 0){
+ if (strcmp(t.task, s) == 0){
free(s);
return 1;
}
else
return GREEN;
} else {
- printf("ERROR: urgency OUT OF RANGE");
- return -1;
+ error('r');
}
-
}
char *gettime(void){
- char *c = malloc(64*sizeof(char));
- time_t t = time(NULL);
+ struct tm *s;
+ time_t *t ;
+ char *c = malloc(16*sizeof(char));
+
+ *t = time(NULL);
if (t == ((t) - 1)){
error('t');
}
+
+ s = localtime(t);
- c = ctime(&t);
+ strftime(c, 16, "%y-%m-%d %H:%M", s);
if (c == NULL){
error('t');
}
return c;
+}
-
+int linecount(FILE *fp){
+ int i = 0;
+ char c;
+ while ((c = fgetc(fp)) != EOF){
+ if (c == '\n')
+ i++;
+ }
+ rewind(fp);
+ return i;
}
if (isdigit((int) *++arg)){
urgency = (int) *arg;
} else {
- printf("Non-integer argument for -i: %s\n", *arg);
+ error('r');
}
} else {
urgency = 5;
int main(int argc, char *argv[]){
char op;
int length,i;
+ struct task t;
note = (char *) malloc(MAXLINE * sizeof(char));
o = (char *) malloc (3 * sizeof(char));
- getnote(argc, argv);
- length = strlen(note)+1;
+ t = maketask(argc, argv);
getopt(argc, argv);
for (i = 0; i < strlen(o); i++)
switch (o[i]) {
case 'n':
- if (listcheck() == 0){
+ if (listcheck(t) == 0){
add();
printf("\033[32mAdded to list\033[0m: %s\n", note);
} else {
}
break;
case 'd':
- if (listcheck() == 1) {
+ if (listcheck(t) == 1) {
rem();
printf("\033[36mRemoved from list\033[0m: %s\n", note);
} else {
#define TIME "%H:%M %m-%d-%y"
#define TODOLIST "/home/huck/.local/state/odot/todo"
+struct task {
+ int length;
+ char *task;
+ char *date;
+ char *due;
+ int urgency;
+};
+
char *getnote(int, char *[]);
void getopt(int, char *[]);
+
void add(void);
void rem(void);
void show(void);
-int listcheck(void);
+
+int listcheck(struct task);
int geturgency(int);
+char *gettime(void);
void error(char);
+struct task maketask(int n, char **arg);
+void puttask(struct task n, FILE *fp);
+struct task gettask(FILE *fp);
+
char *note, *o;
int urgency;
FILE *fp;
extern int urgency;
extern FILE *fp;
-struct task {
- int length;
- char *task;
- int date;
- int due;
- int urgency;
-};
struct task maketask(int n, char **arg){
struct task temp;
temp.task = getnote(n,arg);
temp.length = strlen(temp.task);
- temp.date = 0;
+ temp.date = gettime();
temp.due = 0;
temp.urgency = urgency;
return temp;
}
-void puttask(struct task n){
- fp = fopen(TODOLIST, "w");
+void puttask(struct task n, FILE *fp){
- if (fp == NULL){
- error('f');
- }
-
- fprintf(fp, "%i\t%s\t%i\t%i\t%i\n",n.length,n.task,n.date,n.due,n.urgency);
+ fprintf(fp, "%i\t%s\t%s\t%s\t%i\n",n.length,n.task,n.date,n.due,n.urgency);
}
-struct task gettask(char *c){
+struct task gettask(FILE *fp){
struct task tmp;
- sscanf(c,"%i\t%s\t%i\t%i\t%i\n",&tmp.length,tmp.task,&tmp.date,&tmp.due,&tmp.urgency);
+ fscanf(fp,"%i\t%s\t%s\t%s\t%i\n",&tmp.length,tmp.task,tmp.date,tmp.due,&tmp.urgency);
return tmp;
}
void error(char type){
switch (type) {
case 'f':
+ fprintf(stderr,"ERROR: COULD NOT ACCESS FILE: %s\n", TODOLIST);
+ exit(1);
case 't':
- case ''
+ fprintf(stderr,"ERROR: COULD NOT GET CURRENT TIME\n");
+ exit(2);
+ case 'r':
+ fprintf(stderr,"ERROR: ARGUMENT OUT OF RANGE\n");
+ exit(3);
}
}
+
+void dialogue(char *m1, char *m2, int c){
+ printf("\033[1;3%im%s\033[0m: %s\n", c, m1, m2);
+ return;
+}
void add(void){
fp = fopen(TODOLIST, "a");
- if (fp == NULL){
- printf("ERROR: COULD NOT ACCESS FILE: %s\n",TODOLIST);
- return;
- }
+ if (fp == NULL)
+ error('f');
fputs(strcat(note,"\n"),fp);
fclose(fp);
}
FILE *tmp = fopen("temp", "w");
fp = fopen(TODOLIST, "r");
- if (fp == NULL){
- printf("ERROR: COULD NOT ACCESS FILE: %s\n",TODOLIST);
- return;
- }
+ if (fp == NULL)
+ error('f');
while (fgets(s, MAXLINE, fp) != NULL){
void show(void){
char *c = (char *) malloc(MAXLINE * sizeof(int));
- fp = fopen(TODOLIST,"r");
- if (fp == NULL){
- printf("ERROR: COULD NOT ACCESS FILE %s",TODOLIST);
- return;
- }
+ if (fp == NULL)
+ error('f');
while (fgets(c, MAXLINE, fp) != NULL )
printf("\t\t\033[1;3%im*\033[0m %s",geturgency(urgency), c);
enum color {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
-int listcheck(void){
- char *s = malloc(MAXLINE * sizeof(char));
+
+int listcheck(struct task t){
+ char *s = malloc(t.length* sizeof(char));
fp = fopen(TODOLIST, "r");
+ if (fp == NULL)
+ error('f');
- while (fgets(s, MAXLINE, fp) != NULL){
+ while (fgets(s, t.length, fp) != NULL){
s[strlen(s) - 1] = 0;
- if (strcmp(note, s) == 0){
+ if (strcmp(t.task, s) == 0){
free(s);
return 1;
}
else
return GREEN;
} else {
- printf("ERROR: urgency OUT OF RANGE");
- return -1;
+ error('r');
}
-
}
char *gettime(void){
- char *c = malloc(64*sizeof(char));
- time_t t = time(NULL);
+ struct tm *s;
+ time_t *t ;
+ char *c = malloc(16*sizeof(char));
+
+ *t = time(NULL);
if (t == ((t) - 1)){
error('t');
}
+
+ s = localtime(t);
- c = ctime(&t);
+ strftime(c, 16, "%y-%m-%d %H:%M", s);
if (c == NULL){
error('t');
}
return c;
+}
-
+int linecount(FILE *fp){
+ int i = 0;
+ char c;
+ while ((c = fgetc(fp)) != EOF){
+ if (c == '\n')
+ i++;
+ }
+ rewind(fp);
+ return i;
}
if (isdigit((int) *++arg)){
urgency = (int) *arg;
} else {
- printf("Non-integer argument for -i: %s\n", *arg);
+ error('r');
}
} else {
urgency = 5;
int main(int argc, char *argv[]){
char op;
int length,i;
+ struct task t;
note = (char *) malloc(MAXLINE * sizeof(char));
o = (char *) malloc (3 * sizeof(char));
- getnote(argc, argv);
- length = strlen(note)+1;
+ t = maketask(argc, argv);
getopt(argc, argv);
for (i = 0; i < strlen(o); i++)
switch (o[i]) {
case 'n':
- if (listcheck() == 0){
+ if (listcheck(t) == 0){
add();
printf("\033[32mAdded to list\033[0m: %s\n", note);
} else {
}
break;
case 'd':
- if (listcheck() == 1) {
+ if (listcheck(t) == 1) {
rem();
printf("\033[36mRemoved from list\033[0m: %s\n", note);
} else {
#define TIME "%H:%M %m-%d-%y"
#define TODOLIST "/home/huck/.local/state/odot/todo"
+struct task {
+ int length;
+ char *task;
+ char *date;
+ char *due;
+ int urgency;
+};
+
char *getnote(int, char *[]);
void getopt(int, char *[]);
+
void add(void);
void rem(void);
void show(void);
-int listcheck(void);
+
+int listcheck(struct task);
int geturgency(int);
+char *gettime(void);
void error(char);
+struct task maketask(int n, char **arg);
+void puttask(struct task n, FILE *fp);
+struct task gettask(FILE *fp);
+
char *note, *o;
int urgency;
FILE *fp;
extern int urgency;
extern FILE *fp;
-struct task {
- int length;
- char *task;
- int date;
- int due;
- int urgency;
-};
struct task maketask(int n, char **arg){
struct task temp;
temp.task = getnote(n,arg);
temp.length = strlen(temp.task);
- temp.date = 0;
+ temp.date = gettime();
temp.due = 0;
temp.urgency = urgency;
return temp;
}
-void puttask(struct task n){
- fp = fopen(TODOLIST, "w");
+void puttask(struct task n, FILE *fp){
- if (fp == NULL){
- error('f');
- }
-
- fprintf(fp, "%i\t%s\t%i\t%i\t%i\n",n.length,n.task,n.date,n.due,n.urgency);
+ fprintf(fp, "%i\t%s\t%s\t%s\t%i\n",n.length,n.task,n.date,n.due,n.urgency);
}
-struct task gettask(char *c){
+struct task gettask(FILE *fp){
struct task tmp;
- sscanf(c,"%i\t%s\t%i\t%i\t%i\n",&tmp.length,tmp.task,&tmp.date,&tmp.due,&tmp.urgency);
+ fscanf(fp,"%i\t%s\t%s\t%s\t%i\n",&tmp.length,tmp.task,tmp.date,tmp.due,&tmp.urgency);
return tmp;
}