1 /* 2 * Copyright (c) 1980, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)leave.c 5.6 (Berkeley) 03/01/91"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/time.h> 20 #include <stdio.h> 21 #include <ctype.h> 22 23 /* 24 * leave [[+]hhmm] 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 */ 30 main(argc, argv) 31 int argc; 32 char **argv; 33 { 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]; 41 42 if (argc < 2) { 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); 48 } else 49 cp = argv[1]; 50 51 if (*cp == '+') { 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)) 62 usage(); 63 hours = hours * 10 + (c - '0'); 64 } 65 minutes = hours % 100; 66 hours /= 100; 67 68 if (minutes < 0 || minutes > 59) 69 usage(); 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; 78 } 79 doalarm(secs); 80 exit(0); 81 } 82 83 doalarm(secs) 84 u_int secs; 85 { 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); 97 } 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; 111 } 112 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); 119 } 120 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 } 127 128 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" 129 (void)write(1, MSG5, sizeof(MSG5) - 1); 130 exit(0); 131 } 132 133 usage() 134 { 135 fprintf(stderr, "usage: leave [[+]hhmm]\n"); 136 exit(1); 137 } 138