1 /* File: z-util.c */
2 
3 /* Purpose: Low level utilities -BEN- */
4 
5 #include "z-util.h"
6 #include "z-form.h"
7 
8 
9 
10 /*
11  * Global variables for temporary use
12  */
13 char char_tmp = 0;
14 byte byte_tmp = 0;
15 sint sint_tmp = 0;
16 uint uint_tmp = 0;
17 long long_tmp = 0;
18 huge huge_tmp = 0;
19 errr errr_tmp = 0;
20 
21 
22 /*
23  * Global pointers for temporary use
24  */
25 cptr cptr_tmp = NULL;
26 vptr vptr_tmp = NULL;
27 
28 
29 
30 
31 /*
32  * Constant bool meaning true
33  */
34 bool bool_true = 1;
35 
36 /*
37  * Constant bool meaning false
38  */
39 bool bool_false = 0;
40 
41 
42 /*
43  * Global NULL cptr
44  */
45 cptr cptr_null = NULL;
46 
47 
48 /*
49  * Global NULL vptr
50  */
51 vptr vptr_null = NULL;
52 
53 
54 
55 /*
56  * Global SELF vptr
57  */
58 vptr vptr_self = (vptr)(&vptr_self);
59 
60 
61 
62 /*
63  * Convenient storage of the program name
64  */
65 cptr argv0 = NULL;
66 
67 
68 
69 /*
70  * A routine that does nothing
71  */
func_nothing(void)72 void func_nothing(void)
73 {
74 	/* Do nothing */
75 }
76 
77 
78 /*
79  * A routine that always returns "success"
80  */
func_success(void)81 errr func_success(void)
82 {
83 	return (0);
84 }
85 
86 
87 /*
88  * A routine that always returns a simple "problem code"
89  */
func_problem(void)90 errr func_problem(void)
91 {
92 	return (1);
93 }
94 
95 
96 /*
97  * A routine that always returns a simple "failure code"
98  */
func_failure(void)99 errr func_failure(void)
100 {
101 	return (-1);
102 }
103 
104 
105 
106 /*
107  * A routine that always returns "true"
108  */
func_true(void)109 bool func_true(void)
110 {
111 	return (1);
112 }
113 
114 
115 /*
116  * A routine that always returns "false"
117  */
func_false(void)118 bool func_false(void)
119 {
120 	return (0);
121 }
122 
123 
124 
125 
126 #ifdef ultrix
127 
128 /*
129  * A copy of "strdup"
130  *
131  * This code contributed by Hao Chen <hao@mit.edu>
132  */
strdup(cptr s)133 char *strdup(cptr s)
134 {
135 	char *dup;
136 	dup = (char *)malloc(sizeof(char) * (strlen(s) + 1));
137 	strcpy(dup, s);
138 	return dup;
139 }
140 
141 #endif /* ultrix */
142 
143 
144 /*
145  * Determine if string "t" is a suffix of string "s"
146  */
suffix(cptr s,cptr t)147 bool suffix(cptr s, cptr t)
148 {
149 	int tlen = strlen(t);
150 	int slen = strlen(s);
151 
152 	/* Check for incompatible lengths */
153 	if (tlen > slen) return (FALSE);
154 
155 	/* Compare "t" to the end of "s" */
156 	return (!strcmp(s + slen - tlen, t));
157 }
158 
159 
160 /*
161  * Determine if string "t" is a prefix of string "s"
162  */
prefix(cptr s,cptr t)163 bool prefix(cptr s, cptr t)
164 {
165 	/* Scan "t" */
166 	while (*t)
167 	{
168 		/* Compare content and length */
169 		if (*t++ != *s++) return (FALSE);
170 	}
171 
172 	/* Matched, we have a prefix */
173 	return (TRUE);
174 }
175 
176 
177 
178 /*
179  * Redefinable "plog" action
180  */
181 void (*plog_aux)(cptr) = NULL;
182 
183 /*
184  * Print (or log) a "warning" message (ala "perror()")
185  * Note the use of the (optional) "plog_aux" hook.
186  */
plog(cptr str)187 void plog(cptr str)
188 {
189 	/* Use the "alternative" function if possible */
190 	if (plog_aux) (*plog_aux)(str);
191 
192 	/* Just do a labeled fprintf to stderr */
193 	else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
194 }
195 
196 
197 
198 /*
199  * Redefinable "quit" action
200  */
201 void (*quit_aux)(cptr) = NULL;
202 
203 /*
204  * Exit (ala "exit()").  If 'str' is NULL, do "exit(0)".
205  * If 'str' begins with "+" or "-", do "exit(atoi(str))".
206  * Otherwise, plog() 'str' and exit with an error code of -1.
207  * But always use 'quit_aux', if set, before anything else.
208  */
quit(cptr str)209 void quit(cptr str)
210 {
211 	char buf[1024];
212 
213 	/* Save exit string */
214 	if (str) {
215 		strncpy(buf, str, 1024);
216 		buf[1023] = '\0';
217 	} else
218 		buf[0] = '\0';
219 
220 	/* Attempt to use the aux function */
221 	if (quit_aux) (*quit_aux)(str ? buf : NULL);
222 
223 	/* Success */
224 	if (!str) (void)(exit(0));
225 
226 	/* Extract a "special error code" */
227 	if ((buf[0] == '-') || (buf[0] == '+')) (void)(exit(atoi(buf)));
228 
229 	/* Send the string to plog() */
230 	if (str) plog(buf);
231 	plog("Quitting!");
232 
233 	/* Failure */
234 	if (!strcmp(str, "Terminating")) {
235 		(void)(exit(-2));
236 	} else {
237 		(void)(exit(-1));
238 	}
239 }
240 
241 
242 
243 /*
244  * Redefinable "core" action
245  */
246 void (*core_aux)(cptr) = NULL;
247 
248 /*
249  * Dump a core file, after printing a warning message
250  * As with "quit()", try to use the "core_aux()" hook first.
251  */
core(cptr str)252 void core(cptr str)
253 {
254 	char *crash = NULL;
255 
256 	/* Use the aux function */
257 	if (core_aux) (*core_aux)(str);
258 
259 	/* Dump the warning string */
260 	if (str) plog(str);
261 
262 	/* Attempt to Crash */
263 	(*crash) = (*crash);
264 
265 	/* Be sure we exited */
266 	quit("core() failed");
267 }
268 
269 
270 
271 
272