summaryrefslogtreecommitdiff
path: root/nautilus-installer/helixcode-utils.c
blob: 5103a67770d19cff42d47104886da2ccc9d2c6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
 * Copyright (C) 2000 Helix Code, Inc
 * Copyright (C) 2000 Eazel, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Joe Shaw <joe@helixcode.com>
 *          J. Shane Culpepper <pepper@eazel.com>
 */

/* Most of this code is taken directly from Joe Shaw's Helix Code install / Updater
 * with a few very minor changes made by me. */

#include <config.h>
#include "helixcode-utils.h"

static float determine_redhat_version (void);
static float determine_mandrake_version (void);
static float determine_turbolinux_version (void);
static float determine_suse_version (void);
static float determine_debian_version (void);

char*
xml_get_value (xmlNode* node, const char* name)
{
	char* ret;
	xmlNode *child;

	ret = xmlGetProp (node, name);
	if (ret) {
		return ret;
	}
	child = node->childs;
	while (child) {
		if (g_strcasecmp (child->name, name) == 0) {
			ret = xmlNodeGetContent (child);
			if (ret) {
				return ret;
			}
		}
		child = child->next;
	}
	return NULL;
} /* end xml_get_value */

xmlDocPtr
prune_xml (char* xmlbuf)
{
	xmlDocPtr doc;
	char* newbuf;
	int length;
	int i;

	newbuf = strstr(xmlbuf, "<?xml");
	if (!newbuf) {
		return NULL;
	}
	length = strlen (newbuf);
	for (i = 0; i < length; i++) {
		if (newbuf[i] == '\0') {
			newbuf[i] = ' ';
		}
	}
	newbuf[length] = '\0';
	doc = xmlParseMemory (newbuf, length);

	if (!doc) {
		fprintf(stderr, "***Could not prune package file !***\n");
		return NULL;
	}

	return doc;
} /* end prune_xml */

gboolean
check_for_root_user (void)
{
	uid_t uid;

	uid = getuid ();
	if (uid == 0) {
		return TRUE;
	}
	else {
		return FALSE;
	}
} /* end check_for_root_user */

gboolean
check_for_redhat (void)
{
	if (g_file_exists ("/etc/redhat-release") != 0) {
		return TRUE;
	}
	else {
		return FALSE;
	}
} /* end check_for_redhat */

/* FIXME bugzilla.eazel.com 908: - the following functions are not
 * closing fd correctly.  This needs to be fixed and the functions
 * need to be cleaned up.  They are ugly right now.
 */
static float
determine_redhat_version (void)
{

	int fd;
	char buf[1024];
	char* text;
	char* v;
	float version;

	fd = open ("/etc/redhat-release", O_RDONLY);
	g_return_val_if_fail (fd != -1, 0);
	read (fd, buf, 1023);
	close (fd);
	buf[1023] = '\0';
	/* These check for LinuxPPC. For whatever reason, they use
	   /etc/redhat-release */
	text = strstr (buf, "1999");
	if (text)
		return 1999;
	text = strstr (buf, "2000");
	if (text)
		return 2000;
	text = strstr (buf, "6.0");
	if (text)
		return 6.0;
	text = strstr (buf, "6.");
	if (text) {
		v = g_strndup (text, 3);
		sscanf (v, "%f", &version);
		g_free (v);
		return version;
	}
	text = strstr (buf, "5.");
	if (text) {
		v = g_strndup (text, 3);
		sscanf (v, "%f", &version);
		g_free (v);
		return version;
	}
	return 0.0;
} /* determine_redhat_version */

static float
determine_mandrake_version (void)
{
	int fd;
	char buf[1024];
	char* text;
	char* v;
	float version;

	fd = open ("/etc/mandrake-release", O_RDONLY);
	g_return_val_if_fail (fd != -1, 0);
	read (fd, buf, 1023);
	close (fd);
	buf[1023] = '\0';
	text = strstr (buf, "6.0");
	if (text)
		return 6.0;
	text = strstr (buf, "6.");
	if (text) {
		v = g_strndup (text, 3);
		sscanf (v, "%f", &version);
		g_free (v);
		return version;
	}
	text = strstr (buf, "7.");
	if (text) {
		v = g_strndup (text, 3);
		sscanf (v, "%f", &version);
		g_free (v);
		return version;
	}
	return 0.0;
} /* determine_mandrake_version */

static float
determine_turbolinux_version (void)
{
	int fd;
	char buf[1024];
	char* text;
	char* v;
	float version;

	fd = open ("/etc/turbolinux-release", O_RDONLY);
	g_return_val_if_fail (fd != -1, 0);
	read (fd, buf, 1023);
	close (fd);
	buf[1023] = '\0';
	text = strstr (buf, "7.");
	if (text)
		return 7;
	text = strstr (buf, "6.");
	if (text) {
		v = g_strndup (text, 3);
		sscanf (v, "%f", &version);
		g_free (v);
		return version;
	}
	text = strstr (buf, "4.");
	if (text) {
		v = g_strndup (text, 3);
		sscanf (v, "%f", &version);
		g_free (v);
		return version;
	}
	return 0.0;
} /* determine_turbolinux_version */

static float
determine_suse_version (void)
{
	int fd;
	char buf[1024];
	char* text;
	char* v;
	float version;

	fd = open ("/etc/SuSE-release", O_RDONLY);
	g_return_val_if_fail (fd != -1, 0);
	read (fd, buf, 1023);
	close (fd);
	buf[1023] = '\0';
	text = strstr (buf, "6.");
	if (!text)
		return 0.0;
	v = g_strndup (text, 3);
	if (!v)
		return 0.0;
	sscanf (v, "%f", &version);
	g_free (v);
	if (version >= 2.2)
		return version;
	else
		return 0.0;
} /* determine_suse_version */

static float
determine_debian_version (void)
{

	int fd;
	char buf[1024];
        float version;
	
	fd = open ("/etc/debian_version", O_RDONLY);
	g_return_val_if_fail (fd != -1, 0);
	read (fd, buf, 1023);
	close (fd);
	buf[1023] = '\0';
	sscanf (buf, "%f", &version);
	if (version < 2.1)
		return 0.0;
	return version;
} /* determine_debian_version */

DistributionType
determine_distribution_type (void)
{
	float version;
	DistributionType rv;

	/* Check for TurboLinux */
	if (g_file_exists ("/etc/turbolinux-release")) {
		version = determine_turbolinux_version ();
		if (version >= 7) {
			rv = DISTRO_TURBOLINUX_6;
		}
		else if (version >= 6) {
			rv = DISTRO_TURBOLINUX_6;
		}
		else if (version >= 4) {
			rv = DISTRO_TURBOLINUX_4;
		}
		return rv;
	}
	/* Check for Mandrake */
	if (g_file_exists ("/etc/mandrake-release")) {
		version = determine_mandrake_version ();
		if (version >= 7) {
			rv = DISTRO_MANDRAKE_7;
		}
		else if (version > 6) {
			rv = DISTRO_MANDRAKE_6_1;
		}
		else {
			rv = DISTRO_UNKNOWN;
		}
		return rv;
	}
	/* Check for SuSE */
	else if (g_file_exists ("/etc/SuSE-release")) {
		version = determine_suse_version ();
		if (version) {
			rv = DISTRO_SUSE;
		}
		else {
			rv = DISTRO_UNKNOWN;
		}
		return rv;
	}
	/* Check for Corel */
	else if (g_file_exists ("/etc/environment.corel")) {
	        rv = DISTRO_COREL;
		return rv;
	}
	/* Check for Debian */
	else if (g_file_exists ("/etc/debian_version")) {
		version = determine_debian_version ();
		if (version == 2.1) {
			rv = DISTRO_DEBIAN_2_1;
		}
		else if (version >= 2.2) {
			rv = DISTRO_DEBIAN_2_2;
		}
		else {
			rv = DISTRO_UNKNOWN;
		}
		return rv;
	}
	/* Check for Caldera */
	else if (g_file_exists ("/etc/coas")) {
		rv = DISTRO_CALDERA;
		return rv;
	}
	/* Check for Red Hat/LinuxPPC */
	/* This has to be checked last because many of the Red Hat knockoff
	   distros keep /etc/redhat-release around. */
	if (g_file_exists ("/etc/redhat-release")) {
		version = determine_redhat_version ();
		if (version >= 1999) {
			rv = DISTRO_LINUXPPC;
		}
		else if (version == 6.0) {
			rv = DISTRO_REDHAT_6;
		}
		else if (version == 6.1) {
			rv = DISTRO_REDHAT_6_1;
		}
		else if (version >= 6.2) {
			rv = DISTRO_REDHAT_6_2;
		}
		else if (version >= 5) {
			rv = DISTRO_REDHAT_5;
		}
		else {
			rv = DISTRO_UNKNOWN;
		}
		return rv;
	}
	/* If all fail... */
	rv = DISTRO_UNKNOWN;
	return rv;
} /* determine_distribution_type */

char *
get_distribution_name (const DistributionType* dtype)
{
	switch ((int) dtype) {
	case DISTRO_REDHAT_5:
		return g_strdup ("RedHat Linux 5.x");
	case DISTRO_REDHAT_6:
		return g_strdup ("RedHat Linux 6.0");
	case DISTRO_REDHAT_6_1:
		return g_strdup ("RedHat Linux 6.1");
	case DISTRO_REDHAT_6_2:
		return g_strdup ("RedHat Linux 6.2");
	case DISTRO_MANDRAKE_6_1:
		return g_strdup ("Mandrake Linux 6.2");
	case DISTRO_MANDRAKE_7:
		return g_strdup ("Mandrake Linux 7.x");
	case DISTRO_CALDERA:
		return g_strdup ("Caldera OpenLinux");
	case DISTRO_SUSE:
		return g_strdup ("SuSe Linux");
	case DISTRO_LINUXPPC:
		return g_strdup ("LinuxPPC");
	case DISTRO_TURBOLINUX_4:
		return g_strdup ("TurboLinux 4.x");
	case DISTRO_TURBOLINUX_6:
		return g_strdup ("TurboLinux 6.x");
	case DISTRO_COREL:
		return g_strdup ("Corel Linux");
	case DISTRO_DEBIAN_2_1:
		return g_strdup ("Debian GNU/Linux 2.1");
	case DISTRO_DEBIAN_2_2:
		return g_strdup ("Debian GNU/Linux 2.2");
	case DISTRO_UNKNOWN:
		return g_strdup ("unknown Linux distribution");
	default:
		fprintf (stderr, "Invalid DistributionType !\n");
		exit (1);
	}
} /* end get_distribution_name */