Problemas com os relocs
[pspdecompiler.git] / utils.c
blobeb0a8b31d3145639f9660dd6d7c8d0aa9572bd50
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <errno.h>
7 #include "utils.h"
9 void report (const char *fmt, ...)
11 va_list ap;
12 va_start (ap, fmt);
13 vfprintf (stdout, fmt, ap);
14 va_end (ap);
17 void error (const char *fmt, ...)
19 va_list ap;
20 va_start (ap, fmt);
21 vfprintf (stderr, fmt, ap);
22 fprintf (stderr, "\n");
23 va_end (ap);
26 void xerror (const char *fmt, ...)
28 va_list ap;
29 va_start (ap, fmt);
30 vfprintf (stderr, fmt, ap);
31 fprintf (stderr, ": %s\n", strerror (errno));
32 va_end (ap);
36 void fatal (const char *fmt, ...)
38 va_list ap;
39 va_start (ap, fmt);
40 fprintf (stderr, "fatal: ");
41 vfprintf (stderr, fmt, ap);
42 fprintf (stderr, "\n");
43 va_end (ap);
44 exit (1);
47 void xfatal (const char *fmt, ...)
49 va_list ap;
50 va_start (ap, fmt);
51 fprintf (stderr, "fatal: ");
52 vfprintf (stderr, fmt, ap);
53 fprintf (stderr, ": %s\n", strerror (errno));
54 va_end (ap);
55 exit (1);
58 void *xmalloc (size_t size)
60 void *ptr = malloc (size);
61 if (!ptr) fatal (__FILE__ ": memory exhausted");
62 return ptr;
65 void *xrealloc (void *ptr, size_t size)
67 void *nptr = realloc (ptr, size);
68 if (!nptr) fatal (__FILE__ ": can't realloc");
69 return nptr;
72 void *read_file (const char *path, size_t *size)
74 FILE *fp;
75 void *buffer;
76 size_t file_size;
77 size_t read_return;
78 long r;
80 fp = fopen (path, "rb");
81 if (!fp) {
82 xerror (__FILE__ ": can't open file `%s'", path);
83 return NULL;
86 if (fseek (fp, 0L, SEEK_END)) {
87 xerror (__FILE__ ": can't seek file `%s'", path);
88 fclose (fp);
89 return NULL;
92 r = ftell (fp);
93 if (r == -1) {
94 xerror (__FILE__ ": can't get file size of `%s'", path);
95 fclose (fp);
96 return NULL;
99 file_size = (size_t) r;
100 buffer = xmalloc (file_size);
101 rewind (fp);
103 read_return = fread (buffer, 1, file_size, fp);
104 fclose (fp);
106 if (read_return != file_size) {
107 error (__FILE__ ": can't fully read file `%s'", path);
108 free (buffer);
109 return NULL;
112 if (size) *size = file_size;
113 return buffer;