3
0

kernel_version.c 866 B

1234567891011121314151617181920212223242526272829303132333435363738
  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 "libbb.h"
  10. /* After libbb.h, since it needs sys/types.h on some systems */
  11. #include <sys/utsname.h> /* for uname(2) */
  12. /* Returns current kernel version encoded as major*65536 + minor*256 + patch,
  13. * so, for example, to check if the kernel is greater than 2.2.11:
  14. *
  15. * if (get_linux_version_code() > KERNEL_VERSION(2,2,11)) { <stuff> }
  16. */
  17. int FAST_FUNC get_linux_version_code(void)
  18. {
  19. struct utsname name;
  20. char *s;
  21. int i, r;
  22. if (uname(&name) == -1) {
  23. bb_perror_msg("can't get system information");
  24. return 0;
  25. }
  26. s = name.release;
  27. r = 0;
  28. for (i = 0; i < 3; i++) {
  29. r = r * 256 + atoi(strtok(s, "."));
  30. s = NULL;
  31. }
  32. return r;
  33. }