wok view busybox/stuff/busybox-1.7.3-patch.u @ rev 2174
Up screen (4.0.3), add screen-terminfo
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun Feb 08 12:53:23 2009 +0000 (2009-02-08) |
parents | 4b37234fc0be |
children |
line source
1 --- busybox-1.7.3/editors/patch.c
2 +++ busybox-1.7.3/editors/patch.c
3 @@ -23,11 +23,9 @@
5 #include "libbb.h"
7 -static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
8 +static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, unsigned int lines_count)
9 {
10 - unsigned int i = 0;
11 -
12 - while (src_stream && (i < lines_count)) {
13 + while (src_stream && lines_count) {
14 char *line;
15 line = xmalloc_fgets(src_stream);
16 if (line == NULL) {
17 @@ -37,10 +35,9 @@
18 bb_perror_msg_and_die("error writing to new file");
19 }
20 free(line);
21 -
22 - i++;
23 + lines_count--;
24 }
25 - return i;
26 + return lines_count;
27 }
29 /* If patch_level is -1 it will remove all directory names
30 @@ -49,26 +46,24 @@
31 * returns malloc'ed filename
32 */
34 -static char *extract_filename(char *line, int patch_level)
35 +static char *extract_filename(char *line, unsigned int patch_level)
36 {
37 char *temp, *filename_start_ptr = line + 4;
38 - int i;
40 /* Terminate string at end of source filename */
41 - temp = strchrnul(filename_start_ptr, '\t');
42 - *temp = '\0';
43 + line[strcspn(line,"\t\n")] = '\0';
45 /* Skip over (patch_level) number of leading directories */
46 - if (patch_level == -1)
47 - patch_level = INT_MAX;
48 - for (i = 0; i < patch_level; i++) {
49 + while (patch_level--) {
50 temp = strchr(filename_start_ptr, '/');
51 if (!temp)
52 break;
53 filename_start_ptr = temp + 1;
54 }
56 - return xstrdup(filename_start_ptr);
57 + temp = xstrdup(filename_start_ptr);
58 + free(line);
59 + return temp;
60 }
62 static int file_doesnt_exist(const char *filename)
63 @@ -82,22 +77,19 @@
64 {
65 int patch_level = -1;
66 char *patch_line;
67 - int ret;
68 - FILE *patch_file = NULL;
69 + FILE *patch_file = stdin;
71 {
72 char *p, *i;
73 - ret = getopt32(argv, "p:i:", &p, &i);
74 + int ret = getopt32(argv, "p:i:", &p, &i);
75 if (ret & 1)
76 patch_level = xatol_range(p, -1, USHRT_MAX);
77 if (ret & 2) {
78 patch_file = xfopen(i, "r");
79 - } else {
80 - patch_file = stdin;
81 }
82 - ret = 0;
83 }
85 + xfunc_error_retval = 2;
86 patch_line = xmalloc_getline(patch_file);
87 while (patch_line) {
88 FILE *src_stream;
89 @@ -122,18 +114,14 @@
90 /* FIXME: patch_line NULL check?? */
92 /* Extract the filename used before the patch was generated */
93 - original_filename = extract_filename(patch_line, patch_level);
94 - free(patch_line);
95 + original_filename = extract_filename(patch_line, (unsigned int) patch_level);
97 patch_line = xmalloc_getline(patch_file);
98 /* FIXME: NULL check?? */
99 if (strncmp(patch_line, "+++ ", 4) != 0) {
100 - ret = 2;
101 - bb_error_msg("invalid patch");
102 - continue;
103 + bb_error_msg_and_die("invalid patch: %s\n",patch_line);
104 }
105 - new_filename = extract_filename(patch_line, patch_level);
106 - free(patch_line);
107 + new_filename = extract_filename(patch_line, (unsigned int) patch_level);
109 if (file_doesnt_exist(new_filename)) {
110 char *line_ptr;
111 @@ -144,7 +132,6 @@
112 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
113 *line_ptr = '/';
114 }
115 - dst_stream = xfopen(new_filename, "w+");
116 backup_filename = NULL;
117 } else {
118 backup_filename = xmalloc(strlen(new_filename) + 6);
119 @@ -154,12 +141,11 @@
120 bb_perror_msg_and_die("cannot create file %s",
121 backup_filename);
122 }
123 - dst_stream = xfopen(new_filename, "w");
124 }
126 - if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
127 - src_stream = NULL;
128 - } else {
129 + dst_stream = xfopen(new_filename, "w");
130 + src_stream = NULL;
131 + if (!file_doesnt_exist(original_filename) && backup_filename) {
132 if (strcmp(original_filename, new_filename) == 0) {
133 src_stream = xfopen(backup_filename, "r");
134 } else {
135 @@ -174,54 +160,59 @@
136 while (patch_line) {
137 unsigned int count;
138 unsigned int src_beg_line;
139 + unsigned int src_last_line = 1;
140 unsigned int unused;
141 unsigned int hunk_offset_start = 0;
142 - int hunk_error = 0;
144 /* This bit should be improved */
145 - if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
146 - (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
147 - (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
148 + if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line,
149 + &src_last_line, &dest_beg_line, &unused) != 4) &&
150 + (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line,
151 + &src_last_line, &dest_beg_line) != 3) &&
152 + (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line,
153 + &dest_beg_line, &unused) != 3) &&
154 + (sscanf(patch_line, "@@ -%d +%d @@", &src_beg_line,
155 + &dest_beg_line) != 2)) {
156 /* No more hunks for this file */
157 break;
158 }
159 - free(patch_line);
160 hunk_count++;
162 if (src_beg_line && dest_beg_line) {
163 /* Copy unmodified lines upto start of hunk */
164 /* src_beg_line will be 0 if its a new file */
165 count = src_beg_line - src_cur_line;
166 - if (copy_lines(src_stream, dst_stream, count) != count) {
167 + if (copy_lines(src_stream, dst_stream, count)) {
168 bb_error_msg_and_die("bad src file");
169 }
170 src_cur_line += count;
171 dest_cur_line += count;
172 copy_trailing_lines_flag = 1;
173 }
174 - hunk_offset_start = src_cur_line;
175 + src_last_line += hunk_offset_start = src_cur_line;
177 - while ((patch_line = xmalloc_fgets(patch_file)) != NULL) {
178 + for (free(patch_line);
179 + (patch_line = xmalloc_fgets(patch_file)) != NULL;
180 + free(patch_line)) {
181 if ((*patch_line == '-') || (*patch_line == ' ')) {
182 char *src_line = NULL;
183 + if (src_cur_line == src_last_line) break;
184 if (src_stream) {
185 src_line = xmalloc_fgets(src_stream);
186 - if (!src_line) {
187 - hunk_error++;
188 - break;
189 - } else {
190 + if (src_line) {
191 + int diff = strcmp(src_line, patch_line + 1);
192 src_cur_line++;
193 + free(src_line);
194 + if (diff) {
195 + src_line = NULL;
196 + }
197 }
198 - if (strcmp(src_line, patch_line + 1) != 0) {
199 - bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start);
200 - hunk_error++;
201 - free(patch_line);
202 - /* Probably need to find next hunk, etc... */
203 - /* but for now we just bail out */
204 - patch_line = NULL;
205 + if (!src_line) {
206 + bb_error_msg("hunk #%d FAILED at %d",
207 + hunk_count, hunk_offset_start);
208 + bad_hunk_count++;
209 break;
210 }
211 - free(src_line);
212 }
213 if (*patch_line == ' ') {
214 fputs(patch_line + 1, dst_stream);
215 @@ -233,16 +224,12 @@
216 } else {
217 break;
218 }
219 - free(patch_line);
220 - }
221 - if (hunk_error) {
222 - bad_hunk_count++;
223 }
224 }
226 /* Cleanup last patched file */
227 if (copy_trailing_lines_flag) {
228 - copy_lines(src_stream, dst_stream, -1);
229 + copy_lines(src_stream, dst_stream, (unsigned int) -1);
230 }
231 if (src_stream) {
232 fclose(src_stream);
233 @@ -251,16 +238,14 @@
234 fclose(dst_stream);
235 }
236 if (bad_hunk_count) {
237 - if (!ret) {
238 - ret = 1;
239 - }
240 bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
241 + return 1;
242 } else {
243 /* It worked, we can remove the backup */
244 if (backup_filename) {
245 unlink(backup_filename);
246 }
247 - if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
248 + if (dest_cur_line == 0) {
249 /* The new patched file is empty, remove it */
250 xunlink(new_filename);
251 if (strcmp(new_filename, original_filename) != 0)
252 @@ -273,5 +258,5 @@
253 * 1 = Some hunks failed
254 * 2 = More serious problems
255 */
256 - return ret;
257 + return 0;
258 }