bday

diff bday.c @ 8:19c1ad697022

beautifing :-)
author meillo@marmaro.de
date Tue, 18 Dec 2007 14:56:05 +0100
parents b6f4c7fba64a
children 4f48b4f86e3d
line diff
     1.1 --- a/bday.c	Tue Dec 18 11:59:21 2007 +0100
     1.2 +++ b/bday.c	Tue Dec 18 14:56:05 2007 +0100
     1.3 @@ -1,57 +1,56 @@
     1.4  /*
     1.5 -   birthday
     1.6 +	bday
     1.7  
     1.8 -   Birthday/Anniversary display on login
     1.9 +	Birthday/Anniversary reminder
    1.10  
    1.11 -   (c) 1996 AS Mortimer
    1.12 +	(c) 1996 AS Mortimer
    1.13 +	(c) 2007 markus schnalke <meillo@marmaro.de>
    1.14  
    1.15 -    This program is free software; you can redistribute it and/or
    1.16 -    modify it under the terms of the GNU General Public License as
    1.17 -    published by the Free Software Foundation; either version 2 of the
    1.18 -    License, or (at your option) any later version.  You may also
    1.19 -    distribute it under the Artistic License, as comes with Perl.
    1.20 +	This program is free software; you can redistribute it and/or
    1.21 +	modify it under the terms of the GNU General Public License as
    1.22 +	published by the Free Software Foundation; either version 2 of the
    1.23 +	License, or (at your option) any later version.
    1.24  
    1.25 -    This program is distributed in the hope that it will be useful,
    1.26 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.27 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.28 +	This program is distributed in the hope that it will be useful,
    1.29 +	but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.30 +	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.31  
    1.32 -    You should have received a copy of the GNU General Public License
    1.33 -    along with this program; if not, write to the Free Software
    1.34 -    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.35 +	You should have received a copy of the GNU General Public License
    1.36 +	along with this program; if not, write to the Free Software
    1.37 +	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.38  
    1.39 -    You should also have recieved a copy of the Artistic license with
    1.40 -    this program.
    1.41 +===============================================================================
    1.42  
    1.43 +	Input is read through standard input. For example: bday < ~/.birthdays
    1.44 +	The input (file) has to have the following format:
    1.45  
    1.46 -	 We're getting there. At the moment, the file used by default is ~/.birthdays
    1.47 -	 under UNIX, or C:\PERSONAL\BDAYS.LST under DOS, but this can be overridden on
    1.48 -	 the command line. The file has the following format:
    1.49 +	text=date flags
    1.50  
    1.51 -	 name/event/whatever=date flags
    1.52 -	 where:
    1.53 -		 date is dd/mm, dd/mm/yy (assumes 20th century!) or dd/mm/yyyy
    1.54 -		 flags is ONE or ZERO of
    1.55 -			 o  bd  for a birthday (default)
    1.56 -			 o  ann for an anniversary
    1.57 -			 o  ev  for an event
    1.58 -		 and zero or more of
    1.59 -			 o  w <n> to set the warn-in-advance time to n days (don't include the
    1.60 -	 brackets! :)
    1.61 -			 o  to <date>
    1.62 -			 o  for <days>
    1.63 -							 to specify the length of time taken by an event, for example a
    1.64 -				 holiday.
    1.65 +	where:
    1.66 +		date is yyyy-mm-dd
    1.67 +		flags is ONE or ZERO of
    1.68 +			bd  for a birthday (default)
    1.69 +			ann for an anniversary
    1.70 +			ev  for an event
    1.71 +		and zero or more of
    1.72 +			w <n> to set the warn-in-advance time to n days (don't include the
    1.73 +				brackets! :)
    1.74 +			to <date>
    1.75 +			for <days>
    1.76 +				to specify the length of time taken by an event, for example a holiday.
    1.77  
    1.78 -		 Comment lines are preceeded by #.
    1.79 +	Lines preceeded by # are treated as comments.
    1.80  
    1.81 -		 Note: If you deviate from this format, I cannot guarantee anything about
    1.82 -		 it's behaviour. In most cases, it will just quietly ignore the error,
    1.83 -		 which probably isn't ideal behaviour. Oh, well.
    1.84 +	Note: If you deviate from this format, I cannot guarantee anything about
    1.85 +	it's behaviour. In most cases, it will just quietly ignore the error,
    1.86 +	which probably isn't ideal behaviour. Oh, well.
    1.87  
    1.88 -		 2003/05/20: Automatic reallocation of output buffer in listsrings() by
    1.89 -								 Sebastian Schmidt <yath@yath.eu.org>.
    1.90 +===============================================================================
    1.91 +*/
    1.92  
    1.93 -*/
    1.94 +
    1.95 +/* standard time to warn in advance, when no explicit w flag is given. */
    1.96 +#define DEF_WARN 14
    1.97  
    1.98  
    1.99  #include <stdarg.h>
   1.100 @@ -64,14 +63,10 @@
   1.101  
   1.102  
   1.103  
   1.104 -/* standard time to warn in advance, when no explicit w flag is given. */
   1.105 -#define DEF_WARN 14
   1.106 -
   1.107  /* ========== Global constants and data types */
   1.108  
   1.109  
   1.110  /* month lengths etc */
   1.111 -
   1.112  #define isleapyear(y) ((y)%4==0 && ((y)%100 != 0 || (y)%400 == 0))
   1.113  const unsigned MLENDAT[];
   1.114  #define mlen(m,y) (MLENDAT[(m)-1] != -1 ? MLENDAT[(m)-1] : (isleapyear((y)) ? 29 : 28))
   1.115 @@ -79,7 +74,6 @@
   1.116  #define ydelta(a,b) ((int) (b).year - (a).year + before((a),(b)))
   1.117  
   1.118  /* -------- modifier flags */
   1.119 -
   1.120  #define F_MTYPE 0x07
   1.121  #define F_TBIRTHDAY 1
   1.122  #define F_TANNIVERSARY 2
   1.123 @@ -91,7 +85,7 @@
   1.124  #define F_FORDAYS 0x16
   1.125  #define F_TODATE 0x24
   1.126  
   1.127 -struct _ftable {char *txt; unsigned flag;};
   1.128 +struct _ftable {char* txt; unsigned flag;};
   1.129  
   1.130  const struct _ftable FTABLE[];
   1.131  
   1.132 @@ -102,7 +96,7 @@
   1.133  };
   1.134  
   1.135  struct event {
   1.136 -  char *text;
   1.137 +  char* text;
   1.138    struct date date;
   1.139    struct date enddate;
   1.140    int warn;
   1.141 @@ -114,14 +108,13 @@
   1.142  
   1.143  struct event* readlist(void);
   1.144  void gettoday(void);
   1.145 -unsigned delta(struct date *);
   1.146 -unsigned ddiff(struct date *D1, struct date *D2);
   1.147 +unsigned delta(struct date*);
   1.148 +unsigned ddiff(struct date* D1, struct date* D2);
   1.149  void liststrings(struct event* evl, prnfunc outf);
   1.150 -char *tdelta(struct date *d);
   1.151 -char *ttime(int yr, int mn, int wk, int dy);
   1.152 -
   1.153 -int skptok(int j, char *ptr);
   1.154 -int evcmp(const void *e1, const void *e2);
   1.155 +char* tdelta(struct date* d);
   1.156 +char* ttime(int yr, int mn, int wk, int dy);
   1.157 +int skptok(int j, char* ptr);
   1.158 +int evcmp(const void* e1, const void* e2);
   1.159  
   1.160  
   1.161  struct date today;
   1.162 @@ -159,7 +152,6 @@
   1.163  	return value;
   1.164  }
   1.165  
   1.166 -
   1.167  void* xrealloc (void* ptr, size_t size) {
   1.168  	register void* value = realloc (ptr, size);
   1.169  	if (value == 0) {
   1.170 @@ -169,10 +161,10 @@
   1.171  	return value;
   1.172  }
   1.173  
   1.174 +
   1.175  /* ========== */
   1.176  
   1.177  
   1.178 -
   1.179  /* like strcat(), but lets the buffer automagically grow :-)
   1.180   * (needs local variable "size" with the buffer size) */
   1.181  #define append(where, what) do {                            \
   1.182 @@ -187,10 +179,10 @@
   1.183  
   1.184  /* returns delta(d) in days, weeks, months, etc
   1.185   * the returned buffer is malloc()ed, do not forget to free() it */
   1.186 -char *tdelta(struct date *d) {
   1.187 +char* tdelta(struct date* d) {
   1.188  	int dy, wk, mn, yr;
   1.189 -	char *tmp;
   1.190 -	char *buf = xmalloc(128);
   1.191 +	char* tmp;
   1.192 +	char* buf = xmalloc(128);
   1.193  	int size = 128;
   1.194  	*buf = 0;
   1.195  
   1.196 @@ -203,9 +195,9 @@
   1.197  			return buf;
   1.198  		default:
   1.199  			/* like delta(), we ignore the year */
   1.200 -			yr=-before(*d,today);
   1.201 -			mn=d->month - today.month;
   1.202 -			dy=d->day - today.day;
   1.203 +			yr = -before(*d, today);
   1.204 +			mn = d->month - today.month;
   1.205 +			dy = d->day - today.day;
   1.206  
   1.207  			if (dy < 0) {
   1.208  				dy += mlen(today.month, today.year);
   1.209 @@ -361,10 +353,10 @@
   1.210  
   1.211  /* sort the events by the time before the next time they come up, putting those
   1.212  	 where the start has passed but we are still in the time-period first */
   1.213 -int evcmp(const void *p1, const void *p2) {
   1.214 -	struct event *e1=(struct event *)p1;
   1.215 -	struct event *e2=(struct event *)p2;
   1.216 -	unsigned d1,d2;
   1.217 +int evcmp(const void* p1, const void* p2) {
   1.218 +	struct event* e1=(struct event*) p1;
   1.219 +	struct event* e2=(struct event*) p2;
   1.220 +	unsigned d1, d2;
   1.221  
   1.222  	/* if the delta for the enddate is less than that for the start date, then we
   1.223  		have passed the start date but not yet the end date, and so we should
   1.224 @@ -379,7 +371,7 @@
   1.225  		d2=delta(&(e2->enddate));
   1.226  
   1.227  	if (d1 < d2) return -1;
   1.228 -	if (d1 > d2) return  1;
   1.229 +	if (d1 > d2) return 1;
   1.230  
   1.231  	return strcmp(e1->text, e2->text);
   1.232  }
   1.233 @@ -391,40 +383,40 @@
   1.234  
   1.235  /* difference in days between two dates */
   1.236  /* it is assumed that D1 < D2, and so the result is always positive */
   1.237 -unsigned ddiff(struct date *D1, struct date *D2) {
   1.238 -	struct date d1,d2;
   1.239 -	int dd,m;
   1.240 +unsigned ddiff(struct date* D1, struct date* D2) {
   1.241 +	struct date d1, d2;
   1.242 +	int dd, m;
   1.243  
   1.244  	/* make working copies */
   1.245 -	d1=*D1;
   1.246 -	d2=*D2;
   1.247 +	d1 = *D1;
   1.248 +	d2 = *D2;
   1.249  
   1.250  	/* sort out zero years */
   1.251  	if (d1.year == 0 || d2.year==0) {
   1.252  		if (d1.year != d2.year) {
   1.253  			if (d1.year == 0) {
   1.254 -	if (before(d1,d2))
   1.255 -		d1.year=d2.year;
   1.256 -	else
   1.257 -		d1.year=d2.year-1;
   1.258 -			} else {
   1.259 -	if (before(d1,d2))
   1.260 -		d2.year=d1.year;
   1.261 -	else
   1.262 -		d2.year=d1.year+1;
   1.263 +				if (before(d1,d2))
   1.264 +					d1.year = d2.year;
   1.265 +				else
   1.266 +					d1.year = d2.year - 1;
   1.267 +						} else {
   1.268 +				if (before(d1, d2))
   1.269 +					d2.year = d1.year;
   1.270 +				else
   1.271 +					d2.year = d1.year + 1;
   1.272  			}
   1.273  		} else { /* both years zero */
   1.274 -			if (before(d1,d2))
   1.275 -	d1.year=d2.year=today.year;
   1.276 +			if (before(d1, d2))
   1.277 +				d1.year = d2.year = today.year;
   1.278  			else {
   1.279 -	d1.year=today.year;
   1.280 -	d2.year=d1.year+1;
   1.281 +				d1.year = today.year;
   1.282 +				d2.year = d1.year + 1;
   1.283  			}
   1.284  		}
   1.285  	}
   1.286  
   1.287  	/* now we can actually do the comparison ... */
   1.288 -	dd=0;
   1.289 +	dd = 0;
   1.290  
   1.291  	/* to start with, we work in months */
   1.292  	for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++)
   1.293 @@ -460,8 +452,9 @@
   1.294  		d.year = 0;
   1.295  	}
   1.296  
   1.297 -	for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++)
   1.298 +	for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) {
   1.299  		dt += mlen(((mn-1)%12) + 1,today.year + mn/12);
   1.300 +	}
   1.301  
   1.302  	dt -= today.day;
   1.303  	dt += d.day;