-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
408 lines (345 loc) · 8.6 KB
/
utility.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* utility.c: Rog-O-Matic XIV (CMU) Tue Mar 26 15:27:03 1985 - mlm
* Copyright (C) 1985 by A. Appel, G. Jacobson, L. Hamey, and M. Mauldin
*
* This file contains all of the miscellaneous system functions which
* determine the baud rate, time of day, etc.
*
* If CMU is not defined, then various functions from libcmu.a are
* defined here (otherwise the functions from -lcmu are used).
*/
# include <sgtty.h>
# include <stdio.h>
# include <signal.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <string.h>
# include <stdlib.h>
# include <pwd.h>
# include <curses.h>
# include "install.h"
# ifndef BSD42
# include <time.h>
# else
# include <sys/time.h>
# endif
# define TRUE 1
# define FALSE 0
/*
* baudrate: Determine the baud rate of the terminal
*/
#if !defined(_XOPEN_CURSES) && !defined(__NCURSES_H)
baudrate ()
{ static short baud_convert[] =
{ 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600 };
static struct sgttyb sg;
static short baud_rate;
gtty (fileno (stdin), &sg);
baud_rate = sg.sg_ospeed == 0 ? 1200
: sg.sg_ospeed < sizeof baud_convert / sizeof baud_convert[0]
? baud_convert[sg.sg_ospeed] : 9600;
return (baud_rate);
}
#endif
/*
* getname: get userid of player.
*/
char *getname ()
{ static char name[100];
struct passwd *pw;
name[0] = 0;
pw = getpwuid(getuid());
if (pw != NULL)
strncpy(name, pw->pw_name, 100);
name[99] = 0;
return (name);
}
/*
* wopen: Open a file for world access.
*/
FILE *wopen(fname, mode)
char *fname, *mode;
{ int oldmask;
FILE *newlog;
oldmask = umask (0111);
newlog = fopen (fname, mode);
umask (oldmask);
return (newlog);
}
/*
* fexists: return a boolean if the named file exists
*/
fexists (fn)
char *fn;
{ struct stat pbuf;
return (stat (fn, &pbuf) == 0);
}
/*
* filelength: Do a stat and return the length of a file.
*/
int filelength (f)
char *f;
{ struct stat sbuf;
if (stat (f, &sbuf) == 0)
return (sbuf.st_size);
else
return (-1);
}
/*
* critical: Disable interrupts
*/
static void (*hstat)(int), (*istat)(int), (*qstat)(int), (*pstat)(int);
critical ()
{
hstat = signal (SIGHUP, SIG_IGN);
istat = signal (SIGINT, SIG_IGN);
pstat = signal (SIGPIPE, SIG_IGN);
qstat = signal (SIGQUIT, SIG_IGN);
}
/*
* uncritical: Enable interrupts
*/
uncritical ()
{
signal (SIGHUP, hstat);
signal (SIGINT, istat);
signal (SIGPIPE, pstat);
signal (SIGQUIT, qstat);
}
/*
* reset_int: Set all interrupts to default
*/
reset_int ()
{
signal (SIGHUP, SIG_DFL);
signal (SIGINT, SIG_DFL);
signal (SIGPIPE, SIG_DFL);
signal (SIGQUIT, SIG_DFL);
}
/*
* int_exit: Set up a function to call if we get an interrupt
*/
int_exit (exitproc)
void (*exitproc)(int);
{
if (signal (SIGHUP, SIG_IGN) != SIG_IGN) signal (SIGHUP, exitproc);
if (signal (SIGINT, SIG_IGN) != SIG_IGN) signal (SIGINT, exitproc);
if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) signal (SIGPIPE, exitproc);
if (signal (SIGQUIT, SIG_IGN) != SIG_IGN) signal (SIGQUIT, exitproc);
}
/*
* lock_file: lock a file for a maximum number of seconds.
* Based on the method used in Rogue 5.2.
*/
# define NOWRITE 0
lock_file (lokfil, maxtime)
char *lokfil;
int maxtime;
{ int try;
struct stat statbuf;
long time ();
start:
if (creat (lokfil, NOWRITE) > 0)
return TRUE;
for (try = 0; try < 60; try++)
{ sleep (1);
if (creat (lokfil, NOWRITE) > 0)
return TRUE;
}
if (stat (lokfil, &statbuf) < 0)
{ creat (lokfil, NOWRITE);
return TRUE;
}
if (time (0) - statbuf.st_mtime > maxtime)
{ if (unlink (lokfil) < 0)
return FALSE;
goto start;
}
else
return FALSE;
}
/*
* unlock_file: Unlock a lock file.
*/
unlock_file (lokfil)
char *lokfil;
{ unlink (lokfil);
}
# ifndef CMU
/*
* quit: Defined for compatibility with Berkeley 4.2 system
*/
/* VARARGS2 */
quit (int code, char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
exit (code);
}
/*
* stlmatch -- match leftmost part of string
*
* Usage: i = stlmatch (big,small)
* int i;
* char *small, *big;
*
* Returns 1 iff initial characters of big match small exactly;
* else 0.
*
* HISTORY
* 18-May-82 Michael Mauldin (mlm) at Carnegie-Mellon University
* Ripped out of CMU lib for Rog-O-Matic portability
* 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
* Rewritten for VAX from Ken Greer's routine.
*
* Originally from klg (Ken Greer) on IUS/SUS UNIX
*/
int stlmatch (big, small)
char *small, *big;
{ register char *s, *b;
s = small;
b = big;
do
{ if (*s == '\0')
return (1);
}
while (*s++ == *b++);
return (0);
}
# endif
#if 0
/*
* putenv -- put value into environment
*
* Usage: i = putenv (name,value)
* int i;
* char *name, *value;
*
* Putenv associates "value" with the environment parameter "name".
* If "value" is 0, then "name" will be deleted from the environment.
* Putenv returns 0 normally, -1 on error (not enough core for malloc).
*
* Putenv may need to add a new name into the environment, or to
* associate a value longer than the current value with a particular
* name. So, to make life simpler, putenv() copies your entire
* environment into the heap (i.e. malloc()) from the stack
* (i.e. where it resides when your process is initiated) the first
* time you call it.
*
* HISTORY
* 25-Nov-82 Michael Mauldin (mlm) at Carnegie-Mellon University
* Ripped out of CMU lib for Rog-O-Matic portability
* 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
* Created for VAX. Too bad Bell Labs didn't provide this. It's
* unfortunate that you have to copy the whole environment onto the
* heap, but the bookkeeping-and-not-so-much-copying approach turns
* out to be much hairier. So, I decided to do the simple thing,
* copying the entire environment onto the heap the first time you
* call putenv(), then doing realloc() uniformly later on.
* Note that "putenv(name,getenv(name))" is a no-op; that's the reason
* for the use of a 0 pointer to tell putenv() to delete an entry.
*
*/
#define EXTRASIZE 5 /* increment to add to env. size */
static int envsize = -1; /* current size of environment */
extern char **environ; /* the global which is your env. */
static int findenv (); /* look for a name in the env. */
static int newenv (); /* copy env. from stack to heap */
static int moreenv (); /* incr. size of env. */
int putenv (name, value)
char *name, *value;
{ register int i, j;
register char *p;
if (envsize < 0)
{ /* first time putenv called */
if (newenv () < 0) /* copy env. to heap */
return (-1);
}
i = findenv (name); /* look for name in environment */
if (value)
{ /* put value into environment */
if (i < 0)
{ /* name must be added */
for (i = 0; environ[i]; i++);
if (i >= (envsize - 1))
{ /* need new slot */
if (moreenv () < 0)
return (-1);
}
p = malloc (strlen (name) + strlen (value) + 2);
if (p == 0) /* not enough core */
return (-1);
environ[i + 1] = 0; /* new end of env. */
}
else
{ /* name already in env. */
p = realloc (environ[i],
strlen (name) + strlen (value) + 2);
if (p == 0)
return (-1);
}
sprintf (p, "%s=%s", name, value);/* copy into env. */
environ[i] = p;
}
else
{ /* delete name from environment */
if (i >= 0)
{ /* name is currently in env. */
free (environ[i]);
for (j = i; environ[j]; j++);
environ[i] = environ[j - 1];
environ[j - 1] = 0;
}
}
return (0);
}
static int findenv (name)
char *name;
{ register char *namechar, *envchar;
register int i, found;
found = 0;
for (i = 0; environ[i] && !found; i++)
{ envchar = environ[i];
namechar = name;
while (*namechar && (*namechar == *envchar))
{ namechar++;
envchar++;
}
found = (*namechar == '\0' && *envchar == '=');
}
return (found ? i - 1 : -1);
}
static int newenv ()
{ register char **env, *elem;
register int i, esize;
for (i = 0; environ[i]; i++);
esize = i + EXTRASIZE + 1;
env = (char **) malloc (esize * sizeof (elem));
if (env == 0)
return (-1);
for (i = 0; environ[i]; i++)
{ elem = malloc (strlen (environ[i]) + 1);
if (elem == 0)
return (-1);
env[i] = elem;
strcpy (elem, environ[i]);
}
env[i] = 0;
environ = env;
envsize = esize;
return (0);
}
static int moreenv ()
{ register int esize;
register char **env;
esize = envsize + EXTRASIZE;
env = (char **) realloc (environ, esize * sizeof (*env));
if (env == 0)
return (-1);
environ = env;
envsize = esize;
return (0);
}
#endif