~peter-pearse/ubuntu/natty/unzip/prop001

« back to all changes in this revision

Viewing changes to unix/zipgrep

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2009-05-08 20:02:40 UTC
  • mfrom: (1.1.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090508200240-7l4gypruop5863bd
* New upstream release. Closes: #496989.
* Enabled new Unicode support. Closes: #197427. This may or may not work
  for your already created zipfiles, but it's not a bug unless they were
  created using the Unicode feature present in zip 3.0.
* Built using DATE_FORMAT=DF_YMD so that unzip -l show dates in ISO format,
  as that's the only available one which makes sense. Closes: #312886.
* Enabled new bzip2 support. Closes: #426798.
* Exit code for zipgrep should now be the right one. Closes: #441997.
* The reason why a file may not be created is now shown. Closes: #478791.
* Summary of changes in this version not being the debian/* files:
- Manpages in section 1, not 1L.
- Branding patch. UnZip by Debian. Original by Info-ZIP.
- Always #include <unistd.h>. Debian GNU/kFreeBSD needs it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/sh
2
 
# zipgrep: searches the given zip members for a string or pattern
3
 
# This shell script assumes that you have installed UnZip.
 
1
#!/bin/sh
 
2
#
 
3
#    zipgrep: Use unzip and egrep to search the specified members of a
 
4
# Zip archive for a string or pattern.  Search all members if no members
 
5
# are specified explicitly.  The script attempts to handle egrep's "-h"
 
6
# and "-l" options internally.
 
7
#
 
8
# This script assumes that the desired "unzip" and "egrep" (and "sed")
 
9
# programs are on the user's PATH.
 
10
#
4
11
 
5
12
pat=""
6
13
opt=""
18
25
done
19
26
 
20
27
if test $# = 0; then
21
 
  echo "usage: `basename $0` [egrep_options] pattern zipfile [members...]"
22
 
  echo searches the given zip members for a string or pattern
 
28
  echo usage: `basename "$0"` "[egrep_options] pattern zipfile [members...]"
 
29
  echo Uses unzip and egrep to search the zip members for a string or pattern.
23
30
  exit 1
24
31
fi
25
32
zipfile="$1"; shift
37
44
  opt="-$opt"
38
45
fi
39
46
 
40
 
res=0
 
47
status_grep_global=1
41
48
IFS='
42
49
'
43
 
for i in `unzip -Z1 "$zipfile" ${1+"$@"}`; do
 
50
 
 
51
# Escape shell-special characters in "pat".
 
52
pat=` echo "$pat" | \
 
53
 sed -e 's/\\\\/\\\\\\\\/g' -e 's/|/\\\|/g' -e 's/&/\\\&/g' `
 
54
 
 
55
# Use "unzip -Z1" to get a listing of the specified members from the
 
56
# specified archive.  Escape any backslashes in a file name.
 
57
for i in `unzip -Z1 "$zipfile" ${1+"$@"} | sed -e 's/\\\\/\\\\\\\\/g' `; do
44
58
  if test $list -eq 1; then
45
 
 
46
 
    unzip -p-L "$zipfile" "$i" | egrep $opt "$pat" > /dev/null && echo $i
47
 
    r=$?
 
59
    # "-l": Show only the archive member name, not the matching line(s).
 
60
    unzip -p-L "$zipfile" "$i" | \
 
61
     egrep $opt "$pat" > /dev/null && echo "$i"
 
62
    status_grep=$?
48
63
  elif test $silent -eq 1; then
49
 
 
50
 
    unzip -p-L "$zipfile" "$i" | egrep $opt "$pat"
51
 
    r=$?
 
64
    # "-h": Show only the matching line(s), not the archive member name.
 
65
    # ("-s" in "opt" will silence "egrep", stopping all output.)
 
66
    unzip -p-L "$zipfile" "$i" | \
 
67
     egrep $opt "$pat"
 
68
    status_grep=$?
52
69
  else
53
 
    unzip -p-L "$zipfile" "$i" | egrep $opt "$pat" | sed "s|^|${i}:|"
54
 
    r=$?
 
70
    # Escape (or re-escape) shell-special characters in the archive
 
71
    # member name, "i".
 
72
    i=` echo "$i" | \
 
73
     sed -e 's/\\\\/\\\\\\\\/g' -e 's/|/\\\|/g' -e 's/&/\\\&/g' `
 
74
 
 
75
    # Globally, send fd 4 to stdout.  In the pipeline, send normal
 
76
    # stdout to fd 4, and send grep status to fd 3.  Collect fd 3
 
77
    # with ``.
 
78
    exec 4>&1
 
79
    status_grep=` ( \
 
80
     ( unzip -p-L "$zipfile" "$i" | \
 
81
     egrep $opt "$pat" 1>&4 ; echo $? >&3 ) 4>&1 | \
 
82
     sed "s|^|${i}:|" 1>&4 \
 
83
     ) 3>&1 `
55
84
  fi
56
 
  test "$r" -ne 0 && res="$r"
 
85
 
 
86
  # Save the primary command status.  (May be the grep status.)
 
87
  sts=$?
 
88
  # If this grep status was zero, set the global grep status to zero.
 
89
  test "$status_grep" -eq 0 && status_grep_global=0
 
90
  # If this grep status was not zero or one, exit now.
 
91
  test "$status_grep" -gt 1 && exit "$status_grep"
 
92
 
57
93
done
58
 
exit $res
 
94
 
 
95
# If "sts" is good (0), then exit with the global grep status.
 
96
# Else, when "sts" is bad, exit with the worst status we can find.
 
97
if test $sts -eq 0 ; then
 
98
  exit $status_grep_global
 
99
else
 
100
  if test "$status_grep" -gt 1 ; then
 
101
    exit "$status_grep"
 
102
  else
 
103
    exit $sts
 
104
  fi
 
105
fi