Code

octo@casella:~/collectd $ svn merge -r753:807 trunk branches/processes
[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 **/
25 #ifndef COMMON_H
26 #define COMMON_H
28 #include "collectd.h"
30 #define sfree(ptr) \
31         if((ptr) != NULL) { \
32                 free(ptr); \
33         } \
34         (ptr) = NULL
36 void sstrncpy(char *d, const char *s, int len);
37 char *sstrdup(const char *s);
38 void *smalloc(size_t size);
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)'.
47  *
48  * PARAMETERS
49  *   `fd'          File descriptor to write to.
50  *   `buf'         Buffer that is to be written.
51  *   `count'       Numver of bytes in the buffer.
52  *
53  * RETURN VALUE
54  *   Zero upon success or non-zero if an error occured. `errno' is set in this
55  *   case.
56  */
57 ssize_t sread (int fd, void *buf, size_t count);
59 /*
60  * NAME
61  *   swrite
62  *
63  * DESCRIPTION
64  *   Writes exactly `n' bytes or failes. Syntax and other behavior is analogous
65  *   to `write(2)'.
66  *
67  * PARAMETERS
68  *   `fd'          File descriptor to write to.
69  *   `buf'         Buffer that is to be written.
70  *   `count'       Numver of bytes in the buffer.
71  *
72  * RETURN VALUE
73  *   Zero upon success or non-zero if an error occured. `errno' is set in this
74  *   case.
75  */
76 ssize_t swrite (int fd, const void *buf, size_t count);
78 /*
79  * NAME
80  *   strsplit
81  *
82  * DESCRIPTION
83  *   Splits a string into parts and stores pointers to the parts in `fields'.
84  *   The characters split at are ` ' (space) and "\t" (tab).
85  *
86  * PARAMETERS
87  *   `string'      String to split. This string will be modified. `fields' will
88  *                 contain pointers to parts of this string, so free'ing it
89  *                 will destroy `fields' as well.
90  *   `fields'      Array of strings where pointers to the parts will be stored.
91  *   `size'        Number of elements in the array. No more than `size'
92  *                 pointers will be stored in `fields'.
93  *
94  * RETURN VALUE
95  *    Returns the number of parts stored in `fields'.
96  */
97 int strsplit (char *string, char **fields, size_t size);
99 /*
100  * NAME
101  *   strjoin
102  *
103  * DESCRIPTION
104  *   Joins together several parts of a string using `sep' as a seperator. This
105  *   is equipollent to the perl buildin `join'.
106  *
107  * PARAMETERS
108  *   `dst'         Buffer where the result is stored.
109  *   `dst_len'     Length of the destination buffer. No more than this many
110  *                 bytes will be written to the memory pointed to by `dst',
111  *                 including the trailing null-byte.
112  *   `fields'      Array of strings to be joined.
113  *   `fields_num'  Number of elements in the `fields' array.
114  *   `sep'         String to be inserted between any two elements of `fields'.
115  *                 This string is neither prepended nor appended to the result.
116  *                 Instead of passing "" (empty string) one can pass NULL.
117  *
118  * RETURN VALUE
119  *   Returns the number of characters in `dst', NOT including the trailing
120  *   null-byte. If an error occured (empty array or `dst' too small) a value
121  *   smaller than zero will be returned.
122  */
123 int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const char *sep);
125 /*
126  * NAME
127  *   escape_slashes
128  *
129  * DESCRIPTION
130  *   Removes slashes from the string `buf' and substitutes them with something
131  *   appropriate. This function should be used whenever a path is to be used as
132  *   (part of) an instance.
133  *
134  * PARAMETERS
135  *   `buf'         String to be escaped.
136  *   `buf_len'     Length of the buffer. No more then this many bytes will be
137  *   written to `buf', including the trailing null-byte.
138  *
139  * RETURN VALUE
140  *   Returns zero upon success and a value smaller than zero upon failure.
141  */
142 int escape_slashes (char *buf, int buf_len);
144 /* FIXME: `timeval_sub_timespec' needs a description */
145 int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
147 int rrd_update_file (char *host, char *file, char *values,
148                 char **ds_def, int ds_num);
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
155 #endif /* COMMON_H */