logo

utils-std

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

cat.c (2635B)


  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 <errno.h>
  7. #include <fcntl.h> // open, O_RDONLY
  8. #include <limits.h> // SSIZE_MAX
  9. #include <stdio.h> // fprintf, BUFSIZ
  10. #include <stdlib.h> // abort
  11. #include <string.h> // strerror, strncmp
  12. #include <unistd.h> // read, write, close, getopt
  13. const char *argv0 = "cat";
  14. static void
  15. usage(void)
  16. {
  17. fprintf(stderr, "Usage: cat [-u] [files ...]\n");
  18. }
  19. int
  20. main(int argc, char *argv[])
  21. {
  22. int c = -1;
  23. while((c = getopt(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. fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
  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. if(auto_fd_copy(fd, STDOUT_FILENO, SSIZE_MAX) < 0)
  79. {
  80. fprintf(stderr,
  81. "%s: error: Failed copying data from file '%s': %s\n",
  82. argv0,
  83. argv[argi],
  84. strerror(errno));
  85. return 1;
  86. }
  87. if(close(fd) < 0)
  88. {
  89. fprintf(stderr,
  90. "%s: error: Failed closing file '%s': %s\n",
  91. argv0,
  92. argv[argi],
  93. strerror(errno));
  94. errno = 0;
  95. return 1;
  96. }
  97. }
  98. }
  99. }
  100. if(close(STDIN_FILENO) != 0)
  101. {
  102. fprintf(stderr, "%s: error: Failed closing file <stdin>: %s\n", argv0, strerror(errno));
  103. return 1;
  104. }
  105. if(close(STDOUT_FILENO) != 0)
  106. {
  107. fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno));
  108. return 1;
  109. }
  110. return 0;
  111. }