[l10n] Updated Estonian translation
[bug-buddy.git] / src / distribution.c
bloba04f2588498177fd32ffcb67437ff7fd5f01d604
1 /* bug-buddy bug submitting program
3 * Copyright (C) Jacob Berkman
5 * Author: Jacob Berkman <jberkman@andrew.cmu.edu>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 #include "config.h"
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <libxml/tree.h>
26 #include <libxml/parser.h>
27 #include "bug-buddy.h"
28 #include "distribution.h"
30 typedef struct _Package Package;
31 typedef struct _Phylum Phylum;
32 typedef struct _Distribution Distribution;
34 typedef char *(*DistroVersionFunc) (Distribution *distro);
36 struct _Phylum {
37 DistroVersionFunc version;
41 struct _Distribution {
42 char *name;
43 char *version_file;
44 Phylum *phylum;
47 static char *get_lsb_version (Distribution *distro);
48 static char *get_redhat_version (Distribution *distro);
49 static char *get_turbolinux_version (Distribution *distro);
50 static char *get_debian_version (Distribution *distro);
51 static char *get_irix_version (Distribution *distro);
53 Phylum lsb_phy = {
54 get_lsb_version
57 Phylum redhat_phy = {
58 get_redhat_version
61 Phylum turbolinux_phy = {
62 get_turbolinux_version
65 Phylum debian_phy = {
66 get_debian_version
69 Phylum irix_phy = {
70 get_irix_version
73 DistroVersionFunc lsb_ver = get_lsb_version;
74 DistroVersionFunc redhat_ver = get_redhat_version;
75 DistroVersionFunc turbolinux_ver = get_turbolinux_version;
76 DistroVersionFunc debian_ver = get_debian_version;
77 DistroVersionFunc irix_ver = get_irix_version;
79 static Distribution distros[] = {
80 { "Slackware", "/etc/slackware-version", &debian_phy },
81 { "Mandrake", "/etc/mandrake-release", &redhat_phy },
82 { "TurboLinux", "/etc/turbolinux-release", &turbolinux_phy },
83 { "SuSE", "/etc/SuSE-release", &redhat_phy },
84 { "Red Hat", "/etc/redhat-release", &redhat_phy },
85 { "Frugalware", "/etc/frugalware-release", &redhat_phy },
86 { "Fedora", "/etc/fedora-release", &redhat_phy },
87 { "Gentoo", "/etc/gentoo-release", &redhat_phy },
88 { "Solaris", "/etc/release", &redhat_phy },
89 { "PLD Linux", "/etc/pld-release", &redhat_phy },
90 { "LSB Linux", "/etc/lsb-release", &lsb_phy },
91 { "Debian", "/etc/debian_version", &debian_phy },
92 { "IRIX Freeware", "/bin/hinv", &irix_phy },
93 { NULL }
97 static char**
98 get_lines_from_file (const char *filename)
100 char *contents;
101 gsize length;
102 char **lines;
103 GError *error = NULL;
105 if (!g_file_get_contents (filename, &contents, &length, &error)) {
106 g_error_free (error);
107 return NULL;
110 lines = g_strsplit (contents, "\n", 0);
111 g_free (contents);
113 return lines;
117 static char *
118 get_lsb_version (Distribution *distro)
120 char **lines;
121 char *id = NULL;
122 char *release = NULL;
123 char *codename = NULL;
124 char *name = NULL;
125 int i = 0;
127 g_return_val_if_fail (distro, NULL);
128 g_return_val_if_fail (distro->version_file, NULL);
130 lines = get_lines_from_file (distro->version_file);
131 while (lines && lines[i]) {
132 if (g_str_has_prefix (lines[i], "DISTRIB_ID="))
133 id = lines[i] + 11;
134 else if (g_str_has_prefix (lines[i], "DISTRIB_RELEASE="))
135 release = lines[i] + 16;
136 else if (g_str_has_prefix (lines[i], "DISTRIB_CODENAME="))
137 codename = lines[i] + 17;
138 i++;
140 if (id != NULL && release != NULL && codename != NULL)
141 name = g_strdup_printf ("%s %s (%s)", id, release, codename);
143 g_strfreev (lines);
145 return name;
150 static char *
151 get_redhat_version (Distribution *distro)
153 char **lines;
154 char *name = NULL;
156 g_return_val_if_fail (distro, NULL);
157 g_return_val_if_fail (distro->version_file, NULL);
159 lines = get_lines_from_file (distro->version_file);
160 if (lines != NULL && lines[0] != NULL)
161 name = g_strdup (lines[0]);
163 g_strfreev (lines);
165 return name;
168 static char *
169 get_turbolinux_version (Distribution *distro)
171 char **lines;
172 char *name = NULL;
173 g_return_val_if_fail (distro, NULL);
174 g_return_val_if_fail (distro->version_file, NULL);
176 lines = get_lines_from_file (distro->version_file);
177 if (lines != NULL && lines[0] != NULL)
178 name = g_strdup_printf ("TurboLinux %s", lines[0]);
180 g_strfreev (lines);
182 return name;
186 static char *
187 get_debian_version (Distribution *distro)
189 char **lines;
190 char *name = NULL;
192 g_return_val_if_fail (distro, NULL);
193 g_return_val_if_fail (distro->version_file, NULL);
194 g_return_val_if_fail (distro->name, NULL);
196 lines = get_lines_from_file (distro->version_file);
197 if (lines != NULL && lines[0] != NULL)
198 name = g_strdup_printf ("%s %s", distro->name, lines[0]);
200 g_strfreev (lines);
202 return name;
206 static char *
207 get_irix_version (Distribution *distro)
209 g_return_val_if_fail (distro, NULL);
210 g_return_val_if_fail (distro->version_file, NULL);
211 return "Irix";
213 return get_line_from_command ("/usr/bin/echo -n SGI `/sbin/uname -Rs | /bin/cut -d' ' -f1,3-`; "
214 "versions -b fw_common | awk 'NR >= 4 { print \", Freeware\",$3 }'");*/
217 static char *
218 get_distro_name_from_file (void)
220 int i;
221 for (i = 0; distros[i].name; i++) {
222 if (!g_file_test (distros[i].version_file, G_FILE_TEST_EXISTS))
223 continue;
224 return distros[i].phylum->version (&distros[i]);
226 return NULL;
229 char *
230 get_distro_name (void)
232 char *name;
234 name = get_distro_name_from_file ();
235 if (!name) {
236 return g_strdup ("Unknown");
239 return name;