aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/patch.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/editors/patch.c b/editors/patch.c
index 6a68d2ef8..59b70210f 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -107,14 +107,24 @@ extern int patch_main(int argc, char **argv)
107{ 107{
108 unsigned int patch_level = -1; 108 unsigned int patch_level = -1;
109 char *patch_line; 109 char *patch_line;
110 int ret = 0; 110 int ret;
111 FILE *patch_file = NULL;
111 112
112 /* Handle 'p' option */ 113 {
113 if (argv[1] && (argv[1][0] == '-') && (argv[1][1] == 'p')) { 114 char *p, *i;
114 patch_level = atoi(&argv[1][2]); 115 ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
116 if (ret & 1)
117 patch_level = bb_xgetularg10_bnd(p, -1, USHRT_MAX);
118 if (ret & 2) {
119 patch_file = bb_xfopen(i, "r");
120 }
121 ret = 0;
115 } 122 }
116 123
117 patch_line = bb_get_line_from_file(stdin); 124 if (!patch_file)
125 patch_file = stdin;
126
127 patch_line = bb_get_line_from_file(patch_file);
118 while (patch_line) { 128 while (patch_line) {
119 FILE *src_stream; 129 FILE *src_stream;
120 FILE *dst_stream; 130 FILE *dst_stream;
@@ -133,14 +143,14 @@ extern int patch_main(int argc, char **argv)
133 */ 143 */
134 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) { 144 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
135 free(patch_line); 145 free(patch_line);
136 patch_line = bb_get_line_from_file(stdin); 146 patch_line = bb_get_line_from_file(patch_file);
137 } 147 }
138 148
139 /* Extract the filename used before the patch was generated */ 149 /* Extract the filename used before the patch was generated */
140 original_filename = extract_filename(patch_line, patch_level); 150 original_filename = extract_filename(patch_line, patch_level);
141 free(patch_line); 151 free(patch_line);
142 152
143 patch_line = bb_get_line_from_file(stdin); 153 patch_line = bb_get_line_from_file(patch_file);
144 if (strncmp(patch_line, "+++ ", 4) != 0) { 154 if (strncmp(patch_line, "+++ ", 4) != 0) {
145 ret = 2; 155 ret = 2;
146 bb_error_msg("Invalid patch"); 156 bb_error_msg("Invalid patch");
@@ -183,7 +193,7 @@ extern int patch_main(int argc, char **argv)
183 printf("patching file %s\n", new_filename); 193 printf("patching file %s\n", new_filename);
184 194
185 /* Handle each hunk */ 195 /* Handle each hunk */
186 patch_line = bb_get_line_from_file(stdin); 196 patch_line = bb_get_line_from_file(patch_file);
187 while (patch_line) { 197 while (patch_line) {
188 unsigned int count; 198 unsigned int count;
189 unsigned int src_beg_line; 199 unsigned int src_beg_line;
@@ -214,7 +224,7 @@ extern int patch_main(int argc, char **argv)
214 } 224 }
215 hunk_offset_start = src_cur_line; 225 hunk_offset_start = src_cur_line;
216 226
217 while ((patch_line = bb_get_line_from_file(stdin)) != NULL) { 227 while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
218 if ((*patch_line == '-') || (*patch_line == ' ')) { 228 if ((*patch_line == '-') || (*patch_line == ' ')) {
219 char *src_line = NULL; 229 char *src_line = NULL;
220 if (src_stream) { 230 if (src_stream) {