1 | /* $Id: util.h 30 2011-11-18 14:37:04Z mmamonski $ */ |
---|
2 | /* |
---|
3 | * FedStage DRMAA utilities library |
---|
4 | * Copyright (C) 2006-2008 FedStage Systems |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * @file util.h |
---|
22 | * Various functions. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef __DRMAA_UTILS__UTIL_H |
---|
26 | #define __DRMAA_UTILS__UTIL_H |
---|
27 | |
---|
28 | #ifdef HAVE_CONFIG_H |
---|
29 | # include <config.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <sys/time.h> |
---|
33 | #include <string.h> |
---|
34 | #include <stdio.h> |
---|
35 | |
---|
36 | #include <drmaa_utils/common.h> |
---|
37 | #include <drmaa_utils/compat.h> |
---|
38 | |
---|
39 | char *fsd_explode( const char *const *vector, char glue, int n ); |
---|
40 | void fsd_free_vector( char **vector ); |
---|
41 | char **fsd_copy_vector( const char *const * vector ); |
---|
42 | char *fsd_replace( char *input, const char *placeholder, const char *value ); |
---|
43 | |
---|
44 | char *fsd_strdup( const char *s ); |
---|
45 | char *fsd_strndup( const char *s, size_t n ); |
---|
46 | |
---|
47 | int fsd_atoi( const char *s ); |
---|
48 | |
---|
49 | void |
---|
50 | fsd_str_append( |
---|
51 | bool *truncated, |
---|
52 | char **p, char *end, |
---|
53 | const char *fmt, ... |
---|
54 | ) |
---|
55 | __attribute__(( format( printf, 4, 5 ) )); |
---|
56 | |
---|
57 | size_t |
---|
58 | fsd_snprintf( |
---|
59 | bool *truncated, |
---|
60 | char *str, size_t size, |
---|
61 | const char *fmt, ... |
---|
62 | ) |
---|
63 | __attribute__(( format( printf, 4, 5 ) )); |
---|
64 | |
---|
65 | size_t |
---|
66 | fsd_vsnprintf( |
---|
67 | bool *truncated, |
---|
68 | char *str, size_t size, |
---|
69 | const char *fmt, va_list args |
---|
70 | ); |
---|
71 | |
---|
72 | /** |
---|
73 | * Behaves like asprintf function from standard C library |
---|
74 | * except any errors are marked in error context structure. |
---|
75 | * |
---|
76 | * It substitutes `%m` format string as in glibc |
---|
77 | * (with `strerror(errno)`). |
---|
78 | */ |
---|
79 | char *fsd_asprintf( const char *fmt, ... ) |
---|
80 | __attribute__(( format( __printf__, 1, 2 ) )); |
---|
81 | |
---|
82 | /** |
---|
83 | * Behaves like vasprintf function from standard C library |
---|
84 | * except any errors are marked in error context structure. |
---|
85 | */ |
---|
86 | char *fsd_vasprintf( const char *fmt, va_list args ); |
---|
87 | |
---|
88 | /** |
---|
89 | * Implements GNU version of strerror_r function - thread-safe |
---|
90 | * function returning message describing error code. |
---|
91 | * |
---|
92 | * From Linux Programmer's Manual: |
---|
93 | * |
---|
94 | * <code> char *strerror_r(int errnum, char *buf, size_t buflen); </code> |
---|
95 | * |
---|
96 | * The GNU-specific strerror_r() returns a pointer to a string containing |
---|
97 | * the error message. This may be either a pointer to a string that the |
---|
98 | * function stores in buf, or a pointer to some (immutable) static string |
---|
99 | * (in which case buf is unused). If the function stores a string in |
---|
100 | * buf, then at most buflen bytes are stored (the string may be truncated |
---|
101 | * if buflen is too small) and the string always includes a terminating |
---|
102 | * null byte. |
---|
103 | */ |
---|
104 | const char * |
---|
105 | fsd_strerror_r( int errnum, char *buffer, size_t buffer_size ); |
---|
106 | |
---|
107 | char * |
---|
108 | fsd_astrerror( int errnum, bool *malloced ); |
---|
109 | |
---|
110 | /** Retrievs current system timestamp. */ |
---|
111 | void fsd_get_time( struct timespec *ts ); |
---|
112 | |
---|
113 | /** Add delta to timestamp. */ |
---|
114 | void fsd_ts_add( struct timespec *a, const struct timespec *b ); |
---|
115 | |
---|
116 | /** |
---|
117 | * Compares two timestamps. |
---|
118 | * @return Negative integer when a < b (a represents earlier timestamp), |
---|
119 | * positive integer when a > b or 0 when timestamps are equal. |
---|
120 | */ |
---|
121 | int fsd_ts_cmp( const struct timespec *a, const struct timespec *b ); |
---|
122 | |
---|
123 | /** |
---|
124 | * Reads file contents. |
---|
125 | * @param filename Path to the file. |
---|
126 | * @param must_exist Controls behaviour when file not exist |
---|
127 | * (or is not readable). If set to \c true DRMAA_ERRNO_INTERNAL_ERROR |
---|
128 | * is raised on such occasion. If set to \c false only |
---|
129 | * \a content is set to \c NULL and no error is raised. |
---|
130 | * @param content Filled with pointer to buffer with file contents |
---|
131 | * or \c NULL when error is encoureged. Caller is responsible |
---|
132 | * for free()'ing it. |
---|
133 | * @param length Filled with length of \a content buffer. |
---|
134 | */ |
---|
135 | void |
---|
136 | fsd_read_file( |
---|
137 | const char *filename, bool must_exist, |
---|
138 | char **content, size_t *length |
---|
139 | ); |
---|
140 | |
---|
141 | /** |
---|
142 | * Gets path to current working directory. |
---|
143 | * @return Path to process's current working directory |
---|
144 | * in malloc'ed '\0' terminated buffer |
---|
145 | * or \c NULL in case of error. |
---|
146 | */ |
---|
147 | char * |
---|
148 | fsd_getcwd(void); |
---|
149 | |
---|
150 | /** |
---|
151 | * Returns signal name. |
---|
152 | * @param signum Valid signal number. |
---|
153 | */ |
---|
154 | const char * |
---|
155 | fsd_strsignal( int signum ); |
---|
156 | |
---|
157 | /** |
---|
158 | * Gets next line from the file |
---|
159 | * @return The next line of the file (dynamically allocated) |
---|
160 | * or \c NULL on EOF. |
---|
161 | */ |
---|
162 | char * fsd_readline(FILE *f); |
---|
163 | |
---|
164 | #endif /* __DRMAA_UTILS__UTIL_H */ |
---|