Initialize variable
[glib.git] / gio / gio-querymodules.c
blob74c6594628d1bb6b3ade9f06aecd87b1e9cd0328
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
22 #include "giomodule.h"
23 #include "giomodule-priv.h"
25 #include <gstdio.h>
26 #include <errno.h>
27 #include <locale.h>
29 static gboolean
30 is_valid_module_name (const gchar *basename)
32 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
33 return
34 g_str_has_prefix (basename, "lib") &&
35 g_str_has_suffix (basename, ".so");
36 #else
37 return g_str_has_suffix (basename, ".dll");
38 #endif
41 static void
42 query_dir (const char *dirname)
44 GString *data;
45 GDir *dir;
46 GList *list = NULL, *iterator = NULL;
47 const char *name;
48 char *cachename;
49 char **(* query) (void);
50 GError *error;
51 int i;
53 if (!g_module_supported ())
54 return;
56 error = NULL;
57 dir = g_dir_open (dirname, 0, &error);
58 if (!dir)
60 g_printerr ("Unable to open directory %s: %s\n", dirname, error->message);
61 g_error_free (error);
62 return;
65 data = g_string_new ("");
67 while ((name = g_dir_read_name (dir)))
68 list = g_list_prepend (list, g_strdup (name));
70 list = g_list_sort (list, (GCompareFunc) g_strcmp0);
71 for (iterator = list; iterator; iterator = iterator->next)
73 GModule *module;
74 gchar *path;
75 char **extension_points;
77 name = iterator->data;
78 if (!is_valid_module_name (name))
79 continue;
81 path = g_build_filename (dirname, name, NULL);
82 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
83 g_free (path);
85 if (module)
87 gchar *modulename;
88 gchar *symname;
90 modulename = _g_io_module_extract_name (name);
91 symname = g_strconcat ("g_io_", modulename, "_query", NULL);
92 g_module_symbol (module, symname, (gpointer) &query);
93 g_free (symname);
94 g_free (modulename);
96 if (!query)
98 /* Fallback to old name */
99 g_module_symbol (module, "g_io_module_query", (gpointer) &query);
102 if (query)
104 extension_points = query ();
106 if (extension_points)
108 g_string_append_printf (data, "%s: ", name);
110 for (i = 0; extension_points[i] != NULL; i++)
111 g_string_append_printf (data, "%s%s", i == 0 ? "" : ",", extension_points[i]);
113 g_string_append (data, "\n");
114 g_strfreev (extension_points);
118 g_module_close (module);
122 g_dir_close (dir);
123 g_list_free_full (list, g_free);
125 cachename = g_build_filename (dirname, "giomodule.cache", NULL);
127 if (data->len > 0)
129 error = NULL;
131 if (!g_file_set_contents (cachename, data->str, data->len, &error))
133 g_printerr ("Unable to create %s: %s\n", cachename, error->message);
134 g_error_free (error);
137 else
139 if (g_unlink (cachename) != 0 && errno != ENOENT)
141 int errsv = errno;
142 g_printerr ("Unable to unlink %s: %s\n", cachename, g_strerror (errsv));
146 g_free (cachename);
147 g_string_free (data, TRUE);
151 main (gint argc,
152 gchar *argv[])
154 int i;
156 if (argc == 1)
158 g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
159 g_print ("Will update giomodule.cache in the listed directories\n");
160 return 1;
163 setlocale (LC_ALL, "");
165 /* Be defensive and ensure we're linked to GObject */
166 g_type_ensure (G_TYPE_OBJECT);
168 for (i = 1; i < argc; i++)
169 query_dir (argv[i]);
171 return 0;