1: /*
   2:  * line: turn tty lines off or on for uucp to use
   3:  *
   4:  *   Usage: line ttyname on|off
   5:  *   Exit codes for line off:
   6:  *	  0: line wasn't on (and shouldn't be turned on when finished)
   7:  *	1-9: old entry in /etc/ttys (to be restored later)
   8:  *	 10: line is busy
   9:  *	>10: error
  10:  */
  11: #include <stdio.h>
  12: #include <utmp.h>
  13: #include <signal.h>
  14: #define UTMP    "/etc/utmp"
  15: #define TTYS    "/etc/ttys"
  16: #define MAXLINE 50      /* maximum line length in TTYS */
  17: 
  18: int WasOn;          /* flag set if line was in use as dialup */
  19: char    *rindex();
  20: 
  21: main(ac,av)
  22: char **av;
  23: {
  24:     if (ac != 3 || av[2][0] < '0' || av[2][0] > '9') {
  25:         fprintf(stderr,"Usage: line ttyname digit\n");
  26:         exit(20);
  27:     }
  28:     if (av[2][0] == '0') {
  29:         if (inuse(av[1]))
  30:             exit(10);
  31:         if (untty(av[1]))
  32:             exit(12);
  33:         exit(WasOn-'0');
  34:     }
  35:     if (resetline(av[1], av[2][0]))
  36:         exit(14);
  37: }
  38: 
  39: /*
  40:  * Check whether anyone is logged in on devcul.
  41:  */
  42: inuse(devcul)
  43: char *devcul;
  44: {
  45:     register fi;
  46:     struct utmp utmpb;
  47:     char *tmp;
  48: 
  49:     if ((tmp=rindex(devcul,'/')) != NULL)
  50:         devcul = tmp+1;
  51:     fi=open(UTMP,0);
  52:     while(read(fi,&utmpb,sizeof(utmpb)) == sizeof(utmpb))
  53:         if (strcmp(utmpb.ut_line,devcul) == 0) {
  54:             if ( *utmpb.ut_name != 0 ) {
  55:                 fprintf(stderr,"%s is in use\n",devcul);
  56:                 return(-1);
  57:             }
  58:             break;
  59:         }
  60:     close(fi);
  61:     return(0);
  62: }
  63: 
  64: /*
  65:  *  Remove devcul from TTYS file, notify init that line is
  66:  *  no longer available.
  67:  */
  68: untty(devcul)
  69: char *devcul;
  70: {
  71:     register fi, ret;
  72:     char line[MAXLINE];
  73:     char *tty;
  74: 
  75:     if ((tty=rindex(devcul,'/')) != NULL)
  76:         tty++;
  77:     else
  78:         tty = devcul;
  79:     fi = open(TTYS,2);
  80:     if (fi < 0) {
  81:         fprintf(stderr,"can't modify %s\n",TTYS);
  82:         return (-1);
  83:     }
  84:     while (ret=getline(fi,line)) {
  85:         if (!strcmp(line+2,tty)) {
  86:             WasOn = line[0];
  87:             break;
  88:         }
  89:     }
  90:     if (ret == 0)
  91:         return(WasOn = '0');
  92:     if (WasOn != '0') {
  93:         lseek(fi,-(long)(ret+1),1);
  94:         write(fi,"0",1);
  95:     }
  96:     close(fi);
  97:     if (WasOn != '0') {
  98:         kill(1,1);
  99:         sleep(2);       /* wait for init/getty to go away */
 100:     }
 101:     sprintf(line,"/dev/%s",tty);
 102:     chmod(line,0666);
 103:     return(0);
 104: }
 105: 
 106: /*
 107:  *  Restore line to dialup service (removed from service by untty).
 108:  */
 109: resetline(tty, mode)
 110: char *tty;
 111: char mode;
 112: {
 113:     int fi, ret;
 114:     char *tmp;
 115:     char    line[MAXLINE];
 116: 
 117:     if (mode == '0')
 118:         return(-1);
 119:     fi = open(TTYS,2);
 120:     if (fi < 0) {
 121:         fprintf(stderr,"can't modify %s\n",TTYS);
 122:         return (-1);
 123:     }
 124:     while (ret=getline(fi,line)) {
 125:         if (!strcmp(line+2,tty)) {
 126:             lseek(fi,-(long)(ret+1),1);
 127:             write(fi, &mode, 1);
 128:             close(fi);
 129:             kill(1,1);
 130:             return(0);
 131:         }
 132:     }
 133:     fprintf(stderr,"%s not in %s now\n",tty,TTYS);
 134:     return(-1);
 135: }
 136: 
 137: /*
 138:  * Read a line from the ttys file.  Null terminate at the end
 139:  * of the name.
 140:  */
 141: getline(fd,buf)
 142: char *buf;
 143: {
 144:     char c;
 145:     register char *p, *lim = buf+MAXLINE-1;
 146:     register count = 0;
 147: 
 148:     p = buf;
 149:     while (read(fd,&c,1) && c!='\n') {
 150:         count++;
 151:         if (p<lim)
 152:             *p++ = c;
 153:     }
 154:     *p = 0;
 155:     for (p=buf; *p; p++)
 156:         if (*p==' ' || *p=='\t')
 157:             break;
 158:     *p = 0;
 159:     return(count);
 160: }

Defined functions

getline defined in line 141; used 2 times
inuse defined in line 42; used 1 times
  • in line 29
main defined in line 21; never used
resetline defined in line 109; used 1 times
  • in line 35
untty defined in line 68; used 1 times
  • in line 31

Defined variables

WasOn defined in line 18; used 5 times

Defined macros

MAXLINE defined in line 16; used 3 times
TTYS defined in line 15; used 5 times
UTMP defined in line 14; used 1 times
  • in line 51
Last modified: 1983-07-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 999
Valid CSS Valid XHTML 1.0 Strict