]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - warp/util.c
246f0de9874acb7d5d388b76adcb1a05e6afc432
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 */
102 ptr
= malloc(size
?size
:1); /* malloc(0) is NASTY on our system */
112 /* safe version of string copy */
120 Reg3
char *dest
= to
;
123 for (len
--; len
&& (*dest
++ = *from
++); len
--) ;
128 /* copy a string up to some (non-backslashed) delimiter, if any */
131 cpytill(to
,from
,delim
)
136 for (; *from
; from
++,to
++) {
137 if (*from
== '\\' && from
[1] == delim
)
139 else if (*from
== delim
)
147 /* return ptr to little string in big string, NULL if not found */
158 for (t
= big
; *t
; t
++) {
159 for (x
=t
,s
=little
; *s
; x
++,s
++) {
171 /* effective access */
175 eaccess(filename
, mod
)
179 int protection
, euid
;
181 mod
&= 7; /* remove extraneous garbage */
182 if (stat(filename
, &filestat
) < 0)
187 protection
= 7 & (filestat
.st_mode
>>
188 (filestat
.st_uid
== euid
? 6 :
189 (filestat
.st_gid
== getegid() ? 3 : 0)
191 if ((mod
& protection
) == mod
)
199 * Get working directory
210 static struct stat d
, dd
;
211 static struct direct
*dir
;
228 if (d
.st_ino
==rino
&& d
.st_dev
==rdev
)
230 if ((dirp
= opendir(dotdot
)) == Null(DIR *))
231 prexit("getwd: cannot open ..\r\n");
234 if(d
.st_dev
== dd
.st_dev
) {
235 if(d
.st_ino
== dd
.st_ino
)
238 if ((dir
= readdir(dirp
)) == Null(struct direct
*))
239 prexit("getwd: read error in ..\r\n");
240 while (dir
->d_ino
!= d
.st_ino
);
243 if ((dir
= readdir(dirp
)) == Null(struct direct
*))
244 prexit("getwd: read error in ..\r\n");
245 stat(dir
->d_name
, &dd
);
246 } while(dd
.st_ino
!= d
.st_ino
|| dd
.st_dev
!= d
.st_dev
);
252 if (chdir(name
) < 0) {
253 printf("getwd: can't cd back to %s\r\n",name
);
266 while (dir
->d_name
[++i
] != 0);
267 if ((off
+i
+2) > 1024-1)
269 for(j
=off
+1; j
>=0; --j
)
270 name
[j
+i
+1] = name
[j
];
276 name
[i
] = dir
->d_name
[i
];
283 write(2, cp
, strlen(cp
));
288 getwd(np
) /* shorter but slower */
292 FILE *pipefp
= popen("/bin/pwd","r");
294 if (pipefp
== Nullfp
) {
295 printf("Can't run /bin/pwd\r\n");
298 Fgets(np
,512,pipefp
);
299 np
[strlen(np
)-1] = '\0'; /* wipe out newline */
305 /* copy a string to a safe spot */
311 Reg1
char *newaddr
= safemalloc((MEM_SIZE
)(strlen(str
)+1));
323 if ((val
= getenv(nam
)) == Nullch
|| !*val
)