Annotation of 42BSD/ucb/leave.c, revision 1.1.1.1

1.1       root        1: static char *sccsid = "@(#)leave.c     4.3 (Berkeley) 3/31/83";
                      2: #include <stdio.h>
                      3: #include <signal.h>
                      4: /*
                      5:  * leave [hhmm]
                      6:  *
                      7:  * Reminds you when you have to leave.
                      8:  * Leave prompts for input and goes away if you hit return.
                      9:  * It nags you like a mother hen.
                     10:  */
                     11: char origlogin[20], thislogin[20];
                     12: char *getlogin();
                     13: char *whenleave;
                     14: char *ctime();
                     15: char buff[100];
                     16: 
                     17: main(argc,argv)
                     18: char **argv;
                     19: {
                     20:        long when, tod, now, diff, hours, minutes;
                     21:        int *nv;
                     22:        int atoi();
                     23:        int *localtime();
                     24: 
                     25:        if (argc < 2) {
                     26:                printf("When do you have to leave? ");
                     27:                fflush(stdout);
                     28:                buff[read(0,buff,sizeof buff)] = 0;
                     29:        } else {
                     30:                strcpy(buff,argv[1]);
                     31:        }
                     32: 
                     33:        if (buff[0] == '\n')
                     34:                exit(0);
                     35:        if (buff[0] == '+') {
                     36:                diff = atoi(buff+1);
                     37:                doalarm(diff);
                     38:        }
                     39:        if (buff[0] < '0' || buff[0] > '9') {
                     40:                printf("usage: %s [hhmm]\n",argv[0]);
                     41:                exit(1);
                     42:        }
                     43:        strcpy(origlogin,getlogin());
                     44: 
                     45:        tod = atoi(buff);
                     46:        hours = tod / 100;
                     47:        if (hours > 12)
                     48:                hours -= 12;
                     49:        if (hours == 12)
                     50:                hours = 0;
                     51:        minutes = tod % 100;
                     52: 
                     53:        if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59) {
                     54:                printf("usage: %s [hhmm]\n",argv[0]);
                     55:                exit(1);
                     56:        }
                     57: 
                     58:        time(&now);
                     59:        nv = localtime(&now);
                     60:        when = 60*hours+minutes;
                     61:        if (nv[2] > 12) nv[2] -= 12;    /* do am/pm bit */
                     62:        now = 60*nv[2] + nv[1];
                     63:        diff = when - now;
                     64:        while (diff < 0)
                     65:                diff += 12*60;
                     66:        if (diff > 11*60) printf("That time has already passed!\n");
                     67:        doalarm(diff);
                     68:        exit(0);
                     69: }
                     70: 
                     71: 
                     72: doalarm(nmins)
                     73: long nmins;
                     74: {
                     75:        char *msg1, *msg2, *msg3, *msg4;
                     76:        register int i;
                     77:        int slp1, slp2, slp3, slp4;
                     78:        int seconds, gseconds;
                     79:        long daytime;
                     80: 
                     81:        seconds = 60 * nmins;
                     82:        if (seconds <= 0)
                     83:                seconds = 1;
                     84:        gseconds = seconds;
                     85: 
                     86:        msg1 = "You have to leave in 5 minutes";
                     87:        if (seconds <= 60*5) {
                     88:                slp1 = 0;
                     89:        } else {
                     90:                slp1 = seconds - 60*5;
                     91:                seconds = 60*5;
                     92:        }
                     93: 
                     94:        msg2 = "Just one more minute!";
                     95:        if (seconds <= 60) {
                     96:                slp2 = 0;
                     97:        } else {
                     98:                slp2 = seconds - 60;
                     99:                seconds = 60;
                    100:        }
                    101: 
                    102:        msg3 = "Time to leave!";
                    103:        slp3 = seconds;
                    104: 
                    105:        msg4 = "You're going to be late!";
                    106:        slp4 = 60;
                    107: 
                    108:        time(&daytime);
                    109:        daytime += gseconds;
                    110:        whenleave = ctime(&daytime);
                    111:        printf("Alarm set for %s\n",whenleave);
                    112:        if (fork())
                    113:                exit(0);
                    114:        signal(SIGINT, SIG_IGN);
                    115:        signal(SIGQUIT, SIG_IGN);
                    116:        signal(SIGTERM, SIG_IGN);
                    117:        signal(SIGTTOU, SIG_IGN);
                    118: 
                    119:        if (slp1)
                    120:                bother(slp1,msg1);
                    121:        if (slp2)
                    122:                bother(slp2,msg2);
                    123:        bother(slp3,msg3);
                    124:        for (;;) {
                    125:                bother(slp4,msg4);
                    126:        }
                    127: }
                    128: 
                    129: bother(slp,msg)
                    130: int slp;
                    131: char *msg;
                    132: {
                    133: 
                    134:        delay(slp);
                    135:        printf("\7\7\7");
                    136:        printf("%s\n",msg);
                    137: }
                    138: 
                    139: /*
                    140:  * delay is like sleep but does it in 100 sec pieces and
                    141:  * knows what zero means.
                    142:  */
                    143: delay(secs) int secs; {
                    144:        int n;
                    145: 
                    146:        while(secs>0) {
                    147:                n = 100;
                    148:                secs = secs - 100;
                    149:                if (secs < 0) {
                    150:                        n = n + secs;
                    151:                }
                    152:                if (n > 0)
                    153:                        sleep(n);
                    154:                strcpy(thislogin,getlogin());
                    155:                if (strcmp(origlogin, thislogin))
                    156:                        exit(0);
                    157:        }
                    158: }
                    159: 
                    160: #ifdef V6
                    161: char *getlogin() {
                    162: #include <utmp.h>
                    163: 
                    164:        static struct utmp ubuf;
                    165:        int ufd;
                    166: 
                    167:        ufd = open("/etc/utmp",0);
                    168:        seek(ufd, ttyn(0)*sizeof(ubuf), 0);
                    169:        read(ufd, &ubuf, sizeof(ubuf));
                    170:        ubuf.ut_name[sizeof(ubuf.ut_name)] = 0;
                    171:        return(&ubuf.ut_name);
                    172: }
                    173: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.