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