cat.c 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * cat implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file License in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
  11. #include "libbb.h"
  12. /* This is a NOFORK applet. Be very careful! */
  13. int bb_cat(char **argv)
  14. {
  15. int fd;
  16. int retval = EXIT_SUCCESS;
  17. if (!*argv)
  18. argv = (char**) &bb_argv_dash;
  19. do {
  20. fd = open_or_warn_stdin(*argv);
  21. if (fd >= 0) {
  22. /* This is not a xfunc - never exits */
  23. off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
  24. if (fd != STDIN_FILENO)
  25. close(fd);
  26. if (r >= 0)
  27. continue;
  28. }
  29. retval = EXIT_FAILURE;
  30. } while (*++argv);
  31. return retval;
  32. }
  33. int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int cat_main(int argc ATTRIBUTE_UNUSED, char **argv)
  35. {
  36. getopt32(argv, "u");
  37. argv += optind;
  38. return bb_cat(argv);
  39. }