loop.c 919 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#c
  2. *
  3. * Loop and print an integer whenever a period is reached:
  4. *
  5. * ....
  6. * ./loop.out [max=10 [period=1]]
  7. * ....
  8. *
  9. * * period: period for printing integers to stdout
  10. * 0 means disable printing.
  11. * * max: Stop counting when max is reached.
  12. * 0 means count to infinity.
  13. */
  14. #include <inttypes.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. int main(int argc, char **argv) {
  19. uintmax_t i, j, period, max;
  20. if (argc > 1) {
  21. max = strtoumax(argv[1], NULL, 10);
  22. } else {
  23. max = 10;
  24. }
  25. if (argc > 2) {
  26. period = strtoumax(argv[2], NULL, 10);
  27. } else {
  28. period = 1;
  29. }
  30. i = 0;
  31. j = 0;
  32. while (1) {
  33. if (period != 0 && i % period == 0) {
  34. printf("%ju\n", j);
  35. j++;
  36. }
  37. i++;
  38. if (i == max)
  39. break;
  40. }
  41. }