logo

utils-std

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

cat.c (2719B)


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