Annotation of src/usr.bin/leave/leave.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: leave.c,v 1.13 2013/11/26 13:19:07 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.
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: #include <sys/param.h>
34: #include <sys/time.h>
1.11 otto 35: #include <ctype.h>
36: #include <err.h>
37: #include <stdio.h>
38: #include <stdlib.h>
1.3 deraadt 39: #include <unistd.h>
1.1 deraadt 40:
1.11 otto 41: static __dead void usage(void);
42: static void doalarm(u_int secs);
43:
44: #define SECOND 1
45: #define MINUTE (SECOND * 60)
46: #define FIVEMIN (5 * MINUTE)
47: #define HOUR (MINUTE * 60)
1.3 deraadt 48:
1.1 deraadt 49: /*
50: * leave [[+]hhmm]
51: *
52: * Reminds you when you have to leave.
53: * Leave prompts for input and goes away if you hit return.
54: * It nags you like a mother hen.
55: */
1.3 deraadt 56: int
1.10 deraadt 57: main(int argc, char *argv[])
1.1 deraadt 58: {
1.6 mpech 59: u_int secs;
60: int hours, minutes;
61: char c, *cp;
1.3 deraadt 62: struct tm *t;
63: time_t now;
1.11 otto 64: int plusnow = 0, twentyfour;
1.1 deraadt 65: char buf[50];
1.11 otto 66:
1.14 ! millert 67: if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
1.11 otto 68: errx(1, "Cannot set stdout to line buffered.");
1.1 deraadt 69:
70: if (argc < 2) {
1.11 otto 71: (void)fputs("When do you have to leave? ", stdout);
1.1 deraadt 72: cp = fgets(buf, sizeof(buf), stdin);
73: if (cp == NULL || *cp == '\n')
74: exit(0);
1.5 pjanzen 75: } else if (argc > 2)
76: usage();
77: else
1.1 deraadt 78: cp = argv[1];
79:
80: if (*cp == '+') {
81: plusnow = 1;
82: ++cp;
83: }
84:
85: for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
1.13 deraadt 86: if (!isdigit((unsigned char)c))
1.1 deraadt 87: usage();
88: hours = hours * 10 + (c - '0');
89: }
90: minutes = hours % 100;
91: hours /= 100;
1.11 otto 92: /* determine 24 hours mode */
93: twentyfour = hours > 12;
1.1 deraadt 94:
95: if (minutes < 0 || minutes > 59)
96: usage();
97: if (plusnow)
1.11 otto 98: secs = (hours * HOUR) + (minutes * MINUTE);
1.1 deraadt 99: else {
1.3 deraadt 100: if (hours > 23)
1.1 deraadt 101: usage();
1.11 otto 102: (void)time(&now);
103: t = localtime(&now);
104: while (t->tm_hour > hours ||
1.4 espie 105: (t->tm_hour == hours && t->tm_min >= minutes)) {
1.11 otto 106: if (twentyfour)
1.4 espie 107: hours += 24;
108: else
109: hours += 12;
110: }
111:
1.11 otto 112: secs = (hours - t->tm_hour) * HOUR;
113: secs += (minutes - t->tm_min) * MINUTE;
1.1 deraadt 114: }
115: doalarm(secs);
116: exit(0);
117: }
118:
1.11 otto 119: static void
1.10 deraadt 120: doalarm(u_int secs)
1.1 deraadt 121: {
1.6 mpech 122: int bother;
1.3 deraadt 123: time_t daytime;
1.8 mpech 124: pid_t pid;
1.1 deraadt 125:
1.11 otto 126: switch (pid = fork()) {
127: case 0:
128: break;
129: case -1:
130: err(1, "Fork failed");
131: /* NOTREACHED */
132: default:
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: }
1.11 otto 139: sleep(2); /* let parent print set message */
1.1 deraadt 140:
141: /*
142: * if write fails, we've lost the terminal through someone else
143: * causing a vhangup by logging in.
144: */
145: if (secs >= FIVEMIN) {
146: sleep(secs - FIVEMIN);
1.11 otto 147: if (puts("\a\aYou have to leave in 5 minutes.") == EOF)
1.1 deraadt 148: exit(0);
149: secs = FIVEMIN;
150: }
151:
1.11 otto 152: if (secs >= MINUTE) {
153: sleep(secs - MINUTE);
154: if (puts("\a\aJust one more minute!") == EOF)
1.1 deraadt 155: exit(0);
156: }
157:
158: for (bother = 10; bother--;) {
1.11 otto 159: sleep(MINUTE);
160: if (puts("\a\aTime to leave!") == EOF)
1.1 deraadt 161: exit(0);
162: }
163:
1.11 otto 164: puts("\a\aThat was the last time I'll tell you. Bye.");
1.1 deraadt 165: exit(0);
166: }
167:
1.11 otto 168: static __dead void
1.10 deraadt 169: usage(void)
1.1 deraadt 170: {
171: fprintf(stderr, "usage: leave [[+]hhmm]\n");
172: exit(1);
173: }