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