Whamcloud - gitweb
Fix various gcc -Wall nits. Fixed a bug in mke2fs where a bogus
[tools/e2fsprogs.git] / misc / fstype.c
1 /*
2  * fstype.c
3  * 
4  * Copyright (C) 2001 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <fcntl.h>
17 #include <sys/types.h>
18
19 #include "fsck.h"
20
21 struct fs_magic {
22         const char      *fs_name;
23         int             offset;
24         int             len;
25         const char      *magic;
26 };
27
28 struct fs_magic type_array[] = {
29         { "ext2", 1024+56, 2, "\123\357" },
30         { "minix", 1040, 2, "\177\023" },
31         { "minix", 1040, 2, "\217\023" },
32         { "minix", 1040, 2, "\150\044" },
33         { "minix", 1040, 2, "\170\044" },
34         { "xfs", 0, 4, "XFSB" },
35         { 0, 0, 0, 0 }
36 };
37
38 const char *identify_fs(const char *fs_name)
39 {
40         char    buf[2048];
41         struct fs_magic *p;
42         int     fd;
43
44         fd = open(fs_name, O_RDONLY);
45         if (fd < 0)
46                 return NULL;
47         if (lseek(fd, 0, SEEK_SET) < 0)
48                 return NULL;
49         if (read(fd, buf, sizeof(buf)) != sizeof(buf))
50                 return NULL;
51         for (p = type_array; p->fs_name; p++) {
52                 if (memcmp(p->magic, buf+p->offset, p->len) == 0)
53                         return p->fs_name;
54         }
55         return NULL;
56 }
57
58 #ifdef TEST_PROGRAM
59 int main(int argc, char **argv)
60 {
61         const char      *type;
62         
63         if (argc != 2) {
64                 fprintf(stderr, "Usage: %s device\n", argv[0]);
65                 exit(1);
66         }
67         type = identify_fs(argv[1]);
68         printf("%s is a %s filesystem\n", argv[1], type);
69         return (0);
70 }
71 #endif
72
73         
74