forked from dungeons-of-moria/icmoria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix.c
385 lines (343 loc) · 8.56 KB
/
unix.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* unix/unix.c: UNIX dependent code. -CJS-
Copyright (c) 1989-91 James E. Wilson, Christopher J. Stuart
This software may be copied and distributed for educational, research, and
not for profit purposes provided that this copyright and statement are
included in all such copies. */
/* defines NULL */
#include <stdio.h>
/* defines CTRL */
#include <sys/ioctl.h>
/* defines TRUE and FALSE */
#include <curses.h>
#include <string.h>
#if 0
#include "config.h"
#include "constant.h"
#include "types.h"
#endif
#include "term.h"
#if defined(unix) || defined(__NetBSD__)
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <unistd.h>
#if defined(SYS_V) && defined(lint)
/* for AIX, prevent hundreds of unnecessary lint errors, must define before
signal.h is included */
#define _h_IEEETRAP
typedef struct { int stuff; } fpvmach;
#endif
#include <signal.h>
#ifdef M_XENIX
#include <sys/types.h>
#include <sys/select.h>
/* For various selects from TCP/IP. */
#define bzero(addr,n) memset((char *)addr, 0, n)
#endif
#ifndef USG
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/param.h>
#endif
#ifdef __linux__
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#ifdef USG
#include <string.h>
#if 0
/* Used to include termio.h here, but this is unnecessary because it is
included in curses.h, and some systems fail when it gets included
twice. */
#include <termio.h>
#endif
#include <fcntl.h>
#else
#include <strings.h>
#if defined(atarist) && defined(__GNUC__)
/* doesn't have <sys/wait.h> */
#else
#include <sys/wait.h>
#endif
#endif
/* This must be included after fcntl.h, which has a prototype for `open'
on some systems. Otherwise, the `open' prototype conflicts with the
`topen' declaration. */
#if 0
#include "externs.h"
#endif
#include <pwd.h>
#include <sys/errno.h>
#ifdef USG
struct passwd *getpwuid();
struct passwd *getpwnam();
#endif
#if defined(SYS_V) && defined(lint)
struct screen { int dumb; };
#endif
/* Fooling lint. Unfortunately, c defines all the TIO constants to be long,
and lint expects them to be int. Also, ioctl is sometimes called with just
two arguments. The following definition keeps lint happy. It may need to be
reset for different systems. */
#ifdef lint
#ifdef Pyramid
/* Pyramid makes constants greater than 65535 into long! Gakk! -CJS- */
/*ARGSUSED*/
/*VARARGS2*/
static Ioctl(i, l, p) long l; char *p; { return 0; }
#else
/*ARGSUSED*/
/*VARARGS2*/
static Ioctl(i, l, p) char *p; { return 0; }
#endif
#define ioctl Ioctl
#endif
/* Provides for a timeout on input. Does a non-blocking read, consuming the
data if any, and then returns 1 if data was read, zero otherwise.
Porting:
In systems without the select call, but with a sleep for
fractional numbers of seconds, one could sleep for the time
and then check for input.
In systems which can only sleep for whole number of seconds,
you might sleep by writing a lot of nulls to the terminal, and
waiting for them to drain, or you might hack a static
accumulation of times to wait. When the accumulation reaches a
certain point, sleep for a second. There would need to be a
way of resetting the count, with a call made for commands like
run or rest. */
int check_input(microsec)
int microsec;
{
#if defined(USG) && !defined(M_XENIX) && !defined(__linux__)
int arg, result;
#else
struct timeval tbuf;
int ch;
#if defined(BSD4_3) || defined(M_XENIX) || defined(__linux__)
fd_set smask;
#else
int smask;
#endif
#endif
/* Return true if a read on descriptor 1 will not block. */
#if !defined(USG) || defined(M_XENIX) || defined(__linux__)
tbuf.tv_sec = 0;
tbuf.tv_usec = microsec;
#if defined(BSD4_3) || defined(M_XENIX) || defined(__linux__)
FD_ZERO(&smask);
FD_SET(fileno(stdin), &smask);
if (select(1, &smask, (fd_set *)0, (fd_set *)0, &tbuf) == 1)
#else
smask = 1; /* i.e. (1 << 0) */
if (select(1, &smask, (int *)0, (int *)0, &tbuf) == 1)
#endif
{
ch = getch();
/* check for EOF errors here, select sometimes works even when EOF */
if (ch == -1)
{
eof_flag++;
return 0;
}
return 1;
}
else
return 0;
#else /* SYS V code follows */
if (microsec != 0 && (turn & 0x7F) == 0)
(void) sleep (1); /* mod 128, sleep one sec every 128 turns */
/* Can't check for input, but can do non-blocking read, so... */
/* Ugh! */
arg = 0;
arg = fcntl(0, F_GETFL, arg);
arg |= O_NDELAY;
(void) fcntl(0, F_SETFL, arg);
result = getch();
arg = 0;
arg = fcntl(0, F_GETFL, arg);
arg &= ~O_NDELAY;
(void) fcntl(0, F_SETFL, arg);
if (result == -1)
return 0;
else
return 1;
#endif
}
#if 0
/* This is not used, however, this should be compared against shell_out
in io.c */
/* A command for the operating system. Standard library function
'system' is unsafe, as it leaves various file descriptors
open. This also is very careful with signals and interrupts,
and does rudimentary job control, and puts the terminal back
in a standard mode. */
int system_cmd(p)
char *p;
{
int pgrp, pid, i, mask;
union wait w;
extern char *getenv();
mask = sigsetmask(~0); /* No interrupts. */
restore_term(); /* Terminal in original state. */
/* Are we in the control terminal group? */
if (ioctl(0, TIOCGPGRP, (char *)&pgrp) < 0 || pgrp != getpgrp(0))
pgrp = -1;
pid = fork();
if (pid < 0)
{
(void) sigsetmask(mask);
moriaterm();
return -1;
}
if (pid == 0)
{
(void) sigsetmask(0); /* Interrupts on. */
/* Transfer control terminal. */
if (pgrp >= 0)
{
i = getpid();
(void) ioctl(0, TIOCSPGRP, (char *)&i);
(void) setpgrp(i, i);
}
for(i = 2; i < 30; i++)
(void) close(i); /* Close all but standard in and out.*/
(void) dup2(1, 2); /* Make standard error as standard out. */
if (p == 0 || *p == 0)
{
p = getenv("SHELL");
if (p)
execl(p, p, 0);
execl("/bin/sh", "sh", 0);
}
else
execl("/bin/sh", "sh", "-c", p, 0);
_exit(1);
}
/* Wait for child termination. */
for(;;)
{
i = wait3(&w, WUNTRACED, (struct rusage *)0);
if (i == pid)
{
if (WIFSTOPPED(w))
{
/* Stop outselves, if child stops. */
(void) kill(getpid(), SIGSTOP);
/* Restore the control terminal, and restart subprocess. */
if (pgrp >= 0)
(void) ioctl(0, TIOCSPGRP, (char *)&pid);
(void) killpg(pid, SIGCONT);
}
else
break;
}
}
/* Get the control terminal back. */
if (pgrp >= 0)
(void) ioctl(0, TIOCSPGRP, (char *)&pgrp);
(void) sigsetmask(mask); /* Interrupts on. */
moriaterm(); /* Terminal in moria mode. */
return 0;
}
#endif
#if 0
#ifdef USG
unsigned short getuid();
#else
#ifndef SECURE
#if defined(BSD4_3) || defined(__NetBSD__)
uid_t getuid();
#else /* other BSD versions */
int getuid();
#endif
#endif
#endif
#endif
/* Find a default user name from the system. */
void user_name(buf)
char *buf;
{
extern char *getlogin();
struct passwd *pwline;
register char *p;
p = getlogin();
if (p && p[0])
(void) strcpy(buf, p);
else
{
pwline = getpwuid((int)getuid());
if (pwline)
(void) strcpy(buf, pwline->pw_name);
}
if (!buf[0])
(void) strcpy(buf, "X"); /* Gotta have some name */
}
/* expands a tilde at the beginning of a file name to a users home
directory */
int tilde(file, exp)
char *file, *exp;
{
*exp = '\0';
if (file)
{
if (*file == '~')
{
char user[128];
struct passwd *pw = NULL;
int i = 0;
user[0] = '\0';
file++;
while (*file != '/' && i < sizeof(user))
user[i++] = *file++;
user[i] = '\0';
if (i == 0)
{
char *login = (char *) getlogin();
if (login != NULL)
(void) strcpy (user, login);
else if ((pw = getpwuid(getuid())) == NULL)
return 0;
}
if (pw == NULL && (pw = getpwnam(user)) == NULL)
return 0;
(void) strcpy (exp, pw->pw_dir);
}
(void) strcat(exp, file);
return 1;
}
return 0;
}
/* undefine these so that tfopen and topen will work */
#undef fopen
#undef open
/* open a file just as does fopen, but allow a leading ~ to specify a home
directory */
FILE *tfopen(file, mode)
char *file;
char *mode;
{
char buf[1024];
extern int errno;
if (tilde(file, buf))
return (fopen(buf, mode));
errno = ENOENT;
return NULL;
}
/* open a file just as does open, but expand a leading ~ into a home directory
name */
/*
int topen(file, flags, mode)
char *file;
int flags, mode;
{
char buf[1024];
extern int errno;
if (tilde(file, buf))
return (open(buf, flags, mode));
errno = ENOENT;
return -1;
}
*/
#endif