| 1 | /*
|
|---|
| 2 | jargrep.c - main functions for jargrep utility
|
|---|
| 3 | Copyright (C) 2002, 2003 Free Software Foundation
|
|---|
| 4 | Copyright (C) 1999, 2000 Bryan Burns
|
|---|
| 5 | Copyright (C) 2000 Cory Hollingsworth
|
|---|
| 6 |
|
|---|
| 7 | Parts of this program are base on Bryan Burns work with fastjar
|
|---|
| 8 | Copyright (C) 1999.
|
|---|
| 9 |
|
|---|
| 10 | This program is free software; you can redistribute it and/or
|
|---|
| 11 | modify it under the terms of the GNU General Public License
|
|---|
| 12 | as published by the Free Software Foundation; either version 2
|
|---|
| 13 | of the License, or (at your option) any later version.
|
|---|
| 14 |
|
|---|
| 15 | This program is distributed in the hope that it will be useful,
|
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 18 | GNU General Public License for more details.
|
|---|
| 19 |
|
|---|
| 20 | You should have received a copy of the GNU General Public License
|
|---|
| 21 | along with this program; if not, write to the Free Software
|
|---|
| 22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | /* Id: jargrep.c,v 1.5 2002/01/03 04:57:56 rodrigc Exp
|
|---|
| 26 |
|
|---|
| 27 | Log: jargrep.c,v
|
|---|
| 28 | Revision 1.5 2002/01/03 04:57:56 rodrigc
|
|---|
| 29 | 2001-01-02 Craig Rodrigues <[email protected]>
|
|---|
| 30 |
|
|---|
| 31 | PR bootstrap/5117
|
|---|
| 32 | * configure.in (AC_CHECK_HEADERS): Check for stdlib.h.
|
|---|
| 33 | * Makefile.am: Move grepjar to bin_PROGRAMS.
|
|---|
| 34 | * config.h.in: Regenerated.
|
|---|
| 35 | * Makefile.in: Regenerated.
|
|---|
| 36 | * aclocal.m4: Regenerated.
|
|---|
| 37 | * jargrep.c: Eliminate some signed/unsigned and default
|
|---|
| 38 | uninitialized warnings. Use HAVE_STDLIB_H instead of
|
|---|
| 39 | STDC_HEADERS macro.
|
|---|
| 40 | * jartool.c: Likewise.
|
|---|
| 41 | * compress.c: Likewise.
|
|---|
| 42 |
|
|---|
| 43 | Revision 1.4 2000/12/15 18:45:09 tromey
|
|---|
| 44 | * jargrep.c: Include getopt.h if it exists.
|
|---|
| 45 | (optind): Declare.
|
|---|
| 46 | * configure, config.h: Rebuilt.
|
|---|
| 47 | * configure.in: Check for getopt.h.
|
|---|
| 48 |
|
|---|
| 49 | Revision 1.3 2000/12/14 18:45:35 ghazi
|
|---|
| 50 | Warning fixes:
|
|---|
| 51 |
|
|---|
| 52 | * compress.c: Include stdlib.h and compress.h.
|
|---|
| 53 | (rcsid): Delete.
|
|---|
| 54 | (report_str_error): Make static.
|
|---|
| 55 | (ez_inflate_str): Delete unused variable. Add parens in if-stmt.
|
|---|
| 56 | (hrd_inflate_str): Likewise.
|
|---|
| 57 |
|
|---|
| 58 | * compress.h (init_compression, end_compression, init_inflation,
|
|---|
| 59 | end_inflation): Prototype void arguments.
|
|---|
| 60 |
|
|---|
| 61 | * dostime.c (rcsid): Delete.
|
|---|
| 62 |
|
|---|
| 63 | * jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h.
|
|---|
| 64 | Make functions static. Cast ctype function argument to `unsigned
|
|---|
| 65 | char'. Add parens in if-stmts. Constify.
|
|---|
| 66 | (Usage): Change into a macro.
|
|---|
| 67 | (jargrep): Remove unused parameter.
|
|---|
| 68 |
|
|---|
| 69 | * jartool.c: Constify. Add parens in if-stmts. Align
|
|---|
| 70 | signed/unsigned char pointers in functions calls using casts.
|
|---|
| 71 | (rcsid): Delete.
|
|---|
| 72 | (list_jar): Fix printf format specifier.
|
|---|
| 73 | (usage): Chop long string into bits. Reformat.
|
|---|
| 74 |
|
|---|
| 75 | * pushback.c (rcsid): Delete.
|
|---|
| 76 |
|
|---|
| 77 | Revision 1.2 2000/12/11 02:59:55 apbianco
|
|---|
| 78 | 2000-12-10 Robert Lipe <[email protected]>
|
|---|
| 79 |
|
|---|
| 80 | * jargrep.c (jargrep): Added null statement after case.
|
|---|
| 81 |
|
|---|
| 82 | 2000-12-10 Alexandre Petit-Bianco <[email protected]>
|
|---|
| 83 |
|
|---|
| 84 | * Makefile: Removed.
|
|---|
| 85 | * Makefile.in: Rebuilt with `-i' and `--enable-foreign'.
|
|---|
| 86 |
|
|---|
| 87 | (http://gcc.gnu.org/ml/gcc/2000-12/msg00294.html)
|
|---|
| 88 |
|
|---|
| 89 | Revision 1.1 2000/12/09 03:08:23 apbianco
|
|---|
| 90 | 2000-12-08 Alexandre Petit-Bianco <[email protected]>
|
|---|
| 91 |
|
|---|
| 92 | * fastjar: Imported.
|
|---|
| 93 |
|
|---|
| 94 | Revision 1.8 2000/09/13 14:02:02 cory
|
|---|
| 95 | Reformatted some of the code to more closly match the layout of the orriginal
|
|---|
| 96 | fastjar utility.
|
|---|
| 97 |
|
|---|
| 98 | Revision 1.7 2000/09/12 22:29:36 cory
|
|---|
| 99 | Jargrep now seems to do what I want it to do. Performs properly on Linux x86,
|
|---|
| 100 | will test some other platforms later.
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | */
|
|---|
| 104 |
|
|---|
| 105 | #include "config.h"
|
|---|
| 106 | #include <stdio.h>
|
|---|
| 107 | #include <unistd.h>
|
|---|
| 108 | #include <errno.h>
|
|---|
| 109 | #include <string.h>
|
|---|
| 110 | #include <sys/types.h>
|
|---|
| 111 | #include <sys/stat.h>
|
|---|
| 112 | #include <fcntl.h>
|
|---|
| 113 | #include <ctype.h>
|
|---|
| 114 | #ifdef HAVE_STDLIB_H
|
|---|
| 115 | #include <stdlib.h>
|
|---|
| 116 | #endif
|
|---|
| 117 |
|
|---|
| 118 | #include "xregex.h"
|
|---|
| 119 |
|
|---|
| 120 | #include "jargrep.h"
|
|---|
| 121 | #include "jartool.h"
|
|---|
| 122 | #include "pushback.h"
|
|---|
| 123 | #include "zipfile.h"
|
|---|
| 124 | #include "zlib.h"
|
|---|
| 125 | #include "compress.h"
|
|---|
| 126 | #include <getopt.h>
|
|---|
| 127 |
|
|---|
| 128 | void version(void);
|
|---|
| 129 | void help(const char *name);
|
|---|
| 130 |
|
|---|
| 131 | #define Usage "Usage: %s [-bcinsVw] [--version|--help] <-e PATTERN | PATTERN> FILE ...\n"
|
|---|
| 132 |
|
|---|
| 133 | /*
|
|---|
| 134 | Function name: opt_valid
|
|---|
| 135 | arg: options Bitfield flag that contains the command line options of grepjar.
|
|---|
| 136 | purpose: To guard agains the occurance of certain incompatible flags being used
|
|---|
| 137 | together.
|
|---|
| 138 | returns: TRUE if options are valid, FALSE otherwise.
|
|---|
| 139 | */
|
|---|
| 140 |
|
|---|
| 141 | static int opt_valid(int options) {
|
|---|
| 142 | int retflag;
|
|---|
| 143 |
|
|---|
| 144 | if((options & JG_PRINT_COUNT) &&
|
|---|
| 145 | (options & (JG_PRINT_BYTEOFFSET | JG_PRINT_LINE_NUMBER)))
|
|---|
| 146 | {
|
|---|
| 147 | retflag = FALSE;
|
|---|
| 148 | }
|
|---|
| 149 | else retflag = TRUE;
|
|---|
| 150 |
|
|---|
| 151 | return retflag;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /*
|
|---|
| 155 | Function name: create_regexp
|
|---|
| 156 | args: regstr String containing the uncompiled regular expression. This may be the
|
|---|
| 157 | expression as is passed in through argv.
|
|---|
| 158 | options This is the flag containing the commandline options that have been
|
|---|
| 159 | parsed by getopt.
|
|---|
| 160 | purpose: Handle the exception handling involved with setting upt a new regular
|
|---|
| 161 | expression.
|
|---|
| 162 | returns: Newly allocated compile regular expression ready to be used in an regexec call.
|
|---|
| 163 | */
|
|---|
| 164 |
|
|---|
| 165 | static regex_t *create_regexp(const char *regstr, int options) {
|
|---|
| 166 | regex_t *exp;
|
|---|
| 167 | int errcode;
|
|---|
| 168 | int msgsize;
|
|---|
| 169 | char *errmsg;
|
|---|
| 170 |
|
|---|
| 171 | if((exp = (regex_t *) malloc(sizeof(regex_t))))
|
|---|
| 172 | {
|
|---|
| 173 | if((errcode = regcomp(exp, regstr, (options & JG_IGNORE_CASE) ? REG_ICASE : 0))) {
|
|---|
| 174 | fprintf(stderr, "regcomp of regex failed,\n");
|
|---|
| 175 | if((errmsg = (char *) malloc(msgsize = regerror(errcode, exp, NULL, 0) + 1))) {
|
|---|
| 176 | regerror(errcode, exp, errmsg, msgsize);
|
|---|
| 177 | fprintf(stderr, "Error: %s\n", errmsg);
|
|---|
| 178 | free(exp);
|
|---|
| 179 | free(errmsg);
|
|---|
| 180 | exit(1);
|
|---|
| 181 | }
|
|---|
| 182 | else {
|
|---|
| 183 | fprintf(stderr, "Malloc of errmsg failed.\n");
|
|---|
| 184 | fprintf(stderr, "Error: %s\n", strerror(errno));
|
|---|
| 185 | free(exp);
|
|---|
| 186 | exit(1);
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 | else {
|
|---|
| 191 | fprintf(stderr, "Malloc of regex failed,\n");
|
|---|
| 192 | fprintf(stderr, "Error: %s\n", strerror(errno));
|
|---|
| 193 | exit(1);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | return exp;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /*
|
|---|
| 200 | Function name: check_sig
|
|---|
| 201 | args: scratch Pointer to array of bytes containing signature.
|
|---|
| 202 | pbf Pointer to push back handle for jar file.
|
|---|
| 203 | purpose: Verify that checksum is correct.
|
|---|
| 204 | returns: 0, 1, or 2. 0 means we are ready to read embedded file information. 1 means
|
|---|
| 205 | we have read beyound the embedded file list and can exit knowing we have read all the
|
|---|
| 206 | relevent information. 2 means we still haven't reached embdedded file list and need to
|
|---|
| 207 | do some more reading.
|
|---|
| 208 | */
|
|---|
| 209 | static int check_sig(ub1 *scratch, pb_file *pbfp) {
|
|---|
| 210 | ub4 signature;
|
|---|
| 211 | int retflag = 0;
|
|---|
| 212 |
|
|---|
| 213 | signature = UNPACK_UB4(scratch, 0);
|
|---|
| 214 |
|
|---|
| 215 | #ifdef DEBUG
|
|---|
| 216 | printf("signature is %x\n", signature);
|
|---|
| 217 | #endif
|
|---|
| 218 | if(signature == 0x08074b50){
|
|---|
| 219 | #ifdef DEBUG
|
|---|
| 220 | printf("skipping data descriptor\n");
|
|---|
| 221 | #endif
|
|---|
| 222 | pb_read(pbfp, scratch, 12);
|
|---|
| 223 | retflag = 2;
|
|---|
|
|---|