Changeset 8076


Ignore:
Timestamp:
11/01/04 16:37:19 (19 years ago)
Author:
piso
Message:

/dev/ptmx

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/jpty/jpty.c

    r2 r8076  
    22 * jpty.c
    33 *
    4  * Copyright (C) 2000-2001 Peter Graves
     4 * Copyright (C) 2000-2004 Peter Graves
    55 *
    66 * This program is free software; you can redistribute it and/or
     
    2525#include <sys/stat.h>
    2626#include <termios.h>
     27#define __USE_XOPEN
    2728#endif
    2829
     
    4041{
    4142#ifndef __CYGWIN__
    42     char slave_name[20];
     43    char slave_name[256];
    4344    pid_t pid;
    4445    int fdm;
     
    4647    int i = 1;
    4748
    48     if (argc < 2) exit(1);
     49    if (argc < 2)
     50  exit(1);
    4951
    5052    /* Check for -cd option. */
    51     if (!strcmp("-cd", argv[i]))
    52     {
     53    if (!strcmp("-cd", argv[i])) {
    5354        /* Next arg is directory to change to. */
    54         if (++i < argc)
    55         {
     55        if (++i < argc) {
    5656            if (chdir(argv[i]) < 0)
    5757                exit(1);
     
    6161
    6262    /* We should not be out of args here! */
    63     if (i >= argc) exit(1);
     63    if (i >= argc)
     64  exit(1);
    6465
    6566#ifdef __CYGWIN__
     
    6869    exit(1); /* Not reached. */
    6970#else
    70 
    7171    fdm = open_master_pty(slave_name, sizeof(slave_name));
    7272
    73     if (fdm < 0) exit(1);
     73    if (fdm < 0)
     74  exit(1);
    7475
    7576    pid = fork();
    7677
    77     if (pid < 0) exit(1);
     78    if (pid < 0)
     79  exit(1);
    7880
    79     if (pid == 0)
    80     {
     81    if (pid == 0) {
    8182        /* Child process. */
    8283        int slave;
     
    8485        close(fdm);
    8586
    86         if (setsid() < 0) exit(1);
     87        if (setsid() < 0)
     88      exit(1);
    8789
    8890        slave = open(slave_name, O_RDWR);
    8991
    90         if (slave < 0) exit(1);
     92        if (slave < 0)
     93      exit(1);
    9194
    9295#ifdef __linux__
    93         if (ioctl(slave, TIOCSCTTY, NULL) < 0) exit(1);
     96        if (ioctl(slave, TIOCSCTTY, NULL) < 0)
     97      exit(1);
    9498#endif
     99        if (dup2(slave, STDIN_FILENO) != STDIN_FILENO)
     100      exit(1);
     101        if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO)
     102      exit(1);
     103        if (dup2(slave, STDERR_FILENO) != STDERR_FILENO)
     104      exit(1);
    95105
    96         if (dup2(slave, STDIN_FILENO) != STDIN_FILENO) exit(1);
    97 
    98         if (dup2(slave, STDOUT_FILENO) != STDOUT_FILENO) exit(1);
    99 
    100         if (dup2(slave, STDERR_FILENO) != STDERR_FILENO) exit(1);
    101 
    102         if (slave > STDERR_FILENO) close(slave);
     106        if (slave > STDERR_FILENO)
     107      close(slave);
    103108
    104109        set_noecho(STDIN_FILENO);
     
    113118
    114119    /* Parent process. */
    115 
    116120    loop(fdm);
    117 
    118121    exit(0);
    119 
    120122#endif
    121123}
     
    125127{
    126128    struct termios t;
    127     if (tcgetattr(fd, &t) < 0) exit(1);
     129    if (tcgetattr(fd, &t) < 0)
     130  exit(1);
    128131    t.c_lflag &= ~(ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
    129132    t.c_oflag &= ~(ONLCR);
    130     if (tcsetattr(fd, TCSANOW, &t) < 0) exit(1);
     133    if (tcsetattr(fd, TCSANOW, &t) < 0)
     134  exit(1);
    131135}
    132136
     
    138142    int done = 0;
    139143
    140     while (!done)
    141     {
     144    while (!done) {
    142145        FD_ZERO(&fdset);
    143146        FD_SET(STDIN_FILENO, &fdset);
     
    146149        select(fdm + 1, &fdset, NULL, NULL, NULL);
    147150
    148         if (FD_ISSET(fdm, &fdset))
    149         {
     151        if (FD_ISSET(fdm, &fdset)) {
    150152            int i = read(fdm, buf, sizeof(buf));
    151153
     
    156158        }
    157159
    158         if (FD_ISSET(STDIN_FILENO, &fdset))
    159         {
     160        if (FD_ISSET(STDIN_FILENO, &fdset)) {
    160161            int i = read(STDIN_FILENO, buf, sizeof(buf));
    161162
     
    170171static int open_master_pty(char *namebuf, size_t bufsize)
    171172{
    172     char pattern[] = "/dev/ptyXX";
     173    int fdm;
     174    char *sname;
    173175
    174     if (bufsize >= sizeof(pattern))
    175     {
    176         int i, j;
     176    fdm = open("/dev/ptmx", O_RDWR);
     177    if (fdm < 0)
     178  return -1;
    177179
    178         strcpy(namebuf, pattern);
     180    unlockpt(fdm);
     181    grantpt(fdm);
    179182
    180         for (i = 0; i < 16; i++)
    181         {
    182             struct stat statbuf;
     183    sname = ptsname(fdm);
     184    if (strlen(sname) >= bufsize)
     185  return -1;
     186    strcpy(namebuf, sname);
    183187
    184             namebuf[8] = "pqrstuvwxyzabcde"[i];
    185             namebuf[9] = '0';
    186 
    187             if (stat(namebuf, &statbuf) < 0)
    188                 continue;
    189 
    190             for (j = 0; j < 16; j++)
    191             {
    192                 int fdm;
    193 
    194                 namebuf[9] = "0123456789abcdef"[j];
    195 
    196                 fdm = open(namebuf, O_RDWR);
    197 
    198                 if (fdm > 0)
    199                 {
    200                     namebuf[5] = 't';
    201                     return fdm;
    202                 }
    203             }
    204         }
    205     }
    206 
    207     return -1;
     188    return fdm;
    208189}
    209190#endif
    210 
Note: See TracChangeset for help on using the changeset viewer.