cat.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 source tree.
  8. */
  9. //config:config CAT
  10. //config: bool "cat"
  11. //config: default y
  12. //config: help
  13. //config: cat is used to concatenate files and print them to the standard
  14. //config: output. Enable this option if you wish to enable the 'cat' utility.
  15. //applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat))
  16. //kbuild:lib-$(CONFIG_CAT) += cat.o
  17. /* BB_AUDIT SUSv3 compliant */
  18. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
  19. //usage:#define cat_trivial_usage
  20. //usage: "[FILE]..."
  21. //usage:#define cat_full_usage "\n\n"
  22. //usage: "Concatenate FILEs and print them to stdout"
  23. //usage:
  24. //usage:#define cat_example_usage
  25. //usage: "$ cat /proc/uptime\n"
  26. //usage: "110716.72 17.67"
  27. #include "libbb.h"
  28. /* This is a NOFORK applet. Be very careful! */
  29. int bb_cat(char **argv)
  30. {
  31. int fd;
  32. int retval = EXIT_SUCCESS;
  33. if (!*argv)
  34. argv = (char**) &bb_argv_dash;
  35. do {
  36. fd = open_or_warn_stdin(*argv);
  37. if (fd >= 0) {
  38. /* This is not a xfunc - never exits */
  39. off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
  40. if (fd != STDIN_FILENO)
  41. close(fd);
  42. if (r >= 0)
  43. continue;
  44. }
  45. retval = EXIT_FAILURE;
  46. } while (*++argv);
  47. return retval;
  48. }
  49. int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  50. int cat_main(int argc UNUSED_PARAM, char **argv)
  51. {
  52. getopt32(argv, "u");
  53. argv += optind;
  54. return bb_cat(argv);
  55. }