-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneframe.c
198 lines (179 loc) · 4.49 KB
/
oneframe.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Lyon's Cochlear Model, The Program
* Malcolm Slaney
* Advanced Technology Group
* Apple Computer, Inc.
* malcolm@apple.com
* November 1988
*
* This program implements a model of acoustic propagation and detection
* in the human cochlea. This model was first described by Richard F.
* Lyon. Please see
* Malcolm Slaney, "Lyon's Cochlear Model, the Mathematica
* Notebook," Apple Technical Report #13, 1988
* for more information. This report is available from the Apple
* Corporate Library.
*
* Warranty Information
* Even though Apple has reviewed this software, Apple makes no warranty
* or representation, either express or implied, with respect to this
* software, its quality, accuracy, merchantability, or fitness for a
* particular purpose. As a result, this software is provided "as is,"
* and you, its user, are assuming the entire risk as to its quality
* and accuracy.
*
* Copyright (c) 1988-1989 by Apple Computer, Inc
* All Rights Reserved.
*
* $Header: oneframe.c,v 2.2 90/11/06 20:52:51 malcolm Exp $
*
* $Log: oneframe.c,v $
* Revision 2.2 90/11/06 20:52:51 malcolm
* Changed copyright message.
*
* Revision 2.1 89/11/09 23:09:51 malcolm
* Removed an unused Buffer.
*
* Revision 2.0 89/07/25 18:58:48 malcolm
* Completely debugged and tested version on the following machines (roughly
* in order of performance):
* Cray, Stellar, SGI, Sun-4, Sequent Balance, Sun-3, VAX, Macintosh under
* both MPW and LightSpeed C.
*
* Revision 1.1 89/07/19 12:48:39 malcolm
* Initial revision
*
*/
#include <stdio.h>
#include <ctype.h>
FILE *ToRemote, *FromRemote;
int RemotePID;
OpenConnection(machine)
char *machine;
{
int i;
int topipe[2], frompipe[2];
i = pipe(topipe);
if (i < 0){
perror("OpenConnection: To Pipe failed");
exit(1);
}
pipe(frompipe);
if (i < 0){
perror("OpenConnection: From Pipe failed");
exit(1);
}
RemotePID = fork();
if (RemotePID < 0){
fprintf(stderr, "Couldn't fork to open connection to %s.\n",
machine);
fprintf(stderr,
"Try setting NCPUS to one to disable multiprocessing.\n");
exit(1);
}
if (RemotePID > 0){ /* Child */
i = dup2(topipe[0],0); /* Standard Input */
if (i < 0){
perror("OpenConnection: Dup2 ToPipe Failed");
exit(1);
}
i = dup2(frompipe[1],1); /* Standard Output */
if (i < 0){
perror("OpenConnection: Dup2 FromPipe Failed");
exit(1);
}
i = dup2(frompipe[1],2); /* Standard Error */
if (i < 0){
perror("OpenConnection: Dup2 FromPipe Failed");
exit(1);
}
i = close(topipe[0]);
if (i < 0){
perror("OpenConnection: Close ToPipe[0] Failed");
exit(1);
}
i = close(topipe[1]);
if (i < 0){
perror("OpenConnection: Close ToPipe[1] Failed");
exit(1);
}
i = close(frompipe[0]);
if (i < 0){
perror("OpenConnection: Close FromPipe[0] Failed");
exit(1);
}
i = close(frompipe[1]);
if (i < 0){
perror("OpenConnection: Close FromPipe[1] Failed");
exit(1);
}
execl("/usr/ucb/remsh", "remsh", machine, "exec",
"/bin/csh", "-i", 0);
fprintf(stderr, "Couldn't exec remsh from child.\n");
exit(1);
}
/* Parent */
ToRemote = fdopen(topipe[1],"a");
if (ToRemote == 0){
fprintf(stderr, "OpenConnection: FDOpen topipe failed.\n");
exit(1);
}
FromRemote = fdopen(frompipe[0], "r");
if (FromRemote == 0){
fprintf(stderr, "OpenConnection: FDOpen frompipe failed.\n");
exit(1);
}
i = close(topipe[0]);
if (i < 0){
perror("OpenConnection: Close ToPipe[0] Failed");
exit(1);
}
close(frompipe[1]);
if (i < 0){
perror("OpenConnection: Close FromPipe[1] Failed");
exit(1);
}
while((i = getc(FromRemote)) != '%')
putchar(i);
getc(FromRemote); /* Get the trailing space */
SendCommandToRemote("set prompt = \"ok\\\n\"\n");
}
SendCommandToRemote(command)
char *command;
{
int i;
char Buffer[BUFSIZ];
i = strchr(command, '\n');
if (i == 0){
command[strlen(command)] = '\n';
command[strlen(command)+1] = 0;
}
i = fputs(command,ToRemote);
fflush(ToRemote);
do {
fgets(Buffer, BUFSIZ, FromRemote);
#ifdef DEBUG
printf(" Got back %s from remote side.\n", Buffer);
#endif DEBUG
} while (!feof(FromRemote) && strcmp(Buffer,"ok\n") != 0);
if (feof(FromRemote)){
printf("Got an end of file from the remote command.\n");
exit(1);
}
}
CloseConnection(){
if (ToRemote)
fclose(ToRemote);
if (FromRemote)
fclose(FromRemote);
}
#ifdef MAIN
main(){
int i;
OpenConnection("dumbo");
for (i=0;i<10;i++){
SendCommandToRemote("date");
}
CloseConnection();
}
#endif MAIN