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

Defined functions

bother defined in line 158; used 4 times
delay defined in line 171; used 1 times
doalarm defined in line 100; used 2 times
getlogin defined in line 189; used 9 times
main defined in line 32; never used
usage defined in line 94; used 4 times

Defined variables

buff defined in line 30; used 4 times
copyright defined in line 8; never used
origlogin defined in line 26; used 2 times
sccsid defined in line 14; never used
whenleave defined in line 28; used 2 times
Last modified: 1985-05-31
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1229
Valid CSS Valid XHTML 1.0 Strict