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