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