1 /* 2 * Copyright (c) 1980, 1988, 1993 3 * The Regents of the University of California. 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 * @(#) Copyright (c) 1980, 1988, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)leave.c 8.1 (Berkeley) 6/6/93 35 * $FreeBSD: src/usr.bin/leave/leave.c,v 1.5.2.2 2001/07/30 09:43:30 dd Exp $ 36 * $DragonFly: src/usr.bin/leave/leave.c,v 1.2 2003/06/17 04:29:27 dillon Exp $ 37 */ 38 39 #include <err.h> 40 #include <ctype.h> 41 #include <locale.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <time.h> 45 #include <unistd.h> 46 47 void doalarm __P((u_int)); 48 static void usage __P((void)); 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 int 58 main(argc, argv) 59 int argc; 60 char **argv; 61 { 62 register u_int secs; 63 register int hours, minutes; 64 register char c, *cp = NULL; 65 struct tm *t; 66 time_t now; 67 int plusnow, t_12_hour; 68 char buf[50]; 69 70 if (setlocale(LC_TIME, "") == NULL) 71 warn("setlocale"); 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 if (argc > 2) 80 usage(); 81 else 82 cp = argv[1]; 83 84 if (*cp == '+') { 85 plusnow = 1; 86 ++cp; 87 } else 88 plusnow = 0; 89 90 for (hours = 0; (c = *cp) && c != '\n'; ++cp) { 91 if (!isdigit(c)) 92 usage(); 93 hours = hours * 10 + (c - '0'); 94 } 95 minutes = hours % 100; 96 hours /= 100; 97 98 if (minutes < 0 || minutes > 59) 99 usage(); 100 if (plusnow) 101 secs = hours * 60 * 60 + minutes * 60; 102 else { 103 (void)time(&now); 104 t = localtime(&now); 105 106 if (hours > 23) 107 usage(); 108 109 /* Convert tol to 12 hr time (0:00...11:59) */ 110 if (hours > 11) 111 hours -= 12; 112 113 /* Convert tm to 12 hr time (0:00...11:59) */ 114 if (t->tm_hour > 11) 115 t_12_hour = t->tm_hour - 12; 116 else 117 t_12_hour = t->tm_hour; 118 119 if (hours < t_12_hour || 120 (hours == t_12_hour && minutes <= t->tm_min)) 121 /* Leave time is in the past so we add 12 hrs */ 122 hours += 12; 123 124 secs = (hours - t_12_hour) * 60 * 60; 125 secs += (minutes - t->tm_min) * 60; 126 secs -= now % 60; /* truncate (now + secs) to min */ 127 } 128 doalarm(secs); 129 exit(0); 130 } 131 132 void 133 doalarm(secs) 134 u_int secs; 135 { 136 register int bother; 137 time_t daytime; 138 char tb[80]; 139 int pid; 140 141 if ((pid = fork())) { 142 (void)time(&daytime); 143 daytime += secs; 144 strftime(tb, sizeof(tb), "%+", localtime(&daytime)); 145 printf("Alarm set for %s. (pid %d)\n", tb, pid); 146 exit(0); 147 } 148 sleep((u_int)2); /* let parent print set message */ 149 if (secs >= 2) 150 secs -= 2; 151 152 /* 153 * if write fails, we've lost the terminal through someone else 154 * causing a vhangup by logging in. 155 */ 156 #define FIVEMIN (5 * 60) 157 #define MSG2 "\07\07You have to leave in 5 minutes.\n" 158 if (secs >= FIVEMIN) { 159 sleep(secs - FIVEMIN); 160 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1) 161 exit(0); 162 secs = FIVEMIN; 163 } 164 165 #define ONEMIN (60) 166 #define MSG3 "\07\07Just one more minute!\n" 167 if (secs >= ONEMIN) { 168 sleep(secs - ONEMIN); 169 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1) 170 exit(0); 171 } 172 173 #define MSG4 "\07\07Time to leave!\n" 174 for (bother = 10; bother--;) { 175 sleep((u_int)ONEMIN); 176 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1) 177 exit(0); 178 } 179 180 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" 181 (void)write(1, MSG5, sizeof(MSG5) - 1); 182 exit(0); 183 } 184 185 static void 186 usage() 187 { 188 fprintf(stderr, "usage: leave [[+]hhmm]\n"); 189 exit(1); 190 } 191