Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2013, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#include "openaxiom-c-macros.h"
37
#include <stdlib.h>
38
#include <unistd.h>
39
#include <stdio.h>
40
#include <fcntl.h>
41
#include <string.h>
42
43
#ifdef HAVE_SYS_IOCTL_H
44
# include <sys/ioctl.h>
45
#endif
46
#ifdef HAVE_PTY_H
47
# include <pty.h>
48
#endif
49
#ifdef HAVE_UTIL_H
50
# include <util.h>
51
#endif
52
#ifdef HAVE_TERMIOS_H
53
# include <termios.h>
54
#endif
55
#ifdef HAVE_LIBUTIL_H
56
# include <sys/types.h>
57
# include <libutil.h>
58
#endif
59
60
61
#include "openpty.h"
62
63
#if defined(SUNplatform)
64
static void
65
makeNextPtyNames(char *cont,char * serv)
66
{
67
static int channelNo = 0;
68
static char group[] = "pqrstuvwxyzPQRST";
69
static int groupNo = 0;
70
71
sprintf(cont, "/dev/pty%c%x", group[groupNo], channelNo);
72
sprintf(serv, "/dev/tty%c%x", group[groupNo], channelNo);
73
channelNo++; /* try next */
74
if (channelNo == 16) { /* move to new group */
75
channelNo = 0;
76
groupNo++;
77
if (groupNo == 16) groupNo = 0; /* recycle */
78
}
79
}
80
#endif
81
82
83
/*
84
* The main function is ptyopen. It simply opens up both sides of a
85
* pseudo-terminal. It uses and saves the pathnames for
86
* the devices which were actually opened.
87
*
88
* If it fails it simply exits the program.
89
*
90
*
91
* ptyopen(controller, server, controllerPath, serverPath)
92
* int *controller; The file descriptor for controller side of the pty
93
* int *server; The file descriptor for the server side
94
* char *serverPath;
95
*
96
* The path name vars should be declared of size 11 or more
97
*/
98
99
100
int
101
ptyopen(int *controller,int * server,char * serverPath)
102
{
103
#if HAVE_DECL_OPENPTY
104
return openpty(controller,server, serverPath, NULL, NULL);
105
#elif defined(SUNplatform)
106
int looking = 1, i;
107
int oflag = O_RDWR; /* flag for opening the pty */
108
char controllerPath[128];
109
110
for (i = 0; looking && i < 1000; i++) {
111
makeNextPtyNames(controllerPath, serverPath);
112
if (access(controllerPath, 6) != 0) continue;
113
*controller = open(controllerPath, oflag, 0);
114
if (*controller >= 0) {
115
*server = open(serverPath, oflag, 0);
116
if (*server > 0)
117
looking = 0;
118
else
119
close(*controller);
120
}
121
}
122
if (looking) {
123
fprintf(stderr, "Couldn't find a free pty.\n");
124
exit(-1);
125
}
126
return (*controller);
127
#elif defined(SUN4OS5platform)
128
extern int grantpt(int);
129
extern int unlockpt(int);
130
extern char* ptsname(int);
131
int fdm,fds;
132
char *slavename;
133
134
/* open master */
135
if ((fdm = open("/dev/ptmx", O_RDWR)) < 0 )
136
perror("ptyopen: Failed to open /dev/ptmx");
137
else {
138
/* change permission ofslave */
139
if (grantpt(fdm) < 0)
140
perror("ptyopen: Failed to grant access to slave device");
141
/* unlock slave */
142
if (unlockpt(fdm) < 0)
143
perror("ptyopen: Failed to unlock master/slave pair");
144
/* get name of slave */
145
if ((slavename = ptsname(fdm)) == NULL)
146
perror("ptyopen: Failed to get name of slave device");
147
/* open slave */
148
if ((fds = open(slavename, O_RDWR)) < 0 )
149
perror("ptyopen: Failed to open slave");
150
else {
151
/* push ptem */
152
if (ioctl(fds, I_PUSH, "ptem") < 0)
153
perror("ptyopen: Failed to push ptem");
154
/* push ldterm */
155
if (ioctl(fds, I_PUSH, "ldterm") < 0)
156
perror("ptyopen: Failed to push idterm");
157
strcpy(serverPath,slavename);
158
*controller=fdm;
159
*server=fds;
160
}
161
}
162
return(fdm);
163
#else
164
# error "don't know how to open a pty"
165
#endif
166
}
167
168
169
170