meillo@0: /* meillo@0: birthday.c meillo@0: meillo@0: Birthday/Anniversary display on login meillo@0: meillo@0: (c) 1996 AS Mortimer meillo@0: meillo@0: This program is free software; you can redistribute it and/or meillo@0: modify it under the terms of the GNU General Public License as meillo@0: published by the Free Software Foundation; either version 2 of the meillo@0: License, or (at your option) any later version. You may also meillo@0: distribute it under the Artistic License, as comes with Perl. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA meillo@0: meillo@0: You should also have recieved a copy of the Artistic license with meillo@0: this program. meillo@0: meillo@0: $Id: bdengine.c,v 1.14 2001/10/21 07:03:49 andy Exp $ meillo@0: meillo@0: We're getting there. At the moment, the file used by default is ~/.birthdays meillo@0: under UNIX, or C:\PERSONAL\BDAYS.LST under DOS, but this can be overridden on meillo@0: the command line. The file has the following format: meillo@0: meillo@0: name/event/whatever=date flags meillo@0: where: meillo@0: date is dd/mm, dd/mm/yy (assumes 20th century!) or dd/mm/yyyy meillo@0: flags is ONE or ZERO of meillo@0: o bd for a birthday (default) meillo@0: o bir for a birthday (exactly equivalent to `bd') meillo@0: o ann for an anniversary meillo@0: o ev for an event meillo@0: and zero or more of meillo@0: o w to set the warn-in-advance time to n days (don't include the meillo@0: brackets! :) meillo@0: o to meillo@0: o for meillo@0: to specify the length of time taken by an event, for example a meillo@0: holiday. meillo@0: meillo@0: Comment lines are preceeded by #. meillo@0: meillo@0: Note: If you deviate from this format, I cannot guarantee anything about meillo@0: it's behaviour. In most cases, it will just quietly ignore the error, meillo@0: which probably isn't ideal behaviour. Oh, well. meillo@0: meillo@0: 2003/05/20: Automatic reallocation of output buffer in listsrings() by meillo@0: Sebastian Schmidt . meillo@0: meillo@0: */ meillo@0: meillo@0: meillo@0: meillo@0: /* ========== */ meillo@0: meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@0: #include "birthday.h" meillo@0: meillo@0: /* ========== */ meillo@0: meillo@0: meillo@0: meillo@0: /* meillo@0: xmalloc/xrealloc functions, and fatal exit function meillo@0: Note: the x* functions are lifted straight from the GNU libc info docs meillo@0: $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ meillo@0: */ meillo@0: meillo@3: void* xmalloc (size_t size) { meillo@3: register void* value = malloc (size); meillo@0: if (value == 0) { meillo@0: fprintf(stderr, "virtual memory exhausted\n"); meillo@0: exit(1); meillo@0: } meillo@0: return value; meillo@0: } meillo@0: meillo@0: meillo@3: void* xrealloc (void* ptr, size_t size) { meillo@3: register void* value = realloc (ptr, size); meillo@0: if (value == 0) { meillo@0: fprintf(stderr, "virtual memory exhausted\n"); meillo@0: exit(1); meillo@0: } meillo@0: return value; meillo@0: } meillo@0: meillo@0: /* ========== */ meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: int skptok(int j, char *ptr); meillo@0: int evcmp(const void *e1, const void *e2); meillo@0: char *deffname(void); meillo@0: meillo@0: meillo@0: /* ========== Global variables */ meillo@0: meillo@0: struct date today; meillo@0: int iDWarn = DEF_WARN; meillo@0: meillo@0: const unsigned MLENDAT[]={31,-1,31,30,31,30,31,31,30,31,30,31}; meillo@0: meillo@0: const struct _ftable FTABLE[] = { meillo@0: {"bir",F_TBIRTHDAY}, meillo@0: {"bd", F_TBIRTHDAY}, meillo@0: {"ann",F_TANNIVERSARY}, meillo@0: {"ev", F_TEVENT}, meillo@0: {"mes", F_TMESSAGE}, meillo@0: {"w", F_WTIME_P}, meillo@0: {"to", F_TODATE}, meillo@0: {"for", F_FORDAYS}, meillo@0: {NULL, 0} meillo@0: }; meillo@0: meillo@0: meillo@0: meillo@0: /* ========== */ meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: /* compare the first strlen(a) characters of a and b */ meillo@0: #define strbegcmp(a,b) strncmp(a,b,strlen(a)) meillo@0: meillo@0: /* like strcat(), but lets the buffer automagically grow :-) meillo@0: * (needs local variable "size" with the buffer size) */ meillo@0: #define append(where, what) do { \ meillo@0: if (strlen(what) > (size - strlen(where))) { \ meillo@0: xrealloc(where, size + 128 + strlen(what)); \ meillo@0: size += 128 + strlen(what); \ meillo@0: } \ meillo@0: strcat(where, what); \ meillo@0: } while(0) meillo@0: meillo@0: /* ========== */ meillo@0: meillo@0: /* returns delta(d) in days, weeks, months, etc meillo@0: * the returned buffer is malloc()ed, do not forget to free() it */ meillo@0: char *tdelta(struct date *d) { meillo@0: int dy, wk, mn, yr; meillo@0: char *tmp; meillo@0: char *buf = xmalloc(128); meillo@0: int size = 128; meillo@0: *buf = 0; meillo@0: meillo@0: switch (delta(d)) { meillo@0: case 0: meillo@0: append(buf, "today"); meillo@0: return buf; meillo@0: case 1: meillo@0: append(buf, "tomorrow"); meillo@0: return buf; meillo@0: default: meillo@0: /* like delta(), we ignore the year */ meillo@0: yr=-before(*d,today); meillo@0: mn=d->month - today.month; meillo@0: dy=d->day - today.day; meillo@0: meillo@0: if (dy < 0) { meillo@0: dy += mlen(today.month, today.year); meillo@0: mn--; meillo@0: } meillo@0: if (mn < 0) { meillo@0: mn += 12; meillo@0: yr++; meillo@0: } meillo@0: meillo@0: wk = (dy/7); meillo@0: dy%=7; meillo@0: meillo@0: append(buf, "in "); meillo@0: tmp = ttime(yr, mn, wk, dy); meillo@0: append(buf, tmp); meillo@0: free(tmp); meillo@0: meillo@0: if (*(buf + strlen(buf) - 1) == 's') meillo@0: append(buf, "'"); meillo@0: else meillo@0: append(buf, "'s"); meillo@0: meillo@0: append(buf, " time"); meillo@0: meillo@0: return buf; meillo@0: } meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: /* meillo@0: void donum(n,txt) { meillo@0: do { meillo@0: if (n > 0) { meillo@0: snprintf(tmp, sizeof(tmp), "%d", n); meillo@0: append(buf, tmp); meillo@0: append(buf, " " txt); meillo@0: if (n != 1) meillo@0: append(buf, "s"); meillo@0: terms--; meillo@0: if (orgterms > 1) { meillo@0: if (terms == 1) meillo@0: append(buf, " and "); meillo@0: else if (terms > 1) meillo@0: append(buf, ", "); meillo@0: } meillo@0: } meillo@0: } while(0) meillo@0: } meillo@0: */ meillo@0: meillo@0: meillo@0: #define donum(n,txt) do { \ meillo@0: if (n > 0) { \ meillo@0: snprintf(tmp, sizeof(tmp), "%d", n); \ meillo@0: append(buf, tmp); \ meillo@0: append(buf, " " txt); \ meillo@0: if (n != 1) \ meillo@0: append(buf, "s"); \ meillo@0: terms--; \ meillo@0: if (orgterms > 1) { \ meillo@0: if (terms == 1) \ meillo@0: append(buf, " and "); \ meillo@0: else if (terms > 1) \ meillo@0: append(buf, ", "); \ meillo@0: } \ meillo@0: } \ meillo@0: } while(0) meillo@0: meillo@0: meillo@0: /* returns allocated buffer, don't forget to free() */ meillo@0: char* ttime(int yr, int mn, int wk, int dy) { meillo@0: char* buf = xmalloc(128); meillo@0: int size = 128; meillo@0: int terms, orgterms; meillo@0: char tmp[128]; meillo@0: meillo@0: *buf = 0; /* Initialize buffer */ meillo@0: terms = orgterms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0); meillo@0: meillo@0: donum(yr, "year"); meillo@0: donum(mn, "month"); meillo@0: donum(wk, "week"); meillo@0: donum(dy, "day"); meillo@0: meillo@0: return buf; meillo@0: } meillo@0: #undef donum meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: /* lists the birthdays in their string format, one by one, and passes the string meillo@0: to a function. */ meillo@0: void liststrings(struct event *evl, prnfunc outf) { meillo@0: int i,j; meillo@0: char *buf, *tmp; meillo@0: int size; meillo@0: meillo@0: for (i = 0; evl[i].text != NULL; i++) { meillo@0: buf = xmalloc(128); meillo@0: *buf = '\0'; meillo@0: size = 128; meillo@0: meillo@0: if (evl[i].warn == -1 && delta(&(evl[i].date))==0) { meillo@0: append(buf, evl[i].text); meillo@0: } else if (evl[i].enddate.day == 0) { meillo@3: if (delta(&(evl[i].date)) <= evl[i].warn) { meillo@0: append(buf, evl[i].text); meillo@0: append(buf, " "); meillo@0: tmp = tdelta(&(evl[i].date)); meillo@0: append(buf, tmp); meillo@0: free(tmp); meillo@0: } meillo@0: } else { meillo@3: if (delta(&(evl[i].date)) <= evl[i].warn) { meillo@0: append(buf, evl[i].text); meillo@0: append(buf, " for "); meillo@0: /* +1 because, if the difference between two dates is one day, meillo@0: then the length of an event on those days is two days */ meillo@0: j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; meillo@0: tmp = ttime(0, 0, j/7, j%7); meillo@0: append(buf, tmp); meillo@0: free(tmp); meillo@0: append(buf, " "); meillo@0: tmp = tdelta(&(evl[i].date)); meillo@0: append(buf, tmp); meillo@3: } else if (delta(&(evl[i].enddate)) <= evl[i].warn) { meillo@0: append(buf, evl[i].text); meillo@0: append(buf, " "); meillo@0: j = delta(&(evl[i].enddate)); meillo@0: if (j) { meillo@0: append(buf, "for "); meillo@0: tmp = ttime(0, 0, j/7, j%7); meillo@0: append(buf, tmp); meillo@0: free(tmp); meillo@0: append(buf, " longer"); meillo@0: } else { meillo@0: append(buf, "finishes today"); meillo@0: } meillo@0: } meillo@0: } meillo@0: if (*buf) { meillo@0: append(buf, "."); meillo@0: outf(buf); meillo@0: } meillo@0: free(buf); meillo@0: } meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: char* deffname(void) { meillo@0: char buf[256]; meillo@0: meillo@2: strcpy(buf, getpwuid(getuid())->pw_dir); meillo@0: strcat(buf, "/" DEFAULT_FILE); meillo@0: meillo@0: return strdup(buf); meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: /* sort the events by the time before the next time they come up, putting those meillo@0: where the start has passed but we are still in the time-period first */ meillo@0: int evcmp(const void *p1, const void *p2) { meillo@0: struct event *e1=(struct event *)p1; meillo@0: struct event *e2=(struct event *)p2; meillo@0: unsigned d1,d2; meillo@0: meillo@0: /* if the delta for the enddate is less than that for the start date, then we meillo@0: have passed the start date but not yet the end date, and so we should meillo@0: display the enddate; otherwise, we should display the start date */ meillo@0: meillo@0: d1=delta(&(e1->date)); meillo@0: if (e1->enddate.day && delta(&(e1->enddate)) < d1) meillo@0: d1=delta(&(e1->enddate)); meillo@0: meillo@0: d2=delta(&(e2->date)); meillo@0: if (e2->enddate.day && delta(&(e2->enddate)) < d2) meillo@0: d2=delta(&(e2->enddate)); meillo@0: meillo@0: if (d1 < d2) return -1; meillo@0: if (d1 > d2) return 1; meillo@0: meillo@0: return strcmp(e1->text, e2->text); meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: /* difference in days between two dates */ meillo@0: /* it is assumed that D1 < D2, and so the result is always positive */ meillo@0: unsigned ddiff(struct date *D1, struct date *D2) { meillo@0: struct date d1,d2; meillo@0: int dd,m; meillo@0: meillo@0: /* make working copies */ meillo@0: d1=*D1; meillo@0: d2=*D2; meillo@0: meillo@0: /* sort out zero years */ meillo@0: if (d1.year == 0 || d2.year==0) { meillo@0: if (d1.year != d2.year) { meillo@0: if (d1.year == 0) { meillo@0: if (before(d1,d2)) meillo@0: d1.year=d2.year; meillo@0: else meillo@0: d1.year=d2.year-1; meillo@0: } else { meillo@0: if (before(d1,d2)) meillo@0: d2.year=d1.year; meillo@0: else meillo@0: d2.year=d1.year+1; meillo@0: } meillo@0: } else { /* both years zero */ meillo@0: if (before(d1,d2)) meillo@0: d1.year=d2.year=today.year; meillo@0: else { meillo@0: d1.year=today.year; meillo@0: d2.year=d1.year+1; meillo@0: } meillo@0: } meillo@0: } meillo@0: meillo@0: /* now we can actually do the comparison ... */ meillo@0: dd=0; meillo@0: meillo@0: /* to start with, we work in months */ meillo@0: for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) meillo@0: dd += mlen(((m-1)%12)+1, d1.year + m/12); meillo@0: meillo@0: /* and then we renormalise for the days within the months */ meillo@0: /* the first month was included in our calculations */ meillo@0: dd -= d1.day; meillo@0: /* but the last one wasn't */ meillo@0: dd += d2.day; meillo@0: meillo@0: return dd; meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: /* actually until the next anniversary of ... */ meillo@0: unsigned delta(struct date *date) { meillo@0: struct date d; meillo@0: unsigned dt, mn; meillo@0: meillo@0: memcpy(&d, date, sizeof(struct date)); meillo@0: meillo@0: /* past the end of the year */ meillo@0: if (before(d, today)) { meillo@0: d.year = 1; meillo@0: } else { meillo@0: d.year = 0; meillo@0: } meillo@0: meillo@0: for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) meillo@0: dt += mlen(((mn-1)%12) + 1,today.year + mn/12); meillo@0: meillo@0: dt -= today.day; meillo@0: dt += d.day; meillo@0: meillo@0: return dt; meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: void gettoday(void) { meillo@0: struct tm *tm; meillo@0: time_t t; meillo@0: meillo@0: time(&t); meillo@0: tm = localtime(&t); meillo@0: today.day = tm->tm_mday; meillo@0: today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ meillo@0: today.year = tm->tm_year; meillo@0: today.year += 1900; meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: struct event *readlist(char *fname) { meillo@0: FILE *file; meillo@0: int i,j,k,l,d; meillo@0: struct event *evl; meillo@0: char buf[1024], buf2[1024]; meillo@0: char *ptr; meillo@0: unsigned flags; meillo@2: meillo@0: /* initialise */ meillo@0: if (fname==NULL) { meillo@0: fname=deffname(); meillo@0: } meillo@0: meillo@0: gettoday(); meillo@0: meillo@0: if (fname[0] == '-' && fname[1] == 0) { meillo@0: /* read from stdin */ meillo@0: file=stdin; meillo@0: } else { meillo@0: /* now read it */ meillo@0: if((file=fopen(fname, "rt"))==NULL) { meillo@0: fprintf(stderr, "Unable to open file \"%s\"\n", fname); meillo@0: exit(1); meillo@0: } meillo@0: } meillo@0: meillo@0: meillo@0: for (i = 0, evl=NULL; fgets(buf, sizeof(buf), file) != NULL; i++) { meillo@0: evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); meillo@0: meillo@2: /* ignore comments and empty lines */ meillo@2: if (*buf == '#' || *buf == '\n') { meillo@2: i--; meillo@0: continue; meillo@0: } meillo@0: meillo@0: /* parse string in buf */ meillo@0: ptr = strrchr(buf, '='); /* allow '=' in text */ meillo@2: meillo@2: /* not a valid line, so ignore it! Cool, huh? */ meillo@2: if (ptr == NULL) { meillo@2: fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); meillo@2: i--; meillo@2: continue; meillo@2: } meillo@0: meillo@0: *(ptr++) = 0; meillo@0: meillo@0: j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); meillo@0: /* if our year is only two digits, add 1900 to it ... */ meillo@0: if(evl[i].date.year < 100) evl[i].date.year+=1900; meillo@0: /* ... unless it wasn't read, in which case set it to zero */ meillo@0: if(j==2) evl[i].date.year=0; meillo@0: meillo@0: /* parse flags */ meillo@0: meillo@0: evl[i].warn=iDWarn; meillo@0: evl[i].enddate.day=evl[i].enddate.month=evl[i].enddate.year=0; meillo@0: meillo@0: flags=j=0; meillo@2: while(j = skptok(j, ptr),ptr[j]!=0) { meillo@2: for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++); meillo@2: switch (FTABLE[k].flag) { meillo@0: case F_WTIME_P: /* w -- sets warning time */ meillo@0: sscanf(ptr + j, "w %u", &(evl[i].warn)); meillo@0: break; meillo@0: case F_FORDAYS: /* for -- sets the duration of the event */ meillo@0: sscanf(ptr + j, "for %d", &d); meillo@0: evl[i].enddate=evl[i].date; meillo@0: for (l=1; l < d; l++) { meillo@0: evl[i].enddate.day++; meillo@0: if (evl[i].enddate.day > mlen(evl[i].enddate.month, meillo@0: evl[i].enddate.year)) { meillo@0: evl[i].enddate.month++; meillo@0: evl[i].enddate.day=1; meillo@0: } meillo@0: if (evl[i].enddate.month > 12) { meillo@0: evl[i].enddate.year++; meillo@0: evl[i].enddate.month=1; meillo@0: } meillo@0: } meillo@0: break; meillo@0: case F_TODATE: meillo@0: l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); meillo@2: if (evl[i].enddate.year < 100) { meillo@2: evl[i].enddate.year+=1900; meillo@2: } meillo@2: if (l == 2) { meillo@2: evl[i].enddate.year=0; meillo@2: } meillo@0: break; meillo@0: case 0: meillo@0: break; meillo@0: default: meillo@0: flags|=FTABLE[k].flag; meillo@0: break; meillo@0: } meillo@2: } meillo@2: meillo@0: meillo@0: /* construct event text */ meillo@0: meillo@0: switch(flags & F_MTYPE) { meillo@2: case F_TBIRTHDAY: meillo@2: default: /* assume it's a birthday */ meillo@2: if (evl[i].date.year != 0) { meillo@2: int tmp_age=ydelta(evl[i].date, today); meillo@2: if (tmp_age!=1) { meillo@2: sprintf(buf2, "%s is %d years old", buf, tmp_age); meillo@2: } else { meillo@2: sprintf(buf2, "%s is %d year old", buf, tmp_age); meillo@2: } meillo@0: } else { meillo@2: sprintf(buf2, "%s has a birthday", buf); meillo@0: } meillo@2: break; meillo@2: case F_TANNIVERSARY: meillo@2: if (evl[i].date.year != 0) { meillo@2: sprintf(buf2, "%s %d years ago", buf, ydelta(evl[i].date, today)); meillo@2: } else { meillo@2: strcpy(buf2, buf); meillo@2: } meillo@2: break; meillo@2: case F_TEVENT: meillo@2: /* if a year was specified, and this warning isn't for it, ignore! */ meillo@2: if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) && meillo@2: (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) { meillo@2: i--; meillo@2: continue; meillo@2: } meillo@0: strcpy(buf2, buf); meillo@2: break; meillo@2: case F_TMESSAGE: meillo@2: /* Like an event, except that it only comes up on the given date, and no text at all is appended */ meillo@2: if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) && meillo@2: (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) { meillo@2: i--; meillo@2: continue; meillo@2: } meillo@2: strcpy(buf2, buf); meillo@2: evl[i].warn=-1; /* special code! */ meillo@2: break; meillo@0: } meillo@0: evl[i].text = strdup(buf2); meillo@0: } meillo@0: meillo@0: evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); meillo@0: evl[i].date.day=evl[i].date.month=evl[i].date.year=0; meillo@0: evl[i].text = (char *) NULL; meillo@0: meillo@0: fclose(file); meillo@0: free(fname); meillo@0: meillo@0: /* NB uses i from above */ meillo@0: qsort(evl, i, sizeof(struct event), evcmp); meillo@0: return evl; meillo@0: } meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: meillo@0: int skptok(int j, char *ptr) { meillo@0: for (; ptr[j] != 0 && ptr[j] != ' ' && ptr[j] != '\t' ; j++); meillo@0: for (; ptr[j] != 0 && (ptr[j] == ' ' || ptr[j] == '\t'); j++); meillo@0: meillo@0: return j; meillo@0: } meillo@0: meillo@0: