cat.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /* 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. //usage:#define cat_trivial_usage
  22. //usage: "[FILE]..."
  23. //usage:#define cat_full_usage "\n\n"
  24. //usage: "Concatenate FILEs and print them to stdout"
  25. //usage:
  26. //usage:#define cat_example_usage
  27. //usage: "$ cat /proc/uptime\n"
  28. //usage: "110716.72 17.67"
  29. #include "libbb.h"
  30. /* This is a NOFORK applet. Be very careful! */
  31. int bb_cat(char **argv)
  32. {
  33. int fd;
  34. int retval = EXIT_SUCCESS;
  35. if (!*argv)
  36. argv = (char**) &bb_argv_dash;
  37. do {
  38. fd = open_or_warn_stdin(*argv);
  39. if (fd >= 0) {
  40. /* This is not a xfunc - never exits */
  41. off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
  42. if (fd != STDIN_FILENO)
  43. close(fd);
  44. if (r >= 0)
  45. continue;
  46. }
  47. retval = EXIT_FAILURE;
  48. } while (*++argv);
  49. return retval;
  50. }
  51. int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  52. int cat_main(int argc UNUSED_PARAM, char **argv)
  53. {
  54. getopt32(argv, "u");
  55. argv += optind;
  56. return bb_cat(argv);
  57. }