2 * collectd - src/common.h
3 * Copyright (C) 2005-2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
20 * Niki W. Waibel <niki.waibel@gmx.net>
34 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
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);
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
51 * `fd' File descriptor to write to.
52 * `buf' Buffer that is to be written.
53 * `count' Numver of bytes in the buffer.
56 * Zero upon success or non-zero if an error occured. `errno' is set in this
59 ssize_t sread (int fd, void *buf, size_t count);
66 * Writes exactly `n' bytes or failes. Syntax and other behavior is analogous
70 * `fd' File descriptor to write to.
71 * `buf' Buffer that is to be written.
72 * `count' Numver of bytes in the buffer.
75 * Zero upon success or non-zero if an error occured. `errno' is set in this
78 ssize_t swrite (int fd, const void *buf, size_t count);
85 * Splits a string into parts and stores pointers to the parts in `fields'.
86 * The characters split at are ` ' (space) and "\t" (tab).
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'.
97 * Returns the number of parts stored in `fields'.
99 int strsplit (char *string, char **fields, size_t size);
106 * Joins together several parts of a string using `sep' as a seperator. This
107 * is equipollent to the perl buildin `join'.
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.
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.
125 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
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.
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.
142 * Returns zero upon success and a value smaller than zero upon failure.
144 int escape_slashes (char *buf, int buf_len);
146 int strsubstitute (char *str, char c_from, char c_to);
148 /* FIXME: `timeval_sub_timespec' needs a description */
149 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
151 int check_create_dir (const char *file_orig);
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);
158 unsigned long long ntohll (unsigned long long n);
159 unsigned long long htonll (unsigned long long n);
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)
169 #endif /* COMMON_H */