| 1 | /* $Id: pushback.c,v 1.3 2002/01/03 04:57:56 rodrigc Exp $
|
|---|
| 2 |
|
|---|
| 3 | $Log: pushback.c,v $
|
|---|
| 4 | Revision 1.3 2002/01/03 04:57:56 rodrigc
|
|---|
| 5 | 2001-01-02 Craig Rodrigues <rodrigc@gcc.gnu.org>
|
|---|
| 6 |
|
|---|
| 7 | PR bootstrap/5117
|
|---|
| 8 | * configure.in (AC_CHECK_HEADERS): Check for stdlib.h.
|
|---|
| 9 | * Makefile.am: Move grepjar to bin_PROGRAMS.
|
|---|
| 10 | * config.h.in: Regenerated.
|
|---|
| 11 | * Makefile.in: Regenerated.
|
|---|
| 12 | * aclocal.m4: Regenerated.
|
|---|
| 13 | * jargrep.c: Eliminate some signed/unsigned and default
|
|---|
| 14 | uninitialized warnings. Use HAVE_STDLIB_H instead of
|
|---|
| 15 | STDC_HEADERS macro.
|
|---|
| 16 | * jartool.c: Likewise.
|
|---|
| 17 | * compress.c: Likewise.
|
|---|
| 18 |
|
|---|
| 19 | Revision 1.2 2000/12/14 18:45:35 ghazi
|
|---|
| 20 | Warning fixes:
|
|---|
| 21 |
|
|---|
| 22 | * compress.c: Include stdlib.h and compress.h.
|
|---|
| 23 | (rcsid): Delete.
|
|---|
| 24 | (report_str_error): Make static.
|
|---|
| 25 | (ez_inflate_str): Delete unused variable. Add parens in if-stmt.
|
|---|
| 26 | (hrd_inflate_str): Likewise.
|
|---|
| 27 |
|
|---|
| 28 | * compress.h (init_compression, end_compression, init_inflation,
|
|---|
| 29 | end_inflation): Prototype void arguments.
|
|---|
| 30 |
|
|---|
| 31 | * dostime.c (rcsid): Delete.
|
|---|
| 32 |
|
|---|
| 33 | * jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h.
|
|---|
| 34 | Make functions static. Cast ctype function argument to `unsigned
|
|---|
| 35 | char'. Add parens in if-stmts. Constify.
|
|---|
| 36 | (Usage): Change into a macro.
|
|---|
| 37 | (jargrep): Remove unused parameter.
|
|---|
| 38 |
|
|---|
| 39 | * jartool.c: Constify. Add parens in if-stmts. Align
|
|---|
| 40 | signed/unsigned char pointers in functions calls using casts.
|
|---|
| 41 | (rcsid): Delete.
|
|---|
| 42 | (list_jar): Fix printf format specifier.
|
|---|
| 43 | (usage): Chop long string into bits. Reformat.
|
|---|
| 44 |
|
|---|
| 45 | * pushback.c (rcsid): Delete.
|
|---|
| 46 |
|
|---|
| 47 | Revision 1.1 2000/12/09 03:08:23 apbianco
|
|---|
| 48 | 2000-12-08 Alexandre Petit-Bianco <apbianco@cygnus.com>
|
|---|
| 49 |
|
|---|
| 50 | * fastjar: Imported.
|
|---|
| 51 |
|
|---|
| 52 | Revision 1.2 2000/08/23 19:42:17 cory
|
|---|
| 53 | Added support for more Unix platforms. The following code has been hacked
|
|---|
| 54 | to work on AIX, Solaris, True 64, and HP-UX.
|
|---|
| 55 | Added bigendian check. Probably works on most big and little endian platforms
|
|---|
| 56 | now.
|
|---|
| 57 |
|
|---|
| 58 | Revision 1.1.1.1 1999/12/06 03:09:13 toast
|
|---|
| 59 | initial checkin..
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | Revision 1.1 1999/05/10 08:32:37 burnsbr
|
|---|
| 64 | Initial revision
|
|---|
| 65 |
|
|---|
| 66 | */
|
|---|
| 67 |
|
|---|
| 68 | /*
|
|---|
| 69 | pushback.c - code for a pushback buffer to handle file I/O
|
|---|
| 70 | Copyright (C) 1999 Bryan Burns
|
|---|
| 71 |
|
|---|
| 72 | This program is free software; you can redistribute it and/or
|
|---|
| 73 | modify it under the terms of the GNU General Public License
|
|---|
| 74 | as published by the Free Software Foundation; either version 2
|
|---|
| 75 | of the License, or (at your option) any later version.
|
|---|
| 76 |
|
|---|
| 77 | This program is distributed in the hope that it will be useful,
|
|---|
| 78 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 79 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 80 | GNU General Public License for more details.
|
|---|
| 81 |
|
|---|
| 82 | You should have received a copy of the GNU General Public License
|
|---|
| 83 | along with this program; if not, write to the Free Software
|
|---|
| 84 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 85 | */
|
|---|
| 86 |
|
|---|
| 87 | #include <unistd.h>
|
|---|
| 88 | #include <string.h>
|
|---|
| 89 | #include <stdio.h>
|
|---|
| 90 |
|
|---|
| 91 | #include "jartool.h"
|
|---|
| 92 | #include "pushback.h"
|
|---|
| 93 |
|
|---|
| 94 | void pb_init(pb_file *pbf, int fd){
|
|---|
| 95 | pbf->fd = fd;
|
|---|
| 96 | pbf->next = pbf->pb_buff;
|
|---|
| 97 | pbf->buff_amt = 0;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | int pb_push(pb_file *pbf, void *buff, int amt){
|
|---|
| 101 | int in_amt;
|
|---|
| 102 | int wrap = 0;
|
|---|
| 103 |
|
|---|
| 104 | #ifdef DEBUG
|
|---|
| 105 | printf("%d bytes being pushed back to the buffer\n", amt);
|
|---|
| 106 | #endif
|
|---|
| 107 |
|
|---|
| 108 | /* determine how much we can take */
|
|---|
| 109 | if((int)(RDSZ - pbf->buff_amt) < amt)
|
|---|
| 110 | in_amt = RDSZ - pbf->buff_amt;
|
|---|
| 111 | else
|
|---|
| 112 | in_amt = amt;
|
|---|
| 113 |
|
|---|
| 114 | if(in_amt == 0)
|
|---|
| 115 | return 0;
|
|---|
| 116 |
|
|---|
| 117 | /* figure out if we need to wrap around, and if so, by how much */
|
|---|
| 118 | if(((pbf->pb_buff + RDSZ) - pbf->next) < in_amt)
|
|---|
| 119 | wrap = in_amt - ((pbf->pb_buff + RDSZ) - pbf->next);
|
|---|
| 120 |
|
|---|
| 121 | /* write everything up til the end of the buffer */
|
|---|
| 122 | memcpy(pbf->next, buff, (in_amt - wrap));
|
|---|
| 123 |
|
|---|
| 124 | /* finish writing what's wrapped around */
|
|---|
| 125 | memcpy(pbf->pb_buff, ((char *)buff + (in_amt - wrap)), wrap);
|
|---|
| 126 |
|
|---|
| 127 | /* update the buff_amt field */
|
|---|
| 128 | pbf->buff_amt += in_amt;
|
|---|
| 129 |
|
|---|
| 130 | #ifdef DEBUG
|
|---|
| 131 | printf("%d bytes we can't accept\n", (amt - in_amt));
|
|---|
| 132 | #endif
|
|---|
| 133 |
|
|---|
| 134 | return in_amt;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | int pb_read(pb_file *pbf, void *buff, int amt){
|
|---|
| 139 | int out_amt = 0;
|
|---|
| 140 | int wrap = 0;
|
|---|
| 141 | void *bp = buff;
|
|---|
| 142 | int tmp;
|
|---|
| 143 |
|
|---|
| 144 | #ifdef DEBUG
|
|---|
| 145 | printf("%d bytes requested from us\n", amt);
|
|---|
| 146 | #endif
|
|---|
| 147 | while(out_amt < amt){
|
|---|
| 148 | /* if our push-back buffer contains some data */
|
|---|
| 149 | if(pbf->buff_amt > 0){
|
|---|
| 150 |
|
|---|
| 151 | #ifdef DEBUG
|
|---|
| 152 | printf("giving data from buffer\n");
|
|---|
| 153 | #endif
|
|---|
| 154 |
|
|---|
| 155 | /* calculate how much we can actually give the caller */
|
|---|
| 156 | if( (amt - out_amt) < (int)pbf->buff_amt )
|
|---|
| 157 | tmp = (amt - out_amt);
|
|---|
| 158 | else
|
|---|
| 159 | tmp = pbf->buff_amt;
|
|---|
| 160 |
|
|---|
| 161 | /* Determine if we're going to need to wrap around the buffer */
|
|---|
| 162 | if(tmp > ((pbf->pb_buff + RDSZ) - pbf->next))
|
|---|
| 163 | wrap = tmp - ((pbf->pb_buff + RDSZ) - pbf->next);
|
|---|
| 164 |
|
|---|
| 165 | memcpy(bp, pbf->next, (tmp - wrap));
|
|---|
| 166 | bp = &(((char *)bp)[tmp - wrap]);
|
|---|
| 167 |
|
|---|
| 168 | /* If we need to wrap, read from the start of the buffer */
|
|---|
| 169 | if(wrap > 0){
|
|---|
| 170 | memcpy(bp, pbf->pb_buff, wrap);
|
|---|
| 171 | bp = &(((char *)bp)[wrap]);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /* update the buff_amt field */
|
|---|
| 175 | pbf->buff_amt -= tmp;
|
|---|
| 176 | pbf->next += tmp;
|
|---|
| 177 |
|
|---|
| 178 | #ifdef DEBUG
|
|---|
| 179 | printf("%d bytes remaining in buffer\n", pbf->buff_amt);
|
|---|
| 180 | #endif
|
|---|
| 181 |
|
|---|
| 182 | /* if the buffer is empty, reset the next header to the front of the
|
|---|
| 183 | buffer so subsequent pushbacks/reads won't have to wrap */
|
|---|
| 184 | if(pbf->buff_amt == 0)
|
|---|
| 185 | pbf->next = pbf->pb_buff;
|
|---|
| 186 |
|
|---|
| 187 | out_amt += tmp;
|
|---|
| 188 |
|
|---|
| 189 | } else {
|
|---|
| 190 | #ifdef DEBUG
|
|---|
| 191 | printf("Reading from file..\n");
|
|---|
| 192 | #endif
|
|---|
| 193 |
|
|---|
| 194 | /* The pushback buffer was empty, so we just need to read from the file */
|
|---|
| 195 | tmp = read(pbf->fd, bp, (amt - out_amt));
|
|---|
| 196 | if(tmp == 0)
|
|---|
| 197 | break;
|
|---|
| 198 | else
|
|---|
| 199 | out_amt += tmp;
|
|---|
| 200 |
|
|---|
| 201 | bp = &(((char *)bp)[tmp]);
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | #ifdef DEBUG
|
|---|
| 206 | printf("managed to read %d bytes\n", out_amt);
|
|---|
| 207 | #endif
|
|---|
| 208 | return out_amt;
|
|---|
| 209 | }
|
|---|