]>
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.
24 struct timespec timebuf
;
33 movc3(int len
, char *src
, char *dest
)
50 no_can_do(const char *what
)
52 fprintf(stderr
,"Sorry, your terminal is too %s to play warp.\r\n",what
);
61 temp
= (double) maxnum
;
63 temp2
= (double) myrand();
68 return (int) exp(temp2
* log(temp
)/0x7fff);
71 return (int) exp(temp2
* log(temp
)/0xffff);
73 return (int) exp(temp2
* log(temp
)/0x7fffffff);
78 static char nomem
[] = "warp: out of memory!\r\n";
80 /* paranoid version of malloc */
83 safemalloc(size_t size
)
87 ptr
= malloc(size
?size
:1); /* malloc(0) is NASTY on our system */
98 /* safe version of string copy */
101 safecpy(char *to
, const char *from
, size_t len
)
106 for (len
--; len
&& (*dest
++ = *from
++); len
--)
112 /* copy a string up to some (non-backslashed) delimiter, if any */
115 cpytill(char *to
, const char *from
, int delim
)
117 for (; *from
; from
++,to
++) {
118 if (*from
== '\\' && from
[1] == delim
)
120 else if (*from
== delim
)
125 return __UNCONST(from
);
128 /* return ptr to little string in big string, NULL if not found */
131 instr(const char *big
, const char *little
)
138 for (t
= big
; *t
; t
++) {
139 for (x
=t
,s
=little
; *s
; x
++,s
++) {
151 /* effective access */
155 eaccess(const char *filename
, mode_t mod
)
160 mod
&= 7; /* remove extraneous garbage */
161 if (stat(filename
, &filestat
) < 0)
166 protection
= 7 & (filestat
.st_mode
>>
167 (filestat
.st_uid
== euid
? 6 :
168 (filestat
.st_gid
== getegid() ? 3 : 0)
170 if ((mod
& protection
) == mod
)
178 prexit(const char *cp
)
180 write(2, cp
, strlen(cp
));
184 /* copy a string to a safe spot */
187 savestr(const char *str
)
189 char *newaddr
= safemalloc((size_t)(strlen(str
)+1));
191 strcpy(newaddr
, str
);
196 getval(const char *nam
, const char *def
)
200 if ((val
= getenv(nam
)) == NULL
|| !*val
)
202 return __UNCONST(val
);