xref: /dragonfly/usr.bin/leave/leave.c (revision 3170ffd7)
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  * 4. 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  * $DragonFly: src/usr.bin/leave/leave.c,v 1.7 2006/08/14 07:37:38 swildner Exp $
33  */
34 
35 #include <err.h>
36 #include <ctype.h>
37 #include <langinfo.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <unistd.h>
43 
44 void doalarm(u_int);
45 static void usage(void);
46 
47 /*
48  * leave [[+]hhmm]
49  *
50  * Reminds you when you have to leave.
51  * Leave prompts for input and goes away if you hit return.
52  * It nags you like a mother hen.
53  */
54 int
55 main(int argc, char **argv)
56 {
57 	u_int secs;
58 	int hours, minutes;
59 	char c, *cp = NULL;
60 	struct tm *t;
61 	time_t now;
62 	int plusnow, t_12_hour;
63 	char buf[50];
64 
65 	if (setlocale(LC_TIME, "") == NULL)
66 		warn("setlocale");
67 
68 	if (argc < 2) {
69 #define	MSG1	"When do you have to leave? "
70 		(void)write(1, MSG1, sizeof(MSG1) - 1);
71 		cp = fgets(buf, sizeof(buf), stdin);
72 		if (cp == NULL || *cp == '\n')
73 			exit(0);
74 	} else if (argc > 2)
75 		usage();
76 	else
77 		cp = argv[1];
78 
79 	if (*cp == '+') {
80 		plusnow = 1;
81 		++cp;
82 	} else
83 		plusnow = 0;
84 
85 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
86 		if (!isdigit(c))
87 			usage();
88 		hours = hours * 10 + (c - '0');
89 	}
90 	minutes = hours % 100;
91 	hours /= 100;
92 
93 	if (minutes < 0 || minutes > 59)
94 		usage();
95 	if (plusnow)
96 		secs = hours * 60 * 60 + minutes * 60;
97 	else {
98 		(void)time(&now);
99 		t = localtime(&now);
100 
101 		if (hours > 23)
102 			usage();
103 
104 		/* Convert tol to 12 hr time (0:00...11:59) */
105 		if (hours > 11)
106 			hours -= 12;
107 
108 		/* Convert tm to 12 hr time (0:00...11:59) */
109 		if (t->tm_hour > 11)
110 			t_12_hour = t->tm_hour - 12;
111 		else
112 			t_12_hour = t->tm_hour;
113 
114 		if (hours < t_12_hour ||
115 	 	   (hours == t_12_hour && minutes <= t->tm_min))
116 			/* Leave time is in the past so we add 12 hrs */
117 			hours += 12;
118 
119 		secs = (hours - t_12_hour) * 60 * 60;
120 		secs += (minutes - t->tm_min) * 60;
121 		secs -= now % 60;	/* truncate (now + secs) to min */
122 	}
123 	doalarm(secs);
124 	exit(0);
125 }
126 
127 void
128 doalarm(u_int secs)
129 {
130 	int bother;
131 	time_t daytime;
132 	char tb[80];
133 	int pid;
134 
135 	if ((pid = fork())) {
136 		(void)time(&daytime);
137 		daytime += secs;
138 		strftime(tb, sizeof(tb), nl_langinfo(D_T_FMT),
139 		    localtime(&daytime));
140 		printf("Alarm set for %s. (pid %d)\n", tb, pid);
141 		exit(0);
142 	}
143 	sleep((u_int)2);		/* let parent print set message */
144 	if (secs >= 2)
145 		secs -= 2;
146 
147 	/*
148 	 * if write fails, we've lost the terminal through someone else
149 	 * causing a vhangup by logging in.
150 	 */
151 #define	FIVEMIN	(5 * 60)
152 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
153 	if (secs >= FIVEMIN) {
154 		sleep(secs - FIVEMIN);
155 		if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
156 			exit(0);
157 		secs = FIVEMIN;
158 	}
159 
160 #define	ONEMIN	(60)
161 #define	MSG3	"\07\07Just one more minute!\n"
162 	if (secs >= ONEMIN) {
163 		sleep(secs - ONEMIN);
164 		if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
165 			exit(0);
166 	}
167 
168 #define	MSG4	"\07\07Time to leave!\n"
169 	for (bother = 10; bother--;) {
170 		sleep((u_int)ONEMIN);
171 		if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
172 			exit(0);
173 	}
174 
175 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
176 	(void)write(1, MSG5, sizeof(MSG5) - 1);
177 	exit(0);
178 }
179 
180 static void
181 usage(void)
182 {
183 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
184 	exit(1);
185 }
186