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