~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/util/pty/getpty.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman, Russ Allbery, Sam Hartman
  • Date: 2008-08-21 10:41:41 UTC
  • mfrom: (11.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080821104141-a0f9c4o4cpo8xd0o
Tags: 1.6.dfsg.4~beta1-4
[ Russ Allbery ]
* Translation updates:
  - Swedish, thanks Martin Bagge.  (Closes: #487669, #491774)
  - Italian, thanks Luca Monducci.  (Closes: #493962)

[ Sam Hartman ]
* Translation Updates:
    - Dutch, Thanks Vincent Zweije, Closes: #495733

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * pty_getpty: open a PTY master.
3
 
 *
4
 
 * Copyright 1995, 1996 by the Massachusetts Institute of Technology.
5
 
 *
6
 
 * 
7
 
 * Permission to use, copy, modify, and distribute this software and
8
 
 * its documentation for any purpose and without fee is hereby
9
 
 * granted, provided that the above copyright notice appear in all
10
 
 * copies and that both that copyright notice and this permission
11
 
 * notice appear in supporting documentation, and that the name of
12
 
 * M.I.T. not be used in advertising or publicity pertaining to
13
 
 * distribution of the software without specific, written prior
14
 
 * permission.  Furthermore if you modify this software you must label
15
 
 * your software as modified software and not distribute it in such a
16
 
 * fashion that it might be confused with the original M.I.T. software.
17
 
 * M.I.T. makes no representations about the suitability
18
 
 * of this software for any purpose.  It is provided "as is" without
19
 
 * express or implied warranty.
20
 
 * 
21
 
 */
22
 
 
23
 
#include "com_err.h"
24
 
#include "libpty.h"
25
 
#include "pty-int.h"
26
 
 
27
 
long
28
 
ptyint_getpty_ext(int *fd, char *slave, int slavelength, int do_grantpt)
29
 
{
30
 
#if !defined(HAVE__GETPTY) && !defined(HAVE_OPENPTY)
31
 
    char *cp;
32
 
    char *p;
33
 
    int i,ptynum;
34
 
    struct stat stb;
35
 
    char slavebuf[1024];
36
 
#endif
37
 
 
38
 
#ifdef HAVE__GETPTY
39
 
    char *slaveret; /*Temporary to hold pointer to slave*/
40
 
#endif /*HAVE__GETPTY*/
41
 
 
42
 
#ifdef HAVE_OPENPTY
43
 
    int slavefd;
44
 
 
45
 
    if(openpty(fd, &slavefd, slave, (struct termios *) 0,
46
 
         (struct winsize *) 0)) return 1;
47
 
    close(slavefd);
48
 
    return 0;
49
 
#else /*HAVE_OPENPTY*/
50
 
#ifdef HAVE__GETPTY
51
 
    /* This code is included for Irix; as of version 5.3, Irix has /dev/ptmx,
52
 
     * but it fails to work properly; even after calling unlockpt,
53
 
     * root gets permission denied opening the pty.
54
 
     * The code to support _getpty should be removed if Irix gets working
55
 
     * streams ptys in favor of maintaining the least needed code
56
 
     * paths.
57
 
     */
58
 
    if ((slaveret = _getpty(fd, O_RDWR|O_NDELAY, 0600, 0)) == 0) {
59
 
        *fd = -1;
60
 
        return PTY_GETPTY_NOPTY;
61
 
    }
62
 
    if (strlen(slaveret) > slavelength - 1) {
63
 
        close(*fd);
64
 
        *fd = -1;
65
 
        return PTY_GETPTY_SLAVE_TOOLONG;
66
 
    }
67
 
    else strcpy(slave, slaveret);
68
 
    return 0;
69
 
#else /*HAVE__GETPTY*/
70
 
    
71
 
    *fd = open("/dev/ptym/clone", O_RDWR|O_NDELAY);     /* HPUX*/
72
 
#ifdef HAVE_STREAMS
73
 
    if (*fd < 0) *fd = open("/dev/ptmx",O_RDWR|O_NDELAY); /*Solaris*/
74
 
#endif
75
 
    if (*fd < 0) *fd = open("/dev/ptc", O_RDWR|O_NDELAY); /* AIX */
76
 
    if (*fd < 0) *fd = open("/dev/pty", O_RDWR|O_NDELAY); /* sysvimp */
77
 
 
78
 
    if (*fd >= 0) {
79
 
 
80
 
#if defined(HAVE_GRANTPT)&&defined(HAVE_STREAMS)
81
 
        if (do_grantpt)
82
 
            if (grantpt(*fd) || unlockpt(*fd)) return PTY_GETPTY_STREAMS;
83
 
#endif
84
 
    
85
 
#ifdef HAVE_PTSNAME
86
 
        p = ptsname(*fd);
87
 
#else
88
 
#ifdef  HAVE_TTYNAME
89
 
        p = ttyname(*fd);
90
 
#else
91
 
        /* XXX If we don't have either what do we do */
92
 
#endif
93
 
#endif
94
 
        if (p) {
95
 
            if (strlen(p) > slavelength - 1) {
96
 
                    close (*fd);
97
 
                    *fd = -1;
98
 
                    return PTY_GETPTY_SLAVE_TOOLONG;
99
 
            }
100
 
            strcpy(slave, p);
101
 
            return 0;
102
 
        }
103
 
 
104
 
        if (fstat(*fd, &stb) < 0) {
105
 
            close(*fd);
106
 
            return PTY_GETPTY_FSTAT;
107
 
        }
108
 
        ptynum = (int)(stb.st_rdev&0xFF);
109
 
        sprintf(slavebuf, "/dev/ttyp%x", ptynum);
110
 
        if (strlen(slavebuf) > slavelength - 1) {
111
 
            close(*fd);
112
 
            *fd = -1;
113
 
            return PTY_GETPTY_SLAVE_TOOLONG;
114
 
        }
115
 
        strncpy(slave, slavebuf, slavelength);
116
 
        return 0;
117
 
    } else {
118
 
        for (cp = "pqrstuvwxyzPQRST";*cp; cp++) {
119
 
            sprintf(slavebuf,"/dev/ptyXX");
120
 
            slavebuf[sizeof("/dev/pty") - 1] = *cp;
121
 
            slavebuf[sizeof("/dev/ptyp") - 1] = '0';
122
 
            if (stat(slavebuf, &stb) < 0)
123
 
                break;
124
 
            for (i = 0; i < 16; i++) {
125
 
                slavebuf[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i];
126
 
                *fd = open(slavebuf, O_RDWR);
127
 
                if (*fd < 0) continue;
128
 
 
129
 
                /* got pty */
130
 
                slavebuf[sizeof("/dev/") - 1] = 't';
131
 
                if (strlen(slavebuf) > slavelength -1) {
132
 
                    close(*fd);
133
 
                    *fd = -1;
134
 
                    return PTY_GETPTY_SLAVE_TOOLONG;
135
 
                }
136
 
                strncpy(slave, slavebuf, slavelength);
137
 
                return 0;
138
 
            }
139
 
        }
140
 
        return PTY_GETPTY_NOPTY;
141
 
    }
142
 
#endif /*HAVE__GETPTY*/
143
 
#endif /* HAVE_OPENPTY */
144
 
}
145
 
 
146
 
long
147
 
pty_getpty(int *fd, char *slave, int slavelength)
148
 
{
149
 
    return ptyint_getpty_ext(fd, slave, slavelength, 1);
150
 
}