d4194bbe4578ca9e9e3efaa3a5b02184f713a2d8
[dockapps.git] / wmSMPmon / wmSMPmon / general.c
blobd4194bbe4578ca9e9e3efaa3a5b02184f713a2d8
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) perror(str);
27 else {
28 fputs(str, stderr);
29 eno = 1;
32 exit(eno);
36 * This routine simply formats whatever the caller wants and
37 * returns a pointer to the resulting 'const char' string... */
38 const char *fmtmk (const char *fmts, ...)
40 static char buf[BIGBUFSIZ]; /* with help stuff, our buffer */
41 va_list va; /* requirements exceed 1k */
43 va_start(va, fmts);
44 vsnprintf(buf, sizeof(buf), fmts, va);
45 va_end(va);
46 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) ++numb;
75 if (!(p = calloc(1, numb)))
76 std_err("failed memory allocate");
77 return p;