wmshutdown: Destroy dialog window before shutting down. This is especially useful...
[dockapps.git] / wmSMPmon / wmSMPmon / general.c
blobece43f6d828107424437c61a129dcff69bc77c18
1 /*######################################################################
2 # #
3 # This file contains some general utility functions for wmSMPmon. #
4 # #
5 # All of them were taken from the program 'top' from the procps #
6 # suite. #
7 # With thanks to the author of top: #
8 # James C. Warner <warnerjc@worldnet.att.net> #
9 # #
10 # This file is placed under the conditions of the GNU Library #
11 # General Public License, version 2, or any later version. #
12 # See file COPYING for information on distribution conditions. #
13 # #
14 ######################################################################*/
16 #include "standards.h"
17 #include "general.h"
20 * The usual program end --
21 * called only by functions in this section. */
22 void bye_bye(int eno, const char *str)
24 fflush(stdout);
25 if (str) {
26 if (eno)
27 perror(str);
28 else {
29 fputs(str, stderr);
30 eno = 1;
33 exit(eno);
37 * This routine simply formats whatever the caller wants and
38 * returns a pointer to the resulting 'const char' string... */
39 const char *fmtmk(const char *fmts, ...)
41 static char buf[BIGBUFSIZ]; /* with help stuff, our buffer */
42 va_list va; /* requirements exceed 1k */
44 va_start(va, fmts);
45 vsnprintf(buf, sizeof(buf), fmts, va);
46 va_end(va);
47 return ((const char *)buf);
51 * Standard error handler to normalize the look of all err o/p */
52 void std_err(const char *str)
54 static char buf[SMLBUFSIZ];
56 fflush(stdout);
57 /* we'll use our own buffer so callers can still use fmtmk()
58 * and, yes the leading tab is not the standard convention,
59 * but the standard is wrong -- OUR msg won't get lost in
60 * screen clutter, like so many others! */
61 snprintf(buf, sizeof(buf), "\t%s: %s\n", Myname, str);
63 /* not to worry, he'll change our exit code to 1 due to 'buf' */
64 bye_bye(0, buf);
68 * Handle our own memory stuff without the risk of leaving the
69 * user's terminal in an ugly state should things go sour. */
70 void *alloc_c(unsigned numb)
72 void * p;
74 if (!numb)
75 ++numb;
76 if (!(p = calloc(1, numb)))
77 std_err("failed memory allocate");
78 return (p);