1 /* $NetBSD: ffilecopy.c,v 1.10 2009/10/17 22:26:13 christos Exp $ */
4 * Copyright (c) 1991 Carnegie Mellon University
7 * Permission to use, copy, modify and distribute this software and its
8 * documentation is hereby granted, provided that both the copyright
9 * notice and this permission notice appear in all copies of the
10 * software, derivative works or modified versions, and any portions
11 * thereof, and that both notices appear in supporting documentation.
13 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
15 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 * Carnegie Mellon requests users of this software to return to
19 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
20 * School of Computer Science
21 * Carnegie Mellon University
22 * Pittsburgh PA 15213-3890
24 * any improvements or extensions that they make and grant Carnegie the rights
25 * to redistribute these changes.
30 #include "supextern.h"
33 * Define two macros for fast file copying
35 * FBUF_HACK: this will copy data directly between the
36 * underlying OSs FILE buffers
38 * FFLAG_HACK: this will set necessary OS FILE flags
39 * after we've hacked the buffers up
44 # define FBUF_HACK(here,there) \
46 if ((here)->_r > 0) { \
47 i = write(therefile, (here)->_p, (size_t)(here)->_r); \
48 if (i != (here)->_r) \
50 (here)->_p = (here)->_bf._base; \
53 } while (/*CONSTCOND*/0)
54 # define FFLAG_HACK(file) (file)->_flags |= __SEOF
58 # define FBUF_HACK(here, there) \
60 if ((here)->_cnt > 0) { \
61 i = write(therefile, (here)->_ptr, (here)->_cnt ); \
62 if (i != (here)->_cnt) \
64 (here)->_ptr = (here)->_base; \
67 } while (/*CONSTCOND*/0)
68 # define FFLAG_HACK(file) (file)->_flag |= _IOEOF;
72 # define FBUF_HACK(here, there) /* Nada */
73 # define FFLAG_HACK(here) (void)fseeko(here, (off_t)0, SEEK_END);
77 /* ffilecopy -- very fast buffered file copy
79 * Usage: i = ffilecopy (here,there)
83 * Ffilecopy is a very fast routine to copy the rest of a buffered
84 * input file to a buffered output file. Here and there are open
85 * buffers for reading and writing (respectively); ffilecopy
86 * performs a file-copy faster than you should expect to do it
87 * yourself. Ffilecopy returns 0 if everything was OK; EOF if
88 * there was any error. Normally, the input file will be left in
89 * EOF state (feof(here) will return TRUE), and the output file will be
90 * flushed (i.e. all data on the file rather in the core buffer).
91 * It is not necessary to flush the output file before ffilecopy.
94 * 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
100 ffilecopy(FILE * here
, FILE * there
)
102 int i
, herefile
, therefile
;
104 herefile
= fileno(here
);
105 therefile
= fileno(there
);
107 if (fflush(there
) == EOF
) /* flush pending output */
110 /* if we know any buffer tricks, do them now */
111 FBUF_HACK(here
,there
);
113 /* copy the rest of the file directly (avoiding FILE buffers) */
114 i
= filecopy(herefile
, therefile
); /* fast file copy */
118 /* ensure we are at the end of the file */