forked from kesnol/Aircrack-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatchchk
152 lines (127 loc) · 3.07 KB
/
patchchk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /bin/sh
PATCH=`which patch 2>/dev/null`
FGREP1="^--- "
FGREP2="^+++ "
PREDIR="`pwd`"
if [ x"`echo "$1" | grep "^/"`" = x ]
then
WHOLEPATCH="`pwd`/$1"
else
WHOLEPATCH="$1"
fi
if [ ! -f $WHOLEPATCH ]
then
echo "Patch \"$1\" doesn't exist."
exit
fi
if [ x"$PATCH" = x ]
then
echo "You need to install \"patch\" prior to patching anything."
exit
fi
if [ x"$1" = x ]
then
echo "Usage: \"$0 foo.patch\""
exit
fi
if [ x"`grep "$FGREP1" "$WHOLEPATCH" | awk '{print $2}'`" = x ]
then
echo "No valid patch file, you need to "
fi
fcount1=`grep "$FGREP1" "$WHOLEPATCH" | awk '{print $2}' | wc -l`
fcount2=`grep "$FGREP2" "$WHOLEPATCH" | awk '{print $2}' | wc -l`
if [ $fcount1 -ne $fcount2 ]
then
echo "Different number of Files: add($fcount2) vs. sub($fcount1). Broken file?"
exit
fi
echo "Trying to find correct path and arguments..."
cdr=1
hits=0
maxhits=0
lastfiles=$fcount1
value=0
godown=0
actdown=0
maxcutglobaldirs=`pwd | sed 's/[^\/]//g' | wc -c`
j=0
down=0
while [ $j -lt $maxcutglobaldirs ]
do
fcurnum=`grep "$FGREP1" "$WHOLEPATCH" | awk '{print $2}' | cut -d/ -f$cdr- | grep -v "^$" | wc -l`
if [ $fcurnum -lt $fcount1 ]
then
#too many dirs cut, lost some files
down=1
fi
hits=0
i=1
while [ $i -lt $fcurnum ]
do
filename1=`grep "$FGREP1" "$WHOLEPATCH" | awk '{print $2}' | cut -d/ -f$cdr- | grep -v "^$" | head -n $i | tail -n 1`
filename2=`grep "$FGREP2" "$WHOLEPATCH" | awk '{print $2}' | cut -d/ -f$cdr- | grep -v "^$" | head -n $i | tail -n 1`
if [ -f $PREDIR/$filename1 -o -f $PREDIR/$filename2 ]
then
hits=$(($hits+1))
fi
i=$(($i+1))
done
if [ $hits -gt $maxhits ]
then
maxhits=$hits
value=$(($cdr-1))
actdown=$godown
fi
cdr=$(($cdr+1))
if [ $down -eq 1 ]
then
down=0
if [ $maxhits -eq $fcount1 ]
then
break
fi
godown=$(($godown+1))
PREDIR=`echo $PREDIR | cut -d/ -f1-$(($maxcutglobaldirs-$godown))`
j=$(($j+1))
cdr=0
fi
done
change="cd ./"
i=0
while [ $i -lt $actdown ]
do
change="$change../"
i=$(($i+1))
done
$change
if [ x"`$PATCH -f --dry-run -Np$value -i "$WHOLEPATCH" | grep "ignored\|ERROR"`" != x ]
then
echo "Found problems with applying "$WHOLEPATCH" from this location."
exit
fi
if [ $maxhits -eq 0 ]
then
echo "Found no files at all."
exit
fi
if [ $maxhits -lt $fcount1 ]
then
#echo "I didn't found all files, which should be patched."
#echo "Just got $maxhits hits out of $fcount1 proposed"
if [ $actdown -gt 0 ]
then
echo "You may run:"
echo "\"($change; patch --dry-run -Np$value -i "$WHOLEPATCH")\""
else
echo "You may run:"
echo "\"patch --dry-run -Np$value -i $1\""
fi
echo "and in case you got no errors, just omit \"--dry-run\"."
else
if [ $actdown -gt 0 ]
then
echo "You need to run \"($change; patch -Np$value -i "$WHOLEPATCH")\""
else
echo "You need to run \"patch -Np$value -i $1\""
fi
fi