1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1992-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
23 * common support for tail and rev
29 #define BUFSIZE SF_BUFSIZE
30 #define rounddown(n,size) (((n)-1)&~((size)-1))
33 * copy the lines starting at offset <start> from in <in> to <out>
36 int rev_line(Sfio_t
*in
, Sfio_t
*out
, off_t start
)
38 register char *cp
, *cpold
;
39 register int n
, nleft
=0;
42 if(sfseek(in
,(off_t
)0,SEEK_CUR
) < 0)
44 Sfio_t
*tmp
= sftmp(4*SF_BUFSIZE
);
47 if(start
>0 && sfmove(in
, (Sfio_t
*)0, start
, -1) != start
)
49 if(sfmove(in
, tmp
, SF_UNBOUND
, -1) < 0 || !sfeof(in
) || sferror(tmp
))
54 if((offset
= sfseek(in
,(off_t
)0,SEEK_END
)) <= start
)
56 offset
= rounddown(offset
,BUFSIZE
);
65 sfseek(in
, offset
, SEEK_SET
);
66 if((n
=sfread(in
, buff
, n
)) <=0)
82 if(cp
==buff
&& n
!='\n')
90 if(sfwrite(out
,cp
,cpold
-cp
) < 0)
96 else if(sfmove(in
,out
,nleft
,-1) != nleft
)
107 sfseek(in
, start
, SEEK_SET
);
108 if(sfmove(in
,out
,nleft
,-1) != nleft
)