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