2001-03-17 04:17:14 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
FILE* FAST_FUNC fopen_or_warn(const char *path, const char *mode)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
2006-10-27 04:55:17 +05:30
|
|
|
FILE *fp = fopen(path, mode);
|
|
|
|
if (!fp) {
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg(path);
|
2009-04-06 17:34:42 +05:30
|
|
|
//errno = 0; /* why? */
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
2008-07-22 04:35:26 +05:30
|
|
|
|
|
|
|
FILE* FAST_FUNC fopen_for_read(const char *path)
|
|
|
|
{
|
|
|
|
return fopen(path, "r");
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* FAST_FUNC xfopen_for_read(const char *path)
|
|
|
|
{
|
|
|
|
return xfopen(path, "r");
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* FAST_FUNC fopen_for_write(const char *path)
|
|
|
|
{
|
|
|
|
return fopen(path, "w");
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* FAST_FUNC xfopen_for_write(const char *path)
|
|
|
|
{
|
|
|
|
return xfopen(path, "w");
|
|
|
|
}
|
2009-11-16 03:58:11 +05:30
|
|
|
|
|
|
|
static FILE* xfdopen_helper(unsigned fd_and_rw_bit)
|
|
|
|
{
|
|
|
|
FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
|
|
|
|
if (!fp)
|
|
|
|
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
FILE* FAST_FUNC xfdopen_for_read(int fd)
|
|
|
|
{
|
|
|
|
return xfdopen_helper(fd << 1);
|
|
|
|
}
|
|
|
|
FILE* FAST_FUNC xfdopen_for_write(int fd)
|
|
|
|
{
|
|
|
|
return xfdopen_helper((fd << 1) + 1);
|
|
|
|
}
|