1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1988, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #endif /* not lint */ 40 #include <sys/cdefs.h> 41 #include <err.h> 42 #include <ctype.h> 43 #include <locale.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <time.h> 47 #include <unistd.h> 48 49 static void doalarm(u_int); 50 static void usage(void) __dead2; 51 52 /* 53 * leave [[+]hhmm] 54 * 55 * Reminds you when you have to leave. 56 * Leave prompts for input and goes away if you hit return. 57 * It nags you like a mother hen. 58 */ 59 int 60 main(int argc, char **argv) 61 { 62 u_int secs; 63 int hours, minutes; 64 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(STDOUT_FILENO, 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(u_int secs) 134 { 135 int bother; 136 time_t daytime; 137 char tb[80]; 138 int pid; 139 140 if ((pid = fork())) { 141 (void)time(&daytime); 142 daytime += secs; 143 strftime(tb, sizeof(tb), "%+", localtime(&daytime)); 144 printf("Alarm set for %s. (pid %d)\n", tb, pid); 145 exit(0); 146 } 147 sleep((u_int)2); /* let parent print set message */ 148 if (secs >= 2) 149 secs -= 2; 150 151 /* 152 * if write fails, we've lost the terminal through someone else 153 * causing a vhangup by logging in. 154 */ 155 #define FIVEMIN (5 * 60) 156 #define MSG2 "\07\07You have to leave in 5 minutes.\n" 157 if (secs >= FIVEMIN) { 158 sleep(secs - FIVEMIN); 159 if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1) 160 exit(0); 161 secs = FIVEMIN; 162 } 163 164 #define ONEMIN (60) 165 #define MSG3 "\07\07Just one more minute!\n" 166 if (secs >= ONEMIN) { 167 sleep(secs - ONEMIN); 168 if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1) 169 exit(0); 170 } 171 172 #define MSG4 "\07\07Time to leave!\n" 173 for (bother = 10; bother--;) { 174 sleep((u_int)ONEMIN); 175 if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1) 176 exit(0); 177 } 178 179 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" 180 (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1); 181 exit(0); 182 } 183 184 static void 185 usage(void) 186 { 187 fprintf(stderr, "usage: leave [[+]hhmm]\n"); 188 exit(1); 189 } 190