src/util.c: shut up gcc warnings
[vlock.git] / src / util.c
blobd3c0010c58969a4d8c7fb1b440afc6eae3b5374e
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
9 * the author.
13 #if !defined(__FreeBSD__) && !defined(_GNU_SOURCE)
14 #define _GNU_SOURCE
15 #endif
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <time.h>
24 #include <glib.h>
26 #include "util.h"
28 /* Parse the given string (interpreted as seconds) into a
29 * timespec. On error NULL is returned. The caller is responsible
30 * to free the result. The argument may be NULL, in which case NULL
31 * is returned, too. "0" is also parsed as NULL. */
32 struct timespec *parse_seconds(const char *s)
34 if (s == NULL)
35 return NULL;
36 else {
37 char *n;
38 struct timespec *t = calloc(sizeof *t, 1);
40 if (t == NULL)
41 return NULL;
43 t->tv_sec = strtol(s, &n, 10);
45 if (*n != '\0' || t->tv_sec <= 0) {
46 free(t);
47 t = NULL;
50 return t;
54 static GList *atexit_functions;
56 typedef union
58 void *as_pointer;
59 void (*as_function)(void);
60 } function_pointer;
62 void vlock_invoke_atexit(void)
64 while (atexit_functions != NULL) {
65 function_pointer p = { .as_pointer = atexit_functions->data };
66 p.as_function();
67 atexit_functions = g_list_delete_link(atexit_functions,
68 atexit_functions);
72 void vlock_atexit(void (*function)(void))
74 if (atexit_functions == NULL)
75 atexit(vlock_invoke_atexit);
77 function_pointer p = { .as_function = function };
79 atexit_functions = g_list_prepend(atexit_functions, p.as_pointer);