Annotation of src/usr.bin/leave/leave.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: leave.c,v 1.2 1996/06/26 05:34:57 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: leave.c,v 1.4 1995/07/03 16:50:13 phil Exp $ */
3:
4: /*
5: * Copyright (c) 1980, 1988, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1988, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)leave.c 8.1 (Berkeley) 6/6/93";
46: #endif
1.3 ! deraadt 47: static char rcsid[] = "$OpenBSD: leave.c,v 1.2 1996/06/26 05:34:57 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/param.h>
51: #include <sys/time.h>
1.3 ! deraadt 52: #include <unistd.h>
! 53: #include <stdlib.h>
1.1 deraadt 54: #include <stdio.h>
55: #include <ctype.h>
56:
1.3 ! deraadt 57: void usage __P((void));
! 58: void doalarm __P((u_int secs));
! 59:
1.1 deraadt 60: /*
61: * leave [[+]hhmm]
62: *
63: * Reminds you when you have to leave.
64: * Leave prompts for input and goes away if you hit return.
65: * It nags you like a mother hen.
66: */
1.3 ! deraadt 67: int
1.1 deraadt 68: main(argc, argv)
69: int argc;
70: char **argv;
71: {
72: register u_int secs;
73: register int hours, minutes;
74: register char c, *cp;
1.3 ! deraadt 75: struct tm *t;
! 76: time_t now;
1.1 deraadt 77: int plusnow;
78: char buf[50];
79:
80: if (argc < 2) {
81: #define MSG1 "When do you have to leave? "
1.3 ! deraadt 82: (void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
1.1 deraadt 83: cp = fgets(buf, sizeof(buf), stdin);
84: if (cp == NULL || *cp == '\n')
85: exit(0);
86: } else
87: cp = argv[1];
88:
89: if (*cp == '+') {
90: plusnow = 1;
91: ++cp;
92: } else {
93: plusnow = 0;
94: (void)time(&now);
95: t = localtime(&now);
96: }
97:
98: for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
99: if (!isdigit(c))
100: usage();
101: hours = hours * 10 + (c - '0');
102: }
103: minutes = hours % 100;
104: hours /= 100;
105:
106: if (minutes < 0 || minutes > 59)
107: usage();
108: if (plusnow)
109: secs = hours * 60 * 60 + minutes * 60;
110: else {
1.3 ! deraadt 111: if (hours > 23)
1.1 deraadt 112: usage();
1.3 ! deraadt 113: if (t->tm_hour >= 12)
! 114: t->tm_hour -= 12;
! 115: if (t->tm_hour > hours ||
! 116: (t->tm_hour == hours && minutes <= t->tm_min))
! 117: hours += 12;
1.1 deraadt 118: secs = (hours - t->tm_hour) * 60 * 60;
119: secs += (minutes - t->tm_min) * 60;
120: }
121: doalarm(secs);
122: exit(0);
123: }
124:
1.3 ! deraadt 125: void
1.1 deraadt 126: doalarm(secs)
127: u_int secs;
128: {
129: register int bother;
1.3 ! deraadt 130: time_t daytime;
1.1 deraadt 131: int pid;
132:
1.3 ! deraadt 133: if ((pid = fork())) {
1.1 deraadt 134: (void)time(&daytime);
135: daytime += secs;
136: printf("Alarm set for %.16s. (pid %d)\n",
137: ctime(&daytime), pid);
138: exit(0);
139: }
140: sleep((u_int)2); /* let parent print set message */
141:
142: /*
143: * if write fails, we've lost the terminal through someone else
144: * causing a vhangup by logging in.
145: */
146: #define FIVEMIN (5 * 60)
147: #define MSG2 "\07\07You have to leave in 5 minutes.\n"
148: if (secs >= FIVEMIN) {
149: sleep(secs - FIVEMIN);
1.3 ! deraadt 150: if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
1.1 deraadt 151: exit(0);
152: secs = FIVEMIN;
153: }
154:
155: #define ONEMIN (60)
156: #define MSG3 "\07\07Just one more minute!\n"
157: if (secs >= ONEMIN) {
158: sleep(secs - ONEMIN);
1.3 ! deraadt 159: if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
1.1 deraadt 160: exit(0);
161: }
162:
163: #define MSG4 "\07\07Time to leave!\n"
164: for (bother = 10; bother--;) {
165: sleep((u_int)ONEMIN);
1.3 ! deraadt 166: if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
1.1 deraadt 167: exit(0);
168: }
169:
170: #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
1.3 ! deraadt 171: (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
1.1 deraadt 172: exit(0);
173: }
174:
1.3 ! deraadt 175: void
1.1 deraadt 176: usage()
177: {
178: fprintf(stderr, "usage: leave [[+]hhmm]\n");
179: exit(1);
180: }