2 * Author: Humberto Naves (hsnaves@gmail.com)
13 void report (const char *fmt
, ...)
17 vfprintf (stdout
, fmt
, ap
);
21 void error (const char *fmt
, ...)
25 vfprintf (stderr
, fmt
, ap
);
26 fprintf (stderr
, "\n");
30 void xerror (const char *fmt
, ...)
34 vfprintf (stderr
, fmt
, ap
);
35 fprintf (stderr
, ": %s\n", strerror (errno
));
40 void fatal (const char *fmt
, ...)
44 fprintf (stderr
, "fatal: ");
45 vfprintf (stderr
, fmt
, ap
);
46 fprintf (stderr
, "\n");
51 void xfatal (const char *fmt
, ...)
55 fprintf (stderr
, "fatal: ");
56 vfprintf (stderr
, fmt
, ap
);
57 fprintf (stderr
, ": %s\n", strerror (errno
));
62 void *xmalloc (size_t size
)
64 void *ptr
= malloc (size
);
65 if (!ptr
) fatal (__FILE__
": memory exhausted");
69 void *xrealloc (void *ptr
, size_t size
)
71 void *nptr
= realloc (ptr
, size
);
72 if (!nptr
) fatal (__FILE__
": can't realloc");
78 int _file_size (FILE *fp
, const char *path
, size_t *size
)
82 if (fseek (fp
, 0L, SEEK_END
)) {
83 xerror (__FILE__
": can't seek file `%s'", path
);
90 xerror (__FILE__
": can't get file size of `%s'", path
);
94 if (size
) *size
= (size_t) r
;
98 void *read_file (const char *path
, size_t *size
)
105 fp
= fopen (path
, "rb");
107 xerror (__FILE__
": can't open file `%s'", path
);
111 if (!_file_size (fp
, path
, &file_size
)) {
115 buffer
= xmalloc (file_size
);
118 read_return
= fread (buffer
, 1, file_size
, fp
);
121 if (read_return
!= file_size
) {
122 error (__FILE__
": can't fully read file `%s'", path
);
127 if (size
) *size
= file_size
;