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