changeset 8:19c1ad697022

beautifing :-)
author meillo@marmaro.de
date Tue, 18 Dec 2007 14:56:05 +0100
parents b6f4c7fba64a
children 4f48b4f86e3d
files bday.c
diffstat 1 files changed, 85 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/bday.c	Tue Dec 18 11:59:21 2007 +0100
+++ b/bday.c	Tue Dec 18 14:56:05 2007 +0100
@@ -1,57 +1,56 @@
 /*
-   birthday
+	bday
+
+	Birthday/Anniversary reminder
+
+	(c) 1996 AS Mortimer
+	(c) 2007 markus schnalke <meillo@marmaro.de>
 
-   Birthday/Anniversary display on login
+	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.
 
-   (c) 1996 AS Mortimer
+	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
+
+===============================================================================
 
-    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.
+	Input is read through standard input. For example: bday < ~/.birthdays
+	The input (file) has to have the following format:
+
+	text=date flags
 
-    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.
+	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 <n> to set the warn-in-advance time to n days (don't include the
+				brackets! :)
+			to <date>
+			for <days>
+				to specify the length of time taken by an event, for example a holiday.
 
-    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
+	Lines preceeded by # are treated as comments.
 
-    You should also have recieved a copy of the Artistic license with
-    this program.
+	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.
+
+===============================================================================
+*/
 
 
-	 We're getting there. At the moment, the file used by default is ~/.birthdays
-	 under UNIX, or C:\PERSONAL\BDAYS.LST under DOS, but this can be overridden on
-	 the command line. The file has the following format:
-
-	 name/event/whatever=date flags
-	 where:
-		 date is dd/mm, dd/mm/yy (assumes 20th century!) or dd/mm/yyyy
-		 flags is ONE or ZERO of
-			 o  bd  for a birthday (default)
-			 o  ann for an anniversary
-			 o  ev  for an event
-		 and zero or more of
-			 o  w <n> to set the warn-in-advance time to n days (don't include the
-	 brackets! :)
-			 o  to <date>
-			 o  for <days>
-							 to specify the length of time taken by an event, for example a
-				 holiday.
-
-		 Comment lines are preceeded by #.
-
-		 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.
-
-		 2003/05/20: Automatic reallocation of output buffer in listsrings() by
-								 Sebastian Schmidt <yath@yath.eu.org>.
-
-*/
+/* standard time to warn in advance, when no explicit w flag is given. */
+#define DEF_WARN 14
 
 
 #include <stdarg.h>
@@ -64,14 +63,10 @@
 
 
 
-/* standard time to warn in advance, when no explicit w flag is given. */
-#define DEF_WARN 14
-
 /* ========== Global constants and data types */
 
 
 /* month lengths etc */
-
 #define isleapyear(y) ((y)%4==0 && ((y)%100 != 0 || (y)%400 == 0))
 const unsigned MLENDAT[];
 #define mlen(m,y) (MLENDAT[(m)-1] != -1 ? MLENDAT[(m)-1] : (isleapyear((y)) ? 29 : 28))
@@ -79,7 +74,6 @@
 #define ydelta(a,b) ((int) (b).year - (a).year + before((a),(b)))
 
 /* -------- modifier flags */
-
 #define F_MTYPE 0x07
 #define F_TBIRTHDAY 1
 #define F_TANNIVERSARY 2
@@ -91,7 +85,7 @@
 #define F_FORDAYS 0x16
 #define F_TODATE 0x24
 
-struct _ftable {char *txt; unsigned flag;};
+struct _ftable {char* txt; unsigned flag;};
 
 const struct _ftable FTABLE[];
 
@@ -102,7 +96,7 @@
 };
 
 struct event {
-  char *text;
+  char* text;
   struct date date;
   struct date enddate;
   int warn;
@@ -114,14 +108,13 @@
 
 struct event* readlist(void);
 void gettoday(void);
-unsigned delta(struct date *);
-unsigned ddiff(struct date *D1, struct date *D2);
+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);
+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;
@@ -159,7 +152,6 @@
 	return value;
 }
 
-
 void* xrealloc (void* ptr, size_t size) {
 	register void* value = realloc (ptr, size);
 	if (value == 0) {
@@ -169,10 +161,10 @@
 	return value;
 }
 
+
 /* ========== */
 
 
-
 /* like strcat(), but lets the buffer automagically grow :-)
  * (needs local variable "size" with the buffer size) */
 #define append(where, what) do {                            \
@@ -187,10 +179,10 @@
 
 /* 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) {
+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;
 
@@ -203,9 +195,9 @@
 			return buf;
 		default:
 			/* like delta(), we ignore the year */
-			yr=-before(*d,today);
-			mn=d->month - today.month;
-			dy=d->day - today.day;
+			yr = -before(*d, today);
+			mn = d->month - today.month;
+			dy = d->day - today.day;
 
 			if (dy < 0) {
 				dy += mlen(today.month, today.year);
@@ -361,10 +353,10 @@
 
 /* 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;
+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
@@ -379,7 +371,7 @@
 		d2=delta(&(e2->enddate));
 
 	if (d1 < d2) return -1;
-	if (d1 > d2) return  1;
+	if (d1 > d2) return 1;
 
 	return strcmp(e1->text, e2->text);
 }
@@ -391,40 +383,40 @@
 
 /* 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;
+unsigned ddiff(struct date* D1, struct date* D2) {
+	struct date d1, d2;
+	int dd, m;
 
 	/* make working copies */
-	d1=*D1;
-	d2=*D2;
+	d1 = *D1;
+	d2 = *D2;
 
 	/* sort out zero years */
 	if (d1.year == 0 || d2.year==0) {
 		if (d1.year != d2.year) {
 			if (d1.year == 0) {
-	if (before(d1,d2))
-		d1.year=d2.year;
-	else
-		d1.year=d2.year-1;
-			} else {
-	if (before(d1,d2))
-		d2.year=d1.year;
-	else
-		d2.year=d1.year+1;
+				if (before(d1,d2))
+					d1.year = d2.year;
+				else
+					d1.year = d2.year - 1;
+						} else {
+				if (before(d1, d2))
+					d2.year = d1.year;
+				else
+					d2.year = d1.year + 1;
 			}
 		} else { /* both years zero */
-			if (before(d1,d2))
-	d1.year=d2.year=today.year;
+			if (before(d1, d2))
+				d1.year = d2.year = today.year;
 			else {
-	d1.year=today.year;
-	d2.year=d1.year+1;
+				d1.year = today.year;
+				d2.year = d1.year + 1;
 			}
 		}
 	}
 
 	/* now we can actually do the comparison ... */
-	dd=0;
+	dd = 0;
 
 	/* to start with, we work in months */
 	for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++)
@@ -460,8 +452,9 @@
 		d.year = 0;
 	}
 
-	for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++)
+	for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) {
 		dt += mlen(((mn-1)%12) + 1,today.year + mn/12);
+	}
 
 	dt -= today.day;
 	dt += d.day;