logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

cat.c (2696B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../libutils/fs.h"
  6. #include "../libutils/getopt_nolong.h"
  7. #include <errno.h>
  8. #include <fcntl.h> // open, O_RDONLY
  9. #include <limits.h> // SSIZE_MAX
  10. #include <stdio.h> // fprintf, BUFSIZ
  11. #include <stdlib.h> // abort
  12. #include <string.h> // strerror, strncmp
  13. #include <unistd.h> // read, write, close, getopt
  14. const char *argv0 = "cat";
  15. static void
  16. usage(void)
  17. {
  18. fprintf(stderr, "Usage: cat [-u] [files ...]\n");
  19. }
  20. int
  21. main(int argc, char *argv[])
  22. {
  23. for(int c = -1; (c = getopt_nolong(argc, argv, ":u")) != -1;)
  24. {
  25. switch(c)
  26. {
  27. case 'u':
  28. // POSIX: Ignored, buffered streams aren't used
  29. break;
  30. case ':':
  31. fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt);
  32. usage();
  33. return 1;
  34. case '?':
  35. GETOPT_UNKNOWN_OPT
  36. usage();
  37. return 1;
  38. default:
  39. abort();
  40. }
  41. }
  42. argc -= optind;
  43. argv += optind;
  44. if(argc < 1)
  45. {
  46. if(auto_fd_copy(STDIN_FILENO, STDOUT_FILENO, SSIZE_MAX) < 0)
  47. {
  48. fprintf(stderr, "%s: error: Failed copying data from <stdin>: %s\n", argv0, strerror(errno));
  49. return 1;
  50. }
  51. }
  52. else
  53. {
  54. for(int argi = 0; argi < argc; argi++)
  55. {
  56. if(strncmp(argv[argi], "-", 2) == 0)
  57. {
  58. if(auto_fd_copy(STDIN_FILENO, STDOUT_FILENO, SSIZE_MAX) < 0)
  59. {
  60. fprintf(
  61. stderr, "%s: error: Failed copying data from <stdin>: %s\n", argv0, strerror(errno));
  62. return 1;
  63. }
  64. }
  65. else
  66. {
  67. int fd = open(argv[argi], O_RDONLY);
  68. if(fd < 0)
  69. {
  70. fprintf(stderr,
  71. "%s: error: Failed opening file '%s': %s\n",
  72. argv0,
  73. argv[argi],
  74. strerror(errno));
  75. errno = 0;
  76. return 1;
  77. }
  78. posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
  79. errno = 0;
  80. if(auto_fd_copy(fd, STDOUT_FILENO, SSIZE_MAX) < 0)
  81. {
  82. fprintf(stderr,
  83. "%s: error: Failed copying data from file '%s': %s\n",
  84. argv0,
  85. argv[argi],
  86. strerror(errno));
  87. return 1;
  88. }
  89. if(close(fd) < 0)
  90. {
  91. fprintf(stderr,
  92. "%s: error: Failed closing file '%s': %s\n",
  93. argv0,
  94. argv[argi],
  95. strerror(errno));
  96. errno = 0;
  97. return 1;
  98. }
  99. }
  100. }
  101. }
  102. if(close(STDIN_FILENO) != 0)
  103. {
  104. fprintf(stderr, "%s: error: Failed closing file <stdin>: %s\n", argv0, strerror(errno));
  105. return 1;
  106. }
  107. if(close(STDOUT_FILENO) != 0)
  108. {
  109. fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno));
  110. return 1;
  111. }
  112. return 0;
  113. }