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