summaryrefslogtreecommitdiff
path: root/misc/fstype.c
blob: 8f127386ecf4e57657970027735c407cacf93452 (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
/*
 * fstype.c
 * 
 * Copyright (C) 2001 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include <stdio.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/types.h>

#include "fsck.h"

struct fs_magic {
	const char 	*fs_name;
	int		offset;
	int		len;
	const char	*magic;
};

struct fs_magic type_array[] = {
	{ "ext2", 1024+56, 2, "\123\357" },
	{ "minix", 1040, 2, "\177\023" },
	{ "minix", 1040, 2, "\217\023" },
	{ "minix", 1040, 2, "\150\044" },
	{ "minix", 1040, 2, "\170\044" },
	{ "xfs", 0, 4, "XFSB" },
	{ 0, 0, 0, 0 }
};

const char *identify_fs(const char *fs_name)
{
	char	buf[2048];
	struct fs_magic *p;
	int	fd;

	fd = open(fs_name, O_RDONLY);
	if (fd < 0)
		return NULL;
	if (lseek(fd, 0, SEEK_SET) < 0)
		return NULL;
	if (read(fd, buf, sizeof(buf)) != sizeof(buf))
		return NULL;
	for (p = type_array; p->fs_name; p++) {
		if (memcmp(p->magic, buf+p->offset, p->len) == 0)
			return p->fs_name;
	}
	return NULL;
}

#ifdef TEST_PROGRAM
int main(int argc, char **argv)
{
	const char	*type;
	
	if (argc != 2) {
		fprintf(stderr, "Usage: %s device\n", argv[0]);
		exit(1);
	}
	type = identify_fs(argv[1]);
	printf("%s is a %s filesystem\n", argv[1], type);
	return (0);
}
#endif