| 1 | #ifndef lint |
| 2 | static char sccsid[] = "@(#)cal.c 4.4 (Berkeley) 87/05/28"; |
| 3 | #endif |
| 4 | |
| 5 | #include <sys/types.h> |
| 6 | #include <time.h> |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | char dayw[] = { |
| 10 | " S M Tu W Th F S" |
| 11 | }; |
| 12 | char *smon[]= { |
| 13 | "January", "February", "March", "April", |
| 14 | "May", "June", "July", "August", |
| 15 | "September", "October", "November", "December", |
| 16 | }; |
| 17 | char string[432]; |
| 18 | main(argc, argv) |
| 19 | char *argv[]; |
| 20 | { |
| 21 | register y, i, j; |
| 22 | int m; |
| 23 | |
| 24 | if(argc == 2) |
| 25 | goto xlong; |
| 26 | /* |
| 27 | * print out just month |
| 28 | */ |
| 29 | if(argc < 2) { /* current month */ |
| 30 | time_t t; |
| 31 | struct tm *tm; |
| 32 | |
| 33 | t = time(0); |
| 34 | tm = localtime(&t); |
| 35 | m = tm->tm_mon + 1; |
| 36 | y = tm->tm_year + 1900; |
| 37 | } else { |
| 38 | m = atoi(argv[1]); |
| 39 | if(m<1 || m>12) { |
| 40 | fprintf(stderr, "cal: %s: Bad month.\n", argv[1]); |
| 41 | exit(1); |
| 42 | } |
| 43 | y = atoi(argv[2]); |
| 44 | if(y<1 || y>9999) { |
| 45 | fprintf(stderr, "cal: %s: Bad year.\n", argv[2]); |
| 46 | exit(2); |
| 47 | } |
| 48 | } |
| 49 | printf(" %s %u\n", smon[m-1], y); |
| 50 | printf("%s\n", dayw); |
| 51 | cal(m, y, string, 24); |
| 52 | for(i=0; i<6*24; i+=24) |
| 53 | pstr(string+i, 24); |
| 54 | exit(0); |
| 55 | |
| 56 | xlong: |
| 57 | /* |
| 58 | * print out complete year |
| 59 | */ |
| 60 | y = atoi(argv[1]); |
| 61 | if(y<1 || y>9999) { |
| 62 | fprintf(stderr, "cal: %s: Bad year.\n", argv[1]); |
| 63 | exit(2); |
| 64 | } |
| 65 | printf("\n\n\n"); |
| 66 | printf(" %u\n", y); |
| 67 | printf("\n"); |
| 68 | for(i=0; i<12; i+=3) { |
| 69 | for(j=0; j<6*72; j++) |
| 70 | string[j] = '\0'; |
| 71 | printf(" %.3s", smon[i]); |
| 72 | printf(" %.3s", smon[i+1]); |
| 73 | printf(" %.3s\n", smon[i+2]); |
| 74 | printf("%s %s %s\n", dayw, dayw, dayw); |
| 75 | cal(i+1, y, string, 72); |
| 76 | cal(i+2, y, string+23, 72); |
| 77 | cal(i+3, y, string+46, 72); |
| 78 | for(j=0; j<6*72; j+=72) |
| 79 | pstr(string+j, 72); |
| 80 | } |
| 81 | printf("\n\n\n"); |
| 82 | exit(0); |
| 83 | } |
| 84 | |
| 85 | pstr(str, n) |
| 86 | char *str; |
| 87 | { |
| 88 | register i; |
| 89 | register char *s; |
| 90 | |
| 91 | s = str; |
| 92 | i = n; |
| 93 | while(i--) |
| 94 | if(*s++ == '\0') |
| 95 | s[-1] = ' '; |
| 96 | i = n+1; |
| 97 | while(i--) |
| 98 | if(*--s != ' ') |
| 99 | break; |
| 100 | s[1] = '\0'; |
| 101 | printf("%s\n", str); |
| 102 | } |
| 103 | |
| 104 | char mon[] = { |
| 105 | 0, |
| 106 | 31, 29, 31, 30, |
| 107 | 31, 30, 31, 31, |
| 108 | 30, 31, 30, 31, |
| 109 | }; |
| 110 | |
| 111 | cal(m, y, p, w) |
| 112 | char *p; |
| 113 | { |
| 114 | register d, i; |
| 115 | register char *s; |
| 116 | |
| 117 | s = p; |
| 118 | d = jan1(y); |
| 119 | mon[2] = 29; |
| 120 | mon[9] = 30; |
| 121 | |
| 122 | switch((jan1(y+1)+7-d)%7) { |
| 123 | |
| 124 | /* |
| 125 | * non-leap year |
| 126 | */ |
| 127 | case 1: |
| 128 | mon[2] = 28; |
| 129 | break; |
| 130 | |
| 131 | /* |
| 132 | * 1752 |
| 133 | */ |
| 134 | default: |
| 135 | mon[9] = 19; |
| 136 | break; |
| 137 | |
| 138 | /* |
| 139 | * leap year |
| 140 | */ |
| 141 | case 2: |
| 142 | ; |
| 143 | } |
| 144 | for(i=1; i<m; i++) |
| 145 | d += mon[i]; |
| 146 | d %= 7; |
| 147 | s += 3*d; |
| 148 | for(i=1; i<=mon[m]; i++) { |
| 149 | if(i==3 && mon[m]==19) { |
| 150 | i += 11; |
| 151 | mon[m] += 11; |
| 152 | } |
| 153 | if(i > 9) |
| 154 | *s = i/10+'0'; |
| 155 | s++; |
| 156 | *s++ = i%10+'0'; |
| 157 | s++; |
| 158 | if(++d == 7) { |
| 159 | d = 0; |
| 160 | s = p+w; |
| 161 | p = s; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * return day of the week |
| 168 | * of jan 1 of given year |
| 169 | */ |
| 170 | |
| 171 | jan1(yr) |
| 172 | { |
| 173 | register y, d; |
| 174 | |
| 175 | /* |
| 176 | * normal gregorian calendar |
| 177 | * one extra day per four years |
| 178 | */ |
| 179 | |
| 180 | y = yr; |
| 181 | d = 4+y+(y+3)/4; |
| 182 | |
| 183 | /* |
| 184 | * julian calendar |
| 185 | * regular gregorian |
| 186 | * less three days per 400 |
| 187 | */ |
| 188 | |
| 189 | if(y > 1800) { |
| 190 | d -= (y-1701)/100; |
| 191 | d += (y-1601)/400; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * great calendar changeover instant |
| 196 | */ |
| 197 | |
| 198 | if(y > 1752) |
| 199 | d += 3; |
| 200 | |
| 201 | return(d%7); |
| 202 | } |