[BACK]Return to leave.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / leave

Annotation of src/usr.bin/leave/leave.c, Revision 1.9

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