3
0

kernel_version.c 793 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include <sys/utsname.h> /* for uname(2) */
  10. #include "libbb.h"
  11. /* Returns current kernel version encoded as major*65536 + minor*256 + patch,
  12. * so, for example, to check if the kernel is greater than 2.2.11:
  13. *
  14. * if (get_linux_version_code() > KERNEL_VERSION(2,2,11)) { <stuff> }
  15. */
  16. int get_linux_version_code(void)
  17. {
  18. struct utsname name;
  19. char *s;
  20. int i, r;
  21. if (uname(&name) == -1) {
  22. bb_perror_msg("cannot get system information");
  23. return 0;
  24. }
  25. s = name.release;
  26. r = 0;
  27. for (i = 0; i < 3; i++) {
  28. r = r * 256 + atoi(strtok(s, "."));
  29. s = NULL;
  30. }
  31. return r;
  32. }