wok-6.x view busybox/stuff/busybox-1.10.0-patch.u @ rev 493

Up: busybox 1.10.0
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Mar 22 22:05:45 2008 +0100 (2008-03-22)
parents
children bb8a2bcece7d
line source
1 --- busybox-1.10.0/editors/patch.c Sat Mar 22 02:31:52 2008
2 +++ busybox-1.10.0/editors/patch.c Sat Mar 22 02:31:52 2008
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 @@ -38,9 +36,9 @@
18 }
19 free(line);
21 - i++;
22 + lines_count--;
23 }
24 - return i;
25 + return lines_count;
26 }
28 /* If patch_level is -1 it will remove all directory names
29 @@ -49,26 +47,24 @@
30 * returns malloc'ed filename
31 */
33 -static char *extract_filename(char *line, int patch_level)
34 +static char *extract_filename(char *line, unsigned int patch_level)
35 {
36 char *temp, *filename_start_ptr = line + 4;
37 - int i;
39 /* Terminate string at end of source filename */
40 - temp = strchrnul(filename_start_ptr, '\t');
41 - *temp = '\0';
42 + line[strcspn(line,"\t\n")] = '\0';
44 /* Skip over (patch_level) number of leading directories */
45 - if (patch_level == -1)
46 - patch_level = INT_MAX;
47 - for (i = 0; i < patch_level; i++) {
48 + while (patch_level--) {
49 temp = strchr(filename_start_ptr, '/');
50 if (!temp)
51 break;
52 filename_start_ptr = temp + 1;
53 }
55 - return xstrdup(filename_start_ptr);
56 + temp = xstrdup(filename_start_ptr);
57 + free(line);
58 + return temp;
59 }
61 int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62 @@ -77,7 +73,7 @@
63 int patch_level = -1;
64 char *patch_line;
65 int ret;
66 - FILE *patch_file = NULL;
67 + FILE *patch_file = stdin;
68 struct stat saved_stat;
70 {
71 @@ -87,12 +83,11 @@
72 patch_level = xatol_range(p, -1, USHRT_MAX);
73 if (ret & 2) {
74 patch_file = xfopen(i, "r");
75 - } else {
76 - patch_file = stdin;
77 }
78 ret = 0;
79 }
81 + //xfunc_error_retval = 2;
82 patch_line = xmalloc_getline(patch_file);
83 while (patch_line) {
84 FILE *src_stream;
85 @@ -118,17 +113,13 @@
87 /* Extract the filename used before the patch was generated */
88 original_filename = extract_filename(patch_line, patch_level);
89 - free(patch_line);
91 patch_line = xmalloc_getline(patch_file);
92 /* FIXME: NULL check?? */
93 if (strncmp(patch_line, "+++ ", 4) != 0) {
94 - ret = 2;
95 - bb_error_msg("invalid patch");
96 - continue;
97 + bb_error_msg_and_die("invalid patch: %s\n",patch_line);
98 }
99 new_filename = extract_filename(patch_line, patch_level);
100 - free(patch_line);
102 /* Get access rights from the file to be patched, -1 file does not exist */
103 if (stat(new_filename, &saved_stat)) {
104 @@ -140,20 +131,19 @@
105 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
106 *line_ptr = '/';
107 }
108 - dst_stream = xfopen(new_filename, "w+");
109 backup_filename = NULL;
110 + saved_stat.st_mode = 0644;
111 } else {
112 backup_filename = xmalloc(strlen(new_filename) + 6);
113 strcpy(backup_filename, new_filename);
114 strcat(backup_filename, ".orig");
115 xrename(new_filename, backup_filename);
116 - dst_stream = xfopen(new_filename, "w");
117 - fchmod(fileno(dst_stream), saved_stat.st_mode);
118 }
120 - if ((backup_filename == NULL) || stat(original_filename, &saved_stat)) {
121 - src_stream = NULL;
122 - } else {
123 + dst_stream = xfopen(new_filename, "w");
124 + fchmod(fileno(dst_stream), saved_stat.st_mode);
125 + src_stream = NULL;
126 + if (backup_filename && !stat(original_filename, &saved_stat)) {
127 if (strcmp(original_filename, new_filename) == 0) {
128 src_stream = xfopen(backup_filename, "r");
129 } else {
130 @@ -168,54 +158,57 @@
131 while (patch_line) {
132 unsigned int count;
133 unsigned int src_beg_line;
134 + unsigned int src_last_line = 1;
135 unsigned int unused;
136 unsigned int hunk_offset_start = 0;
137 int hunk_error = 0;
139 /* This bit should be improved */
140 - if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
141 - (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
142 - (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
143 + if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line,
144 + &src_last_line, &dest_beg_line, &unused) != 4) &&
145 + (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line,
146 + &src_last_line, &dest_beg_line) != 3) &&
147 + (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line,
148 + &dest_beg_line, &unused) != 3) &&
149 + (sscanf(patch_line, "@@ -%d +%d @@", &src_beg_line,
150 + &dest_beg_line) != 2)) {
151 /* No more hunks for this file */
152 break;
153 }
154 - free(patch_line);
155 hunk_count++;
157 if (src_beg_line && dest_beg_line) {
158 /* Copy unmodified lines upto start of hunk */
159 /* src_beg_line will be 0 if its a new file */
160 count = src_beg_line - src_cur_line;
161 - if (copy_lines(src_stream, dst_stream, count) != count) {
162 + if (copy_lines(src_stream, dst_stream, count)) {
163 bb_error_msg_and_die("bad src file");
164 }
165 src_cur_line += count;
166 dest_cur_line += count;
167 copy_trailing_lines_flag = 1;
168 }
169 - hunk_offset_start = src_cur_line;
170 + src_last_line += hunk_offset_start = src_cur_line;
172 - while ((patch_line = xmalloc_fgets(patch_file)) != NULL) {
173 + while (free(patch_line), (patch_line = xmalloc_fgets(patch_file)) != NULL) {
174 if ((*patch_line == '-') || (*patch_line == ' ')) {
175 char *src_line = NULL;
176 + if (src_cur_line == src_last_line) break;
177 if (src_stream) {
178 src_line = xmalloc_fgets(src_stream);
179 - if (!src_line) {
180 - hunk_error++;
181 - break;
182 - } else {
183 + if (src_line) {
184 + int diff = strcmp(src_line, patch_line + 1);
185 src_cur_line++;
186 + free(src_line);
187 + if (diff) {
188 + src_line = NULL;
189 + }
190 }
191 - if (strcmp(src_line, patch_line + 1) != 0) {
192 + if (!src_line) {
193 bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start);
194 hunk_error++;
195 - free(patch_line);
196 - /* Probably need to find next hunk, etc... */
197 - /* but for now we just bail out */
198 - patch_line = NULL;
199 break;
200 }
201 - free(src_line);
202 }
203 if (*patch_line == ' ') {
204 fputs(patch_line + 1, dst_stream);
205 @@ -227,7 +220,6 @@
206 } else {
207 break;
208 }
209 - free(patch_line);
210 }
211 if (hunk_error) {
212 bad_hunk_count++;