logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git

range.c (869B)


  1. // Collection of Unix tools, comparable to coreutils
  2. // Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
  4. #define _POSIX_C_SOURCE 200809L
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. char *IFS;
  8. int a, b;
  9. void
  10. usage(char *a0)
  11. {
  12. printf("usage: %s [ini <max>]\n", a0);
  13. }
  14. void
  15. range(int initial, int maximum)
  16. {
  17. printf("%i", initial);
  18. if(initial < maximum)
  19. {
  20. for(initial++; initial <= maximum; initial++)
  21. printf("%s%i", IFS, initial);
  22. }
  23. else if(initial > maximum)
  24. {
  25. for(initial--; initial >= maximum; initial--)
  26. printf("%s%i", IFS, initial);
  27. }
  28. }
  29. int
  30. main(int argc, char *argv[])
  31. {
  32. if(!(IFS = getenv("IFS")))
  33. {
  34. IFS = " ";
  35. }
  36. if(argc == 2)
  37. {
  38. range(0, atoi(argv[1]));
  39. }
  40. else if(argc == 3)
  41. {
  42. range(atoi(argv[1]), atoi(argv[2]));
  43. }
  44. else
  45. {
  46. usage(argv[0]);
  47. }
  48. }