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