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