|
|
1.1 root 1: /*
2: * Rec'd from Lauren Weinstein, 7-16-84.
3: * enable/disable/ttystat
4: *
5: * All are links to the same executable.
6: *
7: * enable tty ... -- enable tty(s) for dialup use and return status
8: * disable tty ... -- disable tty(s) for dialup use and return status
9: * ttystat [-d] tty -- return status for tty
10: *
11: * When only one tty is specified the commands return exit status 1 if the
12: * tty was enabled when the command was given. They return exit status 0 if
13: * the tty was disabled when the command was given.
14: *
15: * If multiple ttys are specified to "enable" or "disable", the exit
16: * value will be based on the last specified tty which was successfully
17: * found by the program, unless no tty's were found, in which case -1
18: * is returned.
19: *
20: * If the -d option is given to "ttystat", the a message giving the status
21: * of the terminal will be written to standard output. Otherwise, the return
22: * value of 1 (if enabled) or 0 (if not enabled) is given.
23: *
24: * All three commands return exit status -2 in case of serious errors
25: * such as bad usage, file access failures, etc.
26: *
27: * Typical usage: enable tty50
28: *
29: * -d option to ttystat added by John Hobson, 1-7-85.
30: */
31:
32: #include <stdio.h>
33: #include <signal.h>
34:
35: #define TTYS "/etc/ttys"
36: #define DIRSIZ 14
37:
38: #define ONCHAR '1'
39: #define OFFCHAR '0'
40:
41: struct terms {
42: char enable;
43: char linetype;
44: char gettype;
45: char name[DIRSIZ];
46: } t[200];
47:
48: extern char *rindex();
49:
50: main(argc, argv)
51: int argc;
52: char **argv;
53: {
54: register FILE *ttys;
55: register int c;
56: register struct terms *tp = t;
57: char *cp, *cmd, *ttysave, flagchar, display;
58: char *myname;
59: int found, current;
60: char fulldev[20] = {"/dev/"};
61:
62: if ((cmd = rindex(argv[0], '/')) == NULL) /* get command name */
63: cmd = argv[0];
64: else
65: cmd++;
66:
67: if(strcmp(cmd, "enable") == 0)
68: flagchar = ONCHAR; /* flag enable */
69: else if(strcmp(cmd, "disable") == 0)
70: flagchar = OFFCHAR; /* flag disable */
71: else if(strcmp(cmd, "ttystat") == 0)
72: flagchar = -1; /* flag ttystat */
73: else {
74: fprintf(stderr, "Unknown program name: %s\n", cmd);
75: exit(-2);
76: }
77:
78: if (flagchar == -1 && (strcmp(argv[1], "-d") == 0))
79: {
80: display = 1;
81: ttysave = argv[2];
82: argc--;
83: argv++;
84: }
85: else
86: display = 0;
87:
88: if (flagchar != -1 && argc < 2) /* disable and enable */
89: {
90: fprintf(stderr, "Usage: /etc/%s tty ...\n", cmd);
91: exit(-2);
92: }
93: else if (argc != 2) /* ttystat */
94: {
95: fprintf(stderr, "Usage: /etc/ttystat [-d] tty\n");
96: exit(-2);
97: }
98:
99: if ((ttys = fopen(TTYS, "r")) == NULL) {
100: fprintf(stderr, "%s: can't open %s\n", cmd, TTYS);
101: exit(-2);
102: }
103:
104: while ((c = getc(ttys)) != EOF) {
105: tp->enable = c;
106: #if NEWTTYS
107: tp->linetype = getc(ttys); /* get line type */
108: #endif
109: tp->gettype = getc(ttys);
110: for (cp = tp->name; (((c = getc(ttys)) != '\n') && c != EOF);
111: *cp++ = c);
112: if (cp != &tp->name[DIRSIZ])
113: *cp = '\0';
114: tp++;
115: }
116: myname = rindex(ttyname(0), '/') + 1;
117:
118: for (cp = *(++argv); --argc; cp = *(++argv)) {
119: if (strncmp(cp, "/dev/", 5) == 0)
120: cp += 5;
121: strcat(fulldev,cp);
122: found = 0;
123: for (tp = t; *tp->name; tp++) {
124: if (strncmp(cp, tp->name, DIRSIZ) == 0)
125: found++;
126: else
127: continue; /* go for next entry */
128: if (strncmp(cp, myname, DIRSIZ) == 0) {
129: fprintf(stderr,
130: "%s: invalid on your own terminal\n", cmd);
131: break;
132: }
133: current = tp->enable; /* get current status */
134: tp->enable = flagchar; /* set new status */
135: }
136: if (found == 0)
137: fprintf(stderr, "%s: tty line %s not found\n",
138: cmd, cp);
139: }
140: fclose(ttys);
141:
142: if (!found) /* if no tty's found */
143: exit(-1);
144:
145: if (flagchar == -1) /* if just checking status */
146: {
147: if (display == 1)
148: {
149: if (current == '1')
150: printf("%s is enabled\n", ttysave);
151: else
152: printf("%s is disabled\n", ttysave);
153: }
154: exit(current == '1' ? 1 : 0); /* return appropriate status */
155: }
156:
157: if ((ttys = fopen(TTYS, "w")) == NULL) {
158: fprintf(stderr, "%s: can't write %s\n", cmd, TTYS);
159: exit(-2);
160: }
161:
162: for (tp = t; *tp->name; tp++) {
163: putc(tp->enable, ttys);
164: #if NEWTTYS
165: putc(tp->linetype, ttys);
166: #endif
167: putc(tp->gettype, ttys);
168: for (cp = tp->name; *cp && cp < &tp->name[DIRSIZ];)
169: putc(*cp++, ttys);
170: putc('\n', ttys);
171: }
172: fclose(ttys);
173: if (kill(1, SIGQUIT) < 0) {
174: fprintf(stderr, "%s: can't signal init\n", cmd);
175: exit(-2);
176: }
177: sleep(4); /* wait for init, so "disable tty5;enable tty5" works */
178:
179: /* change the modes of the device to allow global r/w, this is to
180: * allow uucp to disable a local device and then call out on a local
181: * device.
182: */
183:
184: if (flagchar == '0'){
185: chmod(fulldev, 0666);
186: }
187:
188: exit(current == '1' ? 1 : 0); /* return appropriate status */
189: }
190:
191:
192:
193:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.