memprof.glade: Open and save the file
[memprof.git] / src / inode.c
blob52b4faf17a705c1f9fe997ae07fd4e842a36f2b7
1 /* -*- mode: C; c-file-style: "linux" -*- */
3 /* MemProf -- memory profiler and leak detector
4 * Copyright 1999, 2000, 2001, Red Hat, Inc.
5 * Copyright 2002, Kristian Rietveld
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /*====*/
23 #include <glib.h>
24 #include <sys/stat.h>
25 #include <dirent.h>
26 #include "memprof.h"
28 /************************************************************
29 * Inode finding code - not needed for kernel 2.2 or greater
30 ************************************************************/
32 GHashTable *inode_table = NULL;
34 typedef struct {
35 dev_t device;
36 ino_t inode;
37 gchar *name;
38 } Inode;
40 static guint
41 inode_hash (gconstpointer data)
43 return (((Inode *)data)->device + (((Inode *)data)->inode << 11));
46 static gint
47 inode_compare (gconstpointer a, gconstpointer b)
49 return ((((Inode *)a)->device == ((Inode *)b)->device) &&
50 (((Inode *)a)->inode == ((Inode *)b)->inode));
53 void
54 read_inode (const gchar *path)
56 struct stat stbuf;
58 g_return_if_fail (path != NULL);
60 if (!inode_table)
61 inode_table = g_hash_table_new (inode_hash, inode_compare);
63 if (!stat (path, &stbuf)) {
64 Inode *inode = g_new (Inode, 1);
65 inode->device = stbuf.st_dev;
66 inode->inode = stbuf.st_ino;
67 if (!g_hash_table_lookup (inode_table, inode)) {
68 inode->name = g_strdup (path);
69 g_hash_table_insert (inode_table, inode, inode);
70 } else
71 g_free (inode);
75 static void
76 read_inodes ()
78 static const char *directories[] = {
79 "/lib",
80 "/usr/lib",
81 "/usr/X11R6/lib",
82 "/usr/local/lib",
83 "/opt/gnome/lib",
84 NULL
87 const char **dirname;
89 for (dirname = directories; *dirname; dirname++)
91 DIR *dir = opendir (*dirname);
93 if (dir) {
94 struct dirent *ent;
95 while ((ent = readdir (dir))) {
96 gchar buf[1024];
97 snprintf(buf, 1024-1, "%s/%s", *dirname, ent->d_name);
98 read_inode (buf);
101 closedir (dir);
106 gchar *
107 locate_inode (dev_t device, ino_t inode)
109 Inode lookup;
110 Inode *result;
112 lookup.device = device;
113 lookup.inode = inode;
115 if (!inode_table)
116 read_inodes ();
118 result = g_hash_table_lookup (inode_table, &lookup);
119 if (result)
120 return result->name;
121 else
122 return NULL;