3 /* Purpose: Low level utilities -BEN- */
9 * Convenient storage of the program name
15 * Determine if string "t" is equal to string "t"
17 bool streq(cptr a, cptr b)
19 return (!strcmp(a, b));
24 * Determine if string "t" is a suffix of string "s"
26 bool suffix(cptr s, cptr t)
31 /* Check for incompatible lengths */
32 if (tlen > slen) return (FALSE);
34 /* Compare "t" to the end of "s" */
35 return (!strcmp(s + slen - tlen, t));
40 * Determine if string "t" is a prefix of string "s"
42 bool prefix(cptr s, cptr t)
47 /* Compare content and length */
48 if (*t++ != *s++) return (FALSE);
51 /* Matched, we have a prefix */
58 * Redefinable "plog" action
60 void (*plog_aux)(cptr) = NULL;
63 * Print (or log) a "warning" message (ala "perror()")
64 * Note the use of the (optional) "plog_aux" hook.
68 /* Use the "alternative" function if possible */
69 if (plog_aux) (*plog_aux)(str);
71 /* Just do a labeled fprintf to stderr */
72 else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
78 * Redefinable "quit" action
80 void (*quit_aux)(cptr) = NULL;
83 * Exit (ala "exit()"). If 'str' is NULL, do "exit(0)".
84 * If 'str' begins with "+" or "-", do "exit(atoi(str))".
85 * Otherwise, plog() 'str' and exit with an error code of -1.
86 * But always use 'quit_aux', if set, before anything else.
90 /* Attempt to use the aux function */
91 if (quit_aux) (*quit_aux)(str);
94 if (!str) (void)(exit(0));
96 /* Extract a "special error code" */
97 if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));
99 /* Send the string to plog() */
103 (void)(exit(EXIT_FAILURE));
109 * Redefinable "core" action
111 void (*core_aux)(cptr) = NULL;
114 * Dump a core file, after printing a warning message
115 * As with "quit()", try to use the "core_aux()" hook first.
121 /* Use the aux function */
122 if (core_aux) (*core_aux)(str);
124 /* Dump the warning string */
127 /* Attempt to Crash */
130 /* Be sure we exited */
131 quit("core() failed");