blob: ea5d1d2225d9e1e4756659905aae9ffaf93d7982 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
10#include <stdio.h>
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000011#include <stdlib.h>
Eric Andersen8ee2b272004-03-27 11:26:32 +000012#include <unistd.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000013#include "libbb.h"
14
Rob Landleydfba7412006-03-06 20:47:33 +000015void bb_xprint_and_close_file(FILE *file)
Eric Andersenaad1a882001-03-16 22:47:14 +000016{
Manuel Novoa III cad53642003-03-19 09:13:01 +000017 bb_xfflush_stdout();
18 /* Note: Do not use STDOUT_FILENO here, as this is a lib routine
19 * and the calling code may have reassigned stdout. */
Eric Andersen70060d22004-03-27 10:02:48 +000020 if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 /* bb_copyfd outputs any needed messages, so just die. */
22 exit(bb_default_error_retval);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000023 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 /* Note: Since we're reading, don't bother checking the return value
25 * of fclose(). The only possible failure is EINTR which
26 * should already have been taken care of. */
Eric Andersen55f98722001-05-15 17:48:09 +000027 fclose(file);
Eric Andersenaad1a882001-03-16 22:47:14 +000028}
29
Manuel Novoa III cad53642003-03-19 09:13:01 +000030/* Returns:
31 * 0 if successful
32 * -1 if 'filename' does not exist or is a directory
33 * exits with default error code if an error occurs
34 */
Eric Andersene5dfced2001-04-09 22:48:12 +000035
Rob Landleydfba7412006-03-06 20:47:33 +000036int bb_xprint_file_by_name(const char *filename)
Manuel Novoa III cad53642003-03-19 09:13:01 +000037{
38 FILE *f;
39
40#if 0
41 /* This check shouldn't be necessary for linux, but is left
42 * here disabled just in case. */
43 struct stat statBuf;
44
45 if(is_directory(filename, TRUE, &statBuf)) {
46 bb_error_msg("%s: Is directory", filename);
47 } else
48#endif
49 if ((f = bb_wfopen(filename, "r")) != NULL) {
50 bb_xprint_and_close_file(f);
51 return 0;
Eric Andersene5dfced2001-04-09 22:48:12 +000052 }
53
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +000055}