source: trunk/minix/lib/util/openpty.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 1.8 KB
RevLine 
[9]1/*
2 * openpty() tries to open a pty; applications won't have to
3 * duplicate this code all the time (or change it if the system
4 * pty interface changes).
5 *
6 * First version by Ben Gras <beng@few.vu.nl>,
7 * Initially heavily based on telnetd/pty.c
8 * by Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>.
9 *
10 */
11#include <libutil.h>
12#include <termios.h>
13#include <fcntl.h>
14#include <grp.h>
15#include <string.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <sys/ioc_tty.h>
23
24#define DEV_DIR "/dev"
25
26/*
27 * Allocate a PTY, by trying to open one repeatedly.
28 */
29int openpty(int *amaster, int *aslave, char *name,
30 struct termios *termp, struct winsize *winp)
31{
32 char buff[128], temp[128];
33 register int i, j;
34 int pty_fd = -1, gr;
35 static char tty_name[128];
36 struct group *ttygroup;
37 gid_t tty_gid = 0;
38
39 if(!amaster || !aslave) {
40 errno = EINVAL;
41 return -1;
42 }
43
44 for(i = 'p'; i < 'w'; i++) {
45 j = 0;
46 do {
47 sprintf(buff, "%s/pty%c%c",
48 DEV_DIR, i, (j < 10) ? j + '0' : j + 'a' - 10);
49
50 if((*amaster = open(buff, O_RDWR)) >= 0) {
51 sprintf(tty_name, "%s/tty%c%c", DEV_DIR,
52 i, (j < 10) ? j + '0' : j + 'a' - 10);
53 if((*aslave = open(tty_name, O_RDWR)) >= 0)
54 break;
55 }
56 close(*amaster);
57
58 j++;
59 if (j == 16) break;
60 } while(1);
61
62 /* Did we find one? */
63 if (j < 16) break;
64 }
65 if (*amaster < 0) { errno = ENOENT; return(-1); }
66
67 setgrent();
68 ttygroup = getgrnam("tty");
69 endgrent();
70 if(ttygroup) tty_gid = ttygroup->gr_gid;
71
72 if(name) strcpy(name, tty_name);
73
74 /* Ignore errors on these. */
75 chown(tty_name, getuid(), tty_gid);
76 chmod(tty_name, 0620); /* -rw--w---- */
77 if(termp) tcsetattr(*aslave, TCSAFLUSH, termp);
78 if(winp) ioctl(*aslave, TIOCSWINSZ, winp);
79
80 return(0);
81}
82
Note: See TracBrowser for help on using the repository browser.