wfopen.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. FILE* FAST_FUNC fopen_or_warn(const char *path, const char *mode)
  11. {
  12. FILE *fp = fopen(path, mode);
  13. if (!fp) {
  14. bb_simple_perror_msg(path);
  15. //errno = 0; /* why? */
  16. }
  17. return fp;
  18. }
  19. FILE* FAST_FUNC fopen_for_read(const char *path)
  20. {
  21. return fopen(path, "r");
  22. }
  23. FILE* FAST_FUNC xfopen_for_read(const char *path)
  24. {
  25. return xfopen(path, "r");
  26. }
  27. FILE* FAST_FUNC fopen_for_write(const char *path)
  28. {
  29. return fopen(path, "w");
  30. }
  31. FILE* FAST_FUNC xfopen_for_write(const char *path)
  32. {
  33. return xfopen(path, "w");
  34. }
  35. static FILE* xfdopen_helper(unsigned fd_and_rw_bit)
  36. {
  37. FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
  38. if (!fp)
  39. bb_die_memory_exhausted();
  40. return fp;
  41. }
  42. FILE* FAST_FUNC xfdopen_for_read(int fd)
  43. {
  44. return xfdopen_helper(fd << 1);
  45. }
  46. FILE* FAST_FUNC xfdopen_for_write(int fd)
  47. {
  48. return xfdopen_helper((fd << 1) + 1);
  49. }