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