Patch from vodz:
[oweals/busybox.git] / libbb / print_file.c
index 52a39774f68b5fe567d0d66989752789f1a38894..6d3667b60a282333d87b4ac3779302b424cc045c 100644 (file)
@@ -2,9 +2,7 @@
 /*
  * Utility routines.
  *
- * Copyright (C) tons of folks.  Tracking down who wrote what
- * isn't something I'm going to worry about...  If you wrote something
- * here, please feel free to acknowledge your work.
+ * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 
- * Permission has been granted to redistribute this code under the GPL.
- *
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "libbb.h"
 
-
-extern void print_file(FILE *file)
+extern void bb_xprint_and_close_file(FILE *file)
 {
-       int c;
-
-       while ((c = getc(file)) != EOF)
-               putc(c, stdout);
+       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);
-       fflush(stdout);
 }
 
-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 *file;
-       if ((file = wfopen(filename, "r")) == NULL)
-               return FALSE;
-       print_file(file);
-       return TRUE;
-}
+       FILE *f;
+
+#if 0
+       /* This check shouldn't be necessary for linux, but is left
+       * here disabled just in case. */
+       struct stat statBuf;
 
+       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 -1;
+}
 
 /* END CODE */
 /*