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