Deleted Added
full compact
leave.c (77451) leave.c (78717)
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 "$FreeBSD: head/usr.bin/leave/leave.c 77451 2001-05-30 02:38:10Z dd $";
45 "$FreeBSD: head/usr.bin/leave/leave.c 78717 2001-06-24 19:41:18Z dd $";
46#endif /* not lint */
47
48#include <err.h>
49#include <ctype.h>
50#include <locale.h>
51#include <stdio.h>
46#endif /* not lint */
47
48#include <err.h>
49#include <ctype.h>
50#include <locale.h>
51#include <stdio.h>
52#include <stdlib.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 = NULL;
73 struct tm *t;
74 time_t now;
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 if (argc > 2)
88 usage();
89 else
90 cp = argv[1];
91
92 if (*cp == '+') {
93 plusnow = 1;
94 ++cp;
95 } else
96 plusnow = 0;
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 {
111 (void)time(&now);
112 t = localtime(&now);
113
114 if (hours > 23)
115 usage();
116
117 /* Convert tol to 12 hr time (0:00...11:59) */
118 if (hours > 11)
119 hours -= 12;
120
121 /* Convert tm to 12 hr time (0:00...11:59) */
122 if (t->tm_hour > 11)
123 t_12_hour = t->tm_hour - 12;
124 else
125 t_12_hour = t->tm_hour;
126
127 if (hours < t_12_hour ||
128 (hours == t_12_hour && minutes <= t->tm_min))
129 /* Leave time is in the past so we add 12 hrs */
130 hours += 12;
131
132 secs = (hours - t_12_hour) * 60 * 60;
133 secs += (minutes - t->tm_min) * 60;
134 secs -= now % 60; /* truncate (now + secs) to min */
135 }
136 doalarm(secs);
137 exit(0);
138}
139
140void
141doalarm(secs)
142 u_int secs;
143{
144 register int bother;
145 time_t daytime;
146 char tb[80];
147 int pid;
148
149 if ((pid = fork())) {
150 (void)time(&daytime);
151 daytime += secs;
152 strftime(tb, sizeof(tb), "%+", localtime(&daytime));
153 printf("Alarm set for %s. (pid %d)\n", tb, pid);
154 exit(0);
155 }
156 sleep((u_int)2); /* let parent print set message */
157 if (secs >= 2)
158 secs -= 2;
159
160 /*
161 * if write fails, we've lost the terminal through someone else
162 * causing a vhangup by logging in.
163 */
164#define FIVEMIN (5 * 60)
165#define MSG2 "\07\07You have to leave in 5 minutes.\n"
166 if (secs >= FIVEMIN) {
167 sleep(secs - FIVEMIN);
168 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
169 exit(0);
170 secs = FIVEMIN;
171 }
172
173#define ONEMIN (60)
174#define MSG3 "\07\07Just one more minute!\n"
175 if (secs >= ONEMIN) {
176 sleep(secs - ONEMIN);
177 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
178 exit(0);
179 }
180
181#define MSG4 "\07\07Time to leave!\n"
182 for (bother = 10; bother--;) {
183 sleep((u_int)ONEMIN);
184 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
185 exit(0);
186 }
187
188#define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
189 (void)write(1, MSG5, sizeof(MSG5) - 1);
190 exit(0);
191}
192
193static void
194usage()
195{
196 fprintf(stderr, "usage: leave [[+]hhmm]\n");
197 exit(1);
198}
53#include <time.h>
54#include <unistd.h>
55
56void doalarm __P((u_int));
57static void usage __P((void));
58
59/*
60 * leave [[+]hhmm]
61 *
62 * Reminds you when you have to leave.
63 * Leave prompts for input and goes away if you hit return.
64 * It nags you like a mother hen.
65 */
66int
67main(argc, argv)
68 int argc;
69 char **argv;
70{
71 register u_int secs;
72 register int hours, minutes;
73 register char c, *cp = NULL;
74 struct tm *t;
75 time_t now;
76 int plusnow, t_12_hour;
77 char buf[50];
78
79 if (setlocale(LC_TIME, "") == NULL)
80 warn("setlocale");
81
82 if (argc < 2) {
83#define MSG1 "When do you have to leave? "
84 (void)write(1, MSG1, sizeof(MSG1) - 1);
85 cp = fgets(buf, sizeof(buf), stdin);
86 if (cp == NULL || *cp == '\n')
87 exit(0);
88 } else if (argc > 2)
89 usage();
90 else
91 cp = argv[1];
92
93 if (*cp == '+') {
94 plusnow = 1;
95 ++cp;
96 } else
97 plusnow = 0;
98
99 for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
100 if (!isdigit(c))
101 usage();
102 hours = hours * 10 + (c - '0');
103 }
104 minutes = hours % 100;
105 hours /= 100;
106
107 if (minutes < 0 || minutes > 59)
108 usage();
109 if (plusnow)
110 secs = hours * 60 * 60 + minutes * 60;
111 else {
112 (void)time(&now);
113 t = localtime(&now);
114
115 if (hours > 23)
116 usage();
117
118 /* Convert tol to 12 hr time (0:00...11:59) */
119 if (hours > 11)
120 hours -= 12;
121
122 /* Convert tm to 12 hr time (0:00...11:59) */
123 if (t->tm_hour > 11)
124 t_12_hour = t->tm_hour - 12;
125 else
126 t_12_hour = t->tm_hour;
127
128 if (hours < t_12_hour ||
129 (hours == t_12_hour && minutes <= t->tm_min))
130 /* Leave time is in the past so we add 12 hrs */
131 hours += 12;
132
133 secs = (hours - t_12_hour) * 60 * 60;
134 secs += (minutes - t->tm_min) * 60;
135 secs -= now % 60; /* truncate (now + secs) to min */
136 }
137 doalarm(secs);
138 exit(0);
139}
140
141void
142doalarm(secs)
143 u_int secs;
144{
145 register int bother;
146 time_t daytime;
147 char tb[80];
148 int pid;
149
150 if ((pid = fork())) {
151 (void)time(&daytime);
152 daytime += secs;
153 strftime(tb, sizeof(tb), "%+", localtime(&daytime));
154 printf("Alarm set for %s. (pid %d)\n", tb, pid);
155 exit(0);
156 }
157 sleep((u_int)2); /* let parent print set message */
158 if (secs >= 2)
159 secs -= 2;
160
161 /*
162 * if write fails, we've lost the terminal through someone else
163 * causing a vhangup by logging in.
164 */
165#define FIVEMIN (5 * 60)
166#define MSG2 "\07\07You have to leave in 5 minutes.\n"
167 if (secs >= FIVEMIN) {
168 sleep(secs - FIVEMIN);
169 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
170 exit(0);
171 secs = FIVEMIN;
172 }
173
174#define ONEMIN (60)
175#define MSG3 "\07\07Just one more minute!\n"
176 if (secs >= ONEMIN) {
177 sleep(secs - ONEMIN);
178 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
179 exit(0);
180 }
181
182#define MSG4 "\07\07Time to leave!\n"
183 for (bother = 10; bother--;) {
184 sleep((u_int)ONEMIN);
185 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
186 exit(0);
187 }
188
189#define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
190 (void)write(1, MSG5, sizeof(MSG5) - 1);
191 exit(0);
192}
193
194static void
195usage()
196{
197 fprintf(stderr, "usage: leave [[+]hhmm]\n");
198 exit(1);
199}