xref: /onnv-gate/usr/src/lib/libast/common/port/astcopy.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * Glenn Fowler
25*4887Schin  * AT&T Bell Laboratories
26*4887Schin  *
27*4887Schin  * copy from rfd to wfd (with conditional mmap hacks)
28*4887Schin  */
29*4887Schin 
30*4887Schin #include <ast.h>
31*4887Schin #include <ast_mmap.h>
32*4887Schin 
33*4887Schin #if _mmap_worthy > 1
34*4887Schin 
35*4887Schin #include <ls.h>
36*4887Schin 
37*4887Schin #define MAPSIZE		(1024*256)
38*4887Schin 
39*4887Schin #endif
40*4887Schin 
41*4887Schin #undef	BUFSIZ
42*4887Schin #define BUFSIZ		4096
43*4887Schin 
44*4887Schin /*
45*4887Schin  * copy n bytes from rfd to wfd
46*4887Schin  * actual byte count returned
47*4887Schin  * if n<=0 then ``good'' size is used
48*4887Schin  */
49*4887Schin 
50*4887Schin off_t
51*4887Schin astcopy(int rfd, int wfd, off_t n)
52*4887Schin {
53*4887Schin 	register off_t	c;
54*4887Schin #ifdef MAPSIZE
55*4887Schin 	off_t		pos;
56*4887Schin 	off_t		mapsize;
57*4887Schin 	char*		mapbuf;
58*4887Schin 	struct stat	st;
59*4887Schin #endif
60*4887Schin 
61*4887Schin 	static int	bufsiz;
62*4887Schin 	static char*	buf;
63*4887Schin 
64*4887Schin 	if (n <= 0 || n >= BUFSIZ * 2)
65*4887Schin 	{
66*4887Schin #if MAPSIZE
67*4887Schin 		if (!fstat(rfd, &st) && S_ISREG(st.st_mode) && (pos = lseek(rfd, (off_t)0, 1)) != ((off_t)-1))
68*4887Schin 		{
69*4887Schin 			if (pos >= st.st_size) return(0);
70*4887Schin 			mapsize = st.st_size - pos;
71*4887Schin 			if (mapsize > MAPSIZE) mapsize = (mapsize > n && n > 0) ? n : MAPSIZE;
72*4887Schin 			if (mapsize >= BUFSIZ * 2 && (mapbuf = (char*)mmap(NiL, mapsize, PROT_READ, MAP_SHARED, rfd, pos)) != ((caddr_t)-1))
73*4887Schin 			{
74*4887Schin 				if (write(wfd, mapbuf, mapsize) != mapsize || lseek(rfd, mapsize, 1) == ((off_t)-1)) return(-1);
75*4887Schin 				munmap((caddr_t)mapbuf, mapsize);
76*4887Schin 				return(mapsize);
77*4887Schin 			}
78*4887Schin 		}
79*4887Schin #endif
80*4887Schin 		if (n <= 0) n = BUFSIZ;
81*4887Schin 	}
82*4887Schin 	if (n > bufsiz)
83*4887Schin 	{
84*4887Schin 		if (buf) free(buf);
85*4887Schin 		bufsiz = roundof(n, BUFSIZ);
86*4887Schin 		if (!(buf = newof(0, char, bufsiz, 0))) return(-1);
87*4887Schin 	}
88*4887Schin 	if ((c = read(rfd, buf, (size_t)n)) > 0 && write(wfd, buf, (size_t)c) != c) c = -1;
89*4887Schin 	return(c);
90*4887Schin }
91