bday
changeset 5:5af6bf2cb271
reads only stdin now, no files anymore; removed -f option aswell; code beatifing
author | meillo@marmaro.de |
---|---|
date | Mon, 17 Dec 2007 15:09:03 +0100 |
parents | 5326c222cd4e |
children | fc6e40f7bd5a |
files | Makefile bdengine.c birthday.c birthday.h |
diffstat | 4 files changed, 46 insertions(+), 99 deletions(-) [+] |
line diff
1.1 --- a/Makefile Mon Dec 17 12:14:41 2007 +0100 1.2 +++ b/Makefile Mon Dec 17 15:09:03 2007 +0100 1.3 @@ -15,16 +15,17 @@ 1.4 SRC=birthday.c bdengine.c 1.5 OBJ=$(SRC:.c=.o) 1.6 1.7 -CFLAGS=-O2 -Wall -Wstrict-prototypes 1.8 +CFLAGS=-O2 -Wall 1.9 1.10 -all: 1.11 - @echo usage: make [un]install 1.12 +all: build 1.13 1.14 build: ${NAME} 1.15 1.16 ${NAME}: $(OBJ) 1.17 $(CC) $(LDFLAGS) $(OBJ) -o $@ 1.18 1.19 +car: clean all 1.20 + 1.21 dist: build changelog 1.22 @mkdir -p ${NV} 1.23 @cp -f ${NAME} ${NAME}.1 Makefile ${DOCS} ${NV}
2.1 --- a/bdengine.c Mon Dec 17 12:14:41 2007 +0100 2.2 +++ b/bdengine.c Mon Dec 17 15:09:03 2007 +0100 2.3 @@ -67,7 +67,6 @@ 2.4 2.5 #include <sys/types.h> 2.6 #include <unistd.h> 2.7 -#include <pwd.h> 2.8 2.9 #include "birthday.h" 2.10 2.11 @@ -76,7 +75,7 @@ 2.12 2.13 2.14 /* 2.15 - xmalloc/xrealloc functions, and fatal exit function 2.16 + xmalloc/xrealloc functions 2.17 Note: the x* functions are lifted straight from the GNU libc info docs 2.18 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ 2.19 */ 2.20 @@ -109,7 +108,6 @@ 2.21 2.22 int skptok(int j, char *ptr); 2.23 int evcmp(const void *e1, const void *e2); 2.24 -char *deffname(void); 2.25 2.26 2.27 /* ========== Global variables */ 2.28 @@ -136,10 +134,6 @@ 2.29 2.30 2.31 2.32 - 2.33 -/* compare the first strlen(a) characters of a and b */ 2.34 -#define strbegcmp(a,b) strncmp(a,b,strlen(a)) 2.35 - 2.36 /* like strcat(), but lets the buffer automagically grow :-) 2.37 * (needs local variable "size" with the buffer size) */ 2.38 #define append(where, what) do { \ 2.39 @@ -162,43 +156,36 @@ 2.40 *buf = 0; 2.41 2.42 switch (delta(d)) { 2.43 - case 0: 2.44 - append(buf, "today"); 2.45 - return buf; 2.46 - case 1: 2.47 - append(buf, "tomorrow"); 2.48 - return buf; 2.49 - default: 2.50 - /* like delta(), we ignore the year */ 2.51 - yr=-before(*d,today); 2.52 - mn=d->month - today.month; 2.53 - dy=d->day - today.day; 2.54 + case 0: 2.55 + append(buf, "today"); 2.56 + return buf; 2.57 + case 1: 2.58 + append(buf, "tomorrow"); 2.59 + return buf; 2.60 + default: 2.61 + /* like delta(), we ignore the year */ 2.62 + yr=-before(*d,today); 2.63 + mn=d->month - today.month; 2.64 + dy=d->day - today.day; 2.65 2.66 - if (dy < 0) { 2.67 - dy += mlen(today.month, today.year); 2.68 - mn--; 2.69 - } 2.70 - if (mn < 0) { 2.71 - mn += 12; 2.72 - yr++; 2.73 - } 2.74 + if (dy < 0) { 2.75 + dy += mlen(today.month, today.year); 2.76 + mn--; 2.77 + } 2.78 + if (mn < 0) { 2.79 + mn += 12; 2.80 + yr++; 2.81 + } 2.82 2.83 - wk = (dy/7); 2.84 - dy%=7; 2.85 + wk = (dy / 7); 2.86 + dy %= 7; 2.87 2.88 - append(buf, "in "); 2.89 - tmp = ttime(yr, mn, wk, dy); 2.90 - append(buf, tmp); 2.91 - free(tmp); 2.92 + append(buf, "in "); 2.93 + tmp = ttime(yr, mn, wk, dy); 2.94 + append(buf, tmp); 2.95 + free(tmp); 2.96 2.97 - if (*(buf + strlen(buf) - 1) == 's') 2.98 - append(buf, "'"); 2.99 - else 2.100 - append(buf, "'s"); 2.101 - 2.102 - append(buf, " time"); 2.103 - 2.104 - return buf; 2.105 + return buf; 2.106 } 2.107 } 2.108 2.109 @@ -270,8 +257,7 @@ 2.110 2.111 2.112 2.113 -/* lists the birthdays in their string format, one by one, and passes the string 2.114 - to a function. */ 2.115 +/* lists the birthdays in their string format, one by one, and passes the string to a function. */ 2.116 void liststrings(struct event *evl, prnfunc outf) { 2.117 int i,j; 2.118 char *buf, *tmp; 2.119 @@ -296,8 +282,7 @@ 2.120 if (delta(&(evl[i].date)) <= evl[i].warn) { 2.121 append(buf, evl[i].text); 2.122 append(buf, " for "); 2.123 - /* +1 because, if the difference between two dates is one day, 2.124 - then the length of an event on those days is two days */ 2.125 + /* +1 because, if the difference between two dates is one day, then the length of an event on those days is two days */ 2.126 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; 2.127 tmp = ttime(0, 0, j/7, j%7); 2.128 append(buf, tmp); 2.129 @@ -334,19 +319,6 @@ 2.130 2.131 2.132 2.133 -char* deffname(void) { 2.134 -char buf[256]; 2.135 - 2.136 - strcpy(buf, getpwuid(getuid())->pw_dir); 2.137 - strcat(buf, "/" DEFAULT_FILE); 2.138 - 2.139 - return strdup(buf); 2.140 -} 2.141 - 2.142 - 2.143 - 2.144 - 2.145 - 2.146 2.147 /* sort the events by the time before the next time they come up, putting those 2.148 where the start has passed but we are still in the time-period first */ 2.149 @@ -471,42 +443,24 @@ 2.150 tm = localtime(&t); 2.151 today.day = tm->tm_mday; 2.152 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ 2.153 - today.year = tm->tm_year; 2.154 - today.year += 1900; 2.155 + today.year = tm->tm_year + 1900; 2.156 } 2.157 2.158 2.159 2.160 2.161 2.162 -struct event *readlist(char *fname) { 2.163 - FILE *file; 2.164 - int i,j,k,l,d; 2.165 +struct event* readlist() { 2.166 + int i, j, k, l, d; 2.167 struct event *evl; 2.168 char buf[1024], buf2[1024]; 2.169 char *ptr; 2.170 unsigned flags; 2.171 2.172 /* initialise */ 2.173 - if (fname==NULL) { 2.174 - fname=deffname(); 2.175 - } 2.176 - 2.177 gettoday(); 2.178 2.179 - if (fname[0] == '-' && fname[1] == 0) { 2.180 - /* read from stdin */ 2.181 - file = stdin; 2.182 - } else { 2.183 - /* now read it */ 2.184 - if ((file = fopen(fname, "rt")) == NULL) { 2.185 - fprintf(stderr, "Unable to open file \"%s\"\n", fname); 2.186 - exit(1); 2.187 - } 2.188 - } 2.189 - 2.190 - 2.191 - for (i = 0, evl = NULL; fgets(buf, sizeof(buf), file) != NULL; i++) { 2.192 + for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { 2.193 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 2.194 2.195 /* ignore comments and empty lines */ 2.196 @@ -521,7 +475,7 @@ 2.197 /* not a valid line, so ignore it! Cool, huh? */ 2.198 /* Attention: only recognizes lines without '=' */ 2.199 if (ptr == NULL) { 2.200 - fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); 2.201 + fprintf(stderr, "WARNING: Invalid line in input:\n%s", buf); 2.202 i--; 2.203 continue; 2.204 } 2.205 @@ -624,8 +578,7 @@ 2.206 evl[i].date.year = 0; 2.207 evl[i].text = (char *) NULL; 2.208 2.209 - fclose(file); 2.210 - free(fname); 2.211 + fclose(stdin); 2.212 2.213 /* NB uses i from above */ 2.214 qsort(evl, i, sizeof(struct event), evcmp);
3.1 --- a/birthday.c Mon Dec 17 12:14:41 2007 +0100 3.2 +++ b/birthday.c Mon Dec 17 15:09:03 2007 +0100 3.3 @@ -34,14 +34,10 @@ 3.4 3.5 int main(int argc, char* argv[]) 3.6 { 3.7 - char* fname = NULL; 3.8 struct event *evl; /* evl => event list */ 3.9 3.10 while (--argc > 0 && (*++argv)[0] == '-') { 3.11 - if (strcmp(argv[0], "-f") == 0) { 3.12 - fname = strdup((++argv)[0]); 3.13 - argc--; 3.14 - } else if (strcmp(argv[0], "-W") == 0) { 3.15 + if (strcmp(argv[0], "-W") == 0) { 3.16 iDWarn = atoi((++argv)[0]); 3.17 argc--; 3.18 } else { 3.19 @@ -50,8 +46,7 @@ 3.20 } 3.21 } 3.22 3.23 - evl = readlist(fname); /* read and format entries */ 3.24 - 3.25 + evl = readlist(); /* read and format entries */ 3.26 liststrings(evl, puts); 3.27 3.28 return 0;
4.1 --- a/birthday.h Mon Dec 17 12:14:41 2007 +0100 4.2 +++ b/birthday.h Mon Dec 17 15:09:03 2007 +0100 4.3 @@ -25,16 +25,14 @@ 4.4 $Id: birthday.h,v 1.6 1999/04/25 14:01:29 andy Exp $ 4.5 */ 4.6 4.7 +#include <stdio.h> 4.8 + 4.9 /* ========== Configuration section */ 4.10 4.11 -#define DEFAULT_FILE ".birthdays" 4.12 +/* standard time to warn in advance, when no explicit w flag is given. */ 4.13 +#define DEF_WARN 14 4.14 4.15 -/* standard time to warn in advance, when no explicit w flag is given. */ 4.16 -#define DEF_WARN 21 4.17 4.18 -/* ========== Required includes */ 4.19 - 4.20 -#include <stdio.h> 4.21 4.22 /* ========== Global constants and data types */ 4.23 4.24 @@ -81,7 +79,7 @@ 4.25 4.26 /* ========== */ 4.27 4.28 -struct event *readlist(char *fname); 4.29 +struct event* readlist(void); 4.30 void gettoday(void); 4.31 unsigned delta(struct date *); 4.32 unsigned ddiff(struct date *D1, struct date *D2);