kernel_version.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 1996 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. #include <ansidecl.h>
  16. #include <string.h>
  17. #include <sys/utsname.h>
  18. #include <sys/param.h>
  19. static int __linux_kernel_version = -1;
  20. static inline int
  21. asc2int (const char *s)
  22. {
  23. int result = 0;
  24. for (; *s >= '0' && *s <= '9'; s++)
  25. {
  26. result = result * 10 + (*s - '0');
  27. }
  28. return result;
  29. }
  30. static int
  31. set_linux_kernel_version (void)
  32. {
  33. struct utsname uts;
  34. char *version = NULL, *patchlevel = NULL, *sublevel = NULL;
  35. if (uname (&uts))
  36. {
  37. __linux_kernel_version = 0;
  38. return __linux_kernel_version;
  39. }
  40. version = uts.release;
  41. if (version != NULL)
  42. {
  43. patchlevel = strchr (version, '.');
  44. if (patchlevel != NULL)
  45. {
  46. *patchlevel = '\0';
  47. patchlevel++;
  48. sublevel = strchr (patchlevel, '.');
  49. if (sublevel != NULL)
  50. {
  51. *sublevel = '\0';
  52. sublevel++;
  53. }
  54. }
  55. __linux_kernel_version =
  56. GET_LINUX_KERNEL_VERSION (asc2int (version));
  57. if (patchlevel != NULL)
  58. {
  59. __linux_kernel_version |=
  60. GET_LINUX_KERNEL_PATCHLEVEL (asc2int (patchlevel));
  61. }
  62. if (sublevel != NULL)
  63. {
  64. __linux_kernel_version |=
  65. GET_LINUX_KERNEL_SUBLEVEL (asc2int (sublevel));
  66. }
  67. }
  68. else
  69. {
  70. __linux_kernel_version = 0;
  71. }
  72. return __linux_kernel_version;
  73. }
  74. int
  75. __get_linux_kernel_version (void)
  76. {
  77. if (__linux_kernel_version != -1)
  78. return __linux_kernel_version;
  79. return set_linux_kernel_version ();
  80. }