1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 const char *program_name;
33 fatal(0, "virtual memory exhausted");
37 xcalloc(size_t count, size_t size)
39 void *p = count && size ? calloc(count, size) : malloc(1);
49 void *p = malloc(size ? size : 1);
57 xrealloc(void *p, size_t size)
59 p = realloc(p, size ? size : 1);
67 xstrdup(const char *s_)
69 size_t size = strlen(s_) + 1;
70 char *s = xmalloc(size);
76 xasprintf(const char *format, ...)
82 va_start(args, format);
83 needed = vsnprintf(NULL, 0, format, args);
86 s = xmalloc(needed + 1);
88 va_start(args, format);
89 vsnprintf(s, needed + 1, format, args);
95 void fatal(int err_no, const char *format, ...)
99 fprintf(stderr, "%s: ", program_name);
100 va_start(args, format);
101 vfprintf(stderr, format, args);
104 fprintf(stderr, " (%s)", strerror(err_no));
110 void error(int err_no, const char *format, ...)
114 fprintf(stderr, "%s: ", program_name);
115 va_start(args, format);
116 vfprintf(stderr, format, args);
119 fprintf(stderr, " (%s)", strerror(err_no));
123 void debug(int err_no, const char *format, ...)
127 fprintf(stderr, "%s: ", program_name);
128 va_start(args, format);
129 vfprintf(stderr, format, args);
132 fprintf(stderr, " (%s)", strerror(err_no));
136 /* Sets program_name based on 'argv0'. Should be called at the beginning of
137 * main(), as "set_program_name(argv[0]);". */
138 void set_program_name(const char *argv0)
140 const char *slash = strrchr(argv0, '/');
141 program_name = slash ? slash + 1 : argv0;
144 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
145 * line. Numeric offsets are also included, starting at 'ofs' for the first
146 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
147 * are also rendered alongside. */
149 hex_dump(FILE *stream, const void *buf_, size_t size,
150 uintptr_t ofs, bool ascii)
152 const uint8_t *buf = buf_;
153 const size_t per_line = 16; /* Maximum bytes per line. */
157 size_t start, end, n;
160 /* Number of bytes on this line. */
161 start = ofs % per_line;
163 if (end - start > size)
168 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
169 for (i = 0; i < start; i++)
170 fprintf(stream, " ");
172 fprintf(stream, "%02hhx%c",
173 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
176 for (; i < per_line; i++)
177 fprintf(stream, " ");
178 fprintf(stream, "|");
179 for (i = 0; i < start; i++)
180 fprintf(stream, " ");
181 for (; i < end; i++) {
182 int c = buf[i - start];
183 putc(c >= 32 && c < 127 ? c : '.', stream);
185 for (; i < per_line; i++)
186 fprintf(stream, " ");
187 fprintf(stream, "|");
189 fprintf(stream, "\n");