wfopen_input.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 source tree.
  8. */
  9. #include "libbb.h"
  10. /* A number of applets need to open a file for reading, where the filename
  11. * is a command line arg. Since often that arg is '-' (meaning stdin),
  12. * we avoid testing everywhere by consolidating things in this routine.
  13. */
  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. }
  41. int FAST_FUNC xopen_stdin(const char *filename)
  42. {
  43. int fd = open_or_warn_stdin(filename);
  44. if (fd >= 0)
  45. return fd;
  46. xfunc_die(); /* We already output an error message. */
  47. }