/* Usage: leave [ [+] hh[:]mm ] * * Author: Terrence W. Holm * * Revision: * Fred van Kempen, MINIX User Group Holland * -adapted to MSS * -adapted to new utmp database * -adapted to POSIX (MINIX 1.5) * Michael Temari, * -use localtime/mktime to fix bug with DST */ #include #include #include #include #include #include #include #include #define Min(a,b) ((a 59) Usage(); } int Still_Logged_On(user, tty) char *user; char *tty; { FILE *f; struct utmp login; if ((f = fopen(UTMP, "r")) == NULL) /* no login/logout records kept */ return(1); while (fread(&login, sizeof(struct utmp), (size_t)1, f) == 1) { if (!strncmp(login.ut_line, tty, (size_t)8)) if (!strncmp(login.ut_name, user, (size_t)8)) { fclose(f); return(1); } else { fclose(f); return(0); } } fclose(f); return(0); } int main(argc, argv) int argc; char *argv[]; { char when[STRING]; time_t now = time((time_t *)0); time_t leave, delta; struct tm *tm; int hour, min; int pid, i; char *user = cuserid( (char *)NULL); char *tty = ttyname(0) + 5; /* get the argument string "when" either from stdin, or argv */ if (argc <= 1) { printf("When do you have to leave? "); fflush(stdout); if (fgets(when, STRING, stdin) == NULL || when[0] == '\n') exit(0); } else { strcpy(when, argv[1]); if (argc > 2) strcat(when, argv[2]); } /* determine the leave time from the current time and "when" */ tm = localtime(&now); if (when[0] == '+') { Get_Hour_Min(&when[1], &hour, &min); tm->tm_hour += hour; tm->tm_min += min; leave = mktime(tm); } else { /* user entered an absolute time */ Get_Hour_Min(&when[0], &hour, &min); tm->tm_hour = hour; tm->tm_min = min; leave = mktime(tm); if (leave < now) { printf("That time has already passed!\n"); exit(1); } } printf("Alarm set for %s", ctime(&leave)); if ((pid = fork()) == -1) { fprintf(stderr, "leave: can not fork\n"); exit(1); } if (pid != 0) exit(0); /* only the child continues on */ if (user == NULL || tty == NULL) { fprintf(stderr, "leave: Can not determine user and terminal name\n"); exit(1); } signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGTERM, SIG_IGN); for (;;) { if (!Still_Logged_On(user, tty)) exit(0); /* how much longer until the leave time? */ /* XXX - use difftime all over. */ delta = leave - time((time_t *)0); /* which interval are we currently in? */ for (i = 0; i < INTERVALS; ++i) if (delta + intervals[i] > 0) break; /* if we are within intervals[0] then print a warning If * there are more intervals than messages, then use/ * warnings[WARNINGS-1] for all subsequent messages. */ if (i > 0) printf("\007\r%s\r\n", warnings[i > WARNINGS ? WARNINGS - 1 : i - 1]); if (i == INTERVALS) { printf("That was the last time I'll tell you. Bye.\r\n"); exit(0); } /* Sleep until the next interval. For long periods, wake up * every hour to check if the user is still on (also required * because 16 bit ints don't allow long waits). */ sleep((unsigned) Min(delta + intervals[i], HOUR)); } }