# HG changeset patch # User meillo@marmaro.de # Date 1197900543 -3600 # Node ID 5af6bf2cb2716afee0482bd37e66dcd969eb06b9 # Parent 5326c222cd4e0b8101512d97e5ae4f691c583d6e reads only stdin now, no files anymore; removed -f option aswell; code beatifing diff -r 5326c222cd4e -r 5af6bf2cb271 Makefile --- a/Makefile Mon Dec 17 12:14:41 2007 +0100 +++ b/Makefile Mon Dec 17 15:09:03 2007 +0100 @@ -15,16 +15,17 @@ SRC=birthday.c bdengine.c OBJ=$(SRC:.c=.o) -CFLAGS=-O2 -Wall -Wstrict-prototypes +CFLAGS=-O2 -Wall -all: - @echo usage: make [un]install +all: build build: ${NAME} ${NAME}: $(OBJ) $(CC) $(LDFLAGS) $(OBJ) -o $@ +car: clean all + dist: build changelog @mkdir -p ${NV} @cp -f ${NAME} ${NAME}.1 Makefile ${DOCS} ${NV} diff -r 5326c222cd4e -r 5af6bf2cb271 bdengine.c --- a/bdengine.c Mon Dec 17 12:14:41 2007 +0100 +++ b/bdengine.c Mon Dec 17 15:09:03 2007 +0100 @@ -67,7 +67,6 @@ #include #include -#include #include "birthday.h" @@ -76,7 +75,7 @@ /* - xmalloc/xrealloc functions, and fatal exit function + 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 $ */ @@ -109,7 +108,6 @@ int skptok(int j, char *ptr); int evcmp(const void *e1, const void *e2); -char *deffname(void); /* ========== Global variables */ @@ -136,10 +134,6 @@ - -/* compare the first strlen(a) characters of a and b */ -#define strbegcmp(a,b) strncmp(a,b,strlen(a)) - /* like strcat(), but lets the buffer automagically grow :-) * (needs local variable "size" with the buffer size) */ #define append(where, what) do { \ @@ -162,43 +156,36 @@ *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; + 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); - if (*(buf + strlen(buf) - 1) == 's') - append(buf, "'"); - else - append(buf, "'s"); - - append(buf, " time"); - - return buf; + return buf; } } @@ -270,8 +257,7 @@ -/* lists the birthdays in their string format, one by one, and passes the string - to a function. */ +/* 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; @@ -296,8 +282,7 @@ if (delta(&(evl[i].date)) <= evl[i].warn) { append(buf, evl[i].text); append(buf, " for "); - /* +1 because, if the difference between two dates is one day, - then the length of an event on those days is two days */ + /* +1 because, if the difference between two dates is one day, then the length of an event on those days is two days */ j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; tmp = ttime(0, 0, j/7, j%7); append(buf, tmp); @@ -334,19 +319,6 @@ -char* deffname(void) { -char buf[256]; - - strcpy(buf, getpwuid(getuid())->pw_dir); - strcat(buf, "/" DEFAULT_FILE); - - return strdup(buf); -} - - - - - /* 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 */ @@ -471,42 +443,24 @@ tm = localtime(&t); today.day = tm->tm_mday; today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ - today.year = tm->tm_year; - today.year += 1900; + today.year = tm->tm_year + 1900; } -struct event *readlist(char *fname) { - FILE *file; - int i,j,k,l,d; +struct event* readlist() { + int i, j, k, l, d; struct event *evl; char buf[1024], buf2[1024]; char *ptr; unsigned flags; /* initialise */ - if (fname==NULL) { - fname=deffname(); - } - gettoday(); - if (fname[0] == '-' && fname[1] == 0) { - /* read from stdin */ - file = stdin; - } else { - /* now read it */ - if ((file = fopen(fname, "rt")) == NULL) { - fprintf(stderr, "Unable to open file \"%s\"\n", fname); - exit(1); - } - } - - - for (i = 0, evl = NULL; fgets(buf, sizeof(buf), file) != NULL; i++) { + for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); /* ignore comments and empty lines */ @@ -521,7 +475,7 @@ /* not a valid line, so ignore it! Cool, huh? */ /* Attention: only recognizes lines without '=' */ if (ptr == NULL) { - fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); + fprintf(stderr, "WARNING: Invalid line in input:\n%s", buf); i--; continue; } @@ -624,8 +578,7 @@ evl[i].date.year = 0; evl[i].text = (char *) NULL; - fclose(file); - free(fname); + fclose(stdin); /* NB uses i from above */ qsort(evl, i, sizeof(struct event), evcmp); diff -r 5326c222cd4e -r 5af6bf2cb271 birthday.c --- a/birthday.c Mon Dec 17 12:14:41 2007 +0100 +++ b/birthday.c Mon Dec 17 15:09:03 2007 +0100 @@ -34,14 +34,10 @@ int main(int argc, char* argv[]) { - char* fname = NULL; struct event *evl; /* evl => event list */ while (--argc > 0 && (*++argv)[0] == '-') { - if (strcmp(argv[0], "-f") == 0) { - fname = strdup((++argv)[0]); - argc--; - } else if (strcmp(argv[0], "-W") == 0) { + if (strcmp(argv[0], "-W") == 0) { iDWarn = atoi((++argv)[0]); argc--; } else { @@ -50,8 +46,7 @@ } } - evl = readlist(fname); /* read and format entries */ - + evl = readlist(); /* read and format entries */ liststrings(evl, puts); return 0; diff -r 5326c222cd4e -r 5af6bf2cb271 birthday.h --- a/birthday.h Mon Dec 17 12:14:41 2007 +0100 +++ b/birthday.h Mon Dec 17 15:09:03 2007 +0100 @@ -25,16 +25,14 @@ $Id: birthday.h,v 1.6 1999/04/25 14:01:29 andy Exp $ */ +#include + /* ========== Configuration section */ -#define DEFAULT_FILE ".birthdays" +/* standard time to warn in advance, when no explicit w flag is given. */ +#define DEF_WARN 14 -/* standard time to warn in advance, when no explicit w flag is given. */ -#define DEF_WARN 21 -/* ========== Required includes */ - -#include /* ========== Global constants and data types */ @@ -81,7 +79,7 @@ /* ========== */ -struct event *readlist(char *fname); +struct event* readlist(void); void gettoday(void); unsigned delta(struct date *); unsigned ddiff(struct date *D1, struct date *D2);