Release version 2.0
[gmidimonitor.git] / path.c
blob74d5d12d81f203083238bd7be5b5eed9d7b06524
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * DESCRIPTION:
5 * Access to project specific data files.
7 * LICENSE:
8 * GNU GENERAL PUBLIC LICENSE version 2
10 *****************************************************************************/
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <gtk/gtk.h>
18 #include "path.h"
20 static gchar *pszPathToExecutable = NULL;
21 static gchar *pszExecutable = NULL;
23 void
24 path_init(const char * argv0)
26 /* FIXME: pszPathToExecutable calculation is ugly workaround
27 * that assumes that current directory is nevere changed for our process.
28 * We should better get full path (/proc/pid/maps in Linux ?) */
29 pszExecutable = g_path_get_basename(argv0);
30 pszPathToExecutable = g_path_get_dirname(argv0);
33 static void
34 path_check_initialization()
36 if (pszPathToExecutable == NULL ||
37 pszExecutable == NULL)
39 g_warning("path_init() not called.");
40 exit(1);
44 gchar *
45 path_get_data_filename(const gchar * filename)
47 gchar * full_path;
48 struct stat st;
50 path_check_initialization();
52 /* check if it can be found where executable resides */
53 /* This allows executing not installed binary to read right data files */
54 full_path = g_strdup_printf("%s/%s", pszPathToExecutable, filename);
55 if (stat(full_path, &st) == 0)
57 return full_path;
59 g_free(full_path);
61 #if defined(DATA_DIR)
62 /* check in installation data dir */
63 full_path = g_strdup_printf(DATA_DIR "/%s", filename);
64 if (stat(full_path, &st) == 0)
66 return full_path;
68 g_free(full_path);
69 #endif
71 return NULL;
74 void
75 path_uninit()
77 path_check_initialization();
79 g_free(pszExecutable);
80 g_free(pszPathToExecutable);