leave.c revision 27572
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$"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/time.h> 50#include <ctype.h> 51#include <stdio.h> 52#include <unistd.h> 53 54void doalarm __P((u_int)); 55static void usage __P((void)); 56 57/* 58 * leave [[+]hhmm] 59 * 60 * Reminds you when you have to leave. 61 * Leave prompts for input and goes away if you hit return. 62 * It nags you like a mother hen. 63 */ 64int 65main(argc, argv) 66 int argc; 67 char **argv; 68{ 69 register u_int secs; 70 register int hours, minutes; 71 register char c, *cp; 72 struct tm *t, *localtime(); 73 time_t now, time(); 74 int plusnow; 75 char buf[50]; 76 77 if (argc < 2) { 78#define MSG1 "When do you have to leave? " 79 (void)write(1, MSG1, sizeof(MSG1) - 1); 80 cp = fgets(buf, sizeof(buf), stdin); 81 if (cp == NULL || *cp == '\n') 82 exit(0); 83 } else 84 cp = argv[1]; 85 86 if (*cp == '+') { 87 plusnow = 1; 88 ++cp; 89 } else { 90 plusnow = 0; 91 (void)time(&now); 92 t = localtime(&now); 93 } 94 95 for (hours = 0; (c = *cp) && c != '\n'; ++cp) { 96 if (!isdigit(c)) 97 usage(); 98 hours = hours * 10 + (c - '0'); 99 } 100 minutes = hours % 100; 101 hours /= 100; 102 103 if (minutes < 0 || minutes > 59) 104 usage(); 105 if (plusnow) 106 secs = hours * 60 * 60 + minutes * 60; 107 else { 108 if (hours > 23 || t->tm_hour > hours || 109 (t->tm_hour == hours && minutes <= t->tm_min)) 110 usage(); 111 secs = (hours - t->tm_hour) * 60 * 60; 112 secs += (minutes - t->tm_min) * 60; 113 } 114 doalarm(secs); 115 exit(0); 116} 117 118void 119doalarm(secs) 120 u_int secs; 121{ 122 register int bother; 123 time_t daytime, time(); 124 int pid; 125 char *ctime(); 126 127 if ((pid = fork())) { 128 (void)time(&daytime); 129 daytime += secs; 130 printf("Alarm set for %.16s. (pid %d)\n", 131 ctime(&daytime), pid); 132 exit(0); 133 } 134 sleep((u_int)2); /* let parent print set message */ 135 136 /* 137 * if write fails, we've lost the terminal through someone else 138 * causing a vhangup by logging in. 139 */ 140#define FIVEMIN (5 * 60) 141#define MSG2 "\07\07You have to leave in 5 minutes.\n" 142 if (secs >= FIVEMIN) { 143 sleep(secs - FIVEMIN); 144 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1) 145 exit(0); 146 secs = FIVEMIN; 147 } 148 149#define ONEMIN (60) 150#define MSG3 "\07\07Just one more minute!\n" 151 if (secs >= ONEMIN) { 152 sleep(secs - ONEMIN); 153 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1) 154 exit(0); 155 } 156 157#define MSG4 "\07\07Time to leave!\n" 158 for (bother = 10; bother--;) { 159 sleep((u_int)ONEMIN); 160 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1) 161 exit(0); 162 } 163 164#define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" 165 (void)write(1, MSG5, sizeof(MSG5) - 1); 166 exit(0); 167} 168 169static void 170usage() 171{ 172 fprintf(stderr, "usage: leave [[+]hhmm]\n"); 173 exit(1); 174} 175