bday
diff bdengine.c @ 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 |
line diff
1.1 --- a/bdengine.c Mon Dec 17 12:14:41 2007 +0100 1.2 +++ b/bdengine.c Mon Dec 17 15:09:03 2007 +0100 1.3 @@ -67,7 +67,6 @@ 1.4 1.5 #include <sys/types.h> 1.6 #include <unistd.h> 1.7 -#include <pwd.h> 1.8 1.9 #include "birthday.h" 1.10 1.11 @@ -76,7 +75,7 @@ 1.12 1.13 1.14 /* 1.15 - xmalloc/xrealloc functions, and fatal exit function 1.16 + xmalloc/xrealloc functions 1.17 Note: the x* functions are lifted straight from the GNU libc info docs 1.18 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ 1.19 */ 1.20 @@ -109,7 +108,6 @@ 1.21 1.22 int skptok(int j, char *ptr); 1.23 int evcmp(const void *e1, const void *e2); 1.24 -char *deffname(void); 1.25 1.26 1.27 /* ========== Global variables */ 1.28 @@ -136,10 +134,6 @@ 1.29 1.30 1.31 1.32 - 1.33 -/* compare the first strlen(a) characters of a and b */ 1.34 -#define strbegcmp(a,b) strncmp(a,b,strlen(a)) 1.35 - 1.36 /* like strcat(), but lets the buffer automagically grow :-) 1.37 * (needs local variable "size" with the buffer size) */ 1.38 #define append(where, what) do { \ 1.39 @@ -162,43 +156,36 @@ 1.40 *buf = 0; 1.41 1.42 switch (delta(d)) { 1.43 - case 0: 1.44 - append(buf, "today"); 1.45 - return buf; 1.46 - case 1: 1.47 - append(buf, "tomorrow"); 1.48 - return buf; 1.49 - default: 1.50 - /* like delta(), we ignore the year */ 1.51 - yr=-before(*d,today); 1.52 - mn=d->month - today.month; 1.53 - dy=d->day - today.day; 1.54 + case 0: 1.55 + append(buf, "today"); 1.56 + return buf; 1.57 + case 1: 1.58 + append(buf, "tomorrow"); 1.59 + return buf; 1.60 + default: 1.61 + /* like delta(), we ignore the year */ 1.62 + yr=-before(*d,today); 1.63 + mn=d->month - today.month; 1.64 + dy=d->day - today.day; 1.65 1.66 - if (dy < 0) { 1.67 - dy += mlen(today.month, today.year); 1.68 - mn--; 1.69 - } 1.70 - if (mn < 0) { 1.71 - mn += 12; 1.72 - yr++; 1.73 - } 1.74 + if (dy < 0) { 1.75 + dy += mlen(today.month, today.year); 1.76 + mn--; 1.77 + } 1.78 + if (mn < 0) { 1.79 + mn += 12; 1.80 + yr++; 1.81 + } 1.82 1.83 - wk = (dy/7); 1.84 - dy%=7; 1.85 + wk = (dy / 7); 1.86 + dy %= 7; 1.87 1.88 - append(buf, "in "); 1.89 - tmp = ttime(yr, mn, wk, dy); 1.90 - append(buf, tmp); 1.91 - free(tmp); 1.92 + append(buf, "in "); 1.93 + tmp = ttime(yr, mn, wk, dy); 1.94 + append(buf, tmp); 1.95 + free(tmp); 1.96 1.97 - if (*(buf + strlen(buf) - 1) == 's') 1.98 - append(buf, "'"); 1.99 - else 1.100 - append(buf, "'s"); 1.101 - 1.102 - append(buf, " time"); 1.103 - 1.104 - return buf; 1.105 + return buf; 1.106 } 1.107 } 1.108 1.109 @@ -270,8 +257,7 @@ 1.110 1.111 1.112 1.113 -/* lists the birthdays in their string format, one by one, and passes the string 1.114 - to a function. */ 1.115 +/* lists the birthdays in their string format, one by one, and passes the string to a function. */ 1.116 void liststrings(struct event *evl, prnfunc outf) { 1.117 int i,j; 1.118 char *buf, *tmp; 1.119 @@ -296,8 +282,7 @@ 1.120 if (delta(&(evl[i].date)) <= evl[i].warn) { 1.121 append(buf, evl[i].text); 1.122 append(buf, " for "); 1.123 - /* +1 because, if the difference between two dates is one day, 1.124 - then the length of an event on those days is two days */ 1.125 + /* +1 because, if the difference between two dates is one day, then the length of an event on those days is two days */ 1.126 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; 1.127 tmp = ttime(0, 0, j/7, j%7); 1.128 append(buf, tmp); 1.129 @@ -334,19 +319,6 @@ 1.130 1.131 1.132 1.133 -char* deffname(void) { 1.134 -char buf[256]; 1.135 - 1.136 - strcpy(buf, getpwuid(getuid())->pw_dir); 1.137 - strcat(buf, "/" DEFAULT_FILE); 1.138 - 1.139 - return strdup(buf); 1.140 -} 1.141 - 1.142 - 1.143 - 1.144 - 1.145 - 1.146 1.147 /* sort the events by the time before the next time they come up, putting those 1.148 where the start has passed but we are still in the time-period first */ 1.149 @@ -471,42 +443,24 @@ 1.150 tm = localtime(&t); 1.151 today.day = tm->tm_mday; 1.152 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ 1.153 - today.year = tm->tm_year; 1.154 - today.year += 1900; 1.155 + today.year = tm->tm_year + 1900; 1.156 } 1.157 1.158 1.159 1.160 1.161 1.162 -struct event *readlist(char *fname) { 1.163 - FILE *file; 1.164 - int i,j,k,l,d; 1.165 +struct event* readlist() { 1.166 + int i, j, k, l, d; 1.167 struct event *evl; 1.168 char buf[1024], buf2[1024]; 1.169 char *ptr; 1.170 unsigned flags; 1.171 1.172 /* initialise */ 1.173 - if (fname==NULL) { 1.174 - fname=deffname(); 1.175 - } 1.176 - 1.177 gettoday(); 1.178 1.179 - if (fname[0] == '-' && fname[1] == 0) { 1.180 - /* read from stdin */ 1.181 - file = stdin; 1.182 - } else { 1.183 - /* now read it */ 1.184 - if ((file = fopen(fname, "rt")) == NULL) { 1.185 - fprintf(stderr, "Unable to open file \"%s\"\n", fname); 1.186 - exit(1); 1.187 - } 1.188 - } 1.189 - 1.190 - 1.191 - for (i = 0, evl = NULL; fgets(buf, sizeof(buf), file) != NULL; i++) { 1.192 + for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { 1.193 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 1.194 1.195 /* ignore comments and empty lines */ 1.196 @@ -521,7 +475,7 @@ 1.197 /* not a valid line, so ignore it! Cool, huh? */ 1.198 /* Attention: only recognizes lines without '=' */ 1.199 if (ptr == NULL) { 1.200 - fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); 1.201 + fprintf(stderr, "WARNING: Invalid line in input:\n%s", buf); 1.202 i--; 1.203 continue; 1.204 } 1.205 @@ -624,8 +578,7 @@ 1.206 evl[i].date.year = 0; 1.207 evl[i].text = (char *) NULL; 1.208 1.209 - fclose(file); 1.210 - free(fname); 1.211 + fclose(stdin); 1.212 1.213 /* NB uses i from above */ 1.214 qsort(evl, i, sizeof(struct event), evcmp);