|
|
1.1 root 1: /*-
2: * Copyright (c) 1990 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted provided
6: * that: (1) source distributions retain this entire copyright notice and
7: * comment, and (2) distributions including binaries display the following
8: * acknowledgement: ``This product includes software developed by the
9: * University of California, Berkeley and its contributors'' in the
10: * documentation or other materials provided with the distribution and in
11: * all advertising materials mentioning features or use of this software.
12: * Neither the name of the University nor the names of its contributors may
13: * be used to endorse or promote products derived from this software without
14: * specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
16: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
17: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: static char sccsid[] = "@(#)pty.c 1.2 (Berkeley) 6/29/90";
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include <sys/file.h>
25: #include <sys/ioctl.h>
26: #include <termios.h>
27: #include <grp.h>
28: #include <errno.h>
29:
30: openpty(amaster, aslave, name, termp, winp)
31: int *amaster, *aslave;
32: char *name;
33: struct termios *termp;
34: struct winsize *winp;
35: {
36: register char *line = "/dev/ptyXX", *cp1, *cp2;
37: register master, slave, ruid, ttygid;
38: struct group *gr;
39: extern errno;
40:
41: if ((gr = getgrnam("tty")) != NULL)
42: ttygid = gr->gr_gid;
43: else
44: ttygid = -1;
45: ruid = getuid();
46:
47: for (cp1 = "pqrs"; *cp1; cp1++) {
48: line[8] = *cp1;
49: for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
50: line[9] = *cp2;
51: if ((master = open(line, O_RDWR, 0)) == -1) {
52: if (errno != EIO)
53: return (-1); /* out of ptys */
54: } else {
55: line[5] = 't';
56: (void) chown(line, ruid, ttygid);
57: (void) chmod(line, 0620);
58: (void) revoke(line);
59: if ((slave = open(line, O_RDWR, 0)) != -1) {
60: *amaster = master;
61: *aslave = slave;
62: if (name)
63: strcpy(name, line);
64: if (termp)
65: (void) tcsetattr(slave,
66: TCSAFLUSH, &termp);
67: if (winp)
68: (void) ioctl(slave, TIOCSWINSZ,
69: (char *)&winp);
70: return (0);
71: }
72: (void) close(master);
73: line[5] = 'p';
74: }
75: }
76: }
77: errno = ENOENT; /* out of ptys */
78: return (-1);
79: }
80:
81: forkpty(amaster, name, termp, winp)
82: int *amaster;
83: char *name;
84: struct termios *termp;
85: struct winsize *winp;
86: {
87: int master, slave, pid;
88:
89: if (openpty(&master, &slave, name, termp, winp) == -1)
90: return (-1);
91: switch (pid = fork()) {
92: case -1:
93: return (-1);
94: case 0:
95: /*
96: * child
97: */
98: (void) close(master);
99: login_tty(slave);
100: return (0);
101: }
102: /*
103: * parent
104: */
105: *amaster = master;
106: (void) close(slave);
107: return (pid);
108: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.