3 /* Purpose: Low level utilities -BEN- */
10 * Global variables for temporary use
22 * Global pointers for temporary use
31 * Constant bool meaning true
36 * Constant bool meaning false
44 cptr cptr_null = NULL;
50 vptr vptr_null = NULL;
57 vptr vptr_self = (vptr)(&vptr_self);
62 * Convenient storage of the program name
69 * A routine that does nothing
71 void func_nothing(void)
78 * A routine that always returns "success"
80 errr func_success(void)
87 * A routine that always returns a simple "problem code"
89 errr func_problem(void)
96 * A routine that always returns a simple "failure code"
98 errr func_failure(void)
106 * A routine that always returns "true"
115 * A routine that always returns "false"
117 bool func_false(void)
126 * Determine if string "t" is equal to string "t"
128 bool streq(cptr a, cptr b)
130 return (!strcmp(a, b));
135 * Determine if string "t" is a suffix of string "s"
137 bool suffix(cptr s, cptr t)
139 int tlen = strlen(t);
140 int slen = strlen(s);
142 /* Check for incompatible lengths */
143 if (tlen > slen) return (FALSE);
145 /* Compare "t" to the end of "s" */
146 return (!strcmp(s + slen - tlen, t));
151 * Determine if string "t" is a prefix of string "s"
153 bool prefix(cptr s, cptr t)
158 /* Compare content and length */
159 if (*t++ != *s++) return (FALSE);
162 /* Matched, we have a prefix */
169 * Redefinable "plog" action
171 void (*plog_aux)(cptr) = NULL;
174 * Print (or log) a "warning" message (ala "perror()")
175 * Note the use of the (optional) "plog_aux" hook.
179 /* Use the "alternative" function if possible */
180 if (plog_aux) (*plog_aux)(str);
182 /* Just do a labeled fprintf to stderr */
183 else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
189 * Redefinable "quit" action
191 void (*quit_aux)(cptr) = NULL;
194 * Exit (ala "exit()"). If 'str' is NULL, do "exit(0)".
195 * If 'str' begins with "+" or "-", do "exit(atoi(str))".
196 * Otherwise, plog() 'str' and exit with an error code of -1.
197 * But always use 'quit_aux', if set, before anything else.
201 /* Attempt to use the aux function */
202 if (quit_aux) (*quit_aux)(str);
205 if (!str) (void)(exit(0));
207 /* Extract a "special error code" */
208 if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));
210 /* Send the string to plog() */
220 * Redefinable "core" action
222 void (*core_aux)(cptr) = NULL;
225 * Dump a core file, after printing a warning message
226 * As with "quit()", try to use the "core_aux()" hook first.
232 /* Use the aux function */
233 if (core_aux) (*core_aux)(str);
235 /* Dump the warning string */
238 /* Attempt to Crash */
241 /* Be sure we exited */
242 quit("core() failed");