kernel_version.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 1999,2000 by Lineo, inc.
  4. * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Library General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <sys/utsname.h>
  24. static int __linux_kernel_version = -1;
  25. /* Returns kernel version encoded as major*65536 + minor*256 + patch,
  26. * so, for example, to check if the kernel is greater than 2.2.11:
  27. * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
  28. */
  29. static int find_kernel_revision(void)
  30. {
  31. struct utsname name;
  32. int major = 0, minor = 0, patch = 0;
  33. if (uname(&name) == -1) {
  34. return (0);
  35. }
  36. sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
  37. return major * 65536 + minor * 256 + patch;
  38. }
  39. int
  40. __get_linux_kernel_version (void)
  41. {
  42. if (__linux_kernel_version != -1)
  43. return __linux_kernel_version;
  44. return find_kernel_revision ();
  45. }