OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / cal / cal.c
1 /* cal - return monthly calendar
2  *
3  * usage: cal [mm] [yyyy]
4  *
5  * bugs: 1901 - 32767 only
6  *
7  * based on Kent Porter's code from "Software Spare Parts," 1986
8  * (software routines from the book are for use in any project,
9  *  according to the author, but he deserves credit)
10  *
11  * returns current month's calendar with no arguments
12  * prints calendar for specified month and year
13  *
14  * -i causes interactive mode to prompt for month and year
15  *
16  * hacked for uClinux on uCsimm, bball@staffnet.com 2000-3-13
17  * (yes, i know some of this is ugly, but i don't program for a living)
18  *
19  */
20
21 #include <stdio.h>
22 #include <time.h>
23 #include <sys/time.h>
24
25 int cal_lm (mo, yr)
26 int mo, yr;
27 {
28         int d, nd, mt[] = { 0, 21, 59, 90, 120, 151,
29                           181, 212, 243, 273, 304, 334 };
30         
31         if ( yr < 1901 || mo < 1 || mo > 12)
32                 return (7);
33         d = yr - 1901;
34         nd = d * 365;
35         nd += (( d/4) - (d/100) + (d/400)) + mt[mo-1];
36         if (( yr % 4) == 0 && mo > 2)
37                 nd++;
38         return (( nd + 2) % 7);
39 }
40
41 usage() {
42         fputs("usage: cal [option] [mm yyyy]\n", stdout);
43         fputs("-[h][?] - show help\n",stdout);
44         fputs("-i      - interactive\n",stdout);         
45         fputs("print monthly calendar as in: cal 3 2000\n", stdout);
46 }
47
48 int print_cal(mo, year) 
49 int mo, year;
50 {
51         int i, day, c, s, cal_lm();
52         static char *n[] = { "January", "February", "March", "April",
53                             "May", "June", "July", "August", "September",
54                             "October", "November", "December" };
55         static dmp [] = { 31, 28, 31, 30, 31, 30,
56                           31, 31, 30, 31, 30, 31 };
57
58         if (( day = cal_lm (mo, year)) > 6) {
59                 usage();
60                 exit(0);
61         }
62         mo--;
63         printf ("\n      %s %d", n[mo], year);
64         fputs  ("\n Su Mo Tu We Th Fr Sa",stdout);
65         putchar('\n');
66         s = 0;
67         if (day >0)
68                 for (s =0; s < day; s++)
69                         fputs ( "   ",stdout);
70         c = s;
71         day = 1;
72
73         do {
74                 printf ("%3d", day++);
75                 if (++c > 6) {
76                         putchar ('\n');
77                         c = 0;
78                 }
79         } while (day <= dmp [mo] );
80         if ( mo == 1 && (year % 4) == 0)
81                 if (( year % 100 ) != 0 || (year % 400 ) == 0)
82                         printf( "%3d", day );
83         fputs ("\n\n\n",stdout);
84
85
86 int
87 main(int argc, char *argv[])
88 {
89
90         int mo, year;
91         char m[4], y[5];
92         time_t now;
93         struct tm *ptr;
94         char mbuff[4],ybuff[5];
95         char *t;
96
97   if (argc ==1) {
98         /* get time */
99         time(&now);
100         ptr = localtime(&now);
101         t = asctime(ptr);
102
103         /* we only need month and year */
104         sscanf(t,"%s%s%s%s%s",NULL,mbuff,NULL,NULL,ybuff);
105         if (strcmp(mbuff,"Jan") == 0) 
106                 mo = 1;
107         if (strcmp(mbuff,"Feb") == 0)
108                 mo = 2;
109         if (strcmp(mbuff,"Mar") == 0)
110                 mo = 3;
111         if (strcmp(mbuff,"Apr") == 0)
112                 mo = 4;
113         if (strcmp(mbuff,"May") == 0)
114                 mo = 5;
115         if (strcmp(mbuff,"Jun") == 0)
116                 mo = 6;
117         if (strcmp(mbuff,"Jul") == 0)
118                 mo = 7;
119         if (strcmp(mbuff,"Aug") == 0)
120                 mo = 8;
121         if (strcmp(mbuff,"Sep") == 0)
122                 mo = 9;
123         if (strcmp(mbuff,"Oct") == 0)
124                 mo = 10;
125         if (strcmp(mbuff,"Nov") == 0)
126                 mo = 11;
127         if (strcmp(mbuff,"Dec") == 0)
128                 mo = 12;
129         if ((mo <= 0) || (mo > 12)) {
130                 usage(); exit(0);
131         }
132         year = atoi(ybuff);
133         if ((year < 1901) || (year > 32767)) {
134                 usage(); exit(0);
135         }
136         print_cal(mo,year);
137         exit(0);
138   }             
139
140   else if (argc > 1) {  
141         int param = 1;
142         if (argv[param][0] == '-') {
143                 switch(argv[param][1]) {
144                 case 'i':
145                         fputs("Enter month [1-12]:\n", stdout);
146                         fgets(m,3,stdin);
147                         mo = atoi(m);
148                         if ((mo <= 0) || (mo > 12)) {
149                                 usage(); exit(0);
150                         }
151                         fputs("Enter year [2000]:\n", stdout);
152                         fgets(y,5,stdin);
153                         year = atoi(y);
154                         if ((year < 1901) || (year > 32767)) {
155                                 usage(); exit(0);
156                         }
157                         print_cal(mo,year); 
158                         exit(0);
159                         break;
160                 case 'h':
161                 case '?': 
162                 default:  usage(); exit(0);
163                         break;
164                 }
165         }
166         mo = atoi(argv[1]);
167         if ((mo <= 0) || (mo > 12)) {
168                 usage(); exit(0);
169         }
170
171         year = atoi(argv[2]);
172         if ((year < 1901) || (year > 32767)) {
173                 usage(); exit(0);
174         }
175    }
176 print_cal(mo,year);
177 exit(0);
178 }
179