1 /* $Id: pushback.c 119 2004-10-05 20:38:42Z oyvind $
4 Revision 1.1 2004/10/05 20:31:35 oyvind
5 * added GCC to repository
7 Revision 1.2 2000/12/14 18:45:35 ghazi
10 * compress.c: Include stdlib.h and compress.h.
12 (report_str_error): Make static.
13 (ez_inflate_str): Delete unused variable. Add parens in if-stmt.
14 (hrd_inflate_str): Likewise.
16 * compress.h (init_compression, end_compression, init_inflation,
17 end_inflation): Prototype void arguments.
19 * dostime.c (rcsid): Delete.
21 * jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h.
22 Make functions static. Cast ctype function argument to `unsigned
23 char'. Add parens in if-stmts. Constify.
24 (Usage): Change into a macro.
25 (jargrep): Remove unused parameter.
27 * jartool.c: Constify. Add parens in if-stmts. Align
28 signed/unsigned char pointers in functions calls using casts.
30 (list_jar): Fix printf format specifier.
31 (usage): Chop long string into bits. Reformat.
33 * pushback.c (rcsid): Delete.
35 Revision 1.1 2000/12/09 03:08:23 apbianco
36 2000-12-08 Alexandre Petit-Bianco <apbianco@cygnus.com>
40 Revision 1.2 2000/08/23 19:42:17 cory
41 Added support for more Unix platforms. The following code has been hacked
42 to work on AIX, Solaris, True 64, and HP-UX.
43 Added bigendian check. Probably works on most big and little endian platforms
46 Revision 1.1.1.1 1999/12/06 03:09:13 toast
51 Revision 1.1 1999/05/10 08:32:37 burnsbr
57 pushback.c - code for a pushback buffer to handle file I/O
58 Copyright (C) 1999 Bryan Burns
60 This program is free software; you can redistribute it and/or
61 modify it under the terms of the GNU General Public License
62 as published by the Free Software Foundation; either version 2
63 of the License, or (at your option) any later version.
65 This program is distributed in the hope that it will be useful,
66 but WITHOUT ANY WARRANTY; without even the implied warranty of
67 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68 GNU General Public License for more details.
70 You should have received a copy of the GNU General Public License
71 along with this program; if not, write to the Free Software
72 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
82 void pb_init(pb_file
*pbf
, int fd
){
84 pbf
->next
= pbf
->pb_buff
;
88 int pb_push(pb_file
*pbf
, void *buff
, int amt
){
93 printf("%d bytes being pushed back to the buffer\n", amt
);
96 /* determine how much we can take */
97 if((int)(RDSZ
- pbf
->buff_amt
) < amt
)
98 in_amt
= RDSZ
- pbf
->buff_amt
;
105 /* figure out if we need to wrap around, and if so, by how much */
106 if(((pbf
->pb_buff
+ RDSZ
) - pbf
->next
) < in_amt
)
107 wrap
= in_amt
- ((pbf
->pb_buff
+ RDSZ
) - pbf
->next
);
109 /* write everything up til the end of the buffer */
110 memcpy(pbf
->next
, buff
, (in_amt
- wrap
));
112 /* finish writing what's wrapped around */
113 memcpy(pbf
->pb_buff
, ((char *)buff
+ (in_amt
- wrap
)), wrap
);
115 /* update the buff_amt field */
116 pbf
->buff_amt
+= in_amt
;
119 printf("%d bytes we can't accept\n", (amt
- in_amt
));
126 int pb_read(pb_file
*pbf
, void *buff
, int amt
){
133 printf("%d bytes requested from us\n", amt
);
135 while(out_amt
< amt
){
136 /* if our push-back buffer contains some data */
137 if(pbf
->buff_amt
> 0){
140 printf("giving data from buffer\n");
143 /* calculate how much we can actually give the caller */
144 if( (amt
- out_amt
) < (int)pbf
->buff_amt
)
145 tmp
= (amt
- out_amt
);
149 /* Determine if we're going to need to wrap around the buffer */
150 if(tmp
> ((pbf
->pb_buff
+ RDSZ
) - pbf
->next
))
151 wrap
= tmp
- ((pbf
->pb_buff
+ RDSZ
) - pbf
->next
);
153 memcpy(bp
, pbf
->next
, (tmp
- wrap
));
154 bp
= &(((char *)bp
)[tmp
- wrap
]);
156 /* If we need to wrap, read from the start of the buffer */
158 memcpy(bp
, pbf
->pb_buff
, wrap
);
159 bp
= &(((char *)bp
)[wrap
]);
162 /* update the buff_amt field */
163 pbf
->buff_amt
-= tmp
;
167 printf("%d bytes remaining in buffer\n", pbf
->buff_amt
);
170 /* if the buffer is empty, reset the next header to the front of the
171 buffer so subsequent pushbacks/reads won't have to wrap */
172 if(pbf
->buff_amt
== 0)
173 pbf
->next
= pbf
->pb_buff
;
179 printf("Reading from file..\n");
182 /* The pushback buffer was empty, so we just need to read from the file */
183 tmp
= read(pbf
->fd
, bp
, (amt
- out_amt
));
189 bp
= &(((char *)bp
)[tmp
]);
194 printf("managed to read %d bytes\n", out_amt
);