-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmisc.c
88 lines (73 loc) · 1.4 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* wipe
*
* by Berke Durak
*
* General-purpose miscellaneous routines: debugging, etc.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include "misc.h"
void *xmalloc (size_t l)
{
void *m;
m = malloc (l);
if (!m) {
errorf (0, "could not allocate %ld bytes", l);
exit (EXIT_FAILURE);
}
return m;
}
int errorf (unsigned long e, char *fmt, ...)
{
va_list arg;
int en;
en = (e & ERF_ERN)?errno:e & ERF_MASK;
va_start (arg, fmt);
fprintf (stderr, "Error: ");
vfprintf (stderr, fmt, arg);
if (en) fprintf (stderr, " (%s -- error %d)", strerror (en), en);
fputc ('\n', stderr);
va_end (arg);
if (e & ERF_EXIT) exit (EXIT_FAILURE);
if (e & ERF_RET0) return 0;
return -1;
}
void informf (char *fmt, ...)
{
va_list arg;
va_start (arg, fmt);
vfprintf (stderr, fmt, arg);
fputc ('\n', stderr);
va_end (arg);
}
char *msprintf (char *fmt, ...)
{
char *s;
va_list arg;
char buf[1024];
va_start (arg, fmt);
vsprintf (buf, fmt, arg);
s = xmalloc (strlen (buf)+1);
strcpy (s, buf);
va_end (arg);
return s;
}
#ifdef DEBUG
void debug_pf (char *fmt, ...)
{
va_list arg;
va_start (arg, fmt);
fprintf (stderr, "debug: ");
vfprintf (stderr, fmt, arg);
fputc ('\n', stderr);
va_end (arg);
}
#define debugf(x, y...) debug_pf(x,## y)
#else
#define debugf(x, y...)
#endif
/* vim:set sw=4:set ts=8: */