]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ddate/ddate.c
Fixes for cygstart; bump version number and documentation
[cygwin-apps/cygutils.git] / src / ddate / ddate.c
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 /*
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 * See the COPYING file for license information.
26 */
27
28 /*
29 and I'm not responsible if this program messes anything up (except your
30 mind, I'm responsible for that)
31
32 (k) YOLD 3161 and all time before and after.
33 Reprint, reuse, and recycle what you wish.
34 This program is in the public domain. Distribute freely. Or not.
35 (*) Charles Wilson changes under GPL
36
37 Majorly hacked, extended and bogotified/debogotified on
38 Sweetmorn, Bureaucracy 42, 3161 YOLD, by Lee H:. O:. Smith, KYTP,
39 aka Andrew Bulhak, aka acb@dev.null.org
40
41 and I'm not responsible if this program messes anything up (except your
42 mind, I'm responsible for that) (and that goes for me as well --lhos)
43
44 Version history:
45 Bureflux 3161: First release of enhanced ddate with format strings
46 59 Bcy, 3161: PRAISE_BOB and KILL_BOB options split, other minor
47 changes.
48
49 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
50 - added Native Language Support
51
52 2000-03-17 Burt Holzman <bnh@iname.com>
53 - added range checks for dates
54
55 2002-01-26 Charles Wilson
56 - minor changes so that it compiles on cygwin
57 - removed nls.h dependency (but retain NLS capabiltiy)
58 - changes (c) 2002 Charles Wilson. Licensed under the GPL.
59 */
60
61 /* configuration options VVVVV READ THIS!!! */
62
63 /* If you wish ddate(1) to print the date in the same format as Druel's
64 * original ddate when called in immediate mode, define OLD_IMMEDIATE_FMT
65 */
66
67 #define OLD_IMMEDIATE_FMT
68
69 /* If you wish to use the US format for aneristic dates (m-d-y), as opposed to
70 * the Commonwealth format, define US_FORMAT.
71 */
72
73 /* #define US_FORMAT */
74
75 /* If you are ideologically, theologically or otherwise opposed to the
76 * Church of the SubGenius and do not wish your copy of ddate(1) to contain
77 * code for counting down to X-Day, undefine KILL_BOB */
78
79 #define KILL_BOB 13013
80
81 /* If you wish ddate(1) to contain SubGenius slogans, define PRAISE_BOB */
82
83 /*#define PRAISE_BOB 13013*/
84 #if HAVE_CONFIG_H
85 # include "config.h"
86 #endif
87 #include "common.h"
88
89 #ifndef PACKAGE
90 #define PACKAGE "cygutils"
91 #endif
92
93 #ifndef LOCALEDIR
94 #define LOCALEDIR "/usr/share/locale"
95 #endif
96
97 #ifdef HAVE_LOCALE_H
98 # include <locale.h>
99 #endif
100 #if !HAVE_SETLOCALE
101 # undef setlocale(a, b)
102 # define setlocale(a, b) /* empty */
103 #endif
104
105 #ifdef ENABLE_NLS
106 # include <libintl.h>
107 # define _(Text) gettext (Text)
108 # ifdef gettext_noop
109 # define N_(String) gettext_noop (String)
110 # else
111 # define N_(String) (String)
112 # endif
113 #else
114 # undef bindtextdomain
115 # define bindtextdomain(Domain, Directory) /* empty */
116 # undef textdomain
117 # define textdomain(Domain) /* empty */
118 # define _(Text) (Text)
119 # define N_(Text) (Text)
120 #endif
121
122 #ifndef __GNUC__
123 #define inline /* foo */
124 #endif
125
126 #ifdef KILL_BOB
127 int xday_countdown(int yday, int year);
128 #endif
129
130
131 /* string constants */
132
133 char *day_long[5] = {
134 "Sweetmorn", "Boomtime", "Pungenday", "Prickle-Prickle", "Setting Orange"
135 };
136
137 char *day_short[5] = {"SM","BT","PD","PP","SO"};
138
139 char *season_long[5] = {
140 "Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"
141 };
142
143 char *season_short[5] = {"Chs", "Dsc", "Cfn", "Bcy", "Afm"};
144
145 char *holyday[5][2] = {
146 { "Mungday", "Chaoflux" },
147 { "Mojoday", "Discoflux" },
148 { "Syaday", "Confuflux" },
149 { "Zaraday", "Bureflux" },
150 { "Maladay", "Afflux" }
151 };
152
153 struct disc_time {
154 int season; /* 0-4 */
155 int day; /* 0-72 */
156 int yday; /* 0-365 */
157 int year; /* 3066- */
158 };
159
160 char *excl[] = {
161 "Hail Eris!", "All Hail Discordia!", "Kallisti!", "Fnord.", "Or not.",
162 "Wibble.", "Pzat!", "P'tang!", "Frink!",
163 #ifdef PRAISE_BOB
164 "Slack!", "Praise \"Bob\"!", "Or kill me.",
165 #endif /* PRAISE_BOB */
166 /* randomness, from the Net and other places. Feel free to add (after
167 checking with the relevant authorities, of course). */
168 "Grudnuk demand sustenance!", "Keep the Lasagna flying!",
169 "Umlaut Zebra über alles!", "You are what you see.",
170 "Or is it?", "This statement is false.",
171 #if defined(linux) || defined (__linux__) || defined (__linux)
172 "Hail Eris, Hack Linux!",
173 #endif
174 ""
175 };
176
177 char default_fmt[] = "%{%A, %B %d%}, %Y YOLD";
178 char *default_immediate_fmt=
179 #ifdef OLD_IMMEDIATE_FMT
180 "Today is %{%A, the %e day of %B%} in the YOLD %Y%N%nCelebrate %H"
181 #else
182 default_fmt
183 #endif
184 ;
185
186 #define DY(y) (y+1166)
187
188 static inline char *ending(int i) {
189 return (i%10==1)?"st":(i%10==2?"nd":(i%10==3?"rd":"th"));
190 }
191
192 static inline int leapp(int i) {
193 return (!(DY(i)%4))&&((DY(i)%100)||(!(DY(i)%400)));
194 }
195
196 /* select a random string */
197 static inline char *sel(char **strings, int num) {
198 return(strings[random()%num]);
199 }
200
201 void print(struct disc_time,char **); /* old */
202 void format(char *buf, const char* fmt, struct disc_time dt);
203 /* read a fortune file */
204 int load_fortunes(char *fn, char *delim, char** result);
205
206 struct disc_time convert(int,int);
207 struct disc_time makeday(int,int,int);
208
209 int
210 main (int argc, char *argv[]) {
211 long t;
212 struct tm *eris;
213 int bob,raw;
214 struct disc_time hastur;
215 char schwa[23*17], *fnord=0;
216 int pi;
217 char *progname, *p;
218
219 progname = argv[0];
220 if ((p = strrchr(progname, '/')) != NULL)
221 progname = p+1;
222
223 setlocale(LC_ALL, "");
224 bindtextdomain(PACKAGE, LOCALEDIR);
225 textdomain(PACKAGE);
226
227 srandom(time(NULL));
228 /* do args here */
229 for(pi=1; pi<argc; pi++) {
230 switch(argv[pi][0]) {
231 case '+': fnord=argv[pi]+1; break;
232 case '-':
233 switch(argv[pi][1]) {
234 case 'V':
235 printf(_("%s from %s version %s\n"), progname, PACKAGE, VERSION);
236 default: goto usage;
237 }
238 default: goto thud;
239 }
240 }
241
242 thud:
243 if (argc-pi==3){
244 int moe=atoi(argv[pi]), larry=atoi(argv[pi+1]), curly=atoi(argv[pi+2]);
245 hastur=makeday(
246 #ifdef US_FORMAT
247 moe,larry,
248 #else
249 larry,moe,
250 #endif
251 curly);
252 if (hastur.season == -1) {
253 printf("Invalid date -- out of range\n");
254 return -1;
255 }
256 fnord=fnord?fnord:default_fmt;
257 } else if (argc!=pi) {
258 usage:
259 fprintf(stderr,_("usage: %s [+format] [day month year]\n"), argv[0]);
260 exit(1);
261 } else {
262 t= time(NULL);
263 eris=localtime(&t);
264 bob=eris->tm_yday; /* days since Jan 1. */
265 raw=eris->tm_year; /* years since 1980 */
266 hastur=convert(bob,raw);
267 fnord=fnord?fnord:default_immediate_fmt;
268 }
269 format(schwa, fnord, hastur);
270 printf("%s\n", schwa);
271
272 return 0;
273 }
274
275 void format(char *buf, const char* fmt, struct disc_time dt)
276 {
277 int tib_start=-1, tib_end=0;
278 int i, fmtlen=strlen(fmt);
279 char *bufptr=buf;
280
281 /* fprintf(stderr, "format(%p, \"%s\", dt)\n", buf, fmt);*/
282
283 /* first, find extents of St. Tib's Day area, if defined */
284 for(i=0; i<fmtlen; i++) {
285 if(fmt[i]=='%') {
286 switch(fmt[i+1]) {
287 case 'A':
288 case 'a':
289 case 'd':
290 case 'e':
291 if(tib_start>0) tib_end=i+1;
292 else tib_start=i;
293 break;
294 case '{': tib_start=i; break;
295 case '}': tib_end=i+1; break;
296 }
297 }
298 }
299
300 /* now do the formatting */
301 buf[0]=0;
302
303 for(i=0; i<fmtlen; i++) {
304 if((i==tib_start) && (dt.day==-1)) {
305 /* handle St. Tib's Day */
306 strcpy(bufptr, _("St. Tib's Day")); bufptr += 13;
307 i=tib_end;
308 } else {
309 if(fmt[i]=='%') {
310 char *wibble=0, snarf[23];
311 switch(fmt[++i]) {
312 case 'A': wibble=day_long[dt.yday%5]; break;
313 case 'a': wibble=day_short[dt.yday%5]; break;
314 case 'B': wibble=season_long[dt.season]; break;
315 case 'b': wibble=season_short[dt.season]; break;
316 case 'd': sprintf(snarf, "%d", dt.day+1); wibble=snarf; break;
317 case 'e': sprintf(snarf, "%d%s", dt.day+1, ending(dt.day+1));
318 wibble=snarf; break;
319 case 'H': if(dt.day==4||dt.day==49)
320 wibble=holyday[dt.season][dt.day==49]; break;
321 case 'N': if(dt.day!=4&&dt.day!=49) goto eschaton; break;
322 case 'n': *(bufptr++)='\n'; break;
323 case 't': *(bufptr++)='\t'; break;
324
325 case 'Y': sprintf(snarf, "%d", dt.year); wibble=snarf; break;
326 case '.': wibble=sel(excl, sizeof(excl)/sizeof(excl[0]));
327 break;
328 #ifdef KILL_BOB
329 case 'X': sprintf(snarf, "%d",
330 xday_countdown(dt.yday, dt.year));
331 wibble = snarf; break;
332 #endif /* KILL_BOB */
333 }
334 if(wibble) {
335 /* fprintf(stderr, "wibble = (%s)\n", wibble);*/
336 strcpy(bufptr, wibble); bufptr+=strlen(wibble);
337 }
338 } else {
339 *(bufptr++) = fmt[i];
340 }
341 }
342 }
343 eschaton:
344 *(bufptr)=0;
345 }
346
347 struct disc_time makeday(int imonth,int iday,int iyear) /*i for input */
348 {
349 struct disc_time funkychickens;
350
351 int cal[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
352 int dayspast=0;
353
354 /* basic range checks */
355 if (imonth < 1 || imonth > 12) {
356 funkychickens.season = -1;
357 return funkychickens;
358 }
359 if (iday < 1 || iday > cal[imonth-1]) {
360 if (!(imonth == 2 && iday == 29 && iyear%4 == 0 &&
361 (iyear%100 != 0 || iyear%400 == 0))) {
362 funkychickens.season = -1;
363 return funkychickens;
364 }
365 }
366
367 imonth--;
368 funkychickens.year= iyear+1166;
369 while(imonth>0) { dayspast+=cal[--imonth]; }
370 funkychickens.day=dayspast+iday-1;
371 funkychickens.season=0;
372 if((funkychickens.year%4)==2) {
373 if (funkychickens.day==59 && iday==29) funkychickens.day=-1;
374 }
375 funkychickens.yday=funkychickens.day;
376 /* note: EQUAL SIGN...hopefully that fixes it */
377 while(funkychickens.day>=73) {
378 funkychickens.season++;
379 funkychickens.day-=73;
380 }
381 return funkychickens;
382 }
383
384 struct disc_time convert(int nday, int nyear)
385 { struct disc_time funkychickens;
386
387 funkychickens.year = nyear+3066;
388 funkychickens.day=nday;
389 funkychickens.season=0;
390 if ((funkychickens.year%4)==2)
391 {if (funkychickens.day==59)
392 funkychickens.day=-1;
393 else if (funkychickens.day >59)
394 funkychickens.day-=1;
395 }
396 funkychickens.yday=funkychickens.day;
397 while (funkychickens.day>=73)
398 { funkychickens.season++;
399 funkychickens.day-=73;
400 }
401 return funkychickens;
402
403 }
404
405 #ifdef KILL_BOB
406
407 /* Code for counting down to X-Day, X-Day being Cfn 40, 3164
408 *
409 * After `X-Day' passed without incident, the CoSG declared that it had
410 * got the year upside down --- X-Day is actually in 8661 AD rather than
411 * 1998 AD.
412 *
413 * Thus, the True X-Day is Cfn 40, 9827.
414 *
415 */
416
417 int xday_countdown(int yday, int year) {
418 int r=(185-yday)+(((yday<59)&&(leapp(year)))?1:0);
419 while(year<9827) r+=(leapp(++year)?366:365);
420 while(year>9827) r-=(leapp(year--)?366:365);
421 return r;
422 }
423
424 #endif
This page took 0.064452 seconds and 5 git commands to generate.