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, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 char copyright[] = 36 "@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 /*static char sccsid[] = "from: @(#)leave.c 5.6 (Berkeley) 3/1/91";*/ 42 static char rcsid[] = "$Id: leave.c,v 1.2 1993/08/01 18:13:59 mycroft Exp $"; 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/time.h> 47 #include <stdio.h> 48 #include <ctype.h> 49 50 /* 51 * leave [[+]hhmm] 52 * 53 * Reminds you when you have to leave. 54 * Leave prompts for input and goes away if you hit return. 55 * It nags you like a mother hen. 56 */ 57 main(argc, argv) 58 int argc; 59 char **argv; 60 { 61 register u_int secs; 62 register int hours, minutes; 63 register char c, *cp; 64 struct tm *t, *localtime(); 65 time_t now, time(); 66 int plusnow; 67 char buf[50]; 68 69 if (argc < 2) { 70 #define MSG1 "When do you have to leave? " 71 (void)write(1, MSG1, sizeof(MSG1) - 1); 72 cp = fgets(buf, sizeof(buf), stdin); 73 if (*cp == '\n') 74 exit(0); 75 } else 76 cp = argv[1]; 77 78 if (*cp == '+') { 79 plusnow = 1; 80 ++cp; 81 } else { 82 plusnow = 0; 83 (void)time(&now); 84 t = localtime(&now); 85 } 86 87 for (hours = 0; (c = *cp) && c != '\n'; ++cp) { 88 if (!isdigit(c)) 89 usage(); 90 hours = hours * 10 + (c - '0'); 91 } 92 minutes = hours % 100; 93 hours /= 100; 94 95 if (minutes < 0 || minutes > 59) 96 usage(); 97 if (plusnow) 98 secs = hours * 60 * 60 + minutes * 60; 99 else { 100 if (hours > 23 || t->tm_hour > hours || 101 t->tm_hour == hours && minutes <= t->tm_min) 102 usage(); 103 secs = (hours - t->tm_hour) * 60 * 60; 104 secs += (minutes - t->tm_min) * 60; 105 } 106 doalarm(secs); 107 exit(0); 108 } 109 110 doalarm(secs) 111 u_int secs; 112 { 113 register int bother; 114 time_t daytime, time(); 115 int pid; 116 char *ctime(); 117 118 if (pid = fork()) { 119 (void)time(&daytime); 120 daytime += secs; 121 printf("Alarm set for %.16s. (pid %d)\n", 122 ctime(&daytime), pid); 123 exit(0); 124 } 125 sleep((u_int)2); /* let parent print set message */ 126 127 /* 128 * if write fails, we've lost the terminal through someone else 129 * causing a vhangup by logging in. 130 */ 131 #define FIVEMIN (5 * 60) 132 #define MSG2 "\07\07You have to leave in 5 minutes.\n" 133 if (secs >= FIVEMIN) { 134 sleep(secs - FIVEMIN); 135 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1) 136 exit(0); 137 secs = FIVEMIN; 138 } 139 140 #define ONEMIN (60) 141 #define MSG3 "\07\07Just one more minute!\n" 142 if (secs >= ONEMIN) { 143 sleep(secs - ONEMIN); 144 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1) 145 exit(0); 146 } 147 148 #define MSG4 "\07\07Time to leave!\n" 149 for (bother = 10; bother--;) { 150 sleep((u_int)ONEMIN); 151 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1) 152 exit(0); 153 } 154 155 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" 156 (void)write(1, MSG5, sizeof(MSG5) - 1); 157 exit(0); 158 } 159 160 usage() 161 { 162 fprintf(stderr, "usage: leave [[+]hhmm]\n"); 163 exit(1); 164 } 165