Add missing libraries to examples' Makefiles
[dockapps.git] / wmclockmon / wmclockmon-cal / tools.c
blob5fe6bc60d55d6fce3429e036572c7c46827d178c
1 /*
2 * Tools : memory management, file loading and saving
3 */
5 #include "../config.h"
6 #include "defines.h"
7 #include "tools.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include <sys/types.h>
13 #include <ctype.h>
14 #include <pwd.h>
15 #include <unistd.h>
16 #include <sys/types.h>
19 void *xmalloc(size_t size) {
20 void *ret = malloc(size);
21 if (ret == NULL) {
22 perror("malloc() ");
23 exit(-1);
24 } else
25 return ret;
29 char *xstrdup(const char *string) {
30 char *ret = strdup(string);
31 if (ret == NULL) {
32 perror("strdup() ");
33 exit(-1);
34 } else
35 return ret;
39 int getbool(const char *value) {
40 if (strcmp(value, "0") == 0) return FALSE;
41 if (strcmp(value, "1") == 0) return TRUE;
42 if (strcasecmp(value, "true") == 0) return TRUE;
43 if (strcasecmp(value, "false") == 0) return FALSE;
44 if (strcasecmp(value, "yes") == 0) return TRUE;
45 if (strcasecmp(value, "no") == 0) return FALSE;
46 if (strcasecmp(value, "on") == 0) return TRUE;
47 if (strcasecmp(value, "off") == 0) return FALSE;
48 printf("Error in converting \"%s\" to boolean value.\n", value);
49 return FALSE;
53 char *robust_home(void) {
54 if (getenv("HOME"))
55 return getenv("HOME");
56 else if (getenv("USER") && getpwnam(getenv("USER")))
57 return getpwnam(getenv("USER"))->pw_dir;
58 else if (getenv("LOGNAME") && getpwnam(getenv("LOGNAME")))
59 return getpwnam(getenv("LOGNAME"))->pw_dir;
60 else if (getpwuid(getuid()))
61 return getpwuid(getuid())->pw_dir;
62 else
63 return "/";
67 char *get_file(const char *datestr) {
68 char *Home = robust_home();
69 int len = strlen(Home) + strlen(DEFAULT_CONFIGDIR) + strlen(datestr);
70 char *filename = xmalloc(len + 3);
72 sprintf(filename, "%s/%s/%s", Home, DEFAULT_CONFIGDIR, datestr);
73 return filename;