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

Defined functions

bother defined in line 127; used 4 times
delay defined in line 142; used 1 times
doalarm defined in line 71; used 2 times
getlogin defined in line 160; used 3 times
main defined in line 16; never used

Defined variables

buff defined in line 14; used 10 times
origlogin defined in line 10; used 2 times
sccsid defined in line 1; never used
thislogin defined in line 10; used 2 times
whenleave defined in line 12; used 2 times
Last modified: 1981-12-07
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 948
Valid CSS Valid XHTML 1.0 Strict