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
|
|
|
*
|
2006-05-20 00:59:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2002-12-13 13:50:44 +05:30
|
|
|
#include <stdlib.h>
|
2004-03-27 16:56:32 +05:30
|
|
|
#include <unistd.h>
|
2001-03-17 04:17:14 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
void bb_xprint_and_close_file(FILE *file)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_xfflush_stdout();
|
|
|
|
/* Note: Do not use STDOUT_FILENO here, as this is a lib routine
|
|
|
|
* and the calling code may have reassigned stdout. */
|
2004-03-27 15:32:48 +05:30
|
|
|
if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1) {
|
2003-03-19 14:43:01 +05:30
|
|
|
/* bb_copyfd outputs any needed messages, so just die. */
|
|
|
|
exit(bb_default_error_retval);
|
2002-12-13 13:50:44 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
/* Note: Since we're reading, don't bother checking the return value
|
|
|
|
* of fclose(). The only possible failure is EINTR which
|
|
|
|
* should already have been taken care of. */
|
2001-05-15 23:18:09 +05:30
|
|
|
fclose(file);
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
/* Returns:
|
|
|
|
* 0 if successful
|
|
|
|
* -1 if 'filename' does not exist or is a directory
|
|
|
|
* exits with default error code if an error occurs
|
|
|
|
*/
|
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
int bb_xprint_file_by_name(const char *filename)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
2003-03-19 14:43:01 +05:30
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* This check shouldn't be necessary for linux, but is left
|
|
|
|
* here disabled just in case. */
|
2001-04-10 04:18:12 +05:30
|
|
|
struct stat statBuf;
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
if(is_directory(filename, TRUE, &statBuf)) {
|
|
|
|
bb_error_msg("%s: Is directory", filename);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if ((f = bb_wfopen(filename, "r")) != NULL) {
|
|
|
|
bb_xprint_and_close_file(f);
|
|
|
|
return 0;
|
2001-04-10 04:18:12 +05:30
|
|
|
}
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
return -1;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|