aboutsummaryrefslogtreecommitdiff
path: root/libbb/print_file.c
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
committerManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
commitcad5364599eb5062d59e0c397ed638ddd61a8d5d (patch)
treea318d0f03aa076c74b576ea45dc543a5669e8e91 /libbb/print_file.c
parente01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff)
downloadbusybox-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz
Major coreutils update.
Diffstat (limited to 'libbb/print_file.c')
-rw-r--r--libbb/print_file.c49
1 files changed, 30 insertions, 19 deletions
diff --git a/libbb/print_file.c b/libbb/print_file.c
index cdd60e7a0..8417c10d3 100644
--- a/libbb/print_file.c
+++ b/libbb/print_file.c
@@ -21,39 +21,50 @@
#include <stdio.h>
#include <stdlib.h>
-#include <sys/stat.h>
#include "libbb.h"
-
-extern void print_file(FILE *file)
+extern void bb_xprint_and_close_file(FILE *file)
{
- fflush(stdout);
- if (copyfd(fileno(file), fileno(stdout), 0) == -1) {
- exit(EXIT_FAILURE);
+ bb_xfflush_stdout();
+ /* Note: Do not use STDOUT_FILENO here, as this is a lib routine
+ * and the calling code may have reassigned stdout. */
+ if (bb_copyfd(fileno(file), fileno(stdout), 0) == -1) {
+ /* bb_copyfd outputs any needed messages, so just die. */
+ exit(bb_default_error_retval);
}
+ /* 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. */
fclose(file);
}
-extern int print_file_by_name(char *filename)
+/* Returns:
+ * 0 if successful
+ * -1 if 'filename' does not exist or is a directory
+ * exits with default error code if an error occurs
+ */
+
+extern int bb_xprint_file_by_name(const char *filename)
{
+ FILE *f;
+
+#if 0
+ /* This check shouldn't be necessary for linux, but is left
+ * here disabled just in case. */
struct stat statBuf;
- int status = TRUE;
- if(is_directory(filename, TRUE, &statBuf)==TRUE) {
- error_msg("%s: Is directory", filename);
- status = FALSE;
- } else {
- FILE *f = wfopen(filename, "r");
- if(f!=NULL)
- print_file(f);
- else
- status = FALSE;
+ 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;
}
- return status;
+ return -1;
}
-
/* END CODE */
/*
Local Variables: