From 4a51e8f0fab262a9f9fda278b68f37488a1a664b Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 13 Dec 2025 23:59:01 -0800 Subject: [PATCH] chore: format --- .gitignore | 1 + Makefile.am | 3 + inc/add.h | 4 +- inc/args.h | 2 +- inc/copy.h | 4 +- inc/cut.h | 2 +- inc/event.h | 24 +++--- inc/file.h | 8 +- inc/main.h | 2 +- inc/opt.h | 12 +-- inc/postpone.h | 2 +- inc/recur.h | 2 +- inc/rm.h | 2 +- inc/seek.h | 2 +- src/add.c | 186 +++++++++++++++++++++++++++------------------ src/args.c | 139 +++++++++++++++++++++------------ src/copy.c | 56 +++++++++----- src/cut.c | 68 +++++++++++------ src/defaults.c | 4 +- src/event/free.c | 8 +- src/event/init.c | 13 ++-- src/event/name.c | 10 ++- src/event/parse.c | 109 +++++++++++++++----------- src/event/period.c | 68 ++++++++--------- src/event/place.c | 10 ++- src/event/serial.c | 175 +++++++++++++++++++++++++----------------- src/event/time.c | 58 +++++++++----- src/file/line.c | 2 +- src/file/mv.c | 20 +++-- src/file/open.c | 15 ++-- src/file/pipe.c | 10 ++- src/file/tmp.c | 4 +- src/ls.c | 78 ++++++++++++------- src/main.c | 48 +++++++----- src/opt/env.c | 8 +- src/opt/file.c | 8 +- src/opt/filter.c | 76 +++++++++--------- src/opt/recur.c | 12 +-- src/opt/until.c | 10 ++- src/postpone.c | 184 ++++++++++++++++++++++++-------------------- src/prune.c | 75 +++++++++++------- src/recur.c | 68 +++++++++-------- src/rm.c | 93 +++++++++++++---------- src/seek.c | 28 ++++--- src/usage.c | 59 +++++++------- src/validate.c | 28 ++++--- 46 files changed, 1067 insertions(+), 733 deletions(-) diff --git a/.gitignore b/.gitignore index 737de46..d566f79 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ config.status configure configure~ configure.scan +.lineno Makefile Makefile.in stamp-h1 diff --git a/Makefile.am b/Makefile.am index 7fafde0..cf9ad8f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -71,5 +71,8 @@ dist_bashcompletion_SCRIPTS = completions/ev SUBDIRS = . test/unit +format: + VERSION_CONTROL=none indent -linux -brf $(ev_SOURCES) + unit: $(MAKE) -C test/unit check diff --git a/inc/add.h b/inc/add.h index afe6f6b..d111d6c 100644 --- a/inc/add.h +++ b/inc/add.h @@ -13,7 +13,7 @@ #include #include -int add(int, char**); -int add_to_file(struct event*); +int add(int, char **); +int add_to_file(struct event *); #endif diff --git a/inc/args.h b/inc/args.h index 21f5a2d..c1016b3 100644 --- a/inc/args.h +++ b/inc/args.h @@ -17,6 +17,6 @@ enum sub_command { SUB_COMMAND_VALIDATE }; -enum sub_command args(int,char**); +enum sub_command args(int, char **); #endif diff --git a/inc/copy.h b/inc/copy.h index ef9a9df..916a8e6 100644 --- a/inc/copy.h +++ b/inc/copy.h @@ -6,7 +6,7 @@ #include #include -int copy(FILE*,FILE*,time_t); -int copy_next_event(FILE*,FILE*); +int copy(FILE *, FILE *, time_t); +int copy_next_event(FILE *, FILE *); #endif diff --git a/inc/cut.h b/inc/cut.h index 371dac4..8a597cd 100644 --- a/inc/cut.h +++ b/inc/cut.h @@ -5,6 +5,6 @@ #include #include -int cut(struct event*, time_t, size_t); +int cut(struct event *, time_t, size_t); #endif diff --git a/inc/event.h b/inc/event.h index 390253f..e08550f 100644 --- a/inc/event.h +++ b/inc/event.h @@ -30,7 +30,7 @@ enum time_period { struct event_options { unsigned int recur; enum time_period recur_period; - + struct tm until; }; @@ -48,18 +48,18 @@ struct event_filter { time_t end; }; -void event_free(const struct event*); -void event_init(struct event*); -int event_name_set(struct event*, const char*); -void event_options_init(struct event_options*); +void event_free(const struct event *); +void event_init(struct event *); +int event_name_set(struct event *, const char *); +void event_options_init(struct event_options *); enum time_period event_options_period(char); char event_options_period_char(enum time_period); -int event_parse(char*, size_t, struct event*); -int event_place_set(struct event*, const char*); -int event_serialize(char*, size_t, const struct event*); -int event_time_compare(const struct tm*,time_t); -int event_time_date_only(const struct tm*); -int event_time_set(struct tm*, const char*); -int event_within(struct event*, struct event_filter*); +int event_parse(char *, size_t, struct event *); +int event_place_set(struct event *, const char *); +int event_serialize(char *, size_t, const struct event *); +int event_time_compare(const struct tm *, time_t); +int event_time_date_only(const struct tm *); +int event_time_set(struct tm *, const char *); +int event_within(struct event *, struct event_filter *); #endif diff --git a/inc/file.h b/inc/file.h index b2bc20d..b9ae70b 100644 --- a/inc/file.h +++ b/inc/file.h @@ -6,10 +6,10 @@ #include -ssize_t file_line_next(char*, size_t, FILE*); -int file_move(const char*); +ssize_t file_line_next(char *, size_t, FILE *); +int file_move(const char *); FILE *file_open(); -int file_pipe(FILE*, FILE*); -FILE *file_temp(char*); +int file_pipe(FILE *, FILE *); +FILE *file_temp(char *); #endif diff --git a/inc/main.h b/inc/main.h index 35304ae..67b5584 100644 --- a/inc/main.h +++ b/inc/main.h @@ -13,6 +13,6 @@ #include #include -int main(int,char**); +int main(int, char **); #endif diff --git a/inc/opt.h b/inc/opt.h index 28018e0..55a4673 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -17,13 +17,13 @@ struct options { extern struct options global_options; -int opt_event_filter_date_set(time_t*,const char*); -int opt_event_filter_range_set(const char*, const char*); -int opt_event_filter_set(const char*); -int opt_file_set(const char*); +int opt_event_filter_date_set(time_t *, const char *); +int opt_event_filter_range_set(const char *, const char *); +int opt_event_filter_set(const char *); +int opt_file_set(const char *); void opt_global_init(); int opt_load_from_env(); -int opt_recur_set(const char*); -int opt_until_set(const char*); +int opt_recur_set(const char *); +int opt_until_set(const char *); #endif diff --git a/inc/postpone.h b/inc/postpone.h index f6d80c2..41edffb 100644 --- a/inc/postpone.h +++ b/inc/postpone.h @@ -9,6 +9,6 @@ #include #include -int postpone(int, char**); +int postpone(int, char **); #endif diff --git a/inc/recur.h b/inc/recur.h index 9c7710a..e56bd39 100644 --- a/inc/recur.h +++ b/inc/recur.h @@ -3,6 +3,6 @@ #include -int recur(struct event*); +int recur(struct event *); #endif diff --git a/inc/rm.h b/inc/rm.h index bf5e4e5..17ebbda 100644 --- a/inc/rm.h +++ b/inc/rm.h @@ -13,6 +13,6 @@ #define RM_FLAG_DISMISS 1 -int rm(int,char**,int); +int rm(int, char **, int); #endif diff --git a/inc/seek.h b/inc/seek.h index cca9787..8dc8b8e 100644 --- a/inc/seek.h +++ b/inc/seek.h @@ -4,6 +4,6 @@ #include #include -int seek(FILE*,time_t); +int seek(FILE *, time_t); #endif diff --git a/src/add.c b/src/add.c index 20a4228..e1d2e1b 100644 --- a/src/add.c +++ b/src/add.c @@ -1,10 +1,10 @@ #include -static int add_name_set(struct event*,const char*); -static int add_place_set(struct event*,const char*); -static int add_time_set(struct event*,const char*); -static int handle_args(int,char**,struct event*); -static int handle_options(struct event*); +static int add_name_set(struct event *, const char *); +static int add_place_set(struct event *, const char *); +static int add_time_set(struct event *, const char *); +static int handle_args(int, char **, struct event *); +static int handle_options(struct event *); int add(int argc, char **argv) { struct event ev; @@ -13,71 +13,84 @@ int add(int argc, char **argv) { event_init(&ev); - if(handle_options(&ev)<0) { return EXIT_FAILURE; } - if(handle_args(argc,argv,&ev)<0) { return EXIT_FAILURE; } - - strftime(timebuf,40,"%a, %d %b %Y %T %z",&(ev.datetime)); - - if(ev.place!=NULL) { - if(snprintf( - buf, - EVENT_SERIALIZE_MAX_LENGTH, - "Added event '%s' at '%s' on %s\n", - ev.name, - ev.place, - timebuf - )<0) { goto too_long; } + if (handle_options(&ev) < 0) { + return EXIT_FAILURE; + } + if (handle_args(argc, argv, &ev) < 0) { + return EXIT_FAILURE; + } + + strftime(timebuf, 40, "%a, %d %b %Y %T %z", &(ev.datetime)); + + if (ev.place != NULL) { + if (snprintf(buf, + EVENT_SERIALIZE_MAX_LENGTH, + "Added event '%s' at '%s' on %s\n", + ev.name, ev.place, timebuf) < 0) { + goto too_long; + } } else { - if(snprintf( - buf, - EVENT_SERIALIZE_MAX_LENGTH, - "Added event '%s' on %s\n", - ev.name, - timebuf - )<0) { goto too_long; } + if (snprintf(buf, + EVENT_SERIALIZE_MAX_LENGTH, + "Added event '%s' on %s\n", + ev.name, timebuf) < 0) { + goto too_long; + } + } + + if (add_to_file(&ev) < 0) { + return EXIT_FAILURE; } - - if(add_to_file(&ev)<0) { return EXIT_FAILURE; } - fprintf(stdout,buf); + fprintf(stdout, buf); return EXIT_SUCCESS; -too_long: - fprintf(stderr,"event too long\n"); + too_long: + fprintf(stderr, "event too long\n"); return EXIT_FAILURE; } static int add_name_set(struct event *ev, const char *p) { - assert(ev!=NULL); + assert(ev != NULL); - if(NULL==p) { return -1; } + if (NULL == p) { + return -1; + } ev->name = strdup(p); - if(NULL==ev->name) { return -1; } + if (NULL == ev->name) { + return -1; + } return 1; } static int add_place_set(struct event *ev, const char *p) { - assert(ev!=NULL); + assert(ev != NULL); - if(NULL==p) { return -1; } + if (NULL == p) { + return -1; + } ev->place = strdup(p); - if(NULL==ev->place) { return -1; } + if (NULL == ev->place) { + return -1; + } return 1; } static int add_time_set(struct event *ev, const char *str) { - assert(ev!=NULL); + assert(ev != NULL); - if(NULL==str) { return -1; } + if (NULL == str) { + return -1; + } - if(event_time_set(&(ev->datetime),str)<0) { - fprintf(stderr,"invalid date format provided\n"); + if (event_time_set(&(ev->datetime), str) < 0) { + fprintf(stderr, "invalid date format provided\n"); usage(); - return -1; + return -1; } return 1; @@ -90,51 +103,77 @@ int add_to_file(struct event *to_add) { FILE *src, *to; ssize_t i; - assert(to_add!=NULL); + assert(to_add != NULL); - if(NULL==to_add->name) { return -1; } + if (NULL == to_add->name) { + return -1; + } char tempfile[] = "/tmp/XXXXXX"; to = file_temp(tempfile); - if(NULL==to) { return -1; } + if (NULL == to) { + return -1; + } src = file_open(); - if(NULL==src) { return -1; } + if (NULL == src) { + return -1; + } - memcpy(&tm,&(to_add->datetime),sizeof(struct tm)); + memcpy(&tm, &(to_add->datetime), sizeof(struct tm)); to_add_time = mktime(&tm); - if(copy(to,src,to_add_time)<0) { return -1; } + if (copy(to, src, to_add_time) < 0) { + return -1; + } - if(event_serialize(buf,EVENT_SERIALIZE_MAX_LENGTH,to_add)<0) { return -1; } + if (event_serialize(buf, EVENT_SERIALIZE_MAX_LENGTH, to_add) < 0) { + return -1; + } i = strlen(buf); - if(fwrite(buf,1,i,to)!=i) { return -1; } + if (fwrite(buf, 1, i, to) != i) { + return -1; + } - if(file_pipe(to,src)<0) { return -1; } + if (file_pipe(to, src) < 0) { + return -1; + } - if(fclose(src)!=0) { return -1; } - if(fclose(to)!=0) { return -1; } + if (fclose(src) != 0) { + return -1; + } + if (fclose(to) != 0) { + return -1; + } - if(file_move(tempfile)<0) { return -1; } + if (file_move(tempfile) < 0) { + return -1; + } return 1; } int handle_args(int argc, char **argv, struct event *ev) { - assert(ev!=NULL); - - switch(argc-optind) { - case 4: - if(add_place_set(ev,argv[optind+2])<0) { return -1; } - case 3: - if(add_name_set(ev,argv[optind+1])<0) { return -1; } - if(add_time_set(ev,argv[argc-1])<0) { return -1; } - break; - default: - fprintf(stderr,"invalid number of arguments\n"); - usage(); + assert(ev != NULL); + + switch (argc - optind) { + case 4: + if (add_place_set(ev, argv[optind + 2]) < 0) { return -1; + } + case 3: + if (add_name_set(ev, argv[optind + 1]) < 0) { + return -1; + } + if (add_time_set(ev, argv[argc - 1]) < 0) { + return -1; + } + break; + default: + fprintf(stderr, "invalid number of arguments\n"); + usage(); + return -1; } return 1; @@ -143,15 +182,18 @@ int handle_args(int argc, char **argv, struct event *ev) { int handle_options(struct event *ev) { struct tm *time; - if(global_options.event_options.recur>0) { + if (global_options.event_options.recur > 0) { ev->options.recur = global_options.event_options.recur; - ev->options.recur_period = global_options.event_options.recur_period; - - if(global_options.event_filter.end>0) { + ev->options.recur_period = + global_options.event_options.recur_period; + + if (global_options.event_filter.end > 0) { time = localtime(&(global_options.event_filter.end)); - if(NULL==time) { return -1; } + if (NULL == time) { + return -1; + } - memcpy(&(ev->options.until),time,sizeof(struct tm)); + memcpy(&(ev->options.until), time, sizeof(struct tm)); } } diff --git a/src/args.c b/src/args.c index 0efdb5f..f04bd9e 100644 --- a/src/args.c +++ b/src/args.c @@ -9,71 +9,110 @@ struct option long_options[] = { {"recurring", required_argument, 0, 'r'}, {"start-date", required_argument, 0, 's'}, {"upcoming", optional_argument, 0, 'u'}, - {0,0,0,0} + {0, 0, 0, 0} }; enum sub_command args(int argc, char **argv) { char c; - while(1) { + while (1) { int option_index = 0; - if((c = getopt_long(argc,argv,"ae:f:hor:s:t:u::",long_options,&option_index))==-1) { break; } + if ((c = + getopt_long(argc, argv, "ae:f:hor:s:t:u::", long_options, + &option_index)) == -1) { + break; + } - switch(c) { - case 'a': - if(opt_event_filter_set("all")<0) { goto fail; } - break; - case 'e': - if(opt_event_filter_date_set( - &(global_options.event_filter.end), - /* time_t *to_set */ - optarg /* const char *date_string */ - )<0) { goto fail; } - break; - case 'f': - if(opt_file_set(optarg)<0) { goto fail; } - break; - case 'o': - if(opt_event_filter_set("overdue")<0) { goto fail; } - break; - case 'r': - if(opt_recur_set(optarg)<0) { goto fail; } - break; - case 's': - if(opt_event_filter_date_set( - &(global_options.event_filter.start), - /* time_t *to_set */ - optarg /* const char *date_string */ - )<0) { goto fail; } - break; - case 'u': - if((NULL==optarg)&&(argv[optind]!=NULL)&&(argv[optind][0]!='-')) { - if(opt_event_filter_range_set("upcoming",argv[optind])<0) { goto fail; } - optind++; - } else { - if(opt_event_filter_range_set("upcoming",optarg)<0) { goto fail; } - } - break; - case 'h': - case '?': - default: + switch (c) { + case 'a': + if (opt_event_filter_set("all") < 0) { + goto fail; + } + break; + case 'e': + if (opt_event_filter_date_set + (&(global_options.event_filter.end), + /* time_t *to_set */ + optarg /* const char *date_string */ + ) < 0) { + goto fail; + } + break; + case 'f': + if (opt_file_set(optarg) < 0) { + goto fail; + } + break; + case 'o': + if (opt_event_filter_set("overdue") < 0) { goto fail; + } + break; + case 'r': + if (opt_recur_set(optarg) < 0) { + goto fail; + } + break; + case 's': + if (opt_event_filter_date_set + (&(global_options.event_filter.start), + /* time_t *to_set */ + optarg /* const char *date_string */ + ) < 0) { + goto fail; + } + break; + case 'u': + if ((NULL == optarg) && (argv[optind] != NULL) + && (argv[optind][0] != '-')) { + if (opt_event_filter_range_set + ("upcoming", argv[optind]) < 0) { + goto fail; + } + optind++; + } else { + if (opt_event_filter_range_set + ("upcoming", optarg) < 0) { + goto fail; + } + } + break; + case 'h': + case '?': + default: + goto fail; } } - if(optind>=argc) { return SUB_COMMAND_LS; } + if (optind >= argc) { + return SUB_COMMAND_LS; + } - if(strcmp(argv[optind],"add")==0) { return SUB_COMMAND_ADD; } - if(strcmp(argv[optind],"dismiss")==0) { return SUB_COMMAND_DISMISS; } - if(strcmp(argv[optind],"ls")==0) { return SUB_COMMAND_LS; } - if(strcmp(argv[optind],"postpone")==0) { return SUB_COMMAND_POSTPONE; } - if(strcmp(argv[optind],"prune")==0) { return SUB_COMMAND_PRUNE; } - if(strcmp(argv[optind],"rm")==0) { return SUB_COMMAND_RM; } - if(strcmp(argv[optind],"validate")==0) { return SUB_COMMAND_VALIDATE; } + if (strcmp(argv[optind], "add") == 0) { + return SUB_COMMAND_ADD; + } + if (strcmp(argv[optind], "dismiss") == 0) { + return SUB_COMMAND_DISMISS; + } + if (strcmp(argv[optind], "ls") == 0) { + return SUB_COMMAND_LS; + } + if (strcmp(argv[optind], "postpone") == 0) { + return SUB_COMMAND_POSTPONE; + } + if (strcmp(argv[optind], "prune") == 0) { + return SUB_COMMAND_PRUNE; + } + if (strcmp(argv[optind], "rm") == 0) { + return SUB_COMMAND_RM; + } + if (strcmp(argv[optind], "validate") == 0) { + return SUB_COMMAND_VALIDATE; + } goto fail; -fail: + fail: usage(); return SUB_COMMAND_INVALID_ARGS; } diff --git a/src/copy.c b/src/copy.c index 1e2f80e..d337dd0 100644 --- a/src/copy.c +++ b/src/copy.c @@ -5,28 +5,40 @@ int copy(FILE *to, FILE *src, time_t until) { struct event ev; ssize_t i; - if(NULL==to) { return -1; } - if(NULL==src) { return -1; } + if (NULL == to) { + return -1; + } + if (NULL == src) { + return -1; + } + + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + while ((i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, src)) > 0) { - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - while((i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,src))>0) { + if (event_parse(buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + return -1; + } - if(event_parse(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { return -1; } - - if(event_time_compare(&(ev.datetime),until)>0) { + if (event_time_compare(&(ev.datetime), until) > 0) { event_free(&ev); - if(fseek(src,-i,SEEK_CUR)!=0) { return -1; } + if (fseek(src, -i, SEEK_CUR) != 0) { + return -1; + } break; } - if(event_serialize(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { return -1; } + if (event_serialize(buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + return -1; + } - if(fwrite(buf,1,i,to)!=i) { return -1; } + if (fwrite(buf, 1, i, to) != i) { + return -1; + } } - if(i<0) { - if(ferror(src)!=0) { - fprintf(stderr,"failed to get line\n"); + if (i < 0) { + if (ferror(src) != 0) { + fprintf(stderr, "failed to get line\n"); return -1; } } @@ -38,18 +50,20 @@ int copy_next_event(FILE *to, FILE *src) { char buf[EVENT_SERIALIZE_MAX_LENGTH]; ssize_t i; - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,src); + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, src); - if(i<0) { - if(ferror(src)!=0) { - fprintf(stderr,"failed to get line"); + if (i < 0) { + if (ferror(src) != 0) { + fprintf(stderr, "failed to get line"); return -1; } } - - if(i>0) { - if(fwrite(buf,1,i,to)!=i) { return -1; } + + if (i > 0) { + if (fwrite(buf, 1, i, to) != i) { + return -1; + } } return 1; diff --git a/src/cut.c b/src/cut.c index f6239ff..3627922 100644 --- a/src/cut.c +++ b/src/cut.c @@ -1,34 +1,52 @@ #include -static int extract(FILE*, struct event*); +static int extract(FILE *, struct event *); int cut(struct event *ev, time_t start, size_t offset) { FILE *src, *to; - assert(ev!=NULL); + assert(ev != NULL); char tempfile[] = "/tmp/XXXXXX"; to = file_temp(tempfile); - if(NULL==to) { return -1; } + if (NULL == to) { + return -1; + } src = file_open(); - if(NULL==src) { return -1; } + if (NULL == src) { + return -1; + } - if(copy(to,src,start)<0) { return -1; } + if (copy(to, src, start) < 0) { + return -1; + } - while(offset>0) { - if(copy_next_event(to,src)<0) { return -1; } + while (offset > 0) { + if (copy_next_event(to, src) < 0) { + return -1; + } offset--; } - if(extract(src,ev)<0) { return -1; } + if (extract(src, ev) < 0) { + return -1; + } - if(file_pipe(to,src)<0) { return -1; } - - if(fclose(src)!=0) { return -1; } - if(fclose(to)!=0) { return -1; } - - if(file_move(tempfile)<0) { return -1; } + if (file_pipe(to, src) < 0) { + return -1; + } + + if (fclose(src) != 0) { + return -1; + } + if (fclose(to) != 0) { + return -1; + } + + if (file_move(tempfile) < 0) { + return -1; + } return 1; } @@ -37,21 +55,25 @@ static int extract(FILE *src, struct event *ev) { char buf[EVENT_SERIALIZE_MAX_LENGTH]; ssize_t i; - assert(ev!=NULL); + assert(ev != NULL); - if(NULL==src) { return -1; } + if (NULL == src) { + return -1; + } + + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - - i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,src); - if(i<0) { - if(ferror(src)!=0) { - fprintf(stderr,"failed to get line"); + i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, src); + if (i < 0) { + if (ferror(src) != 0) { + fprintf(stderr, "failed to get line"); return -1; } } - if(event_parse(buf,EVENT_SERIALIZE_MAX_LENGTH,ev)<0) { return -1; } + if (event_parse(buf, EVENT_SERIALIZE_MAX_LENGTH, ev) < 0) { + return -1; + } return 1; } diff --git a/src/defaults.c b/src/defaults.c index cd3d8f6..ff853b3 100644 --- a/src/defaults.c +++ b/src/defaults.c @@ -9,7 +9,9 @@ static time_t midnight(); int defaults() { opt_global_init(); - if(opt_file_set(default_file)<0) { return -1; } + if (opt_file_set(default_file) < 0) { + return -1; + } global_options.event_filter.start = 0; global_options.event_filter.end = midnight(); diff --git a/src/event/free.c b/src/event/free.c index 72ab2e7..d5f970c 100644 --- a/src/event/free.c +++ b/src/event/free.c @@ -1,6 +1,10 @@ #include void event_free(const struct event *p) { - if(p->name!=NULL) { free(p->name); } - if(p->place!=NULL) { free(p->place); } + if (p->name != NULL) { + free(p->name); + } + if (p->place != NULL) { + free(p->place); + } } diff --git a/src/event/init.c b/src/event/init.c index 1872152..613f5f8 100644 --- a/src/event/init.c +++ b/src/event/init.c @@ -1,22 +1,21 @@ #include void event_init(struct event *p) { - assert(p!=NULL); + assert(p != NULL); event_options_init(&(p->options)); p->name = NULL; p->place = NULL; - - memset(&(p->datetime),0,sizeof(struct tm)); + + memset(&(p->datetime), 0, sizeof(struct tm)); } void event_options_init(struct event_options *p) { - assert(p!=NULL); + assert(p != NULL); p->recur = 0; p->recur_period = TIME_PERIOD_NONE; - - memset(&(p->until),0,sizeof(struct tm)); -} + memset(&(p->until), 0, sizeof(struct tm)); +} diff --git a/src/event/name.c b/src/event/name.c index ae20e91..d04148d 100644 --- a/src/event/name.c +++ b/src/event/name.c @@ -3,14 +3,18 @@ int event_name_set(struct event *ev, const char *name) { size_t len; - if(ev->name!=NULL) { free(ev->name); } + if (ev->name != NULL) { + free(ev->name); + } ev->name = strdup(name); - if(ev->name==NULL) { return -1; } + if (ev->name == NULL) { + return -1; + } len = strlen(ev->name); len--; - if(ev->name[len]=='\n') { + if (ev->name[len] == '\n') { ev->name[len] = '\0'; } diff --git a/src/event/parse.c b/src/event/parse.c index 58d6a07..c307408 100644 --- a/src/event/parse.c +++ b/src/event/parse.c @@ -1,17 +1,25 @@ #include -static int event_parse_name(char*, size_t, struct event*); -static int event_parse_options(char*, size_t, struct event*); -static int event_parse_place(char*, size_t, struct event*); -static int event_parse_time(char*, size_t, struct event*); +static int event_parse_name(char *, size_t, struct event *); +static int event_parse_options(char *, size_t, struct event *); +static int event_parse_place(char *, size_t, struct event *); +static int event_parse_time(char *, size_t, struct event *); int event_parse(char *buf, size_t buf_size, struct event *ev) { event_init(ev); - if(event_parse_options(buf,buf_size,ev)<0) { return -1; } - if(event_parse_time(buf,buf_size,ev)<0) { return -1; } - if(event_parse_name(buf,buf_size,ev)<0) { return -1; } - if(event_parse_place(buf,buf_size,ev)<0) { return -1; } + if (event_parse_options(buf, buf_size, ev) < 0) { + return -1; + } + if (event_parse_time(buf, buf_size, ev) < 0) { + return -1; + } + if (event_parse_name(buf, buf_size, ev) < 0) { + return -1; + } + if (event_parse_place(buf, buf_size, ev) < 0) { + return -1; + } return 1; } @@ -22,33 +30,37 @@ static int event_parse_name(char *buf, size_t buf_size, struct event *ev) { const char *token; char *p; - if(buf[0]=='"') { + if (buf[0] == '"') { token = escaped; } else { token = normal; } - if(strlen(buf)<1) { return -1; } + if (strlen(buf) < 1) { + return -1; + } - p = strtok(buf,token); - if(p!=NULL) { - if(event_name_set(ev,p)<0) { return -1; } + p = strtok(buf, token); + if (p != NULL) { + if (event_name_set(ev, p) < 0) { + return -1; + } } - p = strtok(NULL,token); + p = strtok(NULL, token); /* * fix string for if place is also escaped */ - if(token[0]!=':') { - if(p[1]=='\0') { + if (token[0] != ':') { + if (p[1] == '\0') { p[1] = '"'; } p++; } - if(p!=NULL) { - memmove(buf,p,(buf_size - (p - buf))); + if (p != NULL) { + memmove(buf, p, (buf_size - (p - buf))); } else { buf[0] = '\0'; } @@ -61,35 +73,38 @@ static int event_parse_options(char *buf, size_t buf_size, struct event *ev) { char *options, *p; size_t diff; - if(buf[0]!='[') { return 0; } + if (buf[0] != '[') { + return 0; + } - options = strtok(buf,"]"); + options = strtok(buf, "]"); - diff = strlen(options)+1; + diff = strlen(options) + 1; - p = strtok(&(options[1]),","); - while(p!=NULL) { - if(memcmp(p,"until:",6)==0) { - if(event_time_set(&(ev->options.until),&(p[6]))<0) { return -1; } - } else if(memcmp(p,"recur",5)==0) { - if(sscanf( - p, - "recur:%u%c", - &(ev->options.recur), - &period - )!=2) { - if(errno!=0) { + p = strtok(&(options[1]), ","); + while (p != NULL) { + if (memcmp(p, "until:", 6) == 0) { + if (event_time_set(&(ev->options.until), &(p[6])) < 0) { + return -1; + } + } else if (memcmp(p, "recur", 5) == 0) { + if (sscanf(p, + "recur:%u%c", + &(ev->options.recur), &period) != 2) { + if (errno != 0) { perror("sccanf"); return -1; } } ev->options.recur_period = event_options_period(period); - } else { return -1; } + } else { + return -1; + } - p = strtok(NULL,","); + p = strtok(NULL, ","); } - memmove(buf,&(buf[diff]),(buf_size - diff)); + memmove(buf, &(buf[diff]), (buf_size - diff)); return 1; } @@ -99,15 +114,17 @@ static int event_parse_place(char *buf, size_t buf_size, struct event *ev) { const char *token; char *p; - if(buf[0]=='"') { + if (buf[0] == '"') { token = escaped; } else { token = normal; } - p = strtok(buf,token); - if(p!=NULL) { - if(event_place_set(ev,p)<0) { return -1; } + p = strtok(buf, token); + if (p != NULL) { + if (event_place_set(ev, p) < 0) { + return -1; + } } return 1; @@ -116,10 +133,10 @@ static int event_parse_place(char *buf, size_t buf_size, struct event *ev) { static int event_parse_time(char *buf, size_t buf_size, struct event *ev) { char *p; - if(buf[10]!=' ') { + if (buf[10] != ' ') { buf[10] = '\0'; p = &(buf[11]); - } else if(buf[19]!=' ') { + } else if (buf[19] != ' ') { buf[19] = '\0'; p = &(buf[20]); } else { @@ -127,10 +144,12 @@ static int event_parse_time(char *buf, size_t buf_size, struct event *ev) { p = &(buf[26]); } - if(event_time_set(&(ev->datetime),buf)<0) { return -1; } + if (event_time_set(&(ev->datetime), buf) < 0) { + return -1; + } - if((buf_size - (p - buf))>0) { - memmove(buf,p,(buf_size - (p - buf))); + if ((buf_size - (p - buf)) > 0) { + memmove(buf, p, (buf_size - (p - buf))); } else { buf[0] = '\0'; } diff --git a/src/event/period.c b/src/event/period.c index 9a7b5dc..9856e3d 100644 --- a/src/event/period.c +++ b/src/event/period.c @@ -1,43 +1,43 @@ #include enum time_period event_options_period(char period) { - switch(period) { - case 'Y': - return TIME_PERIOD_YEAR; - case 'M': - return TIME_PERIOD_MONTH; - case 'W': - return TIME_PERIOD_WEEK; - case 'D': - return TIME_PERIOD_DAY; - case 'h': - return TIME_PERIOD_HOUR; - case 'm': - return TIME_PERIOD_MINUTE; - case 's': - return TIME_PERIOD_SECOND; - default: - return TIME_PERIOD_NONE; + switch (period) { + case 'Y': + return TIME_PERIOD_YEAR; + case 'M': + return TIME_PERIOD_MONTH; + case 'W': + return TIME_PERIOD_WEEK; + case 'D': + return TIME_PERIOD_DAY; + case 'h': + return TIME_PERIOD_HOUR; + case 'm': + return TIME_PERIOD_MINUTE; + case 's': + return TIME_PERIOD_SECOND; + default: + return TIME_PERIOD_NONE; } } char event_options_period_char(enum time_period period) { - switch(period) { - case TIME_PERIOD_SECOND: - return 's'; - case TIME_PERIOD_MINUTE: - return 'm'; - case TIME_PERIOD_HOUR: - return 'h'; - case TIME_PERIOD_DAY: - return 'D'; - case TIME_PERIOD_WEEK: - return 'W'; - case TIME_PERIOD_MONTH: - return 'M'; - case TIME_PERIOD_YEAR: - return 'Y'; - default: - return '\0'; + switch (period) { + case TIME_PERIOD_SECOND: + return 's'; + case TIME_PERIOD_MINUTE: + return 'm'; + case TIME_PERIOD_HOUR: + return 'h'; + case TIME_PERIOD_DAY: + return 'D'; + case TIME_PERIOD_WEEK: + return 'W'; + case TIME_PERIOD_MONTH: + return 'M'; + case TIME_PERIOD_YEAR: + return 'Y'; + default: + return '\0'; } } diff --git a/src/event/place.c b/src/event/place.c index 01f293c..122b879 100644 --- a/src/event/place.c +++ b/src/event/place.c @@ -3,14 +3,18 @@ int event_place_set(struct event *ev, const char *place) { size_t len; - if(ev->place!=NULL) { free(ev->place); } + if (ev->place != NULL) { + free(ev->place); + } ev->place = strdup(place); - if(ev->place==NULL) { return -1; } + if (ev->place == NULL) { + return -1; + } len = strlen(ev->place); len--; - if(ev->place[len]=='\n') { + if (ev->place[len] == '\n') { ev->place[len] = '\0'; } diff --git a/src/event/serial.c b/src/event/serial.c index 64381c7..9657709 100644 --- a/src/event/serial.c +++ b/src/event/serial.c @@ -1,128 +1,165 @@ #include -static int event_serialize_name(char*, size_t, const struct event*); -static int event_serialize_options(char*, size_t, const struct event*); -static int event_serialize_place(char*, size_t, const struct event*); -static int event_serialize_time(char*, size_t, const struct tm*); +static int event_serialize_name(char *, size_t, const struct event *); +static int event_serialize_options(char *, size_t, const struct event *); +static int event_serialize_place(char *, size_t, const struct event *); +static int event_serialize_time(char *, size_t, const struct tm *); int event_serialize(char *buf, size_t buf_size, const struct event *ev) { - assert(buf!=NULL); + assert(buf != NULL); - memset(buf,0,buf_size); + memset(buf, 0, buf_size); - if(event_serialize_options(buf,buf_size,ev)<0) { return -1; } - if(event_serialize_time(buf,buf_size,&(ev->datetime))<0) { return -1; } - if(event_serialize_name(buf,buf_size,ev)<0) { return -1; } - if(event_serialize_place(buf,buf_size,ev)<0) { return -1; } + if (event_serialize_options(buf, buf_size, ev) < 0) { + return -1; + } + if (event_serialize_time(buf, buf_size, &(ev->datetime)) < 0) { + return -1; + } + if (event_serialize_name(buf, buf_size, ev) < 0) { + return -1; + } + if (event_serialize_place(buf, buf_size, ev) < 0) { + return -1; + } event_free(ev); - strcat(buf,"\n"); + strcat(buf, "\n"); return 1; } -static int event_serialize_name(char *buf, size_t buf_size, const struct event *ev) { +static int event_serialize_name(char *buf, size_t buf_size, + const struct event *ev) { char *p; - if(NULL==ev->name) { return -1; } + if (NULL == ev->name) { + return -1; + } + + if (strlen(buf) + strlen(ev->name) + 2 > buf_size) { + return -1; + } - if(strlen(buf)+strlen(ev->name)+2>buf_size) { return -1; } + strcat(buf, ":"); - strcat(buf,":"); + p = strchr(ev->name, ':'); + if (p != NULL) { + strcat(buf, "\""); + } - p = strchr(ev->name,':'); - if(p!=NULL) { strcat(buf,"\""); } + strcat(buf, ev->name); - strcat(buf,ev->name); - - if(p!=NULL) { strcat(buf,"\""); } + if (p != NULL) { + strcat(buf, "\""); + } return 1; } -static int event_serialize_options(char *buf, size_t buf_size, const struct event *ev) { +static int event_serialize_options(char *buf, size_t buf_size, + const struct event *ev) { size_t pos; - if(!(ev->options.recur>0)) { return 0; } + if (!(ev->options.recur > 0)) { + return 0; + } - strcpy(buf,"["); + strcpy(buf, "["); pos = 1; - if(ev->options.recur>0) { - if(sprintf( - &(buf[pos]), - "recur:%u%c", - ev->options.recur, - event_options_period_char(ev->options.recur_period) - )<0) { return -1; } - - if(event_time_compare(&(ev->options.until),0)>0) { - strcat(buf,",until:"); - - if(event_serialize_time( - &(buf[pos]), - buf_size, - &(ev->options.until) - )<0) { return -1; } + if (ev->options.recur > 0) { + if (sprintf(&(buf[pos]), + "recur:%u%c", + ev->options.recur, + event_options_period_char(ev->options.recur_period) + ) < 0) { + return -1; + } + + if (event_time_compare(&(ev->options.until), 0) > 0) { + strcat(buf, ",until:"); + + if (event_serialize_time(&(buf[pos]), + buf_size, &(ev->options.until) + ) < 0) { + return -1; + } pos = strlen(buf); } } - strcat(buf,"]"); + strcat(buf, "]"); return 1; } -static int event_serialize_place(char *buf, size_t buf_size, const struct event *ev) { +static int event_serialize_place(char *buf, size_t buf_size, + const struct event *ev) { char *p; - assert(buf!=NULL); + assert(buf != NULL); + + if (NULL == ev->place) { + return 0; + } - if(NULL==ev->place) { return 0; } - - if(strlen(buf)+strlen(ev->place)+2>buf_size) { return -1; } + if (strlen(buf) + strlen(ev->place) + 2 > buf_size) { + return -1; + } - strcat(buf,":"); - - p = strchr(ev->name,':'); - if(p!=NULL) { strcat(buf,"\""); } + strcat(buf, ":"); - strcat(buf,ev->place); + p = strchr(ev->name, ':'); + if (p != NULL) { + strcat(buf, "\""); + } - if(p!=NULL) { strcat(buf,"\""); } + strcat(buf, ev->place); + + if (p != NULL) { + strcat(buf, "\""); + } return 1; } -static int event_serialize_time(char *buf, size_t buf_size, const struct tm *datetime) { +static int event_serialize_time(char *buf, size_t buf_size, + const struct tm *datetime) { size_t len, i; - assert(buf!=NULL); + assert(buf != NULL); len = strlen(buf); - - if(event_time_date_only(datetime)>0) { - if(0==strftime( - &(buf[len]), /* char *s */ - buf_size - len, /* size_t max */ - "%Y-%m-%d", /* const char *format */ - datetime /* const struct tm *tm */ - )) { return -1; } + + if (event_time_date_only(datetime) > 0) { + if (0 == strftime(&(buf[len]), /* char *s */ + buf_size - len, /* size_t max */ + "%Y-%m-%d", /* const char *format */ + datetime /* const struct tm *tm */ + )) { + return -1; + } } else { long offset = datetime->__tm_gmtoff; - i = strftime( - &(buf[len]), /* char *s */ - buf_size - len, /* size_t max */ - "%Y-%m-%d %H:%M:%S ", /* const char *format */ - datetime /* const struct tm *tm */ - ); - if(i==0) { return -1; } + i = strftime(&(buf[len]), /* char *s */ + buf_size - len, /* size_t max */ + "%Y-%m-%d %H:%M:%S ", /* const char *format */ + datetime /* const struct tm *tm */ + ); + if (i == 0) { + return -1; + } len += i; - if(snprintf(&(buf[len]),buf_size - len,"%+.2li%.2li", offset/3600, offset%60)<0) { return -1; } + if (snprintf + (&(buf[len]), buf_size - len, "%+.2li%.2li", offset / 3600, + offset % 60) < 0) { + return -1; + } } return 1; diff --git a/src/event/time.c b/src/event/time.c index 0245b44..bdb6860 100644 --- a/src/event/time.c +++ b/src/event/time.c @@ -4,26 +4,36 @@ int event_time_compare(const struct tm *datetime, time_t time) { struct tm tm; time_t event_time; - assert(datetime!=NULL); + assert(datetime != NULL); - memcpy(&tm,datetime,sizeof(struct tm)); + memcpy(&tm, datetime, sizeof(struct tm)); event_time = mktime(&tm); - if(event_time>time) { return 1; } - if(event_time time) { + return 1; + } + if (event_time < time) { + return -1; + } return 0; } int event_time_date_only(const struct tm *date) { - if((*date).tm_sec != 0) { return -1; } - if((*date).tm_min != 0) { return -1; } - if((*date).tm_hour != 0) { return -1; } + if ((*date).tm_sec != 0) { + return -1; + } + if ((*date).tm_min != 0) { + return -1; + } + if ((*date).tm_hour != 0) { + return -1; + } return 1; } int event_time_set(struct tm *datetime, const char *str) { const char *p; - + /* this call is necessary here to guarantee * that extern int daylight is set correctly. * see man tzset. @@ -34,18 +44,24 @@ int event_time_set(struct tm *datetime, const char *str) { datetime->tm_isdst = -1; /* minimum expected date format */ - p = strptime(str,"%Y-%m-%d %H:%M:%S %z",datetime); - if(p!=NULL) { return 1; } + p = strptime(str, "%Y-%m-%d %H:%M:%S %z", datetime); + if (p != NULL) { + return 1; + } - p = strptime(str,"%Y-%m-%d %H:%M:%S",datetime); - if(p!=NULL) { - if(mktime(datetime)<0) { return -1; } + p = strptime(str, "%Y-%m-%d %H:%M:%S", datetime); + if (p != NULL) { + if (mktime(datetime) < 0) { + return -1; + } return 1; } - p = strptime(str,"%Y-%m-%d",datetime); - if(NULL==p) { return -1; } - + p = strptime(str, "%Y-%m-%d", datetime); + if (NULL == p) { + return -1; + } + return 1; } @@ -53,12 +69,16 @@ int event_within(struct event *ev, struct event_filter *filter) { struct tm tm; time_t event_time; - memcpy(&tm,&(ev->datetime),sizeof(struct tm)); + memcpy(&tm, &(ev->datetime), sizeof(struct tm)); event_time = mktime(&tm); - if(filter->start>event_time) { return -1; } - if(filter->endstart > event_time) { + return -1; + } + if (filter->end < event_time) { + return -1; + } return 1; } diff --git a/src/file/line.c b/src/file/line.c index b842526..7fa3c00 100644 --- a/src/file/line.c +++ b/src/file/line.c @@ -1,5 +1,5 @@ #include ssize_t file_line_next(char *buf, size_t n, FILE *stream) { - return getline(&buf,&n,stream); + return getline(&buf, &n, stream); } diff --git a/src/file/mv.c b/src/file/mv.c index 22506b7..6d9c6b2 100644 --- a/src/file/mv.c +++ b/src/file/mv.c @@ -4,23 +4,31 @@ int file_move(const char *tempname) { FILE *src, *to; src = fopen(tempname, "r"); - if(NULL==src) { + if (NULL == src) { perror("fopen"); return -1; } to = fopen(global_options.file, "w"); - if(NULL==to) { + if (NULL == to) { perror("fopen"); return -1; } - if(file_pipe(to, src)<0) { return -1; } + if (file_pipe(to, src) < 0) { + return -1; + } - if(fclose(src)!=0) { return -1; } - if(fclose(to)!=0) { return -1; } + if (fclose(src) != 0) { + return -1; + } + if (fclose(to) != 0) { + return -1; + } - if(remove(tempname)!=0) { return -1; } + if (remove(tempname) != 0) { + return -1; + } return 1; } diff --git a/src/file/open.c b/src/file/open.c index ecbf85e..a6b5017 100644 --- a/src/file/open.c +++ b/src/file/open.c @@ -3,17 +3,20 @@ static FILE *file_create_prompt(); static FILE *file_create_prompt() { - fprintf(stderr,"%s doesn't exist... create? [Y/n] ",global_options.file); - if(fgetc(stdin)!='Y') { return NULL; } - return fopen(global_options.file,"w+"); + fprintf(stderr, "%s doesn't exist... create? [Y/n] ", + global_options.file); + if (fgetc(stdin) != 'Y') { + return NULL; + } + return fopen(global_options.file, "w+"); } FILE *file_open() { FILE *fp; - fp = fopen(global_options.file,"r"); - if(NULL==fp) { - if(errno!=ENOENT) { + fp = fopen(global_options.file, "r"); + if (NULL == fp) { + if (errno != ENOENT) { perror("fopen"); return NULL; } else { diff --git a/src/file/pipe.c b/src/file/pipe.c index 1f6c439..3662b3d 100644 --- a/src/file/pipe.c +++ b/src/file/pipe.c @@ -4,11 +4,15 @@ int file_pipe(FILE *to, FILE *from) { char buf[100]; size_t i; - while((i = fread(buf, 1, 100, from))>0) { - if(fwrite(buf,1,i,to)!=i) { return -1; } + while ((i = fread(buf, 1, 100, from)) > 0) { + if (fwrite(buf, 1, i, to) != i) { + return -1; + } } - if(feof(from)==0) { return -1; } + if (feof(from) == 0) { + return -1; + } return 1; } diff --git a/src/file/tmp.c b/src/file/tmp.c index ca4050e..a112a05 100644 --- a/src/file/tmp.c +++ b/src/file/tmp.c @@ -2,10 +2,10 @@ FILE *file_temp(char *template) { int fd = mkstemp(template); - if(fd<0) { + if (fd < 0) { perror("mkstemp"); return NULL; } - return fdopen(fd,"w"); + return fdopen(fd, "w"); } diff --git a/src/ls.c b/src/ls.c index 480dc73..8919b4b 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1,6 +1,6 @@ #include -static int event_print(struct event*); +static int event_print(struct event *); int ls() { char buf[EVENT_SERIALIZE_MAX_LENGTH]; @@ -9,27 +9,33 @@ int ls() { FILE *fp; fp = file_open(); - if(fp==NULL) { return EXIT_FAILURE; } + if (fp == NULL) { + return EXIT_FAILURE; + } - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - while((i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,fp))>0) { + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + while ((i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, fp)) > 0) { + + if (event_parse(buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + goto close; + } - if(event_parse(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { goto close; } - - if(event_within(&ev,&(global_options.event_filter))>0) { - if(event_print(&ev)<0) { goto close; } + if (event_within(&ev, &(global_options.event_filter)) > 0) { + if (event_print(&ev) < 0) { + goto close; + } } - - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); + + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); } - if(fclose(fp)!=0) { + if (fclose(fp) != 0) { perror("fclose"); return EXIT_FAILURE; } return EXIT_SUCCESS; -close: + close: fclose(fp); return EXIT_FAILURE; } @@ -40,35 +46,55 @@ static int event_print(struct event *ev) { struct tm *local; long offset; - assert(ev!=NULL); + assert(ev != NULL); - if(NULL==ev->name) { return -1; } + if (NULL == ev->name) { + return -1; + } - if(event_time_date_only(&(ev->datetime))>0) { - if(strftime(datebuf,20,"%Y-%m-%d",&(ev->datetime))!=10) { return -1; } + if (event_time_date_only(&(ev->datetime)) > 0) { + if (strftime(datebuf, 20, "%Y-%m-%d", &(ev->datetime)) != 10) { + return -1; + } } else { offset = ev->datetime.tm_gmtoff; timestamp = timegm(&(ev->datetime)) - offset; - if(timestamp<0) { return -1; } + if (timestamp < 0) { + return -1; + } local = localtime(×tamp); - if(NULL==local) { return -1; } + if (NULL == local) { + return -1; + } - if(strftime(datebuf,20,"%Y-%m-%d %H:%M:%S",local)!=19) { return -1; } + if (strftime(datebuf, 20, "%Y-%m-%d %H:%M:%S", local) != 19) { + return -1; + } } - if(fprintf(stdout,"%-20s\t",datebuf)<0) { return -1; } - if(fprintf(stdout,"%s",ev->name)<0) { return -1; } + if (fprintf(stdout, "%-20s\t", datebuf) < 0) { + return -1; + } + if (fprintf(stdout, "%s", ev->name) < 0) { + return -1; + } - if(ev->place!=NULL) { - if(fprintf(stdout," @ %s",ev->place)<0) { return -1; } + if (ev->place != NULL) { + if (fprintf(stdout, " @ %s", ev->place) < 0) { + return -1; + } } - if(event_time_compare(&(ev->datetime),time(NULL))<1) { - if(fprintf(stdout," \x1B[31m[OVERDUE]\x1B[0m")<0) { return -1; } + if (event_time_compare(&(ev->datetime), time(NULL)) < 1) { + if (fprintf(stdout, " \x1B[31m[OVERDUE]\x1B[0m") < 0) { + return -1; + } } - if(fprintf(stdout,"\n")<0) { return -1; } + if (fprintf(stdout, "\n") < 0) { + return -1; + } return 1; } diff --git a/src/main.c b/src/main.c index bd3f4e5..a3baf8c 100644 --- a/src/main.c +++ b/src/main.c @@ -3,28 +3,34 @@ int main(int argc, char **argv) { enum sub_command cmd; - if(defaults()<0) { return EXIT_FAILURE; } - if(opt_load_from_env()<0) { return EXIT_FAILURE; } + if (defaults() < 0) { + return EXIT_FAILURE; + } + if (opt_load_from_env() < 0) { + return EXIT_FAILURE; + } - cmd = args(argc,argv); - if(SUB_COMMAND_INVALID_ARGS==cmd) { return EXIT_FAILURE; } + cmd = args(argc, argv); + if (SUB_COMMAND_INVALID_ARGS == cmd) { + return EXIT_FAILURE; + } - switch(cmd) { - case SUB_COMMAND_ADD: - return add(argc,argv); - case SUB_COMMAND_DISMISS: - return rm(argc,argv,RM_FLAG_DISMISS); - case SUB_COMMAND_LS: - return ls(); - case SUB_COMMAND_POSTPONE: - return postpone(argc,argv); - case SUB_COMMAND_PRUNE: - return prune(); - case SUB_COMMAND_RM: - return rm(argc,argv,0); - case SUB_COMMAND_VALIDATE: - return validate(); - default: - return EXIT_FAILURE; + switch (cmd) { + case SUB_COMMAND_ADD: + return add(argc, argv); + case SUB_COMMAND_DISMISS: + return rm(argc, argv, RM_FLAG_DISMISS); + case SUB_COMMAND_LS: + return ls(); + case SUB_COMMAND_POSTPONE: + return postpone(argc, argv); + case SUB_COMMAND_PRUNE: + return prune(); + case SUB_COMMAND_RM: + return rm(argc, argv, 0); + case SUB_COMMAND_VALIDATE: + return validate(); + default: + return EXIT_FAILURE; } } diff --git a/src/opt/env.c b/src/opt/env.c index 0415641..fcc8641 100644 --- a/src/opt/env.c +++ b/src/opt/env.c @@ -10,10 +10,10 @@ int opt_load_from_env() { char *p; - CHECK_ENV("EVENT_FILTER",opt_event_filter_set); - CHECK_ENV("FILE",opt_file_set); - CHECK_ENV("RECUR",opt_recur_set); - CHECK_ENV("UNTIL",opt_until_set); + CHECK_ENV("EVENT_FILTER", opt_event_filter_set); + CHECK_ENV("FILE", opt_file_set); + CHECK_ENV("RECUR", opt_recur_set); + CHECK_ENV("UNTIL", opt_until_set); return 1; } diff --git a/src/opt/file.c b/src/opt/file.c index 2e539db..10decc2 100644 --- a/src/opt/file.c +++ b/src/opt/file.c @@ -1,10 +1,14 @@ #include int opt_file_set(const char *p) { - if(global_options.file!=NULL) { free(global_options.file); } + if (global_options.file != NULL) { + free(global_options.file); + } global_options.file = strdup(p); - if(NULL==global_options.file) { return -1; } + if (NULL == global_options.file) { + return -1; + } return 1; } diff --git a/src/opt/filter.c b/src/opt/filter.c index b0e6e37..29921d5 100644 --- a/src/opt/filter.c +++ b/src/opt/filter.c @@ -1,19 +1,19 @@ #include -static int opt_event_filter_range_set_upcoming(const char*); +static int opt_event_filter_range_set_upcoming(const char *); int opt_event_filter_date_set(time_t *to_set, const char *date_string) { struct event ev; - assert(to_set!=NULL); + assert(to_set != NULL); /* * This function uses the event_time_set(struct tm *datetime, const char *str) * method for consistencies' sake. */ - if(event_time_set(&(ev.datetime),date_string)<0) { - fprintf(stderr,"invalid date given: %s\n",date_string); + if (event_time_set(&(ev.datetime), date_string) < 0) { + fprintf(stderr, "invalid date given: %s\n", date_string); return -1; } @@ -23,17 +23,17 @@ int opt_event_filter_date_set(time_t *to_set, const char *date_string) { } int opt_event_filter_range_set(const char *type, const char *arg) { - if(strcmp(type,"upcoming")==0) { - if(opt_event_filter_range_set_upcoming((NULL==arg)?"1W":arg)<0) { + if (strcmp(type, "upcoming") == 0) { + if (opt_event_filter_range_set_upcoming + ((NULL == arg) ? "1W" : arg) < 0) { fprintf(stderr, - "invalid time period given as argument to --upcoming option\n" - ); + "invalid time period given as argument to --upcoming option\n"); return -1; } return 1; } - fprintf(stderr,"should never get to here\n"); + fprintf(stderr, "should never get to here\n"); return -1; } @@ -46,35 +46,37 @@ static int opt_event_filter_range_set_upcoming(const char *arg) { now_timet = time(NULL); now = localtime(&now_timet); - if(sscanf(arg,"%u%c",&offset,&period)!=2) { - if(errno!=0) { perror("sscanf"); } + if (sscanf(arg, "%u%c", &offset, &period) != 2) { + if (errno != 0) { + perror("sscanf"); + } return -1; } - switch(event_options_period(period)) { - case TIME_PERIOD_YEAR: - now->tm_year += offset; - break; - case TIME_PERIOD_MONTH: - now->tm_mon += offset; - break; - case TIME_PERIOD_WEEK: - now->tm_mday += 7*offset; - break; - case TIME_PERIOD_DAY: - now->tm_mday += offset; - break; - case TIME_PERIOD_HOUR: - now->tm_hour += offset; - break; - case TIME_PERIOD_MINUTE: - now->tm_min += offset; - break; - case TIME_PERIOD_SECOND: - now->tm_sec += offset; - break; - default: - return -1; + switch (event_options_period(period)) { + case TIME_PERIOD_YEAR: + now->tm_year += offset; + break; + case TIME_PERIOD_MONTH: + now->tm_mon += offset; + break; + case TIME_PERIOD_WEEK: + now->tm_mday += 7 * offset; + break; + case TIME_PERIOD_DAY: + now->tm_mday += offset; + break; + case TIME_PERIOD_HOUR: + now->tm_hour += offset; + break; + case TIME_PERIOD_MINUTE: + now->tm_min += offset; + break; + case TIME_PERIOD_SECOND: + now->tm_sec += offset; + break; + default: + return -1; } global_options.event_filter.start = time(NULL); @@ -84,13 +86,13 @@ static int opt_event_filter_range_set_upcoming(const char *arg) { } int opt_event_filter_set(const char *p) { - if(strcmp(p,"all")==0) { + if (strcmp(p, "all") == 0) { global_options.event_filter.start = 0; global_options.event_filter.end = INT_MAX; return 1; } - if(strcmp(p,"overdue")==0) { + if (strcmp(p, "overdue") == 0) { global_options.event_filter.start = 0; global_options.event_filter.end = time(NULL); return 1; diff --git a/src/opt/recur.c b/src/opt/recur.c index d7a4938..4a7421c 100644 --- a/src/opt/recur.c +++ b/src/opt/recur.c @@ -5,16 +5,18 @@ int opt_recur_set(const char *p) { char c; enum time_period period; - if(NULL==p) { return -1; } + if (NULL == p) { + return -1; + } - if(sscanf(p,"%u%c",&recur,&c)!=2) { - fprintf(stderr,"invalid recur option value: %s\n",p); + if (sscanf(p, "%u%c", &recur, &c) != 2) { + fprintf(stderr, "invalid recur option value: %s\n", p); return -1; } period = event_options_period(c); - if(TIME_PERIOD_NONE==period) { - fprintf(stderr,"invalid recur option value: %s\n",p); + if (TIME_PERIOD_NONE == period) { + fprintf(stderr, "invalid recur option value: %s\n", p); return -1; } diff --git a/src/opt/until.c b/src/opt/until.c index 49d1522..0ba5d0c 100644 --- a/src/opt/until.c +++ b/src/opt/until.c @@ -2,13 +2,15 @@ int opt_until_set(const char *p) { struct event ev; - - if(NULL==p) { return -1; } + + if (NULL == p) { + return -1; + } event_init(&ev); - if(event_time_set(&(ev.datetime),p)<0) { - fprintf(stderr,"invalid until option value: %s\n",p); + if (event_time_set(&(ev.datetime), p) < 0) { + fprintf(stderr, "invalid until option value: %s\n", p); return -1; } diff --git a/src/postpone.c b/src/postpone.c index 263971b..62fc7bb 100644 --- a/src/postpone.c +++ b/src/postpone.c @@ -2,51 +2,60 @@ static char *postpone_offset_string = NULL; -static int handle_args(int,char**,unsigned long int*,time_t*); -static int postpone_event(struct event*); +static int handle_args(int, char **, unsigned long int *, time_t *); +static int postpone_event(struct event *); -static int handle_args(int argc, char **argv, unsigned long int *offset, time_t *start_time) { +static int handle_args(int argc, char **argv, unsigned long int *offset, + time_t *start_time) { unsigned long int i; - - assert(offset!=NULL); - assert(start_time!=NULL); - - switch(argc-optind) { - case 4: /* date index period */ - if(opt_event_filter_date_set(start_time,argv[optind+1])<0) { - goto invalid; - } - - i = strtoul(argv[optind+2],NULL,0); - if(i>UINT_MAX) { goto invalid; } - (*offset) = i; - - postpone_offset_string = strdup(argv[argc-1]); - if(NULL==postpone_offset_string) { goto invalid; } - break; - case 3: /* date index */ - if(opt_event_filter_date_set(start_time,argv[optind+1])<0) { - goto invalid; - } - - i = strtoul(argv[argc-1],NULL,0); - if(i>UINT_MAX) { goto invalid; } - (*offset) = i; - break; - case 2: /* index */ - (*start_time) = 0; - - i = strtoul(argv[argc-1],NULL,0); - if(i>UINT_MAX) { goto invalid; } - (*offset) = i; - break; - default: + + assert(offset != NULL); + assert(start_time != NULL); + + switch (argc - optind) { + case 4: /* date index period */ + if (opt_event_filter_date_set(start_time, argv[optind + 1]) < 0) { + goto invalid; + } + + i = strtoul(argv[optind + 2], NULL, 0); + if (i > UINT_MAX) { + goto invalid; + } + (*offset) = i; + + postpone_offset_string = strdup(argv[argc - 1]); + if (NULL == postpone_offset_string) { + goto invalid; + } + break; + case 3: /* date index */ + if (opt_event_filter_date_set(start_time, argv[optind + 1]) < 0) { + goto invalid; + } + + i = strtoul(argv[argc - 1], NULL, 0); + if (i > UINT_MAX) { goto invalid; + } + (*offset) = i; + break; + case 2: /* index */ + (*start_time) = 0; + + i = strtoul(argv[argc - 1], NULL, 0); + if (i > UINT_MAX) { + goto invalid; + } + (*offset) = i; + break; + default: + goto invalid; } return 1; -invalid: - fprintf(stderr,"invalid number of arguments\n"); + invalid: + fprintf(stderr, "invalid number of arguments\n"); usage(); return -1; } @@ -58,29 +67,38 @@ int postpone(int argc, char **argv) { size_t offset; struct event ev; - if(handle_args(argc,argv,&offset,&start_time)<0) { goto fail; } + if (handle_args(argc, argv, &offset, &start_time) < 0) { + goto fail; + } + + if (cut(&ev, start_time, offset) < 0) { + goto fail; + } - if(cut(&ev,start_time,offset)<0) { goto fail; } + if (postpone_event(&ev) < 0) { + goto fail; + } - if(postpone_event(&ev)<0) { goto fail; } - - strftime(timebuf,40,"%a, %d %b %Y %T %z",&(ev.datetime)); + strftime(timebuf, 40, "%a, %d %b %Y %T %z", &(ev.datetime)); - if(snprintf( - buf, - EVENT_SERIALIZE_MAX_LENGTH, - "postponed event '%s' until '%s'\n", - ev.name, - timebuf - )<0) { goto fail; } + if (snprintf(buf, + EVENT_SERIALIZE_MAX_LENGTH, + "postponed event '%s' until '%s'\n", + ev.name, timebuf) < 0) { + goto fail; + } - if(add_to_file(&ev)<0) { goto fail; } + if (add_to_file(&ev) < 0) { + goto fail; + } - fprintf(stdout,buf); + fprintf(stdout, buf); return EXIT_SUCCESS; -fail: - if(postpone_offset_string!=NULL) { free(postpone_offset_string); } + fail: + if (postpone_offset_string != NULL) { + free(postpone_offset_string); + } return EXIT_FAILURE; } @@ -88,44 +106,44 @@ int postpone_event(struct event *ev) { unsigned int offset; char period; - assert(ev!=NULL); + assert(ev != NULL); - if(NULL==postpone_offset_string) { + if (NULL == postpone_offset_string) { ev->datetime.tm_mday += 1; return 1; } - if(sscanf(postpone_offset_string,"%u%c",&offset,&period)!=2) { - if(errno!=0) { + if (sscanf(postpone_offset_string, "%u%c", &offset, &period) != 2) { + if (errno != 0) { perror("sscanf"); return -1; } } - switch(event_options_period(period)) { - case TIME_PERIOD_YEAR: - ev->datetime.tm_year += offset; - break; - case TIME_PERIOD_MONTH: - ev->datetime.tm_mon += offset; - break; - case TIME_PERIOD_WEEK: - ev->datetime.tm_mday += 7*offset; - break; - case TIME_PERIOD_DAY: - ev->datetime.tm_mday += offset; - break; - case TIME_PERIOD_HOUR: - ev->datetime.tm_hour += offset; - break; - case TIME_PERIOD_MINUTE: - ev->datetime.tm_min += offset; - break; - case TIME_PERIOD_SECOND: - ev->datetime.tm_sec += offset; - break; - default: - return -1; + switch (event_options_period(period)) { + case TIME_PERIOD_YEAR: + ev->datetime.tm_year += offset; + break; + case TIME_PERIOD_MONTH: + ev->datetime.tm_mon += offset; + break; + case TIME_PERIOD_WEEK: + ev->datetime.tm_mday += 7 * offset; + break; + case TIME_PERIOD_DAY: + ev->datetime.tm_mday += offset; + break; + case TIME_PERIOD_HOUR: + ev->datetime.tm_hour += offset; + break; + case TIME_PERIOD_MINUTE: + ev->datetime.tm_min += offset; + break; + case TIME_PERIOD_SECOND: + ev->datetime.tm_sec += offset; + break; + default: + return -1; } free(postpone_offset_string); diff --git a/src/prune.c b/src/prune.c index 422f5f8..9f885b9 100644 --- a/src/prune.c +++ b/src/prune.c @@ -1,6 +1,6 @@ #include -static int copy_recur_events(FILE*,FILE*); +static int copy_recur_events(FILE *, FILE *); static int copy_recur_events(FILE *src, FILE *to) { char buf[EVENT_SERIALIZE_MAX_LENGTH]; @@ -10,28 +10,37 @@ static int copy_recur_events(FILE *src, FILE *to) { now = time(NULL); - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - while((i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,src))>0) { - if(event_parse(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { return -1; } + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + while ((i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, src)) > 0) { + if (event_parse(buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + return -1; + } - if(event_time_compare(&(ev.datetime),now)>0) { + if (event_time_compare(&(ev.datetime), now) > 0) { event_free(&ev); - if(fseek(src,-i,SEEK_CUR)!=0) { return -1; } + if (fseek(src, -i, SEEK_CUR) != 0) { + return -1; + } break; } - switch(recur(&ev)) { - case -1: - event_free(&ev); + switch (recur(&ev)) { + case -1: + event_free(&ev); + return -1; + case 1: + if (event_serialize + (buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + return -1; + } + if (fwrite(buf, 1, i, to) != i) { return -1; - case 1: - if(event_serialize(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { return -1; } - if(fwrite(buf,1,i,to)!=i) { return -1; } - break; - default: - event_free(&ev); - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - break; + } + break; + default: + event_free(&ev); + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + break; } } @@ -43,19 +52,33 @@ int prune() { char tempfile[] = "/tmp/XXXXXX"; to = file_temp(tempfile); - if(NULL==to) { return EXIT_FAILURE; } + if (NULL == to) { + return EXIT_FAILURE; + } src = file_open(); - if(NULL==src) { return EXIT_FAILURE; } + if (NULL == src) { + return EXIT_FAILURE; + } + + if (copy_recur_events(src, to) < 0) { + return EXIT_FAILURE; + } + + if (file_pipe(to, src) < 0) { + return EXIT_FAILURE; + } - if(copy_recur_events(src,to)<0) { return EXIT_FAILURE; } - - if(file_pipe(to,src)<0) { return EXIT_FAILURE; } + if (fclose(src) != 0) { + return EXIT_FAILURE; + } + if (fclose(to) != 0) { + return EXIT_FAILURE; + } - if(fclose(src)!=0) { return EXIT_FAILURE; } - if(fclose(to)!=0) { return EXIT_FAILURE; } - - if(file_move(tempfile)<0) { return EXIT_FAILURE; } + if (file_move(tempfile) < 0) { + return EXIT_FAILURE; + } return EXIT_SUCCESS; } diff --git a/src/recur.c b/src/recur.c index 8aec402..ec462b0 100644 --- a/src/recur.c +++ b/src/recur.c @@ -4,39 +4,43 @@ int recur(struct event *ev) { time_t compare; struct tm tm; - if(ev->options.recur==0) { return 0; } - - memcpy(&tm,&(ev->datetime),sizeof(struct tm)); - - switch(ev->options.recur_period) { - case TIME_PERIOD_YEAR: - tm.tm_year += ev->options.recur; - break; - case TIME_PERIOD_MONTH: - tm.tm_mon += ev->options.recur; - break; - case TIME_PERIOD_WEEK: - tm.tm_mday += 7*(ev->options.recur); - break; - case TIME_PERIOD_DAY: - tm.tm_mday += ev->options.recur; - break; - case TIME_PERIOD_HOUR: - tm.tm_hour += ev->options.recur; - break; - case TIME_PERIOD_MINUTE: - tm.tm_min += ev->options.recur; - break; - case TIME_PERIOD_SECOND: - tm.tm_sec += ev->options.recur; - break; - default: - return -1; + if (ev->options.recur == 0) { + return 0; + } + + memcpy(&tm, &(ev->datetime), sizeof(struct tm)); + + switch (ev->options.recur_period) { + case TIME_PERIOD_YEAR: + tm.tm_year += ev->options.recur; + break; + case TIME_PERIOD_MONTH: + tm.tm_mon += ev->options.recur; + break; + case TIME_PERIOD_WEEK: + tm.tm_mday += 7 * (ev->options.recur); + break; + case TIME_PERIOD_DAY: + tm.tm_mday += ev->options.recur; + break; + case TIME_PERIOD_HOUR: + tm.tm_hour += ev->options.recur; + break; + case TIME_PERIOD_MINUTE: + tm.tm_min += ev->options.recur; + break; + case TIME_PERIOD_SECOND: + tm.tm_sec += ev->options.recur; + break; + default: + return -1; } tm.tm_isdst = -1; compare = mktime(&tm); - if(compare<0) { return -1; } + if (compare < 0) { + return -1; + } ev->datetime.tm_year = tm.tm_year; ev->datetime.tm_mon = tm.tm_mon; @@ -46,8 +50,10 @@ int recur(struct event *ev) { ev->datetime.tm_sec = tm.tm_sec; ev->datetime.tm_wday = tm.tm_wday; - if(event_time_compare(&(ev->options.until),0)>0) { - if(event_time_compare(&(ev->options.until),compare)<0) { return 0; } + if (event_time_compare(&(ev->options.until), 0) > 0) { + if (event_time_compare(&(ev->options.until), compare) < 0) { + return 0; + } } return 1; diff --git a/src/rm.c b/src/rm.c index 29adea4..36b54b4 100644 --- a/src/rm.c +++ b/src/rm.c @@ -1,65 +1,68 @@ #include -static int check_need_readd(struct event*); -static int handle_args(int,char**,unsigned long int*, time_t *); +static int check_need_readd(struct event *); +static int handle_args(int, char **, unsigned long int *, time_t *); static int check_need_readd(struct event *ev) { char buf[EVENT_SERIALIZE_MAX_LENGTH]; char timebuf[40]; - assert(ev!=NULL); + assert(ev != NULL); - switch(recur(ev)) { - case -1: - return -1; - case 0: - return 1; - default: - break; + switch (recur(ev)) { + case -1: + return -1; + case 0: + return 1; + default: + break; } - strftime(timebuf,40,"%a, %d %b %Y %T %z",&(ev->datetime)); + strftime(timebuf, 40, "%a, %d %b %Y %T %z", &(ev->datetime)); - if(snprintf( - buf, - EVENT_SERIALIZE_MAX_LENGTH, - "re-added event '%s' on %s\n", - ev->name, - timebuf - )<0) { return -1; } + if (snprintf(buf, + EVENT_SERIALIZE_MAX_LENGTH, + "re-added event '%s' on %s\n", ev->name, timebuf) < 0) { + return -1; + } - if(add_to_file(ev)<0) { return -1; } + if (add_to_file(ev) < 0) { + return -1; + } - fprintf(stdout,buf); + fprintf(stdout, buf); return 1; } -static int handle_args(int argc, char **argv, unsigned long int *offset, time_t *start_time) { +static int handle_args(int argc, char **argv, unsigned long int *offset, + time_t *start_time) { unsigned long i; - assert(offset!=NULL); - assert(start_time!=NULL); + assert(offset != NULL); + assert(start_time != NULL); (*start_time) = 0; - switch(argc-optind) { - case 3: - if(opt_event_filter_date_set(start_time,argv[optind+1])<0) { - goto invalid; - } - case 2: - i = strtoul(argv[argc-1],NULL,0); - if(i>UINT_MAX) { goto invalid; } - (*offset) = i; - break; - default: + switch (argc - optind) { + case 3: + if (opt_event_filter_date_set(start_time, argv[optind + 1]) < 0) { + goto invalid; + } + case 2: + i = strtoul(argv[argc - 1], NULL, 0); + if (i > UINT_MAX) { goto invalid; + } + (*offset) = i; + break; + default: + goto invalid; } return 1; -invalid: - fprintf(stderr,"invalid arguments\n"); + invalid: + fprintf(stderr, "invalid arguments\n"); usage(); return -1; } @@ -70,16 +73,22 @@ int rm(int argc, char **argv, int flags) { size_t offset; struct event ev; - if(handle_args(argc,argv,&offset,&start_time)<0) { return EXIT_FAILURE; } + if (handle_args(argc, argv, &offset, &start_time) < 0) { + return EXIT_FAILURE; + } - if(cut(&ev,start_time,offset)<0) { return EXIT_FAILURE; } + if (cut(&ev, start_time, offset) < 0) { + return EXIT_FAILURE; + } - strftime(timebuf,40,"%a, %d %b %Y %T %z",&(ev.datetime)); + strftime(timebuf, 40, "%a, %d %b %Y %T %z", &(ev.datetime)); - fprintf(stderr,"removed event %s on %s\n",ev.name,timebuf); + fprintf(stderr, "removed event %s on %s\n", ev.name, timebuf); - if(RM_FLAG_DISMISS==flags) { - if(check_need_readd(&ev)<0) { return EXIT_FAILURE; } + if (RM_FLAG_DISMISS == flags) { + if (check_need_readd(&ev) < 0) { + return EXIT_FAILURE; + } } else { event_free(&ev); } diff --git a/src/seek.c b/src/seek.c index 0166ef3..116312e 100644 --- a/src/seek.c +++ b/src/seek.c @@ -5,25 +5,31 @@ int seek(FILE *fp, time_t time) { struct event ev; ssize_t i; - if(NULL==fp) { return -1; } + if (NULL == fp) { + return -1; + } - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - while((i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,fp))>0) { - if(event_parse(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { return -1; } + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + while ((i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, fp)) > 0) { + if (event_parse(buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + return -1; + } - if(event_time_compare(&(ev.datetime),time)>=0) { + if (event_time_compare(&(ev.datetime), time) >= 0) { event_free(&ev); - if(fseek(fp,-i,SEEK_CUR)!=0) { return -1; } + if (fseek(fp, -i, SEEK_CUR) != 0) { + return -1; + } break; } - + event_free(&ev); - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); } - if(i<0) { - if(ferror(fp)!=0) { - fprintf(stderr,"failed to get line"); + if (i < 0) { + if (ferror(fp) != 0) { + fprintf(stderr, "failed to get line"); return -1; } } diff --git a/src/usage.c b/src/usage.c index 1b73693..6dfcccf 100644 --- a/src/usage.c +++ b/src/usage.c @@ -7,38 +7,39 @@ struct option_description { }; static struct option_description options[] = { - {"all",'a',"show all"}, - {"end-date",'e',"ending date"}, - {"file",'f',"events file location"}, - {"help",'h',"show this help"}, - {"overdue",'o',"show only overdue events"}, - {"recurring",'r',"denote event as recurring"}, - {"start-date",'s',"starting date"}, - {"upcoming",'u',"show only upcoming events"}, - {NULL,0,NULL} + {"all", 'a', "show all"}, + {"end-date", 'e', "ending date"}, + {"file", 'f', "events file location"}, + {"help", 'h', "show this help"}, + {"overdue", 'o', "show only overdue events"}, + {"recurring", 'r', "denote event as recurring"}, + {"start-date", 's', "starting date"}, + {"upcoming", 'u', "show only upcoming events"}, + {NULL, 0, NULL} }; static void usage_print_options(); void usage() { - fprintf(stderr,"Usage:\n"); - fprintf(stderr,"\tev [options] [command = 'ls'] [arg1, arg2, ..., argn]\n"); - fprintf(stderr,"\n"); - fprintf(stderr,"Commands:\n"); - fprintf(stderr,"\tadd\n"); - fprintf(stderr,"\tdismiss\n"); - fprintf(stderr,"\tls\n"); - fprintf(stderr,"\tpostpone\n"); - fprintf(stderr,"\tprune\n"); - fprintf(stderr,"\trm\n"); - fprintf(stderr,"\n"); - - fprintf(stderr,"Options:\n"); + fprintf(stderr, "Usage:\n"); + fprintf(stderr, + "\tev [options] [command = 'ls'] [arg1, arg2, ..., argn]\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Commands:\n"); + fprintf(stderr, "\tadd\n"); + fprintf(stderr, "\tdismiss\n"); + fprintf(stderr, "\tls\n"); + fprintf(stderr, "\tpostpone\n"); + fprintf(stderr, "\tprune\n"); + fprintf(stderr, "\trm\n"); + fprintf(stderr, "\n"); + + fprintf(stderr, "Options:\n"); usage_print_options(); - fprintf(stderr,"\n"); - fprintf(stderr,"See `man ev` for more information.\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "See `man ev` for more information.\n"); return; } @@ -49,12 +50,12 @@ static void usage_print_options() { // NOTE: long options defined in src/args.c i = 0; - while( - !((NULL==options[i].long_opt) && - (0==options[i].short_opt) && - (NULL==options[i].description))) { + while (!((NULL == options[i].long_opt) && + (0 == options[i].short_opt) && + (NULL == options[i].description))) { - fprintf(stderr,"\t--%s, -%c\t\t%s\n",options[i].long_opt,options[i].short_opt,options[i].description); + fprintf(stderr, "\t--%s, -%c\t\t%s\n", options[i].long_opt, + options[i].short_opt, options[i].description); i++; } } diff --git a/src/validate.c b/src/validate.c index e0025e9..c6fe8a1 100644 --- a/src/validate.c +++ b/src/validate.c @@ -8,30 +8,34 @@ int validate() { size_t line; src = file_open(); - if(NULL==src) { return EXIT_FAILURE; } - + if (NULL == src) { + return EXIT_FAILURE; + } + line = 1; - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); - while((i = file_line_next(buf,EVENT_SERIALIZE_MAX_LENGTH,src))>0) { - if(event_parse(buf,EVENT_SERIALIZE_MAX_LENGTH,&ev)<0) { - fprintf(stderr,"invalid event: event on line %lu failed to parse\n",line); + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); + while ((i = file_line_next(buf, EVENT_SERIALIZE_MAX_LENGTH, src)) > 0) { + if (event_parse(buf, EVENT_SERIALIZE_MAX_LENGTH, &ev) < 0) { + fprintf(stderr, + "invalid event: event on line %lu failed to parse\n", + line); return EXIT_FAILURE; } event_free(&ev); - memset(buf,0,EVENT_SERIALIZE_MAX_LENGTH); + memset(buf, 0, EVENT_SERIALIZE_MAX_LENGTH); line++; } - - if(i<0) { - if(ferror(src)!=0) { - fprintf(stderr,"failed to get line"); + + if (i < 0) { + if (ferror(src) != 0) { + fprintf(stderr, "failed to get line"); return EXIT_FAILURE; } } - fprintf(stdout,"%s: \x1B[32mVALID\x1B[0m\n",global_options.file); + fprintf(stdout, "%s: \x1B[32mVALID\x1B[0m\n", global_options.file); return EXIT_SUCCESS; } -- 2.52.0