]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - warp/util.c
1 /* Header: util.c,v 7.0.1.2 86/10/20 12:07:46 lwall Exp */
4 * Revision 7.0.1.2 86/10/20 12:07:46 lwall
5 * Made all exits reset tty.
7 * Revision 7.0.1.1 86/10/16 10:54:02 lwall
8 * Added Damage. Fixed random bugs.
10 * Revision 7.0 86/10/08 15:14:31 lwall
11 * Split into separate files. Added amoebas and pirates.
36 asm("movc3 4(ap),*8(ap),*12(ap)");
62 fprintf(stderr
,"Sorry, your terminal is too %s to play warp.\r\n",what
);
74 temp
= (double) maxnum
;
76 temp2
= (double) myrand();
81 return (int) exp(temp2
* log(temp
)/0x7fff);
84 return (int) exp(temp2
* log(temp
)/0xffff);
86 return (int) exp(temp2
* log(temp
)/0x7fffffff);
91 static char nomem
[] = "warp: out of memory!\r\n";
93 /* paranoid version of malloc */
101 ptr
= malloc(size
?size
:1); /* malloc(0) is NASTY on our system */
111 /* safe version of string copy */
119 Reg3
char *dest
= to
;
122 for (len
--; len
&& (*dest
++ = *from
++); len
--) ;
127 /* copy a string up to some (non-backslashed) delimiter, if any */
130 cpytill(to
,from
,delim
)
135 for (; *from
; from
++,to
++) {
136 if (*from
== '\\' && from
[1] == delim
)
138 else if (*from
== delim
)
146 /* return ptr to little string in big string, NULL if not found */
157 for (t
= big
; *t
; t
++) {
158 for (x
=t
,s
=little
; *s
; x
++,s
++) {
170 /* effective access */
174 eaccess(filename
, mod
)
178 int protection
, euid
;
180 mod
&= 7; /* remove extraneous garbage */
181 if (stat(filename
, &filestat
) < 0)
186 protection
= 7 & (filestat
.st_mode
>>
187 (filestat
.st_uid
== euid
? 6 :
188 (filestat
.st_gid
== getegid() ? 3 : 0)
190 if ((mod
& protection
) == mod
)
198 * Get working directory
209 static struct stat d
, dd
;
210 static struct direct
*dir
;
227 if (d
.st_ino
==rino
&& d
.st_dev
==rdev
)
229 if ((dirp
= opendir(dotdot
)) == Null(DIR *))
230 prexit("getwd: cannot open ..\r\n");
233 if(d
.st_dev
== dd
.st_dev
) {
234 if(d
.st_ino
== dd
.st_ino
)
237 if ((dir
= readdir(dirp
)) == Null(struct direct
*))
238 prexit("getwd: read error in ..\r\n");
239 while (dir
->d_ino
!= d
.st_ino
);
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
);
251 if (chdir(name
) < 0) {
252 printf("getwd: can't cd back to %s\r\n",name
);
265 while (dir
->d_name
[++i
] != 0);
266 if ((off
+i
+2) > 1024-1)
268 for(j
=off
+1; j
>=0; --j
)
269 name
[j
+i
+1] = name
[j
];
275 name
[i
] = dir
->d_name
[i
];
282 write(2, cp
, strlen(cp
));
287 getwd(np
) /* shorter but slower */
291 FILE *pipefp
= popen("/bin/pwd","r");
293 if (pipefp
== Nullfp
) {
294 printf("Can't run /bin/pwd\r\n");
297 Fgets(np
,512,pipefp
);
298 np
[strlen(np
)-1] = '\0'; /* wipe out newline */
304 /* copy a string to a safe spot */
310 Reg1
char *newaddr
= safemalloc((MEM_SIZE
)(strlen(str
)+1));
322 if ((val
= getenv(nam
)) == Nullch
|| !*val
)