Add fd.c and ds.h: functions for computing free disk space.
[dss.git] / string.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
5 #include <limits.h>
6 #include <errno.h>
7 #include <pwd.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11
12 #include "cmdline.h"
13 #include "gcc-compat.h"
14 #include "log.h"
15 #include "error.h"
16
17 __noreturn void clean_exit(int status);
18
19 /**
20  * Write a message to a dynamically allocated string.
21  *
22  * \param fmt Usual format string.
23  * \param p Result pointer.
24  *
25  * \sa printf(3). */
26 #define VSPRINTF(fmt, p) \
27 { \
28         int n; \
29         size_t size = 100; \
30         p = dss_malloc(size); \
31         while (1) { \
32                 va_list ap; \
33                 /* Try to print in the allocated space. */ \
34                 va_start(ap, fmt); \
35                 n = vsnprintf(p, size, fmt, ap); \
36                 va_end(ap); \
37                 /* If that worked, return the string. */ \
38                 if (n > -1 && n < size) \
39                         break; \
40                 /* Else try again with more space. */ \
41                 if (n > -1) /* glibc 2.1 */ \
42                         size = n + 1; /* precisely what is needed */ \
43                 else /* glibc 2.0 */ \
44                         size *= 2; /* twice the old size */ \
45                 p = dss_realloc(p, size); \
46         } \
47 }
48
49 /**
50  * dss' version of realloc().
51  *
52  * \param p Pointer to the memory block, may be \p NULL.
53  * \param size The desired new size.
54  *
55  * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
56  * i.e. there is no need to check the return value in the caller.
57  *
58  * \return A pointer to  the newly allocated memory, which is suitably aligned
59  * for any kind of variable and may be different from \a p.
60  *
61  * \sa realloc(3).
62  */
63 __must_check __malloc void *dss_realloc(void *p, size_t size)
64 {
65         /*
66          * No need to check for NULL pointers: If p is NULL, the  call
67          * to realloc is equivalent to malloc(size)
68          */
69         assert(size);
70         if (!(p = realloc(p, size))) {
71                 DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
72                         size);
73                 clean_exit(EXIT_FAILURE);
74         }
75         return p;
76 }
77
78 /**
79  * dss' version of malloc().
80  *
81  * \param size The desired new size.
82  *
83  * A wrapper for malloc(3) which exits on errors.
84  *
85  * \return A pointer to the allocated memory, which is suitably aligned for any
86  * kind of variable.
87  *
88  * \sa malloc(3).
89  */
90 __must_check __malloc void *dss_malloc(size_t size)
91 {
92         assert(size);
93         void *p = malloc(size);
94
95         if (!p) {
96                 DSS_EMERG_LOG("malloc failed (size = %zu),  aborting\n",
97                         size);
98                 clean_exit(EXIT_FAILURE);
99         }
100         return p;
101 }
102
103 /**
104  * dss' version of calloc().
105  *
106  * \param size The desired new size.
107  *
108  * A wrapper for calloc(3) which exits on errors.
109  *
110  * \return A pointer to the allocated and zeroed-out memory, which is suitably
111  * aligned for any kind of variable.
112  *
113  * \sa calloc(3)
114  */
115 __must_check __malloc void *dss_calloc(size_t size)
116 {
117         void *ret = dss_malloc(size);
118
119         memset(ret, 0, size);
120         return ret;
121 }
122
123 /**
124  * dss' version of strdup().
125  *
126  * \param s The string to be duplicated.
127  *
128  * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
129  * there is no need to check the return value in the caller.
130  *
131  * \return A pointer to the duplicated string. If \p s was the NULL pointer,
132  * an pointer to an empty string is returned.
133  *
134  * \sa strdup(3)
135  */
136
137 __must_check __malloc char *dss_strdup(const char *s)
138 {
139         char *ret;
140
141         if ((ret = strdup(s? s: "")))
142                 return ret;
143         DSS_EMERG_LOG("strdup failed, aborting\n");
144         clean_exit(EXIT_FAILURE);
145 }
146
147 /**
148  * Allocate a sufficiently large string and print into it.
149  *
150  * \param fmt A usual format string.
151  *
152  * Produce output according to \p fmt. No artificial bound on the length of the
153  * resulting string is imposed.
154  *
155  * \return This function either returns a pointer to a string that must be
156  * freed by the caller or aborts without returning.
157  *
158  * \sa printf(3).
159  */
160 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
161 {
162         char *msg;
163
164         VSPRINTF(fmt, msg);
165         return msg;
166 }
167
168 __printf_1_2 void make_err_msg(const char* fmt,...)
169 {
170         free(dss_error_txt);
171         VSPRINTF(fmt, dss_error_txt);
172 }
173
174 /**
175  * Get the home directory of the current user.
176  *
177  * \return A dynammically allocated string that must be freed by the caller. If
178  * the home directory could not be found, this function returns "/tmp".
179  */
180 __must_check __malloc char *get_homedir(void)
181 {
182         struct passwd *pw = getpwuid(getuid());
183         return dss_strdup(pw? pw->pw_dir : "/tmp");
184 }
185
186 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
187 #ifndef LLONG_MAX
188 #define LLONG_MAX (1 << (sizeof(long) - 1))
189 #endif
190 #ifndef LLONG_MIN
191 #define LLONG_MIN (-LLONG_MAX - 1LL)
192 #endif
193 /** \endcond */
194
195 /**
196  * Convert a string to a 64-bit signed integer value.
197  *
198  * \param str The string to be converted.
199  * \param value Result pointer.
200  *
201  * \return Standard.
202  *
203  * \sa strtol(3), atoi(3).
204  */
205 int dss_atoi64(const char *str, int64_t *value)
206 {
207         char *endptr;
208         long long tmp;
209
210         errno = 0; /* To distinguish success/failure after call */
211         tmp = strtoll(str, &endptr, 10);
212         if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN)) {
213                 make_err_msg("%s", str);
214                 return -E_ATOI_OVERFLOW;
215         }
216         if (errno != 0 && tmp == 0) { /* other error */
217                 make_err_msg("%s", str);
218                 return -E_STRTOLL;
219         }
220         if (endptr == str) {
221                 make_err_msg("%s", str);
222                 return -E_ATOI_NO_DIGITS;
223         }
224         if (*endptr != '\0') { /* Further characters after number */
225                 make_err_msg("%s", str);
226                 return -E_ATOI_JUNK_AT_END;
227         }
228         *value = tmp;
229         return 1;
230 }
231
232 /**
233  * Get the logname of the current user.
234  *
235  * \return A dynammically allocated string that must be freed by the caller. On
236  * errors, the string "unknown user" is returned, i.e. this function never
237  * returns \p NULL.
238  *
239  * \sa getpwuid(3).
240  */
241 __must_check __malloc char *dss_logname(void)
242 {
243         struct passwd *pw = getpwuid(getuid());
244         return dss_strdup(pw? pw->pw_name : "unknown_user");
245 }
246