Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
Eric Andersen | 8ee2b27 | 2004-03-27 11:26:32 +0000 | [diff] [blame] | 12 | #include <unistd.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
| 14 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 15 | void bb_xprint_and_close_file(FILE *file) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 16 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | 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 Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 20 | if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | /* bb_copyfd outputs any needed messages, so just die. */ |
| 22 | exit(bb_default_error_retval); |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 23 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | /* 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 Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 27 | fclose(file); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | /* 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 Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 35 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 36 | int bb_xprint_file_by_name(const char *filename) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | { |
| 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 Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 55 | } |