cat.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. //kbuild:lib-$(CONFIG_CAT) += cat.o
  12. //kbuild:lib-$(CONFIG_MORE) += cat.o # more uses it if stdout isn't a tty
  13. //kbuild:lib-$(CONFIG_LESS) += cat.o # less too
  14. //kbuild:lib-$(CONFIG_CRONTAB) += cat.o # crontab -l
  15. //config:config CAT
  16. //config: bool "cat"
  17. //config: default y
  18. //config: help
  19. //config: cat is used to concatenate files and print them to the standard
  20. //config: output. Enable this option if you wish to enable the 'cat' utility.
  21. #include "libbb.h"
  22. /* This is a NOFORK applet. Be very careful! */
  23. int bb_cat(char **argv)
  24. {
  25. int fd;
  26. int retval = EXIT_SUCCESS;
  27. if (!*argv)
  28. argv = (char**) &bb_argv_dash;
  29. do {
  30. fd = open_or_warn_stdin(*argv);
  31. if (fd >= 0) {
  32. /* This is not a xfunc - never exits */
  33. off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
  34. if (fd != STDIN_FILENO)
  35. close(fd);
  36. if (r >= 0)
  37. continue;
  38. }
  39. retval = EXIT_FAILURE;
  40. } while (*++argv);
  41. return retval;
  42. }
  43. int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  44. int cat_main(int argc UNUSED_PARAM, char **argv)
  45. {
  46. getopt32(argv, "u");
  47. argv += optind;
  48. return bb_cat(argv);
  49. }