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.5 (Berkeley) 06/01/90"; 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 static 84 doalarm(secs) 85 u_int secs; 86 { 87 register int bother; 88 time_t daytime, time(); 89 int pid; 90 char *ctime(); 91 92 if (pid = fork()) { 93 (void)time(&daytime); 94 daytime += secs; 95 printf("Alarm set for %.16s. (pid %d)\n", 96 ctime(&daytime), pid); 97 exit(0); 98 } 99 sleep((u_int)2); /* let parent print set message */ 100 101 /* 102 * if write fails, we've lost the terminal through someone else 103 * causing a vhangup by logging in. 104 */ 105 #define FIVEMIN (5 * 60) 106 #define MSG2 "\07\07You have to leave in 5 minutes.\n" 107 if (secs >= FIVEMIN) { 108 sleep(secs - FIVEMIN); 109 if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1) 110 exit(0); 111 secs = FIVEMIN; 112 } 113 114 #define ONEMIN (60) 115 #define MSG3 "\07\07Just one more minute!\n" 116 if (secs >= ONEMIN) { 117 sleep(secs - ONEMIN); 118 if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1) 119 exit(0); 120 } 121 122 #define MSG4 "\07\07Time to leave!\n" 123 for (bother = 10; bother--;) { 124 sleep((u_int)ONEMIN); 125 if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1) 126 exit(0); 127 } 128 129 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n" 130 (void)write(1, MSG5, sizeof(MSG5) - 1); 131 exit(0); 132 } 133 134 static 135 usage() 136 { 137 fprintf(stderr, "usage: leave [[+]hhmm]\n"); 138 exit(1); 139 } 140