# HG changeset patch # User markus schnalke # Date 1393260417 -3600 # Node ID 032af48d590bdb0429d480842572b6e4ef62d3f8 # Parent a56120a4678f11a0b8ad482cbe33d0d22902defb reformating of the source code diff -r a56120a4678f -r 032af48d590b bday.c --- a/bday.c Thu Nov 13 13:31:33 2008 +0100 +++ b/bday.c Mon Feb 24 17:46:57 2014 +0100 @@ -1,55 +1,54 @@ /* - bday +bday -- Birthday/Anniversary reminder - Birthday/Anniversary reminder +(c) 2007,2014 markus schnalke +(c) 1994-1999 AS Mortimer - (c) 2007 markus schnalke - (c) 1994-1999 AS Mortimer +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. You may also +distribute it under the Artistic License, as comes with Perl. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. You may also - distribute it under the Artistic License, as comes with Perl. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +You should also have recieved a copy of the Artistic license with +this program. - You should also have recieved a copy of the Artistic license with - this program. +===================================================================== -=============================================================================== +Input is read through standard input. For example: bday < ~/.birthdays +The input (file) has to have the following format: - Input is read through standard input. For example: bday < ~/.birthdays - The input (file) has to have the following format: +text=date flags - text=date flags +where: + date is yyyy-mm-dd + flags is ONE or ZERO of + bd for a birthday (default) + ann for an anniversary + ev for an event + and zero or more of + w to set the warn-in-advance time to n days + (don't include the brackets! :) + to + for + to specify the length of time taken by an + event, for example a holiday. - where: - date is yyyy-mm-dd - flags is ONE or ZERO of - bd for a birthday (default) - ann for an anniversary - ev for an event - and zero or more of - w to set the warn-in-advance time to n days (don't include the - brackets! :) - to - for - to specify the length of time taken by an event, for example a holiday. +Lines preceeded by # are treated as comments. - Lines preceeded by # are treated as comments. +Note: If you deviate from this format, I cannot guarantee anything about + it's behaviour. In most cases, it will just quietly ignore the + error, which probably isn't ideal behaviour. Oh, well. - Note: If you deviate from this format, I cannot guarantee anything about - it's behaviour. In most cases, it will just quietly ignore the error, - which probably isn't ideal behaviour. Oh, well. - -=============================================================================== +===================================================================== */ @@ -94,37 +93,37 @@ const struct _ftable FTABLE[]; struct date { - unsigned day; - unsigned month; - unsigned year; + unsigned day; + unsigned month; + unsigned year; }; struct event { - char* text; - struct date date; - struct date enddate; - int warn; + char* text; + struct date date; + struct date enddate; + int warn; }; typedef int (*prnfunc)(const char *); /* ========== Global Variables */ -struct event* readlist(void); +struct event *readlist(void); void gettoday(void); -unsigned delta(struct date*); -unsigned ddiff(struct date* D1, struct date* D2); -void liststrings(struct event* evl, prnfunc outf); -char* tdelta(struct date* d); -char* ttime(int yr, int mn, int wk, int dy); -int skptok(int j, char* ptr); -int evcmp(const void* e1, const void* e2); +unsigned delta(struct date *); +unsigned ddiff(struct date *D1, struct date *D2); +void liststrings(struct event *evl, prnfunc outf); +char *tdelta(struct date *d); +char *ttime(int yr, int mn, int wk, int dy); +int skptok(int j, char *ptr); +int evcmp(const void *e1, const void *e2); struct date today; -int iDWarn = DEF_WARN; +int iDWarn = DEF_WARN; -const unsigned MLENDAT[]={31,-1,31,30,31,30,31,31,30,31,30,31}; +const unsigned MLENDAT[] = {31,-1,31,30,31,30,31,31,30,31,30,31}; const struct _ftable FTABLE[] = { {"bd", F_TBIRTHDAY}, @@ -142,13 +141,15 @@ /* - xmalloc/xrealloc functions - Note: the x* functions are lifted straight from the GNU libc info docs - $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ +xmalloc/xrealloc functions +Note: the x* functions are lifted straight from the GNU libc info docs +$Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ */ -void* xmalloc (size_t size) { - register void* value = malloc (size); +void * +xmalloc(size_t size) +{ + register void *value = malloc (size); if (value == 0) { fprintf(stderr, "virtual memory exhausted\n"); exit(1); @@ -156,8 +157,10 @@ return value; } -void* xrealloc (void* ptr, size_t size) { - register void* value = realloc (ptr, size); +void * +xrealloc(void *ptr, size_t size) +{ + register void *value = realloc (ptr, size); if (value == 0) { fprintf(stderr, "virtual memory exhausted\n"); exit(1); @@ -169,8 +172,10 @@ /* ========== */ -/* like strcat(), but lets the buffer automagically grow :-) - * (needs local variable "size" with the buffer size) */ +/* +like strcat(), but lets the buffer automagically grow :-) +(needs local variable "size" with the buffer size) +*/ #define append(where, what) do { \ if (strlen(what) > (size - strlen(where))) { \ xrealloc(where, size + 128 + strlen(what)); \ @@ -181,46 +186,50 @@ /* ========== */ -/* returns delta(d) in days, weeks, months, etc - * the returned buffer is malloc()ed, do not forget to free() it */ -char* tdelta(struct date* d) { +/* +returns delta(d) in days, weeks, months, etc +the returned buffer is malloc()ed, do not forget to free() it +*/ +char * +tdelta(struct date *d) +{ int dy, wk, mn, yr; - char* tmp; - char* buf = xmalloc(128); + char *tmp; + char *buf = xmalloc(128); int size = 128; + *buf = 0; + switch (delta(d)) { + case 0: + append(buf, "today"); + return buf; + case 1: + append(buf, "tomorrow"); + return buf; + default: + /* like delta(), we ignore the year */ + yr = -before(*d, today); + mn = d->month - today.month; + dy = d->day - today.day; - switch (delta(d)) { - case 0: - append(buf, "today"); - return buf; - case 1: - append(buf, "tomorrow"); - return buf; - default: - /* like delta(), we ignore the year */ - yr = -before(*d, today); - mn = d->month - today.month; - dy = d->day - today.day; + if (dy < 0) { + dy += mlen(today.month, today.year); + mn--; + } + if (mn < 0) { + mn += 12; + yr++; + } - if (dy < 0) { - dy += mlen(today.month, today.year); - mn--; - } - if (mn < 0) { - mn += 12; - yr++; - } + wk = (dy / 7); + dy %= 7; - wk = (dy / 7); - dy %= 7; + append(buf, "in "); + tmp = ttime(yr, mn, wk, dy); + append(buf, tmp); + free(tmp); - append(buf, "in "); - tmp = ttime(yr, mn, wk, dy); - append(buf, tmp); - free(tmp); - - return buf; + return buf; } } @@ -229,23 +238,23 @@ /* -void donum(n,txt) { - do { - if (n > 0) { - snprintf(tmp, sizeof(tmp), "%d", n); - append(buf, tmp); - append(buf, " " txt); - if (n != 1) - append(buf, "s"); - terms--; - if (orgterms > 1) { - if (terms == 1) - append(buf, " and "); - else if (terms > 1) - append(buf, ", "); - } +void +donum(n,txt) +{ + if (n > 0) { + snprintf(tmp, sizeof(tmp), "%d", n); + append(buf, tmp); + append(buf, " " txt); + if (n != 1) + append(buf, "s"); + terms--; + if (orgterms > 1) { + if (terms == 1) + append(buf, " and "); + else if (terms > 1) + append(buf, ", "); } - } while(0) + } } */ @@ -269,8 +278,10 @@ /* returns allocated buffer, don't forget to free() */ -char* ttime(int yr, int mn, int wk, int dy) { - char* buf = xmalloc(128); +char * +ttime(int yr, int mn, int wk, int dy) +{ + char *buf = xmalloc(128); int size = 128; int terms, orgterms; char tmp[128]; @@ -292,8 +303,13 @@ -/* lists the birthdays in their string format, one by one, and passes the string to a function. */ -void liststrings(struct event* evl, prnfunc outf) { +/* +lists the birthdays in their string format, one by one, and passes +the string to a function. +*/ +void +liststrings(struct event *evl, prnfunc outf) +{ int i,j; char *buf, *tmp; int size; @@ -356,16 +372,24 @@ -/* sort the events by the time before the next time they come up, putting those - where the start has passed but we are still in the time-period first */ -int evcmp(const void* p1, const void* p2) { - struct event* e1=(struct event*) p1; - struct event* e2=(struct event*) p2; +/* +sort the events by the time before the next time they come up, +putting those where the start has passed but we are still in the +time-period first +*/ +int +evcmp(const void *p1, const void *p2) +{ + struct event *e1=(struct event *) p1; + struct event *e2=(struct event *) p2; unsigned d1, d2; - /* if the delta for the enddate is less than that for the start date, then we - have passed the start date but not yet the end date, and so we should - display the enddate; otherwise, we should display the start date */ + /* + if the delta for the enddate is less than that for the start + date, then we have passed the start date but not yet the end + date, and so we should display the enddate; otherwise, we + should display the start date + */ d1=delta(&(e1->date)); if (e1->enddate.day && delta(&(e1->enddate)) < d1) @@ -386,9 +410,13 @@ -/* difference in days between two dates */ -/* it is assumed that D1 < D2, and so the result is always positive */ -unsigned ddiff(struct date* D1, struct date* D2) { +/* +difference in days between two dates +it is assumed that D1 < D2, and so the result is always positive +*/ +unsigned +ddiff(struct date *D1, struct date *D2) +{ struct date d1, d2; int dd, m; @@ -427,8 +455,10 @@ for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) dd += mlen(((m-1)%12)+1, d1.year + m/12); - /* and then we renormalise for the days within the months */ - /* the first month was included in our calculations */ + /* + and then we renormalise for the days within the months + the first month was included in our calculations + */ dd -= d1.day; /* but the last one wasn't */ dd += d2.day; @@ -443,8 +473,12 @@ -/* actually until the next anniversary of ... */ -unsigned delta(struct date *date) { +/* +actually until the next anniversary of ... +*/ +unsigned +delta(struct date *date) +{ struct date d; unsigned dt, mn; @@ -472,7 +506,9 @@ -void gettoday(void) { +void +gettoday(void) +{ struct tm *tm; time_t t; @@ -492,7 +528,9 @@ -struct event* readlist() { +struct event * +readlist() +{ int i, j, k, l, d; struct event *evl; char buf[1024], buf2[1024]; @@ -524,7 +562,8 @@ *(ptr++) = 0; - j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); + j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), + &(evl[i].date.month), &(evl[i].date.day)); /* ... unless it wasn't read, in which case set it to zero */ if (j==2) { evl[i].date.year = 0; @@ -546,35 +585,35 @@ } switch (FTABLE[k].flag) { - case F_WTIME_P: /* w -- sets warning time */ - sscanf(ptr + j, "w %u", &(evl[i].warn)); - break; - case F_FORDAYS: /* for -- sets the duration of the event */ - sscanf(ptr + j, "for %u", &d); - evl[i].enddate=evl[i].date; - for (l = 1; l < d; l++) { - evl[i].enddate.day++; - if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { - evl[i].enddate.month++; - evl[i].enddate.day = 1; - } - if (evl[i].enddate.month > 12) { - evl[i].enddate.year++; - evl[i].enddate.month = 1; - } + case F_WTIME_P: /* w -- sets warning time */ + sscanf(ptr + j, "w %u", &(evl[i].warn)); + break; + case F_FORDAYS: /* for -- sets the duration of the event */ + sscanf(ptr + j, "for %u", &d); + evl[i].enddate=evl[i].date; + for (l = 1; l < d; l++) { + evl[i].enddate.day++; + if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { + evl[i].enddate.month++; + evl[i].enddate.day = 1; } - break; - case F_TODATE: /* to -- sets the end date of the event */ - l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); - if (l == 2) { - evl[i].enddate.year = 0; + if (evl[i].enddate.month > 12) { + evl[i].enddate.year++; + evl[i].enddate.month = 1; } - break; - case 0: - break; - default: - flags |= FTABLE[k].flag; - break; + } + break; + case F_TODATE: /* to -- sets the end date of the event */ + l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); + if (l == 2) { + evl[i].enddate.year = 0; + } + break; + case 0: + break; + default: + flags |= FTABLE[k].flag; + break; } } @@ -632,7 +671,9 @@ -int skptok(int j, char *ptr) { +int +skptok(int j, char *ptr) +{ for (; ptr[j] != 0 && ptr[j] != ' ' && ptr[j] != '\t' ; j++); for (; ptr[j] != 0 && (ptr[j] == ' ' || ptr[j] == '\t'); j++); @@ -644,9 +685,10 @@ -int main(int argc, char* argv[]) { - - while (--argc > 0 && (*++argv)[0] == '-') { +int +main(int argc, char *argv[]) +{ + while (--argc > 0 && (*++argv)[0] == '-') { if (strcmp(argv[0], "-W") == 0) { /* TODO: catch if no value given */ iDWarn = atoi((++argv)[0]); @@ -659,5 +701,5 @@ liststrings(readlist(), puts); - return 0; + return 0; }