|
|
1.1 ! root 1: /* ! 2: * Print an almanac of daily events. Program reads today's date, ! 3: * constructs a date string, and then examines the files ! 4: * /usr/games/lib/almanac.birth, /usr/games/lib/almanac.death, and ! 5: * /usr/games/lib/almanac.event for strings that contain the date string. ! 6: * Originally written in the shell. ! 7: * ! 8: * By fb, 7/12/88. ! 9: */ ! 10: ! 11: #include <stdio.h> ! 12: #include <time.h> ! 13: #include <sys/types.h> ! 14: ! 15: #define EXIT_SUCCESS 0 ! 16: #define EXIT_FAILURE 1 ! 17: ! 18: extern time_t time(); ! 19: ! 20: char *months[] = { ! 21: "Jan", "Feb", "Mar", "Apr", "May", "Jun", ! 22: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ! 23: }; ! 24: ! 25: char *filenames[] = { ! 26: "/usr/games/lib/almanac.birth", ! 27: "/usr/games/lib/almanac.death", ! 28: "/usr/games/lib/almanac.event" ! 29: }; ! 30: ! 31: char *slugs[] = { ! 32: "BIRTHS:", ! 33: "DEATHS:", ! 34: "EVENTS:" ! 35: }; ! 36: ! 37: /* print an error message and exit */ ! 38: fatal(message) ! 39: char *message; ! 40: { ! 41: fprintf (stderr, "%s\n", message); ! 42: exit(EXIT_FAILURE); ! 43: } ! 44: ! 45: findstring(date, fileptr) ! 46: char *date; ! 47: FILE *fileptr; ! 48: { ! 49: char buffer[120]; ! 50: int length = strlen(date); ! 51: ! 52: while (fgets(buffer, 120, fileptr) != NULL) { ! 53: if (strncmp(buffer, date, length) == 0) ! 54: printf ("%s", buffer+length); ! 55: else ! 56: continue; ! 57: } ! 58: } ! 59: ! 60: main(argc, argv) ! 61: int argc; char *argv[]; ! 62: { ! 63: long rawtime; ! 64: struct tm *brokentime; ! 65: FILE *fileptr; ! 66: char buffer[20]; ! 67: int i; ! 68: ! 69: if (argc == 3) { ! 70: if (strlen(argv[1]) < 3) ! 71: fatal("Usage: almanac [Mon date]"); ! 72: argv[1][3] = '\0'; ! 73: sprintf(buffer, "%3s %s ", argv[1], argv[2]); ! 74: } else if (argc == 1) { ! 75: /* build the time string */ ! 76: rawtime = time(NULL); ! 77: brokentime = localtime(&rawtime); ! 78: sprintf(buffer, "%s %d ", months[brokentime->tm_mon], ! 79: brokentime->tm_mday); ! 80: } else ! 81: fatal("Usage: almanac [Mon date]"); ! 82: ! 83: /* open almanac text files; call string printing routine */ ! 84: for (i = 0; i < 3; i++) { ! 85: if ((fileptr = fopen(filenames[i], "r")) == NULL) ! 86: fatal("Cannot open almanac file."); ! 87: ! 88: printf("%s\n", slugs[i]); ! 89: ! 90: findstring(buffer, fileptr); ! 91: ! 92: fclose (fileptr); ! 93: } ! 94: exit(EXIT_SUCCESS); ! 95: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.