util.c revision 1.1 1 /* $Header: /tank/opengrok/rsync2/NetBSD/src/games/warp/util.c,v 1.1 2020/11/09 23:37:05 kamil Exp $ */
2
3 /* $Log: util.c,v $
4 /* Revision 1.1 2020/11/09 23:37:05 kamil
5 /* Add Warp Kit, Version 7.0 by Larry Wall
6 /*
7 /* Warp is a real-time space war game that doesn't get boring very quickly.
8 /* Read warp.doc and the manual page for more information.
9 /*
10 /* games/warp originally distributed with 4.3BSD-Reno, is back to the BSD
11 /* world via NetBSD. Its remnants were still mentioned in games/Makefile.
12 /*
13 /* Larry Wall, the original author and the copyright holder, generously
14 /* donated the game and copyright to The NetBSD Foundation, Inc.
15 /*
16 /* Import the game sources as-is from 4.3BSD-Reno, with the cession
17 /* of the copyright and license to BSD-2-clause NetBSD-style.
18 /*
19 /* Signed-off-by: Larry Wall <larry (at) wall.org>
20 /* Signed-off-by: Kamil Rytarowski <kamil (at) netbsd.org>
21 /*
22 * Revision 7.0.1.2 86/10/20 12:07:46 lwall
23 * Made all exits reset tty.
24 *
25 * Revision 7.0.1.1 86/10/16 10:54:02 lwall
26 * Added Damage. Fixed random bugs.
27 *
28 * Revision 7.0 86/10/08 15:14:31 lwall
29 * Split into separate files. Added amoebas and pirates.
30 *
31 */
32
33 #include "EXTERN.h"
34 #include "warp.h"
35 #include "sys/dir.h"
36 #include "object.h"
37 #include "sig.h"
38 #include "term.h"
39 #include "INTERN.h"
40 #include "util.h"
41
42 void
43 util_init()
44 {
45 ;
46 }
47
48 void
49 movc3(len,src,dest)
50 #ifdef vax
51 char *dest, *src;
52 int len;
53 {
54 asm("movc3 4(ap),*8(ap),*12(ap)");
55 }
56 #else
57 Reg1 char *dest;
58 Reg2 char *src;
59 Reg3 int len;
60 {
61 if (dest <= src) {
62 for (; len; len--) {
63 *dest++ = *src++;
64 }
65 }
66 else {
67 dest += len;
68 src += len;
69 for (; len; len--) {
70 *--dest = *--src;
71 }
72 }
73 }
74 #endif
75
76 void
77 no_can_do(what)
78 char *what;
79 {
80 fprintf(stderr,"Sorry, your terminal is too %s to play warp.\r\n",what);
81 finalize(1);
82 }
83
84 int
85 exdis(maxnum)
86 int maxnum;
87 {
88 double temp, temp2;
89 double exp();
90 double log();
91
92 temp = (double) maxnum;
93 #ifndef lint
94 temp2 = (double) myrand();
95 #else
96 temp2 = 0.0;
97 #endif
98 #if RANDBITS == 15
99 return (int) exp(temp2 * log(temp)/0x7fff);
100 #else
101 #if RANDBITS == 16
102 return (int) exp(temp2 * log(temp)/0xffff);
103 #else
104 return (int) exp(temp2 * log(temp)/0x7fffffff);
105 #endif
106 #endif
107 }
108
109 static char nomem[] = "warp: out of memory!\r\n";
110
111 /* paranoid version of malloc */
112
113 char *
114 safemalloc(size)
115 MEM_SIZE size;
116 {
117 char *ptr;
118 char *malloc();
119
120 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
121 if (ptr != Nullch)
122 return ptr;
123 else {
124 fputs(nomem,stdout);
125 sig_catcher(0);
126 }
127 /*NOTREACHED*/
128 }
129
130 /* safe version of string copy */
131
132 char *
133 safecpy(to,from,len)
134 char *to;
135 Reg2 char *from;
136 Reg1 int len;
137 {
138 Reg3 char *dest = to;
139
140 if (from != Nullch)
141 for (len--; len && (*dest++ = *from++); len--) ;
142 *dest = '\0';
143 return to;
144 }
145
146 /* copy a string up to some (non-backslashed) delimiter, if any */
147
148 char *
149 cpytill(to,from,delim)
150 Reg2 char *to;
151 Reg1 char *from;
152 Reg3 int delim;
153 {
154 for (; *from; from++,to++) {
155 if (*from == '\\' && from[1] == delim)
156 from++;
157 else if (*from == delim)
158 break;
159 *to = *from;
160 }
161 *to = '\0';
162 return from;
163 }
164
165 /* return ptr to little string in big string, NULL if not found */
166
167 char *
168 instr(big, little)
169 char *big, *little;
170
171 {
172 Reg3 char *t;
173 Reg1 char *s;
174 Reg2 char *x;
175
176 for (t = big; *t; t++) {
177 for (x=t,s=little; *s; x++,s++) {
178 if (!*x)
179 return Nullch;
180 if (*s != *x)
181 break;
182 }
183 if (!*s)
184 return t;
185 }
186 return Nullch;
187 }
188
189 /* effective access */
190
191 #ifdef SETUIDGID
192 int
193 eaccess(filename, mod)
194 char *filename;
195 int mod;
196 {
197 int protection, euid;
198
199 mod &= 7; /* remove extraneous garbage */
200 if (stat(filename, &filestat) < 0)
201 return -1;
202 euid = geteuid();
203 if (euid == ROOTID)
204 return 0;
205 protection = 7 & (filestat.st_mode >>
206 (filestat.st_uid == euid ? 6 :
207 (filestat.st_gid == getegid() ? 3 : 0)
208 ));
209 if ((mod & protection) == mod)
210 return 0;
211 errno = EACCES;
212 return -1;
213 }
214 #endif
215
216 /*
217 * Get working directory
218 */
219
220 #ifdef GETWD
221 #define dot "."
222 #define dotdot ".."
223
224 static char *name;
225
226 static DIR *dirp;
227 static int off;
228 static struct stat d, dd;
229 static struct direct *dir;
230
231 char *
232 getwd(np)
233 char *np;
234 {
235 long rdev, rino;
236
237 *np++ = '/';
238 *np = 0;
239 name = np;
240 off = -1;
241 stat("/", &d);
242 rdev = d.st_dev;
243 rino = d.st_ino;
244 for (;;) {
245 stat(dot, &d);
246 if (d.st_ino==rino && d.st_dev==rdev)
247 goto done;
248 if ((dirp = opendir(dotdot)) == Null(DIR *))
249 prexit("getwd: cannot open ..\r\n");
250 stat(dotdot, &dd);
251 chdir(dotdot);
252 if(d.st_dev == dd.st_dev) {
253 if(d.st_ino == dd.st_ino)
254 goto done;
255 do
256 if ((dir = readdir(dirp)) == Null(struct direct *))
257 prexit("getwd: read error in ..\r\n");
258 while (dir->d_ino != d.st_ino);
259 }
260 else do {
261 if ((dir = readdir(dirp)) == Null(struct direct *))
262 prexit("getwd: read error in ..\r\n");
263 stat(dir->d_name, &dd);
264 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
265 cat();
266 closedir(dirp);
267 }
268 done:
269 name--;
270 if (chdir(name) < 0) {
271 printf("getwd: can't cd back to %s\r\n",name);
272 sig_catcher(0);
273 }
274 return (name);
275 }
276
277 void
278 cat()
279 {
280 Reg1 int i;
281 Reg2 int j;
282
283 i = -1;
284 while (dir->d_name[++i] != 0);
285 if ((off+i+2) > 1024-1)
286 return;
287 for(j=off+1; j>=0; --j)
288 name[j+i+1] = name[j];
289 if (off >= 0)
290 name[i] = '/';
291 off=i+off+1;
292 name[off] = 0;
293 for(--i; i>=0; --i)
294 name[i] = dir->d_name[i];
295 }
296
297 void
298 prexit(cp)
299 char *cp;
300 {
301 write(2, cp, strlen(cp));
302 sig_catcher(0);
303 }
304 #else
305 char *
306 getwd(np) /* shorter but slower */
307 char *np;
308 {
309 FILE *popen();
310 FILE *pipefp = popen("/bin/pwd","r");
311
312 if (pipefp == Nullfp) {
313 printf("Can't run /bin/pwd\r\n");
314 finalize(1);
315 }
316 Fgets(np,512,pipefp);
317 np[strlen(np)-1] = '\0'; /* wipe out newline */
318 pclose(pipefp);
319 return np;
320 }
321 #endif
322
323 /* copy a string to a safe spot */
324
325 char *
326 savestr(str)
327 char *str;
328 {
329 Reg1 char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
330
331 strcpy(newaddr,str);
332 return newaddr;
333 }
334
335 char *
336 getval(nam,def)
337 char *nam,*def;
338 {
339 char *val;
340
341 if ((val = getenv(nam)) == Nullch || !*val)
342 val = def;
343 return val;
344 }
345