Merge branch 'collectd-3.11'
[collectd.git] / src / common.h
1 /**
2  * collectd - src/common.h
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21 **/
22
23 #ifndef COMMON_H
24 #define COMMON_H
25
26 #include "collectd.h"
27
28 #define sfree(ptr) \
29         if((ptr) != NULL) { \
30                 free(ptr); \
31         } \
32         (ptr) = NULL
33
34 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
35
36 void sstrncpy(char *d, const char *s, int len);
37 char *sstrdup(const char *s);
38 void *smalloc(size_t size);
39 char *sstrerror (int errnum, char *buf, size_t buflen);
40
41 /*
42  * NAME
43  *   sread
44  *
45  * DESCRIPTION
46  *   Reads exactly `n' bytes or failes. Syntax and other behavior is analogous
47  *   to `read(2)'. If EOF is received the file descriptor is closed and an
48  *   error is returned.
49  *
50  * PARAMETERS
51  *   `fd'          File descriptor to write to.
52  *   `buf'         Buffer that is to be written.
53  *   `count'       Numver of bytes in the buffer.
54  *
55  * RETURN VALUE
56  *   Zero upon success or non-zero if an error occured. `errno' is set in this
57  *   case.
58  */
59 ssize_t sread (int fd, void *buf, size_t count);
60
61 /*
62  * NAME
63  *   swrite
64  *
65  * DESCRIPTION
66  *   Writes exactly `n' bytes or failes. Syntax and other behavior is analogous
67  *   to `write(2)'.
68  *
69  * PARAMETERS
70  *   `fd'          File descriptor to write to.
71  *   `buf'         Buffer that is to be written.
72  *   `count'       Numver of bytes in the buffer.
73  *
74  * RETURN VALUE
75  *   Zero upon success or non-zero if an error occured. `errno' is set in this
76  *   case.
77  */
78 ssize_t swrite (int fd, const void *buf, size_t count);
79
80 /*
81  * NAME
82  *   strsplit
83  *
84  * DESCRIPTION
85  *   Splits a string into parts and stores pointers to the parts in `fields'.
86  *   The characters split at are ` ' (space) and "\t" (tab).
87  *
88  * PARAMETERS
89  *   `string'      String to split. This string will be modified. `fields' will
90  *                 contain pointers to parts of this string, so free'ing it
91  *                 will destroy `fields' as well.
92  *   `fields'      Array of strings where pointers to the parts will be stored.
93  *   `size'        Number of elements in the array. No more than `size'
94  *                 pointers will be stored in `fields'.
95  *
96  * RETURN VALUE
97  *    Returns the number of parts stored in `fields'.
98  */
99 int strsplit (char *string, char **fields, size_t size);
100
101 /*
102  * NAME
103  *   strjoin
104  *
105  * DESCRIPTION
106  *   Joins together several parts of a string using `sep' as a seperator. This
107  *   is equipollent to the perl buildin `join'.
108  *
109  * PARAMETERS
110  *   `dst'         Buffer where the result is stored.
111  *   `dst_len'     Length of the destination buffer. No more than this many
112  *                 bytes will be written to the memory pointed to by `dst',
113  *                 including the trailing null-byte.
114  *   `fields'      Array of strings to be joined.
115  *   `fields_num'  Number of elements in the `fields' array.
116  *   `sep'         String to be inserted between any two elements of `fields'.
117  *                 This string is neither prepended nor appended to the result.
118  *                 Instead of passing "" (empty string) one can pass NULL.
119  *
120  * RETURN VALUE
121  *   Returns the number of characters in `dst', NOT including the trailing
122  *   null-byte. If an error occured (empty array or `dst' too small) a value
123  *   smaller than zero will be returned.
124  */
125 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
126
127 /*
128  * NAME
129  *   escape_slashes
130  *
131  * DESCRIPTION
132  *   Removes slashes from the string `buf' and substitutes them with something
133  *   appropriate. This function should be used whenever a path is to be used as
134  *   (part of) an instance.
135  *
136  * PARAMETERS
137  *   `buf'         String to be escaped.
138  *   `buf_len'     Length of the buffer. No more then this many bytes will be
139  *   written to `buf', including the trailing null-byte.
140  *
141  * RETURN VALUE
142  *   Returns zero upon success and a value smaller than zero upon failure.
143  */
144 int escape_slashes (char *buf, int buf_len);
145
146 int strsubstitute (char *str, char c_from, char c_to);
147
148 /* FIXME: `timeval_sub_timespec' needs a description */
149 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
150
151 int check_create_dir (const char *file_orig);
152
153 #ifdef HAVE_LIBKSTAT
154 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
155 long long get_kstat_value (kstat_t *ksp, char *name);
156 #endif
157
158 unsigned long long ntohll (unsigned long long n);
159 unsigned long long htonll (unsigned long long n);
160
161 int format_name (char *ret, int ret_len,
162                 const char *hostname,
163                 const char *plugin, const char *plugin_instance,
164                 const char *type, const char *type_instance);
165 #define FORMAT_VL(ret, ret_len, vl, ds) \
166         format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
167                         (ds)->type, (vl)->type_instance)
168
169 #endif /* COMMON_H */