xref: /dragonfly/usr.bin/leave/leave.c (revision 0e4e903a)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)leave.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/leave/leave.c,v 1.5.2.2 2001/07/30 09:43:30 dd Exp $
32  */
33 
34 #include <err.h>
35 #include <ctype.h>
36 #include <langinfo.h>
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <time.h>
41 #include <unistd.h>
42 
43 static void doalarm(u_int) __dead2;
44 static void usage(void) __dead2;
45 
46 /*
47  * leave [[+]hhmm]
48  *
49  * Reminds you when you have to leave.
50  * Leave prompts for input and goes away if you hit return.
51  * It nags you like a mother hen.
52  */
53 int
main(int argc,char ** argv)54 main(int argc, char **argv)
55 {
56 	u_int secs;
57 	int hours, minutes;
58 	char c, *cp = NULL;
59 	struct tm *t;
60 	time_t now;
61 	int plusnow, t_12_hour;
62 	char buf[50];
63 
64 	if (setlocale(LC_TIME, "") == NULL)
65 		warn("setlocale");
66 
67 	if (argc < 2) {
68 #define	MSG1	"When do you have to leave? "
69 		(void)write(1, MSG1, sizeof(MSG1) - 1);
70 		cp = fgets(buf, sizeof(buf), stdin);
71 		if (cp == NULL || *cp == '\n')
72 			exit(0);
73 	} else if (argc > 2)
74 		usage();
75 	else
76 		cp = argv[1];
77 
78 	if (*cp == '+') {
79 		plusnow = 1;
80 		++cp;
81 	} else
82 		plusnow = 0;
83 
84 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
85 		if (!isdigit(c))
86 			usage();
87 		hours = hours * 10 + (c - '0');
88 	}
89 	minutes = hours % 100;
90 	hours /= 100;
91 
92 	/*
93 	 * NOTE: Allows minutes specifications > 59 for +now usages,
94 	 *	 just like microwaves have for decades. -Matt
95 	 */
96 	if (minutes < 0)
97 		usage();
98 
99 	if (plusnow) {
100 		secs = hours * 60 * 60 + minutes * 60;
101 	} else {
102 		if (minutes > 59)
103 			usage();
104 		(void)time(&now);
105 		t = localtime(&now);
106 
107 		if (hours > 23)
108 			usage();
109 
110 		/* Convert tol to 12 hr time (0:00...11:59) */
111 		if (hours > 11)
112 			hours -= 12;
113 
114 		/* Convert tm to 12 hr time (0:00...11:59) */
115 		if (t->tm_hour > 11)
116 			t_12_hour = t->tm_hour - 12;
117 		else
118 			t_12_hour = t->tm_hour;
119 
120 		if (hours < t_12_hour ||
121 	 	   (hours == t_12_hour && minutes <= t->tm_min))
122 			/* Leave time is in the past so we add 12 hrs */
123 			hours += 12;
124 
125 		secs = (hours - t_12_hour) * 60 * 60;
126 		secs += (minutes - t->tm_min) * 60;
127 		secs -= now % 60;	/* truncate (now + secs) to min */
128 	}
129 	doalarm(secs);
130 	exit(0);
131 }
132 
133 static void
doalarm(u_int secs)134 doalarm(u_int secs)
135 {
136 	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), nl_langinfo(D_T_FMT),
145 		    localtime(&daytime));
146 		printf("Alarm set for %s. (pid %d)\n", tb, pid);
147 		exit(0);
148 	}
149 	sleep((u_int)2);		/* let parent print set message */
150 	if (secs >= 2)
151 		secs -= 2;
152 
153 	/*
154 	 * if write fails, we've lost the terminal through someone else
155 	 * causing a vhangup by logging in.
156 	 */
157 #define	FIVEMIN	(5 * 60)
158 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
159 	if (secs >= FIVEMIN) {
160 		sleep(secs - FIVEMIN);
161 		if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
162 			exit(0);
163 		secs = FIVEMIN;
164 	}
165 
166 #define	ONEMIN	(60)
167 #define	MSG3	"\07\07Just one more minute!\n"
168 	if (secs >= ONEMIN) {
169 		sleep(secs - ONEMIN);
170 		if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
171 			exit(0);
172 	}
173 
174 #define	MSG4	"\07\07Time to leave!\n"
175 	for (bother = 10; bother--;) {
176 		sleep((u_int)ONEMIN);
177 		if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
178 			exit(0);
179 	}
180 
181 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
182 	(void)write(1, MSG5, sizeof(MSG5) - 1);
183 	exit(0);
184 }
185 
186 static void
usage(void)187 usage(void)
188 {
189 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
190 	exit(1);
191 }
192