ostype.c 1.1 KB

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