#include <langinfo.h>
#include <locale.h>
#include <nl_types.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int number(char *);
static int jan1(const int);
static void badmonth(void);
static void badyear(void);
static void usage(void);
static void cal(const int, const int, char *, const int);
static void load_months(void);
static void pstr(char *, const int);
#define DAYW " S M Tu W Th F S"
#define TITLE " %s %u\n"
#define YEAR "\n\n\n\t\t\t\t%u\n\n"
#define MONTH "\t%4.3s\t\t\t%.3s\t\t%10.3s\n"
static char *months[] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December",
};
static char *short_months[] = {
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec",
};
static char mon[] = {
0,
31, 29, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31,
};
static char *myname;
static char string[432];
static struct tm *thetime;
static time_t timbuf;
int
main(int argc, char *argv[])
{
int y, i, j;
int m;
char *time_locale;
char *ldayw;
myname = argv[0];
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while (getopt(argc, argv, "") != EOF)
usage();
argc -= optind;
argv = &argv[optind];
time_locale = setlocale(LC_TIME, NULL);
if ((time_locale[0] != 'C') || (time_locale[1] != '\0'))
load_months();
ldayw = dcgettext(NULL, DAYW, LC_TIME);
switch (argc) {
case 0:
timbuf = time(&timbuf);
thetime = localtime(&timbuf);
m = thetime->tm_mon + 1;
y = thetime->tm_year + 1900;
break;
case 1:
goto xlong;
case 2:
m = number(argv[0]);
y = number(argv[1]);
break;
default:
usage();
}
if (m < 1 || m > 12)
badmonth();
if (y < 1 || y > 9999)
badyear();
(void) printf(dcgettext(NULL, TITLE, LC_TIME), months[m-1], y);
(void) printf("%s\n", ldayw);
cal(m, y, string, 24);
for (i = 0; i < 6*24; i += 24)
pstr(string+i, 24);
return (0);
xlong:
y = number(argv[0]);
if (y < 1 || y > 9999)
badyear();
(void) printf(dcgettext(NULL, YEAR, LC_TIME), y);
for (i = 0; i < 12; i += 3) {
for (j = 0; j < 6*72; j++)
string[j] = '\0';
(void) printf(
dcgettext(NULL, MONTH, LC_TIME),
short_months[i], short_months[i+1], short_months[i+2]);
(void) printf("%s %s %s\n", ldayw, ldayw, ldayw);
cal(i+1, y, string, 72);
cal(i+2, y, string+23, 72);
cal(i+3, y, string+46, 72);
for (j = 0; j < 6*72; j += 72)
pstr(string+j, 72);
}
(void) printf("\n\n\n");
return (0);
}
static int
number(char *str)
{
int n, c;
char *s;
n = 0;
s = str;
while (c = *s++) {
if (c < '0' || c > '9')
return (0);
n = n*10 + c-'0';
}
return (n);
}
static void
pstr(char *str, const int n)
{
int i;
char *s;
s = str;
i = n;
while (i--)
if (*s++ == '\0')
s[-1] = ' ';
i = n+1;
while (i--)
if (*--s != ' ')
break;
s[1] = '\0';
(void) printf("%s\n", str);
}
static void
cal(const int m, const int y, char *p, const int w)
{
int d, i;
char *s;
s = (char *)p;
d = jan1(y);
mon[2] = 29;
mon[9] = 30;
switch ((jan1(y+1)+7-d)%7) {
case 1:
mon[2] = 28;
break;
default:
mon[9] = 19;
break;
case 2:
;
}
for (i = 1; i < m; i++)
d += mon[i];
d %= 7;
s += 3*d;
for (i = 1; i <= mon[m]; i++) {
if (i == 3 && mon[m] == 19) {
i += 11;
mon[m] += 11;
}
if (i > 9)
*s = i/10+'0';
s++;
*s++ = i%10+'0';
s++;
if (++d == 7) {
d = 0;
s = p+w;
p = s;
}
}
}
static int
jan1(const int yr)
{
int y, d;
y = yr;
d = 4+y+(y+3)/4;
if (y > 1800) {
d -= (y-1701)/100;
d += (y-1601)/400;
}
if (y > 1752)
d += 3;
return (d%7);
}
static void
load_months(void)
{
int month;
for (month = MON_1; month <= MON_12; month++)
months[month - MON_1] = nl_langinfo(month);
for (month = ABMON_1; month <= ABMON_12; month++)
short_months[month - ABMON_1] = nl_langinfo(month);
}
static void
badmonth()
{
(void) fprintf(stderr, gettext("%s: bad month\n"), myname);
usage();
}
static void
badyear()
{
(void) fprintf(stderr, gettext("%s: bad year\n"), myname);
usage();
}
static void
usage(void)
{
(void) fprintf(stderr, gettext("usage: %s [ [month] year ]\n"), myname);
exit(1);
}