4 * Copyright (c) 1997 Ben Harrison
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies.
11 /* Purpose: Low level utilities -BEN- */
17 * Convenient storage of the program name
23 * Determine if string "t" is equal to string "t"
25 bool streq(cptr a, cptr b)
27 return (!strcmp(a, b));
32 * Determine if string "t" is a suffix of string "s"
34 bool suffix(cptr s, cptr t)
39 /* Check for incompatible lengths */
40 if (tlen > slen) return (FALSE);
42 /* Compare "t" to the end of "s" */
43 return (!strcmp(s + slen - tlen, t));
48 * Determine if string "t" is a prefix of string "s"
50 bool prefix(cptr s, cptr t)
55 /* Compare content and length */
56 if (*t++ != *s++) return (FALSE);
59 /* Matched, we have a prefix */
66 * Redefinable "plog" action
68 void (*plog_aux)(cptr) = NULL;
71 * Print (or log) a "warning" message (ala "perror()")
72 * Note the use of the (optional) "plog_aux" hook.
76 /* Use the "alternative" function if possible */
77 if (plog_aux) (*plog_aux)(str);
79 /* Just do a labeled fprintf to stderr */
80 else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
86 * Redefinable "quit" action
88 void (*quit_aux)(cptr) = NULL;
91 * Exit (ala "exit()"). If 'str' is NULL, do "exit(0)".
92 * If 'str' begins with "+" or "-", do "exit(atoi(str))".
93 * Otherwise, plog() 'str' and exit with an error code of -1.
94 * But always use 'quit_aux', if set, before anything else.
98 /* Attempt to use the aux function */
99 if (quit_aux) (*quit_aux)(str);
102 if (!str) (void)(exit(0));
104 /* Extract a "special error code" */
105 if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));
107 /* Send the string to plog() */
111 (void)(exit(EXIT_FAILURE));
117 * Redefinable "core" action
119 void (*core_aux)(cptr) = NULL;
122 * Dump a core file, after printing a warning message
123 * As with "quit()", try to use the "core_aux()" hook first.
129 /* Use the aux function */
130 if (core_aux) (*core_aux)(str);
132 /* Dump the warning string */
135 /* Attempt to Crash */
138 /* Be sure we exited */
139 quit("core() failed");