|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1980, 1988 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: char copyright[] = ! 22: "@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\ ! 23: All rights reserved.\n"; ! 24: #endif /* not lint */ ! 25: ! 26: #ifndef lint ! 27: static char sccsid[] = "@(#)leave.c 5.5 (Berkeley) 6/1/90"; ! 28: #endif /* not lint */ ! 29: ! 30: #include <sys/param.h> ! 31: #include <sys/time.h> ! 32: #include <stdio.h> ! 33: #include <ctype.h> ! 34: ! 35: /* ! 36: * leave [[+]hhmm] ! 37: * ! 38: * Reminds you when you have to leave. ! 39: * Leave prompts for input and goes away if you hit return. ! 40: * It nags you like a mother hen. ! 41: */ ! 42: main(argc, argv) ! 43: int argc; ! 44: char **argv; ! 45: { ! 46: register u_int secs; ! 47: register int hours, minutes; ! 48: register char c, *cp; ! 49: struct tm *t, *localtime(); ! 50: time_t now, time(); ! 51: int plusnow; ! 52: char buf[50]; ! 53: ! 54: if (argc < 2) { ! 55: #define MSG1 "When do you have to leave? " ! 56: (void)write(1, MSG1, sizeof(MSG1) - 1); ! 57: cp = fgets(buf, sizeof(buf), stdin); ! 58: if (*cp == '\n') ! 59: exit(0); ! 60: } else ! 61: cp = argv[1]; ! 62: ! 63: if (*cp == '+') { ! 64: plusnow = 1; ! 65: ++cp; ! 66: } else { ! 67: plusnow = 0; ! 68: (void)time(&now); ! 69: t = localtime(&now); ! 70: } ! 71: ! 72: for (hours = 0; (c = *cp) && c != '\n'; ++cp) { ! 73: if (!isdigit(c)) ! 74: usage(); ! 75: hours = hours * 10 + (c - '0'); ! 76: } ! 77: minutes = hours % 100; ! 78: hours /= 100; ! 79: ! 80: if (minutes < 0 || minutes > 59) ! 81: usage(); ! 82: if (plusnow) ! 83: secs = hours * 60 * 60 + minutes * 60; ! 84: else { ! 85: if (hours > 23 || t->tm_hour > hours || ! 86: t->tm_hour == hours && minutes <= t->tm_min) ! 87: usage(); ! 88: secs = (hours - t->tm_hour) * 60 * 60; ! 89: secs += (minutes - t->tm_min) * 60; ! 90: } ! 91: doalarm(secs); ! 92: exit(0); ! 93: } ! 94: ! 95: static ! 96: doalarm(secs) ! 97: u_int secs; ! 98: { ! 99: register int bother; ! 100: time_t daytime, time(); ! 101: int pid; ! 102: char *ctime(); ! 103: ! 104: if (pid = fork()) { ! 105: (void)time(&daytime); ! 106: daytime += secs; ! 107: printf("Alarm set for %.16s. (pid %d)\n", ! 108: ctime(&daytime), pid); ! 109: exit(0); ! 110: } ! 111: sleep((u_int)2); /* let parent print set message */ ! 112: ! 113: /* ! 114: * if write fails, we've lost the terminal through someone else ! 115: * causing a vhangup by logging in. ! 116: */ ! 117: #define FIVEMIN (5 * 60) ! 118: #define MSG2 "\07\07You have to leave in 5 minutes.\n" ! 119: if (secs >= FIVEMIN) { ! 120: sleep(secs - FIVEMIN); ! 121: if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1) ! 122: exit(0); ! 123: secs = FIVEMIN; ! 124: } ! 125: ! 126: #define ONEMIN (60) ! 127: #define MSG3 "\07\07Just one more minute!\n" ! 128: if (secs >= ONEMIN) { ! 129: sleep(secs - ONEMIN); ! 130: if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1) ! 131: exit(0); ! 132: } ! 133: ! 134: #define MSG4 "\07\07Time to leave!\n" ! 135: for (bother = 10; bother--;) { ! 136: sleep((u_int)ONEMIN); ! 137: if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1) ! 138: exit(0); ! 139: } ! 140: ! 141: #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" ! 142: (void)write(1, MSG5, sizeof(MSG5) - 1); ! 143: exit(0); ! 144: } ! 145: ! 146: static ! 147: usage() ! 148: { ! 149: fprintf(stderr, "usage: leave [[+]hhmm]\n"); ! 150: exit(1); ! 151: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.