Follow changes in pkg.m4, fixing Gnome bug #166537, that happened on 2005-10-17 and...
[gmidimonitor.git] / path.c
blob1ce05188d3f03e4b2a1432014c1747f2a56a3857
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 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 <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <gtk/gtk.h>
31 #include "path.h"
33 static gchar *pszPathToExecutable = NULL;
34 static gchar *pszExecutable = NULL;
36 void
37 path_init(const char * argv0)
39 /* FIXME: pszPathToExecutable calculation is ugly workaround
40 * that assumes that current directory is nevere changed for our process.
41 * We should better get full path (/proc/pid/maps in Linux ?) */
42 pszExecutable = g_path_get_basename(argv0);
43 pszPathToExecutable = g_path_get_dirname(argv0);
46 static void
47 path_check_initialization()
49 if (pszPathToExecutable == NULL ||
50 pszExecutable == NULL)
52 g_warning("path_init() not called.");
53 exit(1);
57 gchar *
58 path_get_data_filename(const gchar * filename)
60 gchar * full_path;
61 struct stat st;
63 path_check_initialization();
65 /* check if it can be found where executable resides */
66 /* This allows executing not installed binary to read right data files */
67 full_path = g_strdup_printf("%s/%s", pszPathToExecutable, filename);
68 if (stat(full_path, &st) == 0)
70 return full_path;
72 g_free(full_path);
74 #if defined(DATA_DIR)
75 /* check in installation data dir */
76 full_path = g_strdup_printf(DATA_DIR "/%s", filename);
77 if (stat(full_path, &st) == 0)
79 return full_path;
81 g_free(full_path);
82 #endif
84 return NULL;
87 void
88 path_uninit()
90 path_check_initialization();
92 g_free(pszExecutable);
93 g_free(pszPathToExecutable);