logo

utils-std

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

cat.c (2703B)


  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 "../lib/fs.h"
  6. #include <assert.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. int c = -1;
  24. while((c = getopt(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. fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
  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. assert(errno == 0);
  69. int fd = open(argv[argi], O_RDONLY);
  70. if(fd < 0)
  71. {
  72. fprintf(stderr,
  73. "%s: error: Failed opening file '%s': %s\n",
  74. argv0,
  75. argv[argi],
  76. strerror(errno));
  77. errno = 0;
  78. return 1;
  79. }
  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. assert(errno == 0);
  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. }