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