update the website url
[gmidimonitor.git] / path.c
blobaaf95bfe62d9fa91546a1f5e057928c6b82fc6c5
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Access to project specific data files.
6 * This file is part of gmidimonitor
8 * Copyright (C) 2005,2006,2007,2008,2011 Nedko Arnaudov <nedko@arnaudov.name>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 *****************************************************************************/
25 #include "config.h"
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <gtk/gtk.h>
33 #include "path.h"
35 static gchar *pszPathToExecutable = NULL;
36 static gchar *pszExecutable = NULL;
38 void
39 path_init(const char * argv0)
41 /* FIXME: pszPathToExecutable calculation is ugly workaround
42 * that assumes that current directory is nevere changed for our process.
43 * We should better get full path (/proc/pid/maps in Linux ?) */
44 pszExecutable = g_path_get_basename(argv0);
45 pszPathToExecutable = g_path_get_dirname(argv0);
48 static void
49 path_check_initialization()
51 if (pszPathToExecutable == NULL ||
52 pszExecutable == NULL)
54 g_warning("path_init() not called.");
55 exit(1);
59 gchar *
60 path_get_data_filename(const gchar * filename)
62 gchar * full_path;
63 struct stat st;
65 path_check_initialization();
67 /* check if it can be found in the source dir */
68 /* This allows a not installed binary to read right data files */
69 full_path = g_strdup_printf("%s/../%s", pszPathToExecutable, filename);
70 if (stat(full_path, &st) == 0)
72 return full_path;
74 g_free(full_path);
76 #if defined(DATA_DIR)
77 /* check in installation data dir */
78 full_path = g_strdup_printf(DATA_DIR "/%s", filename);
79 if (stat(full_path, &st) == 0)
81 return full_path;
83 g_free(full_path);
84 #endif
86 return NULL;
89 void
90 path_uninit()
92 path_check_initialization();
94 g_free(pszExecutable);
95 g_free(pszPathToExecutable);