1 /* 2 * getostype.c - Get the Filesystem OS type 3 * 4 * Copyright (C) 2004,2005 Theodore Ts'o <tytso@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 "config.h" 13 #include "e2p.h" 14 #include <string.h> 15 #include <stdlib.h> 16 17 static const char *os_tab[] = 18 { "Linux", 19 "Hurd", 20 "Masix", 21 "FreeBSD", 22 "Lites", 23 0 }; 24 25 /* 26 * Convert an os_type to a string 27 */ 28 char *e2p_os2string(int os_type) 29 { 30 const char *os; 31 char *ret; 32 33 if (os_type >= 0 && os_type <= EXT2_OS_LITES) 34 os = os_tab[os_type]; 35 else 36 os = "(unknown os)"; 37 38 ret = malloc(strlen(os)+1); 39 if (ret) 40 strcpy(ret, os); 41 return ret; 42 } 43 44 /* 45 * Convert an os_type to a string 46 */ 47 int e2p_string2os(char *str) 48 { 49 const char **cpp; 50 int i = 0; 51 52 for (cpp = os_tab; *cpp; cpp++, i++) { 53 if (!strcasecmp(str, *cpp)) 54 return i; 55 } 56 return -1; 57 } 58 59 #ifdef TEST_PROGRAM 60 int main(int argc, char **argv) 61 { 62 char *s; 63 int i, os; 64 65 for (i=0; i <= EXT2_OS_LITES; i++) { 66 s = e2p_os2string(i); 67 os = e2p_string2os(s); 68 printf("%d: %s (%d)\n", i, s, os); 69 free(s); 70 if (i != os) { 71 fprintf(stderr, "Failure!\n"); 72 exit(1); 73 } 74 } 75 exit(0); 76 } 77 #endif 78 79