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");
74 int _file_size (FILE *fp
, const char *path
, size_t *size
)
78 if (fseek (fp
, 0L, SEEK_END
)) {
79 xerror (__FILE__
": can't seek file `%s'", path
);
86 xerror (__FILE__
": can't get file size of `%s'", path
);
90 if (size
) *size
= (size_t) r
;
94 void *read_file (const char *path
, size_t *size
)
101 fp
= fopen (path
, "rb");
103 xerror (__FILE__
": can't open file `%s'", path
);
107 if (!_file_size (fp
, path
, &file_size
)) {
111 buffer
= xmalloc (file_size
);
114 read_return
= fread (buffer
, 1, file_size
, fp
);
117 if (read_return
!= file_size
) {
118 error (__FILE__
": can't fully read file `%s'", path
);
123 if (size
) *size
= file_size
;