9 void report (const char *fmt
, ...)
13 vfprintf (stdout
, fmt
, ap
);
17 void error (const char *fmt
, ...)
21 vfprintf (stderr
, fmt
, ap
);
22 fprintf (stderr
, "\n");
26 void xerror (const char *fmt
, ...)
30 vfprintf (stderr
, fmt
, ap
);
31 fprintf (stderr
, ": %s\n", strerror (errno
));
36 void fatal (const char *fmt
, ...)
40 fprintf (stderr
, "fatal: ");
41 vfprintf (stderr
, fmt
, ap
);
42 fprintf (stderr
, "\n");
47 void xfatal (const char *fmt
, ...)
51 fprintf (stderr
, "fatal: ");
52 vfprintf (stderr
, fmt
, ap
);
53 fprintf (stderr
, ": %s\n", strerror (errno
));
58 void *xmalloc (size_t size
)
60 void *ptr
= malloc (size
);
61 if (!ptr
) fatal (__FILE__
": memory exhausted");
65 void *xrealloc (void *ptr
, size_t size
)
67 void *nptr
= realloc (ptr
, size
);
68 if (!nptr
) fatal (__FILE__
": can't realloc");
72 void *read_file (const char *path
, size_t *size
)
80 fp
= fopen (path
, "rb");
82 xerror (__FILE__
": can't open file `%s'", path
);
86 if (fseek (fp
, 0L, SEEK_END
)) {
87 xerror (__FILE__
": can't seek file `%s'", path
);
94 xerror (__FILE__
": can't get file size of `%s'", path
);
99 file_size
= (size_t) r
;
100 buffer
= xmalloc (file_size
);
103 read_return
= fread (buffer
, 1, file_size
, fp
);
106 if (read_return
!= file_size
) {
107 error (__FILE__
": can't fully read file `%s'", path
);
112 if (size
) *size
= file_size
;