Whamcloud - gitweb
mke2fs: Fix compile warning message
[tools/e2fsprogs.git] / lib / e2p / ostype.c
1 /*
2  * getostype.c          - Get the Filesystem OS type
3  *
4  * Copyright (C) 2004,2005  Theodore Ts'o <tytso@mit.edu>
5  *
6  * This file can be redistributed under the terms of the GNU Library General
7  * Public License
8  */
9
10 #include "e2p.h"
11 #include <string.h>
12 #include <stdlib.h>
13
14 static const char *os_tab[] =
15         { "Linux",
16           "Hurd",
17           "Masix",
18           "FreeBSD",
19           "Lites",
20           0 };
21
22 /*
23  * Convert an os_type to a string
24  */
25 char *e2p_os2string(int os_type)
26 {
27         const char      *os;
28         char            *ret;
29
30         if (os_type <= EXT2_OS_LITES)
31                 os = os_tab[os_type];
32         else
33                 os = "(unknown os)";
34
35         ret = malloc(strlen(os)+1);
36         if (ret)
37                 strcpy(ret, os);
38         return ret;
39 }
40
41 /*
42  * Convert an os_type to a string
43  */
44 int e2p_string2os(char *str)
45 {
46         const char      **cpp;
47         int             i = 0;
48
49         for (cpp = os_tab; *cpp; cpp++, i++) {
50                 if (!strcasecmp(str, *cpp))
51                         return i;
52         }
53         return -1;
54 }
55
56 #ifdef TEST_PROGRAM
57 int main(int argc, char **argv)
58 {
59         char    *s;
60         int     i, os;
61
62         for (i=0; i <= EXT2_OS_LITES; i++) {
63                 s = e2p_os2string(i);
64                 os = e2p_string2os(s);
65                 printf("%d: %s (%d)\n", i, s, os);
66                 if (i != os) {
67                         fprintf(stderr, "Failure!\n");
68                         exit(1);
69                 }
70         }
71         exit(0);
72 }
73 #endif
74
75