wfopen.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 tarball for details.
  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_error_msg_and_die(bb_msg_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. }