wfopen_input.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * wfopen_input implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* A number of applets need to open a file for reading, where the filename
  10. * is a command line arg. Since often that arg is '-' (meaning stdin),
  11. * we avoid testing everywhere by consolidating things in this routine.
  12. */
  13. #include "libbb.h"
  14. FILE* FAST_FUNC fopen_or_warn_stdin(const char *filename)
  15. {
  16. FILE *fp = stdin;
  17. if (filename != bb_msg_standard_input
  18. && NOT_LONE_DASH(filename)
  19. ) {
  20. fp = fopen_or_warn(filename, "r");
  21. }
  22. return fp;
  23. }
  24. FILE* FAST_FUNC xfopen_stdin(const char *filename)
  25. {
  26. FILE *fp = fopen_or_warn_stdin(filename);
  27. if (fp)
  28. return fp;
  29. xfunc_die(); /* We already output an error message. */
  30. }
  31. int FAST_FUNC open_or_warn_stdin(const char *filename)
  32. {
  33. int fd = STDIN_FILENO;
  34. if (filename != bb_msg_standard_input
  35. && NOT_LONE_DASH(filename)
  36. ) {
  37. fd = open_or_warn(filename, O_RDONLY);
  38. }
  39. return fd;
  40. }