add -N and -M
[unix-history] / usr / src / usr.bin / leave / leave.c
CommitLineData
22e155fc 1/*
8326fc5e 2 * Copyright (c) 1980, 1988 Regents of the University of California.
86565271
KB
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
22e155fc
DF
6 */
7
8#ifndef lint
9char copyright[] =
8326fc5e 10"@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\
22e155fc 11 All rights reserved.\n";
86565271 12#endif /* not lint */
22e155fc
DF
13
14#ifndef lint
b61c5edc 15static char sccsid[] = "@(#)leave.c 5.6 (Berkeley) %G%";
86565271 16#endif /* not lint */
22e155fc 17
8326fc5e
KB
18#include <sys/param.h>
19#include <sys/time.h>
fdd8e0d1 20#include <stdio.h>
86565271 21#include <ctype.h>
8326fc5e 22
fdd8e0d1 23/*
cc4f5b4e 24 * leave [[+]hhmm]
fdd8e0d1
BJ
25 *
26 * Reminds you when you have to leave.
27 * Leave prompts for input and goes away if you hit return.
28 * It nags you like a mother hen.
29 */
cc4f5b4e 30main(argc, argv)
8326fc5e
KB
31 int argc;
32 char **argv;
fdd8e0d1 33{
8326fc5e
KB
34 register u_int secs;
35 register int hours, minutes;
36 register char c, *cp;
37 struct tm *t, *localtime();
38 time_t now, time();
39 int plusnow;
40 char buf[50];
fdd8e0d1
BJ
41
42 if (argc < 2) {
8326fc5e
KB
43#define MSG1 "When do you have to leave? "
44 (void)write(1, MSG1, sizeof(MSG1) - 1);
45 cp = fgets(buf, sizeof(buf), stdin);
46 if (*cp == '\n')
47 exit(0);
cc4f5b4e
RC
48 } else
49 cp = argv[1];
8326fc5e 50
cc4f5b4e 51 if (*cp == '+') {
8326fc5e
KB
52 plusnow = 1;
53 ++cp;
54 } else {
55 plusnow = 0;
56 (void)time(&now);
57 t = localtime(&now);
58 }
59
60 for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
61 if (!isdigit(c))
cc4f5b4e 62 usage();
8326fc5e 63 hours = hours * 10 + (c - '0');
fdd8e0d1 64 }
8326fc5e
KB
65 minutes = hours % 100;
66 hours /= 100;
fdd8e0d1 67
8326fc5e 68 if (minutes < 0 || minutes > 59)
cc4f5b4e 69 usage();
8326fc5e
KB
70 if (plusnow)
71 secs = hours * 60 * 60 + minutes * 60;
72 else {
73 if (hours > 23 || t->tm_hour > hours ||
74 t->tm_hour == hours && minutes <= t->tm_min)
75 usage();
76 secs = (hours - t->tm_hour) * 60 * 60;
77 secs += (minutes - t->tm_min) * 60;
cc4f5b4e 78 }
8326fc5e 79 doalarm(secs);
fdd8e0d1
BJ
80 exit(0);
81}
82
8326fc5e
KB
83doalarm(secs)
84 u_int secs;
86565271 85{
8326fc5e
KB
86 register int bother;
87 time_t daytime, time();
88 int pid;
89 char *ctime();
90
91 if (pid = fork()) {
92 (void)time(&daytime);
93 daytime += secs;
94 printf("Alarm set for %.16s. (pid %d)\n",
95 ctime(&daytime), pid);
96 exit(0);
86565271 97 }
8326fc5e
KB
98 sleep((u_int)2); /* let parent print set message */
99
100 /*
101 * if write fails, we've lost the terminal through someone else
102 * causing a vhangup by logging in.
103 */
104#define FIVEMIN (5 * 60)
105#define MSG2 "\07\07You have to leave in 5 minutes.\n"
106 if (secs >= FIVEMIN) {
107 sleep(secs - FIVEMIN);
108 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
109 exit(0);
110 secs = FIVEMIN;
fdd8e0d1
BJ
111 }
112
8326fc5e
KB
113#define ONEMIN (60)
114#define MSG3 "\07\07Just one more minute!\n"
115 if (secs >= ONEMIN) {
116 sleep(secs - ONEMIN);
117 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
118 exit(0);
fdd8e0d1
BJ
119 }
120
8326fc5e
KB
121#define MSG4 "\07\07Time to leave!\n"
122 for (bother = 10; bother--;) {
123 sleep((u_int)ONEMIN);
124 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
125 exit(0);
126 }
fdd8e0d1 127
8326fc5e
KB
128#define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
129 (void)write(1, MSG5, sizeof(MSG5) - 1);
cc4f5b4e 130 exit(0);
fdd8e0d1
BJ
131}
132
8326fc5e 133usage()
cc4f5b4e 134{
8326fc5e
KB
135 fprintf(stderr, "usage: leave [[+]hhmm]\n");
136 exit(1);
fdd8e0d1 137}