ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/ptytty.C
(Generate patch)

Comparing libptytty/src/ptytty.C (file contents):
Revision 1.41 by sf-exg, Fri Nov 11 15:11:18 2011 UTC vs.
Revision 1.48 by root, Wed Jan 18 12:19:31 2012 UTC

1// This file is part of libptytty. Do not make local modifications.
2// http://software.schmorp.de/pkg/libptytty
3
4/*----------------------------------------------------------------------* 1/*----------------------------------------------------------------------*
5 * File: ptytty.C 2 * File: ptytty.C
6 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
7 * 4 *
8 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
28#include "config.h" 25#include "config.h"
29 26
30#include "ptytty.h" 27#include "ptytty.h"
31 28
32#include <cstdlib> 29#include <cstdlib>
30#include <cstdio>
33#include <cstring> 31#include <cstring>
34#include <csignal>
35 32
36#include <sys/types.h> 33#include <sys/types.h>
37#include <sys/stat.h> 34#include <sys/stat.h>
38#include <unistd.h> 35#include <unistd.h>
39#include <fcntl.h> 36#include <fcntl.h>
56#endif 53#endif
57#ifdef TTY_GID_SUPPORT 54#ifdef TTY_GID_SUPPORT
58#include <grp.h> 55#include <grp.h>
59#endif 56#endif
60 57
61#include <cstdio> 58#ifndef O_NOCTTY
59# define O_NOCTTY 0
60#endif
62 61
63///////////////////////////////////////////////////////////////////////////// 62/////////////////////////////////////////////////////////////////////////////
64 63
65/* ------------------------------------------------------------------------- * 64/* ------------------------------------------------------------------------- *
66 * GET PSEUDO TELETYPE - MASTER AND SLAVE * 65 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
76 get_pty (int *fd_tty, char **ttydev) 75 get_pty (int *fd_tty, char **ttydev)
77 { 76 {
78 int pfd; 77 int pfd;
79 78
80# if defined(HAVE_GETPT) 79# if defined(HAVE_GETPT)
81 pfd = getpt(); 80 pfd = getpt ();
82# elif defined(HAVE_POSIX_OPENPT) 81# elif defined(HAVE_POSIX_OPENPT)
83 pfd = posix_openpt (O_RDWR | O_NOCTTY); 82 pfd = posix_openpt (O_RDWR | O_NOCTTY);
84# else 83# else
85# ifdef _AIX 84# ifdef _AIX
86 pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0); 85 pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0);
130 get_pty (int *fd_tty, char **ttydev) 129 get_pty (int *fd_tty, char **ttydev)
131 { 130 {
132 int pfd; 131 int pfd;
133 char *slave; 132 char *slave;
134 133
135 slave = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 134 slave = _getpty (&pfd, O_RDWR | O_NOCTTY, 0622, 0);
136 135
137 if (slave != NULL) 136 if (slave != NULL)
138 { 137 {
139 *ttydev = strdup (slave); 138 *ttydev = strdup (slave);
140 return pfd; 139 return pfd;
156 const char *majors = "pqrstuvwxyzabcde"; 155 const char *majors = "pqrstuvwxyzabcde";
157 const char *minors = "0123456789abcdef"; 156 const char *minors = "0123456789abcdef";
158 157
159 for (i = 0; i < 256; i++) 158 for (i = 0; i < 256; i++)
160 { 159 {
161 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); 160 snprintf (pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
162 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); 161 snprintf (tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
163 162
164 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) 163 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
165 { 164 {
166 snprintf(pty_name, 32, "/dev/ptyp%d", i); 165 snprintf (pty_name, 32, "/dev/ptyp%d", i);
167 snprintf(tty_name, 32, "/dev/ttyp%d", i); 166 snprintf (tty_name, 32, "/dev/ttyp%d", i);
168 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) 167 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
169 continue; 168 continue;
170 } 169 }
171 170
172 if (access (tty_name, R_OK | W_OK) == 0) 171 if (access (tty_name, R_OK | W_OK) == 0)
328ptytty_unix::get () 327ptytty_unix::get ()
329{ 328{
330 /* get master (pty) */ 329 /* get master (pty) */
331 if ((pty = get_pty (&tty, &name)) < 0) 330 if ((pty = get_pty (&tty, &name)) < 0)
332 return false; 331 return false;
333
334 fcntl (pty, F_SETFL, O_NONBLOCK);
335 332
336 /* get slave (tty) */ 333 /* get slave (tty) */
337 if (tty < 0) 334 if (tty < 0)
338 { 335 {
339#ifndef NO_SETOWNER_TTYDEV 336#ifndef NO_SETOWNER_TTYDEV
376 ioctl (tty, I_PUSH, "ldterm"); 373 ioctl (tty, I_PUSH, "ldterm");
377 ioctl (tty, I_PUSH, "ttcompat"); 374 ioctl (tty, I_PUSH, "ttcompat");
378 } 375 }
379#endif 376#endif
380 377
378#if UTMP_SUPPORT
379# if defined(HAVE_STRUCT_UTMP) && !defined(HAVE_UTMP_PID)
380 int fd_stdin = dup (STDIN_FILENO);
381 dup2 (tty, STDIN_FILENO);
382
383 utmp_pos = ttyslot ();
384
385 dup2 (fd_stdin, STDIN_FILENO);
386 close (fd_stdin);
387# endif
388#endif
389
381 return true; 390 return true;
382} 391}
383 392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines