Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / wfopen.c
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
11 FILE* FAST_FUNC fopen_or_warn(const char *path, const char *mode)
12 {
13         FILE *fp = fopen(path, mode);
14         if (!fp) {
15                 bb_simple_perror_msg(path);
16                 //errno = 0; /* why? */
17         }
18         return fp;
19 }
20
21 FILE* FAST_FUNC fopen_for_read(const char *path)
22 {
23         return fopen(path, "r");
24 }
25
26 FILE* FAST_FUNC xfopen_for_read(const char *path)
27 {
28         return xfopen(path, "r");
29 }
30
31 FILE* FAST_FUNC fopen_for_write(const char *path)
32 {
33         return fopen(path, "w");
34 }
35
36 FILE* FAST_FUNC xfopen_for_write(const char *path)
37 {
38         return xfopen(path, "w");
39 }
40
41 static FILE* xfdopen_helper(unsigned fd_and_rw_bit)
42 {
43         FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
44         if (!fp)
45                 bb_die_memory_exhausted();
46         return fp;
47 }
48 FILE* FAST_FUNC xfdopen_for_read(int fd)
49 {
50         return xfdopen_helper(fd << 1);
51 }
52 FILE* FAST_FUNC xfdopen_for_write(int fd)
53 {
54         return xfdopen_helper((fd << 1) + 1);
55 }