1 /* Copyright (C) 1999 Lucent Technologies */
2 /* Excerpted from 'The Practice of Programming' */
3 /* by Brian W. Kernighan and Rob Pike */
12 static char *name = NULL; /* program name for messages */
14 /* eprintf: print error message and exit */
15 void eprintf(char *fmt, ...)
20 if (progname() != NULL)
21 fprintf(stderr, "%s: ", progname());
24 vfprintf(stderr, fmt, args);
27 if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':')
28 fprintf(stderr, " %s", strerror(errno));
29 fprintf(stderr, "\n");
30 exit(2); /* conventional value for failed execution */
33 /* weprintf: print warning message */
34 void weprintf(char *fmt, ...)
39 fprintf(stderr, "warning: ");
40 if (progname() != NULL)
41 fprintf(stderr, "%s: ", progname());
43 vfprintf(stderr, fmt, args);
45 if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':')
46 fprintf(stderr, " %s\n", strerror(errno));
48 fprintf(stderr, "\n");
51 /* emalloc: malloc and report if error */
52 void *emalloc(size_t n)
58 eprintf("malloc of %u bytes failed:", n);
62 /* erealloc: realloc and report if error */
63 void *erealloc(void *vp, size_t n)
69 eprintf("realloc of %u bytes failed:", n);
73 /* estrdup: duplicate a string, report if error */
74 char *estrdup(char *s)
78 t = (char *) malloc(strlen(s)+1);
80 eprintf("estrdup(\"%.20s\") failed:", s);
85 /* progname: return stored name of program */
91 /* setmyprogname: set stored name of program */
92 void setmyprogname(char *str)