NEWS: Add info about recent bugfix in 489d586.
[metastore.git] / utils.h
blob2dd61c9c98c62bf562067e75b1dc4a7ed439503b
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; only version 2 of the License is applicable.
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.
20 #ifndef UTILS_H
21 #define UTILS_H
23 /* For uint64_t */
24 #include <stdint.h>
25 /* For ssize_t */
26 #include <unistd.h>
27 /* For FILE */
28 #include <stdio.h>
29 /* For struct passwd */
30 #include <pwd.h>
31 /* For struct group */
32 #include <grp.h>
34 /* Adjusts the verbosity level for msg() */
35 void adjust_verbosity(int adj);
37 /* Verbosity levels using stdout */
38 #define MSG_NORMAL 0
39 #define MSG_DEBUG 1
40 #define MSG_QUIET -1
41 /* Verbosity levels using stderr */
42 #define MSG_ERROR -2
43 #define MSG_CRITICAL -3
45 /* Prints messages to console according to the current verbosity */
46 int msg(int level, const char *fmt, ...);
48 /* Malloc which either succeeds or exits */
49 void *xmalloc(size_t size);
51 /* Ditto for strdup */
52 char *xstrdup(const char *s);
54 /* Human-readable printout of binary data */
55 void binary_print(const char *s, ssize_t len);
57 /* Writes data to a file or exits on failure */
58 void xfwrite(const void *ptr, size_t size, FILE *stream);
60 /* Writes an int to a file, using len bytes, in bigendian order */
61 void write_int(uint64_t value, size_t len, FILE *to);
63 /* Writes a binary string to a file */
64 void write_binary_string(const char *string, size_t len, FILE *to);
66 /* Writes a normal C string to a file */
67 void write_string(const char *string, FILE *to);
69 /* Reads an int from a file, using len bytes, in bigendian order */
70 uint64_t read_int(char **from, size_t len, const char *max);
72 /* Reads a binary string from a file */
73 char *read_binary_string(char **from, size_t len, const char *max);
75 /* Reads a normal C string from a file */
76 char *read_string(char **from, const char *max);
78 /* Caching version of getgrnam */
79 struct group *xgetgrnam(const char *name);
81 /* Caching version of getgrgid */
82 struct group *xgetgrgid(gid_t gid);
84 /* Caching version of getpwnam */
85 struct passwd *xgetpwnam(const char *name);
87 /* Caching version of getpwuid */
88 struct passwd *xgetpwuid(uid_t uid);
90 #endif /* UTILS_H */