2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)leave.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
54 static void doalarm(u_int);
55 static void usage(void);
60 * Reminds you when you have to leave.
61 * Leave prompts for input and goes away if you hit return.
62 * It nags you like a mother hen.
65 main(int argc, char **argv)
72 int plusnow, t_12_hour;
75 if (setlocale(LC_TIME, "") == NULL)
79 #define MSG1 "When do you have to leave? "
80 (void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
81 cp = fgets(buf, sizeof(buf), stdin);
82 if (cp == NULL || *cp == '\n')
95 for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
98 hours = hours * 10 + (c - '0');
100 minutes = hours % 100;
103 if (minutes < 0 || minutes > 59)
106 secs = hours * 60 * 60 + minutes * 60;
114 /* Convert tol to 12 hr time (0:00...11:59) */
118 /* Convert tm to 12 hr time (0:00...11:59) */
120 t_12_hour = t->tm_hour - 12;
122 t_12_hour = t->tm_hour;
124 if (hours < t_12_hour ||
125 (hours == t_12_hour && minutes <= t->tm_min))
126 /* Leave time is in the past so we add 12 hrs */
129 secs = (hours - t_12_hour) * 60 * 60;
130 secs += (minutes - t->tm_min) * 60;
131 secs -= now % 60; /* truncate (now + secs) to min */
145 if ((pid = fork())) {
146 (void)time(&daytime);
148 strftime(tb, sizeof(tb), "%+", localtime(&daytime));
149 printf("Alarm set for %s. (pid %d)\n", tb, pid);
152 sleep((u_int)2); /* let parent print set message */
157 * if write fails, we've lost the terminal through someone else
158 * causing a vhangup by logging in.
160 #define FIVEMIN (5 * 60)
161 #define MSG2 "\07\07You have to leave in 5 minutes.\n"
162 if (secs >= FIVEMIN) {
163 sleep(secs - FIVEMIN);
164 if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
170 #define MSG3 "\07\07Just one more minute!\n"
171 if (secs >= ONEMIN) {
172 sleep(secs - ONEMIN);
173 if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
177 #define MSG4 "\07\07Time to leave!\n"
178 for (bother = 10; bother--;) {
179 sleep((u_int)ONEMIN);
180 if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
184 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
185 (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
192 fprintf(stderr, "usage: leave [[+]hhmm]\n");