File: forkpty.c

package info (click to toggle)
procserv 2.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,356 kB
  • sloc: sh: 4,154; cpp: 1,715; ansic: 1,417; python: 870; makefile: 65
file content (39 lines) | stat: -rwxr-xr-x 1,182 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Process server for soft ioc
// Ralph Lange <ralph.lange@gmx.de> 03/25/2010
 * GNU Public License (GPLv3) applies - see www.gnu.org */

#include <unistd.h>
#include <stropts.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

int forkpty(int* fdm, char* name, void* x1, void* x2)
{
    /* From the Solaris manpage for pts(7D) */
    int   ptm, pts;
    pid_t pid;
    char* c;

    ptm = open("/dev/ptmx", O_RDWR);    /* open master */
    grantpt(ptm);                       /* change permission of slave */
    unlockpt(ptm);                      /* unlock slave */
    c = ptsname(ptm);                   /* get name of slave */
    if (c) strcpy(name, c);
    pts = open(name, O_RDWR);           /* open slave */
    ioctl(pts, I_PUSH, "ptem");         /* push ptem */
    ioctl(pts, I_PUSH, "ldterm");       /* push ldterm */

    /* From forums.sun.com */
    pid = fork();
    if (!pid) {                     /* child */
        close(ptm);
        dup2(pts, 0);               /* connect to slave fd */
        dup2(pts, 1);
        dup2(pts, 2);
    } else {
        *fdm = ptm;                 /* return fd to parent */
    }
    close(pts);
    return pid;
}