1 /* util.c -- utility routines for vlock, the VT locking program for linux
3 * This program is copyright (C) 2007 Frank Benkstein, and is free
4 * software which is freely distributable under the terms of the
5 * GNU General Public License version 2, included as the file COPYING in this
6 * distribution. It is NOT public domain software, and any
7 * redistribution not permitted by the GNU General Public License is
8 * expressly forbidden without prior written permission from
13 #if !defined(__FreeBSD__) && !defined(_GNU_SOURCE)
24 /* Parse the given string (interpreted as seconds) into a
25 * timespec. On error NULL is returned. The caller is responsible
26 * to free the result. The string may be NULL, in which case NULL
27 * is returned, too. */
28 struct timespec
*parse_seconds(const char *s
)
34 struct timespec
*t
= calloc(sizeof *t
, 1);
39 t
->tv_sec
= strtol(s
, &n
, 10);
41 if (*n
!= '\0' || t
->tv_sec
<= 0) {
50 void fatal_error(const char *format
, ...)
55 if (vasprintf(&error
, format
, ap
) < 0)
56 error
= "error while formatting error message";
58 fatal_error_free(error
);
61 void fatal_error_free(char *error
)
68 void *ensure_malloc(size_t size
)
70 void *r
= malloc(size
);
73 fatal_error("failed to allocate %d bytes", size
);
78 void *ensure_calloc(size_t number
, size_t size
)
80 void *r
= calloc(number
, size
);
83 fatal_error("failed to allocate %d bytes", size
);
88 void *ensure_realloc(void *ptr
, size_t size
)
90 void *r
= realloc(ptr
, size
);
92 if (size
!= 0 && r
== NULL
)
93 fatal_error("failed to reallocate %d bytes at %p", size
, ptr
);
98 void *ensure_not_null(void *data
, const char *errmsg
)