1 /* $ DVCS ID: $jer|,523/lhos,KYTP!41023161\b"?" <<= DO NOT DELETE! */
2 
3 /* ddate.c .. converts boring normal dates to fun Discordian Date -><-
4    written  the 65th day of The Aftermath in the Year of Our Lady of
5    Discord 3157 by Druel the Chaotic aka Jeremy Johnson aka
6    mpython@gnu.ai.mit.edu
7       28 Sever St Apt #3
8       Worcester MA 01609
9 
10    and I'm not responsible if this program messes anything up (except your
11    mind, I'm responsible for that)
12 
13    (k) YOLD 3161 and all time before and after.
14    Reprint, reuse, and recycle what you wish.
15    This program is in the public domain.  Distribute freely.  Or not.
16 
17    Majorly hacked, extended and bogotified/debogotified on
18    Sweetmorn, Bureaucracy 42, 3161 YOLD, by Lee H:. O:. Smith, KYTP,
19    aka Andrew Bulhak, aka acb@dev.null.org
20 
21    Slightly hackled and crackled by a sweet firey stove on
22    Boomtime, the 53rd day of Bureaucracy in the YOLD 3179,
23    by Chaplain Nyan the Wiser, aka Dan Dart, aka ntw@dandart.co.uk
24 
25    and I'm not responsible if this program messes anything up (except your
26    mind, I'm responsible for that) (and that goes for me as well --lhos)
27 
28    Version history:
29    Bureflux 3161:      First release of enhanced ddate with format strings
30    59 Bcy, 3161:       PRAISE_BOB and KILL_BOB options split, other minor
31                        changes.
32    53 Bcy, 3179:       Fixed gregorian date conversions less than YOLD 1167
33 
34    1999-02-22 Arkadiusz Miskiewicz <misiek@pld.ORG.PL>
35    - added Native Language Support
36 
37    2000-03-17 Burt Holzman <holzman+ddate@gmail.com>
38    - added range checks for dates
39 
40    2014-06-07 William Woodruff <william@tuffbizz.com>
41    - removed gettext dependent locale code
42 
43    15th of Confusion, 3180:
44    - call out adherents of the wrong fruit
45 
46    FIVE TONS OF FLAX
47 */
48 
49 /* configuration options  VVVVV   READ THIS!!! */
50 
51 /* If you wish ddate(1) to print the date in the same format as Druel's
52  * original ddate when called in immediate mode, define OLD_IMMEDIATE_FMT
53  */
54 
55 #define OLD_IMMEDIATE_FMT
56 
57 /* If you wish to use the US format for aneristic dates (m-d-y), as opposed to
58  * the Commonwealth format, define US_FORMAT.
59  */
60 
61 /* #define US_FORMAT */
62 
63 /* If you are ideologically, theologically or otherwise opposed to the
64  * Church of the SubGenius and do not wish your copy of ddate(1) to contain
65  * code for counting down to X-Day, undefine KILL_BOB */
66 
67 /*
68 #define KILL_BOB 13013
69 */
70 
71 /* If you wish ddate(1) to contain SubGenius slogans, define PRAISE_BOB */
72 
73 /*#define PRAISE_BOB 13013*/
74 
75 #include <stdlib.h>
76 #include <string.h>
77 #include <time.h>
78 #include <stdio.h>
79 
80 
81 // work around includes and defines from formerly c.h
82 #ifndef ARRAY_SIZE
83 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
84 #endif
85 
86 /* &a[0] degrades to a pointer: a different type from an array */
87 # define __must_be_array(a) \
88 	BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
89 
90 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
91 
92 /* work around hacks for standalone package */
93 #define PACKAGE "ddate"
94 #define PACKAGE_STRING "Stand Alone"
95 
96 #ifndef __GNUC__
97 #define inline /* foo */
98 #endif
99 
100 #ifdef KILL_BOB
101 int xday_countdown(int yday, int year);
102 #endif
103 
104 
105 /* string constants */
106 
107 char *day_long[5] = {
108     "Sweetmorn", "Boomtime", "Pungenday", "Prickle-Prickle", "Setting Orange"
109 };
110 
111 char *day_short[5] = {"SM","BT","PD","PP","SO"};
112 
113 char *season_long[5] = {
114     "Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"
115 };
116 
117 char *season_short[5] = {"Chs", "Dsc", "Cfn", "Bcy", "Afm"};
118 
119 char *holyday[5][2] = {
120     { "Mungday", "Chaoflux" },
121     { "Mojoday", "Discoflux" },
122     { "Syaday",  "Confuflux" },
123     { "Zaraday", "Bureflux" },
124     { "Maladay", "Afflux" }
125 };
126 
127 struct disc_time {
128     int season; /* 0-4 */
129     int day; /* 0-72 */
130     int yday; /* 0-365 */
131     int year; /* 3066- */
132 };
133 
134 char *excl[] = {
135     "Hail Eris!", "All Hail Discordia!", "Kallisti!", "Fnord.", "Or not.",
136     "Wibble.", "Pzat!", "P'tang!", "Frink!",
137 #ifdef PRAISE_BOB
138     "Slack!", "Praise \"Bob\"!", "Or kill me.",
139 #endif /* PRAISE_BOB */
140     /* randomness, from the Net and other places. Feel free to add (after
141        checking with the relevant authorities, of course). */
142     "Grudnuk demand sustenance!", "Keep the Lasagna flying!",
143     "You are what you see.",
144     "Or is it?", "This statement is false.",
145     "Lies and slander, sire!", "Hee hee hee!",
146 #if defined(linux) || defined (__linux__) || defined (__linux)
147     "Hail Eris, Hack Linux!",
148 #elif defined(__APPLE__)
149     "This Fruit is not the True Fruit of Discord.",
150 #endif
151     ""
152 };
153 
154 char default_fmt[] = "%{%A, %B %d%}, %Y YOLD";
155 char *default_immediate_fmt=
156 #ifdef OLD_IMMEDIATE_FMT
157 "Today is %{%A, the %e day of %B%} in the YOLD %Y%N%nCelebrate %H"
158 #else
159 default_fmt
160 #endif
161 ;
162 
163 #define DY(y) (y+1166)
164 
ending(int i)165 static inline char *ending(int i) {
166 	return i/10==1?"th":(i%10==1?"st":(i%10==2?"nd":(i%10==3?"rd":"th")));
167 }
168 
leapp(int i)169 static inline int leapp(int i) {
170 	return (!(DY(i)%4))&&((DY(i)%100)||(!(DY(i)%400)));
171 }
172 
173 /* select a random string */
sel(char ** strings,int num)174 static inline char *sel(char **strings, int num) {
175 	return(strings[random()%num]);
176 }
177 
178 void print(struct disc_time,char **); /* old */
179 void format(char *buf, const char* fmt, struct disc_time dt);
180 /* read a fortune file */
181 int load_fortunes(char *fn, char *delim, char** result);
182 
183 struct disc_time convert(int,int);
184 struct disc_time makeday(int,int,int);
185 
186 int
main(int argc,char * argv[])187 main (int argc, char *argv[]) {
188     time_t t;
189     struct tm *eris;
190     int bob,raw;
191     struct disc_time hastur;
192     char schwa[23*17], *fnord=0;
193     int pi;
194     char *progname, *p;
195 
196     progname = argv[0];
197     if ((p = strrchr(progname, '/')) != NULL)
198 	progname = p+1;
199 
200     srandom(time(NULL));
201     /* do args here */
202     for(pi=1; pi<argc; pi++) {
203 	switch(argv[pi][0]) {
204 	case '+': fnord=argv[pi]+1; break;
205 	case '-':
206 	    switch(argv[pi][1]) {
207 	    case 'V':
208 		printf(("%s (%s)\n"), progname, PACKAGE_STRING);
209 	    default: goto usage;
210 	    }
211 	default: goto thud;
212 	}
213     }
214 
215   thud:
216     if (argc-pi==3){
217 	int moe=atoi(argv[pi]), larry=atoi(argv[pi+1]), curly=atoi(argv[pi+2]);
218 	hastur=makeday(
219 #ifdef US_FORMAT
220 	    moe,larry,
221 #else
222 	    larry,moe,
223 #endif
224 	    curly);
225 	if (hastur.season == -1) {
226 		printf("Invalid date -- out of range\n");
227 		return -1;
228 	}
229 	fnord=fnord?fnord:default_fmt;
230     } else if (argc!=pi) {
231       usage:
232 	fprintf(stderr,("usage: %s [+format] [day month year]\n"), argv[0]);
233 	exit(1);
234     } else {
235 	t= time(NULL);
236 	eris=localtime(&t);
237 	bob=eris->tm_yday; /* days since Jan 1. */
238 	raw=eris->tm_year; /* years since 1980 */
239 	hastur=convert(bob,raw);
240 	fnord=fnord?fnord:default_immediate_fmt;
241     }
242     format(schwa, fnord, hastur);
243     printf("%s\n", schwa);
244 
245     return 0;
246 }
247 
format(char * buf,const char * fmt,struct disc_time dt)248 void format(char *buf, const char* fmt, struct disc_time dt)
249 {
250     int tib_start=-1, tib_end=0;
251     int i, fmtlen=strlen(fmt);
252     char *bufptr=buf;
253 
254 /*    fprintf(stderr, "format(%p, \"%s\", dt)\n", buf, fmt);*/
255 
256     /* first, find extents of St. Tib's Day area, if defined */
257     for(i=0; i<fmtlen; i++) {
258 	if(fmt[i]=='%') {
259 	    switch(fmt[i+1]) {
260 	    case 'A':
261 	    case 'a':
262 	    case 'd':
263 	    case 'e':
264 		if(tib_start>0)	    tib_end=i+1;
265 		else		    tib_start=i;
266 		break;
267 	    case '{': tib_start=i; break;
268 	    case '}': tib_end=i+1; break;
269 	    }
270 	}
271     }
272 
273     /* now do the formatting */
274     buf[0]=0;
275 
276     for(i=0; i<fmtlen; i++) {
277 	if((i==tib_start) && (dt.day==-1)) {
278 	    /* handle St. Tib's Day */
279 	    strcpy(bufptr, ("St. Tib's Day"));
280 	    bufptr += strlen(bufptr);
281 	    i=tib_end;
282 	} else {
283 	    if(fmt[i]=='%') {
284 		char *wibble=0, snarf[23];
285 		switch(fmt[++i]) {
286 		case 'A': wibble=day_long[dt.yday%5]; break;
287 		case 'a': wibble=day_short[dt.yday%5]; break;
288 		case 'B': wibble=season_long[dt.season]; break;
289 		case 'b': wibble=season_short[dt.season]; break;
290 		case 'd': sprintf(snarf, "%d", dt.day+1); wibble=snarf; break;
291 		case 'e': sprintf(snarf, "%d%s", dt.day+1, ending(dt.day+1));
292 		    wibble=snarf; break;
293 		case 'H': if(dt.day==4||dt.day==49)
294 		    wibble=holyday[dt.season][dt.day==49]; break;
295 		case 'N': if(dt.day!=4&&dt.day!=49) goto eschaton; break;
296 		case 'n': *(bufptr++)='\n'; break;
297 		case 't': *(bufptr++)='\t'; break;
298 
299 		case 'Y': sprintf(snarf, "%d", dt.year); wibble=snarf; break;
300 		case '.': wibble=sel(excl, ARRAY_SIZE(excl));
301 		    break;
302 #ifdef KILL_BOB
303 		case 'X': sprintf(snarf, "%d",
304 				  xday_countdown(dt.yday, dt.year));
305 				  wibble = snarf; break;
306 #endif /* KILL_BOB */
307 		}
308 		if(wibble) {
309 /*		    fprintf(stderr, "wibble = (%s)\n", wibble);*/
310 		    strcpy(bufptr, wibble); bufptr+=strlen(wibble);
311 		}
312 	    } else {
313 		*(bufptr++) = fmt[i];
314 	    }
315 	}
316     }
317   eschaton:
318     *(bufptr)=0;
319 }
320 
makeday(int imonth,int iday,int iyear)321 struct disc_time makeday(int imonth,int iday,int iyear) /*i for input */
322 {
323     struct disc_time funkychickens;
324 
325     int cal[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
326     int dayspast=0;
327 
328     memset(&funkychickens,0,sizeof(funkychickens));
329     /* basic range checks */
330     if (imonth < 1 || imonth > 12 || iyear == 0) {
331 	    funkychickens.season = -1;
332 	    return funkychickens;
333     }
334     if (iday < 1 || iday > cal[imonth-1]) {
335 	    if (!(imonth == 2 && iday == 29 && iyear%4 == 0 &&
336 		  (iyear%100 != 0 || iyear%400 == 0))) {
337 		    funkychickens.season = -1;
338 		    return funkychickens;
339 	    }
340     }
341 
342     imonth--;
343     /*  note: gregorian year 0 doesn't exist so
344      *  add one if user specifies a year less than 0 */
345     funkychickens.year= iyear+1166 + ((0 > iyear)?1:0);
346     while(imonth>0) { dayspast+=cal[--imonth]; }
347     funkychickens.day=dayspast+iday-1;
348     funkychickens.season=0;
349     if((funkychickens.year%4)==2) {
350 	if (funkychickens.day==59 && iday==29)  funkychickens.day=-1;
351     }
352     funkychickens.yday=funkychickens.day;
353 /*               note: EQUAL SIGN...hopefully that fixes it */
354     while(funkychickens.day>=73) {
355 	funkychickens.season++;
356 	funkychickens.day-=73;
357     }
358     return funkychickens;
359 }
360 
convert(int nday,int nyear)361 struct disc_time convert(int nday, int nyear)
362 {  struct disc_time funkychickens;
363 
364    funkychickens.year = nyear+3066;
365    funkychickens.day=nday;
366    funkychickens.season=0;
367    if ((funkychickens.year%4)==2)
368      {if (funkychickens.day==59)
369 	funkychickens.day=-1;
370      else if (funkychickens.day >59)
371        funkychickens.day-=1;
372     }
373    funkychickens.yday=funkychickens.day;
374    while (funkychickens.day>=73)
375      { funkychickens.season++;
376        funkychickens.day-=73;
377      }
378    return funkychickens;
379 
380  }
381 
382 #ifdef KILL_BOB
383 
384 /* Code for counting down to X-Day, X-Day being Cfn 40, 3164
385  *
386  * After `X-Day' passed without incident, the CoSG declared that it had
387  * got the year upside down --- X-Day is actually in 8661 AD rather than
388  * 1998 AD.
389  *
390  * Thus, the True X-Day is Cfn 40, 9827.
391  *
392  */
393 
xday_countdown(int yday,int year)394 int xday_countdown(int yday, int year) {
395     int r=(185-yday)+(((yday<59)&&(leapp(year)))?1:0);
396     while(year<9827) r+=(leapp(++year)?366:365);
397     while(year>9827) r-=(leapp(year--)?366:365);
398     return r;
399 }
400 
401 #endif
402