Add missing dependency
[metastore.git] / utils.c
blobde4993eaa44e676efbb7e84e80a8f9bef704bcad
1 /*
2 * Main functions of the program.
4 * Copyright (C) 2007 David Härdeman <david@hardeman.nu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <stdarg.h>
27 #include <unistd.h>
29 #include "utils.h"
31 /* Controls the verbosity level for msg() */
32 static int verbosity = 0;
34 /* Adjusts the verbosity level for msg() */
35 void
36 adjust_verbosity(int adj)
38 verbosity += adj;
42 * Prints messages to console according to the current verbosity
43 * - see utils.h for level defines
45 int
46 msg(int level, const char *fmt, ...)
48 int ret;
49 va_list ap;
51 if (level > verbosity)
52 return 0;
54 va_start(ap, fmt);
56 if (level < MSG_QUIET)
57 ret = vfprintf(stderr, fmt, ap);
58 else
59 ret = vfprintf(stdout, fmt, ap);
61 va_end(ap);
62 return ret;
65 /* Malloc which either succeeds or exits */
66 void *
67 xmalloc(size_t size)
69 void *result = malloc(size);
70 if (!result) {
71 msg(MSG_CRITICAL, "Failed to malloc %zi bytes\n", size);
72 exit(EXIT_FAILURE);
74 return result;
77 /* Ditto for strdup */
78 char *
79 xstrdup(const char *s)
81 char *result = strdup(s);
82 if (!result) {
83 msg(MSG_CRITICAL, "Failed to strdup %zi bytes\n", strlen(s));
84 exit(EXIT_FAILURE);
86 return result;
89 /* Human-readable printout of binary data */
90 void
91 binary_print(const char *s, ssize_t len)
93 ssize_t i;
95 for (i = 0; i < len; i++) {
96 if (isprint(s[i]))
97 msg(MSG_DEBUG, "%c", s[i]);
98 else
99 msg(MSG_DEBUG, "0x%02X", (int)s[i]);
103 /* Writes data to a file or exits on failure */
104 void
105 xfwrite(const void *ptr, size_t size, FILE *stream)
107 if (fwrite(ptr, size, 1, stream) != 1) {
108 perror("fwrite");
109 exit(EXIT_FAILURE);
113 /* Writes an int to a file, using len bytes, in bigendian order */
114 void
115 write_int(uint64_t value, size_t len, FILE *to)
117 char buf[len];
118 int i;
120 for (i = 0; i < len; i++)
121 buf[i] = ((value >> (8 * i)) & 0xff);
122 xfwrite(buf, len, to);
125 /* Writes a binary string to a file */
126 void
127 write_binary_string(const char *string, size_t len, FILE *to)
129 xfwrite(string, len, to);
132 /* Writes a normal C string to a file */
133 void
134 write_string(const char *string, FILE *to)
136 xfwrite(string, strlen(string) + 1, to);
139 /* Reads an int from a file, using len bytes, in bigendian order */
140 uint64_t
141 read_int(char **from, size_t len, const char *max)
143 uint64_t result = 0;
144 int i;
146 if (*from + len > max) {
147 msg(MSG_CRITICAL,
148 "Attempt to read beyond end of file, corrupt file?\n");
149 exit(EXIT_FAILURE);
152 for (i = 0; i < len; i++)
153 result += (((*from)[i] & 0xff) << (8 * i));
154 *from += len;
155 return result;
158 /* Reads a binary string from a file */
159 char *
160 read_binary_string(char **from, size_t len, const char *max)
162 char *result;
164 if (*from + len > max) {
165 msg(MSG_CRITICAL,
166 "Attempt to read beyond end of file, corrupt file?\n");
167 exit(EXIT_FAILURE);
170 result = xmalloc(len);
171 strncpy(result, *from, len);
172 *from += len;
173 return result;
176 /* Reads a normal C string from a file */
177 char *
178 read_string(char **from, const char *max)
180 return read_binary_string(from, strlen(*from) + 1, max);