Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / lib / libast / port / fdcopy.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: fdcopy.c /main/3 1995/11/01 18:19:56 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
29 *                    AT&T BELL LABORATORIES                    *
30 *         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
31 *            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
32 *                                                              *
33 *          Copyright (c) 1994 AT&T Bell Laboratories           *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Software Engineering Research Department           *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                   advsoft@research.att.com                   *
46 *                 Randy Hackbarth 908-582-5245                 *
47 *                  Dave Belanger 908-582-7427                  *
48 *                                                              *
49 ***************************************************************/
50
51 /* : : generated by proto : : */
52
53 #line 1
54
55 #if !defined(__PROTO__)
56 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
57 #if defined(__cplusplus)
58 #define __MANGLE__      "C"
59 #else
60 #define __MANGLE__
61 #endif
62 #define __STDARG__
63 #define __PROTO__(x)    x
64 #define __OTORP__(x)
65 #define __PARAM__(n,o)  n
66 #if !defined(__STDC__) && !defined(__cplusplus)
67 #if !defined(c_plusplus)
68 #define const
69 #endif
70 #define signed
71 #define void            int
72 #define volatile
73 #define __V_            char
74 #else
75 #define __V_            void
76 #endif
77 #else
78 #define __PROTO__(x)    ()
79 #define __OTORP__(x)    x
80 #define __PARAM__(n,o)  o
81 #define __MANGLE__
82 #define __V_            char
83 #define const
84 #define signed
85 #define void            int
86 #define volatile
87 #endif
88 #if defined(__cplusplus) || defined(c_plusplus)
89 #define __VARARG__      ...
90 #else
91 #define __VARARG__
92 #endif
93 #if defined(__STDARG__)
94 #define __VA_START__(p,a)       va_start(p,a)
95 #else
96 #define __VA_START__(p,a)       va_start(p)
97 #endif
98 #endif
99
100 #line 9
101 #include <ast.h>
102
103 #if __sun__ || sun
104
105 #if _lib_mmap && (_hdr_mman || _sys_mman)
106
107 #if defined(__STDPP__directive) && defined(__STDPP__hide)
108 __STDPP__directive pragma pp:hide mmap munmap
109 #else
110 #define mmap            ______mmap
111 #define munmap          ______munmap
112 #endif
113
114 #include <ls.h>
115 #if _hdr_mman
116 #include <mman.h>
117 #else
118 #include <sys/mman.h>
119 #endif
120
121 #define MAPSIZE         (1024*256)
122
123 #if defined(__STDPP__directive) && defined(__STDPP__hide)
124 __STDPP__directive pragma pp:nohide mmap munmap
125 #else
126 #undef  mmap
127 #undef  munmap
128 #endif
129
130 extern __MANGLE__ int           munmap __PROTO__((caddr_t, size_t));
131 extern __MANGLE__ caddr_t               mmap __PROTO__((caddr_t, size_t, int, int, int, off_t));
132
133 #endif
134
135 #endif
136
137 #define BUFSIZ          4096
138
139 /*
140  * copy n bytes from rfd to wfd
141  * actual byte count returned
142  * if n<=0 then ``good'' size is used
143  */
144
145 off_t
146 fdcopy __PARAM__((int rfd, int wfd, off_t n), (rfd, wfd, n)) __OTORP__(int rfd; int wfd; off_t n;)
147 #line 55
148 {
149         register off_t  c;
150 #ifdef MAPSIZE
151         off_t           pos;
152         off_t           mapsize;
153         char*           mapbuf;
154         struct stat     st;
155 #endif
156
157         static int      bufsiz;
158         static char*    buf;
159
160         if (n <= 0 || n >= BUFSIZ * 2)
161         {
162 #if MAPSIZE
163                 if (!fstat(rfd, &st) && S_ISREG(st.st_mode) && (pos = lseek(rfd, (off_t)0, 1)) != ((off_t)-1))
164                 {
165                         if (pos >= st.st_size) return(0);
166                         mapsize = st.st_size - pos;
167                         if (mapsize > MAPSIZE) mapsize = (mapsize > n && n > 0) ? n : MAPSIZE;
168                         if (mapsize >= BUFSIZ * 2 && (mapbuf = (char*)mmap(NiL, mapsize, PROT_READ, MAP_SHARED, rfd, pos)) != ((caddr_t)-1))
169                         {
170                                 if (write(wfd, mapbuf, mapsize) != mapsize || lseek(rfd, mapsize, 1) == ((off_t)-1)) return(-1);
171                                 munmap((caddr_t)mapbuf, mapsize);
172                                 return(mapsize);
173                         }
174                 }
175 #endif
176                 if (n <= 0) n = BUFSIZ;
177         }
178         if (n > bufsiz)
179         {
180                 if (buf) free(buf);
181                 bufsiz = roundof(n, BUFSIZ);
182                 if (!(buf = newof(0, char, bufsiz, 0))) return(-1);
183         }
184         if ((c = read(rfd, buf, (size_t)n)) > 0 && write(wfd, buf, (size_t)c) != c) c = -1;
185         return(c);
186 }