]>
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.
31 movc3(int len
, char *src
, char *dest
)
34 asm("movc3 4(ap),*8(ap),*12(ap)");
54 no_can_do(const char *what
)
56 fprintf(stderr
,"Sorry, your terminal is too %s to play warp.\r\n",what
);
65 temp
= (double) maxnum
;
67 temp2
= (double) myrand();
72 return (int) exp(temp2
* log(temp
)/0x7fff);
75 return (int) exp(temp2
* log(temp
)/0xffff);
77 return (int) exp(temp2
* log(temp
)/0x7fffffff);
82 static char nomem
[] = "warp: out of memory!\r\n";
84 /* paranoid version of malloc */
87 safemalloc(size_t size
)
91 ptr
= malloc(size
?size
:1); /* malloc(0) is NASTY on our system */
102 /* safe version of string copy */
105 safecpy(char *to
, const char *from
, size_t len
)
110 for (len
--; len
&& (*dest
++ = *from
++); len
--)
116 /* copy a string up to some (non-backslashed) delimiter, if any */
119 cpytill(char *to
, const char *from
, int delim
)
121 for (; *from
; from
++,to
++) {
122 if (*from
== '\\' && from
[1] == delim
)
124 else if (*from
== delim
)
129 return __UNCONST(from
);
132 /* return ptr to little string in big string, NULL if not found */
135 instr(const char *big
, const char *little
)
142 for (t
= big
; *t
; t
++) {
143 for (x
=t
,s
=little
; *s
; x
++,s
++) {
155 /* effective access */
159 eaccess(const char *filename
, mode_t mod
)
164 mod
&= 7; /* remove extraneous garbage */
165 if (stat(filename
, &filestat
) < 0)
170 protection
= 7 & (filestat
.st_mode
>>
171 (filestat
.st_uid
== euid
? 6 :
172 (filestat
.st_gid
== getegid() ? 3 : 0)
174 if ((mod
& protection
) == mod
)
182 prexit(const char *cp
)
184 write(2, cp
, strlen(cp
));
188 /* copy a string to a safe spot */
191 savestr(const char *str
)
193 char *newaddr
= safemalloc((size_t)(strlen(str
)+1));
195 strcpy(newaddr
, str
);
200 getval(const char *nam
, const char *def
)
204 if ((val
= getenv(nam
)) == NULL
|| !*val
)
206 return __UNCONST(val
);