ostype.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "e2p.h"
  10. #include <string.h>
  11. const char *os_tab[] =
  12. { "Linux",
  13. "Hurd",
  14. "Masix",
  15. "FreeBSD",
  16. "Lites",
  17. 0 };
  18. /*
  19. * Convert an os_type to a string
  20. */
  21. char *e2p_os2string(int os_type)
  22. {
  23. const char *os;
  24. char *ret;
  25. if (os_type <= EXT2_OS_LITES)
  26. os = os_tab[os_type];
  27. else
  28. os = "(unknown os)";
  29. ret = xmalloc(strlen(os)+1);
  30. strcpy(ret, os);
  31. return ret;
  32. }
  33. /*
  34. * Convert an os_type to a string
  35. */
  36. int e2p_string2os(char *str)
  37. {
  38. const char **cpp;
  39. int i = 0;
  40. for (cpp = os_tab; *cpp; cpp++, i++) {
  41. if (!strcasecmp(str, *cpp))
  42. return i;
  43. }
  44. return -1;
  45. }
  46. #ifdef TEST_PROGRAM
  47. int main(int argc, char **argv)
  48. {
  49. char *s;
  50. int i, os;
  51. for (i=0; i <= EXT2_OS_LITES; i++) {
  52. s = e2p_os2string(i);
  53. os = e2p_string2os(s);
  54. printf("%d: %s (%d)\n", i, s, os);
  55. if (i != os) {
  56. fprintf(stderr, "Failure!\n");
  57. exit(1);
  58. }
  59. }
  60. exit(0);
  61. }
  62. #endif