Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / sup / source / ffilecopy.c
blob247cd4bac4fd20e732be80d5ea9aa59cf3261d6a
1 /* $NetBSD: ffilecopy.c,v 1.10 2009/10/17 22:26:13 christos Exp $ */
3 /*
4 * Copyright (c) 1991 Carnegie Mellon University
5 * All Rights Reserved.
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.
28 #include <stdio.h>
29 #include "supcdefs.h"
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
42 #if defined(__SEOF)
44 # define FBUF_HACK(here,there) \
45 do { \
46 if ((here)->_r > 0) { \
47 i = write(therefile, (here)->_p, (size_t)(here)->_r); \
48 if (i != (here)->_r) \
49 return EOF; \
50 (here)->_p = (here)->_bf._base; \
51 (here)->_r = 0; \
52 } \
53 } while (/*CONSTCOND*/0)
54 # define FFLAG_HACK(file) (file)->_flags |= __SEOF
56 #elif defined(_IOEOF)
58 # define FBUF_HACK(here, there) \
59 do { \
60 if ((here)->_cnt > 0) { \
61 i = write(therefile, (here)->_ptr, (here)->_cnt ); \
62 if (i != (here)->_cnt) \
63 return EOF; \
64 (here)->_ptr = (here)->_base; \
65 (here)->_cnt = 0; \
66 } \
67 } while (/*CONSTCOND*/0)
68 # define FFLAG_HACK(file) (file)->_flag |= _IOEOF;
70 #else
72 # define FBUF_HACK(here, there) /* Nada */
73 # define FFLAG_HACK(here) (void)fseeko(here, (off_t)0, SEEK_END);
75 #endif
77 /* ffilecopy -- very fast buffered file copy
79 * Usage: i = ffilecopy (here,there)
80 * int i;
81 * FILE *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.
93 * HISTORY
94 * 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
95 * Created for VAX.
99 ssize_t
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 */
108 return EOF;
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 */
115 if (i < 0)
116 return EOF;
118 /* ensure we are at the end of the file */
119 FFLAG_HACK(here);
120 return 0;