Rename LICENSE to LICENSE.GPLv2.
[metastore.git] / utils.h
blobd19c3aea367d9fdea0926bc3d3b1a1cb8b16162a
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 #ifndef UTILS_H
22 #define UTILS_H
24 /* For uint64_t */
25 #include <stdint.h>
26 /* For ssize_t */
27 #include <unistd.h>
28 /* For FILE */
29 #include <stdio.h>
30 /* For struct passwd */
31 #include <pwd.h>
32 /* For struct group */
33 #include <grp.h>
35 /* Adjusts the verbosity level for msg() */
36 void adjust_verbosity(int adj);
38 /* Verbosity levels using stdout */
39 #define MSG_NORMAL 0
40 #define MSG_DEBUG 1
41 #define MSG_QUIET -1
42 /* Verbosity levels using stderr */
43 #define MSG_ERROR -2
44 #define MSG_CRITICAL -3
46 /* Prints messages to console according to the current verbosity */
47 int msg(int level, const char *fmt, ...);
49 /* Malloc which either succeeds or exits */
50 void *xmalloc(size_t size);
52 /* Ditto for strdup */
53 char *xstrdup(const char *s);
55 /* Human-readable printout of binary data */
56 void binary_print(const char *s, ssize_t len);
58 /* Writes data to a file or exits on failure */
59 void xfwrite(const void *ptr, size_t size, FILE *stream);
61 /* Writes an int to a file, using len bytes, in bigendian order */
62 void write_int(uint64_t value, size_t len, FILE *to);
64 /* Writes a binary string to a file */
65 void write_binary_string(const char *string, size_t len, FILE *to);
67 /* Writes a normal C string to a file */
68 void write_string(const char *string, FILE *to);
70 /* Reads an int from a file, using len bytes, in bigendian order */
71 uint64_t read_int(char **from, size_t len, const char *max);
73 /* Reads a binary string from a file */
74 char *read_binary_string(char **from, size_t len, const char *max);
76 /* Reads a normal C string from a file */
77 char *read_string(char **from, const char *max);
79 /* Caching version of getgrnam */
80 struct group *xgetgrnam(const char *name);
82 /* Caching version of getgrgid */
83 struct group *xgetgrgid(gid_t gid);
85 /* Caching version of getpwnam */
86 struct passwd *xgetpwnam(const char *name);
88 /* Caching version of getpwuid */
89 struct passwd *xgetpwuid(uid_t uid);
91 #endif /* UTILS_H */