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)
26 /* Parse the given string (interpreted as seconds) into a
27 * timespec. On error NULL is returned. The caller is responsible
28 * to free the result. The string may be NULL, in which case NULL
29 * is returned, too. */
30 struct timespec
*parse_seconds(const char *s
)
36 struct timespec
*t
= calloc(sizeof *t
, 1);
41 t
->tv_sec
= strtol(s
, &n
, 10);
43 if (*n
!= '\0' || t
->tv_sec
<= 0) {
52 void fatal_error(const char *format
, ...)
57 if (vasprintf(&error
, format
, ap
) < 0)
58 error
= "error while formatting error message";
60 fatal_error_free(error
);
63 void fatal_error_free(char *error
)
71 void fatal_perror(const char *errmsg
)
74 fatal_error("%s: %s", errmsg
, strerror(errno
));
76 fatal_error("%s", errmsg
);
79 void *ensure_malloc(size_t size
)
81 void *r
= malloc(size
);
84 fatal_error("failed to allocate %d bytes", size
);
89 void *ensure_calloc(size_t number
, size_t size
)
91 void *r
= calloc(number
, size
);
94 fatal_error("failed to allocate %d bytes", size
);
99 void *ensure_realloc(void *ptr
, size_t size
)
101 void *r
= realloc(ptr
, size
);
103 if (size
!= 0 && r
== NULL
)
104 fatal_error("failed to reallocate %d bytes at %p", size
, ptr
);
109 void *ensure_not_null(void *data
, const char *errmsg
)