]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - warp/util.c
cgram: allow providing an input file instead of the random fortune
[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 struct timespec timebuf;
25
26 void
27 util_init(void)
28 {
29 ;
30 }
31
32 void
33 movc3(int len, char *src, char *dest)
34 {
35 if (dest <= src) {
36 for (; len; len--) {
37 *dest++ = *src++;
38 }
39 }
40 else {
41 dest += len;
42 src += len;
43 for (; len; len--) {
44 *--dest = *--src;
45 }
46 }
47 }
48
49 void
50 no_can_do(const char *what)
51 {
52 fprintf(stderr,"Sorry, your terminal is too %s to play warp.\r\n",what);
53 finalize(1);
54 }
55
56 int
57 exdis(int maxnum)
58 {
59 double temp, temp2;
60
61 temp = (double) maxnum;
62 #ifndef lint
63 temp2 = (double) myrand();
64 #else
65 temp2 = 0.0;
66 #endif
67 #if RANDBITS == 15
68 return (int) exp(temp2 * log(temp)/0x7fff);
69 #else
70 #if RANDBITS == 16
71 return (int) exp(temp2 * log(temp)/0xffff);
72 #else
73 return (int) exp(temp2 * log(temp)/0x7fffffff);
74 #endif
75 #endif
76 }
77
78 static char nomem[] = "warp: out of memory!\r\n";
79
80 /* paranoid version of malloc */
81
82 void *
83 safemalloc(size_t size)
84 {
85 char *ptr;
86
87 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
88 if (ptr != NULL)
89 return ptr;
90 else {
91 fputs(nomem,stdout);
92 sig_catcher(0);
93 }
94 /*NOTREACHED*/
95 return NULL;
96 }
97
98 /* safe version of string copy */
99
100 char *
101 safecpy(char *to, const char *from, size_t len)
102 {
103 char *dest = to;
104
105 if (from != NULL)
106 for (len--; len && (*dest++ = *from++); len--)
107 continue;
108 *dest = '\0';
109 return to;
110 }
111
112 /* copy a string up to some (non-backslashed) delimiter, if any */
113
114 char *
115 cpytill(char *to, const char *from, int delim)
116 {
117 for (; *from; from++,to++) {
118 if (*from == '\\' && from[1] == delim)
119 from++;
120 else if (*from == delim)
121 break;
122 *to = *from;
123 }
124 *to = '\0';
125 return __UNCONST(from);
126 }
127
128 /* return ptr to little string in big string, NULL if not found */
129
130 char *
131 instr(const char *big, const char *little)
132
133 {
134 const char *t;
135 const char *s;
136 const char *x;
137
138 for (t = big; *t; t++) {
139 for (x=t,s=little; *s; x++,s++) {
140 if (!*x)
141 return NULL;
142 if (*s != *x)
143 break;
144 }
145 if (!*s)
146 return __UNCONST(t);
147 }
148 return NULL;
149 }
150
151 /* effective access */
152
153 #ifdef SETUIDGID
154 int
155 eaccess(const char *filename, mode_t mod)
156 {
157 mode_t protection;
158 uid_t euid;
159
160 mod &= 7; /* remove extraneous garbage */
161 if (stat(filename, &filestat) < 0)
162 return -1;
163 euid = geteuid();
164 if (euid == ROOTID)
165 return 0;
166 protection = 7 & (filestat.st_mode >>
167 (filestat.st_uid == euid ? 6 :
168 (filestat.st_gid == getegid() ? 3 : 0)
169 ));
170 if ((mod & protection) == mod)
171 return 0;
172 errno = EACCES;
173 return -1;
174 }
175 #endif
176
177 void
178 prexit(const char *cp)
179 {
180 write(2, cp, strlen(cp));
181 sig_catcher(0);
182 }
183
184 /* copy a string to a safe spot */
185
186 char *
187 savestr(const char *str)
188 {
189 char *newaddr = safemalloc((size_t)(strlen(str)+1));
190
191 strcpy(newaddr, str);
192 return newaddr;
193 }
194
195 char *
196 getval(const char *nam, const char *def)
197 {
198 const char *val;
199
200 if ((val = getenv(nam)) == NULL || !*val)
201 val = def;
202 return __UNCONST(val);
203 }