14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin * Glenn Fowler
254887Schin * AT&T Bell Laboratories
264887Schin *
274887Schin * copy from rfd to wfd (with conditional mmap hacks)
284887Schin */
294887Schin
304887Schin #include <ast.h>
314887Schin #include <ast_mmap.h>
324887Schin
334887Schin #if _mmap_worthy > 1
344887Schin
354887Schin #include <ls.h>
364887Schin
374887Schin #define MAPSIZE (1024*256)
384887Schin
394887Schin #endif
404887Schin
414887Schin #undef BUFSIZ
424887Schin #define BUFSIZ 4096
434887Schin
444887Schin /*
454887Schin * copy n bytes from rfd to wfd
464887Schin * actual byte count returned
474887Schin * if n<=0 then ``good'' size is used
484887Schin */
494887Schin
504887Schin off_t
astcopy(int rfd,int wfd,off_t n)514887Schin astcopy(int rfd, int wfd, off_t n)
524887Schin {
534887Schin register off_t c;
544887Schin #ifdef MAPSIZE
554887Schin off_t pos;
564887Schin off_t mapsize;
574887Schin char* mapbuf;
584887Schin struct stat st;
594887Schin #endif
604887Schin
614887Schin static int bufsiz;
624887Schin static char* buf;
634887Schin
644887Schin if (n <= 0 || n >= BUFSIZ * 2)
654887Schin {
664887Schin #if MAPSIZE
674887Schin if (!fstat(rfd, &st) && S_ISREG(st.st_mode) && (pos = lseek(rfd, (off_t)0, 1)) != ((off_t)-1))
684887Schin {
694887Schin if (pos >= st.st_size) return(0);
704887Schin mapsize = st.st_size - pos;
714887Schin if (mapsize > MAPSIZE) mapsize = (mapsize > n && n > 0) ? n : MAPSIZE;
724887Schin if (mapsize >= BUFSIZ * 2 && (mapbuf = (char*)mmap(NiL, mapsize, PROT_READ, MAP_SHARED, rfd, pos)) != ((caddr_t)-1))
734887Schin {
744887Schin if (write(wfd, mapbuf, mapsize) != mapsize || lseek(rfd, mapsize, 1) == ((off_t)-1)) return(-1);
754887Schin munmap((caddr_t)mapbuf, mapsize);
764887Schin return(mapsize);
774887Schin }
784887Schin }
794887Schin #endif
804887Schin if (n <= 0) n = BUFSIZ;
814887Schin }
824887Schin if (n > bufsiz)
834887Schin {
844887Schin if (buf) free(buf);
854887Schin bufsiz = roundof(n, BUFSIZ);
864887Schin if (!(buf = newof(0, char, bufsiz, 0))) return(-1);
874887Schin }
884887Schin if ((c = read(rfd, buf, (size_t)n)) > 0 && write(wfd, buf, (size_t)c) != c) c = -1;
894887Schin return(c);
904887Schin }
91