Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/editors/patch.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/editors/patch.c
r821 r1770 16 16 * Issues 17 17 * - Non-interactive 18 * - Patches must apply cleanly or the hunkwill fail.18 * - Patches must apply cleanly or patch (not just one hunk) will fail. 19 19 * - Reject file isnt saved 20 * -21 20 */ 22 21 23 22 #include <getopt.h> 24 #include <string.h> 25 #include <stdlib.h> 26 #include <unistd.h> 27 #include "busybox.h" 23 24 #include "libbb.h" 28 25 29 26 static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count) … … 33 30 while (src_stream && (i < lines_count)) { 34 31 char *line; 35 line = bb_get_line_from_file(src_stream);32 line = xmalloc_fgets(src_stream); 36 33 if (line == NULL) { 37 34 break; 38 35 } 39 36 if (fputs(line, dest_stream) == EOF) { 40 bb_perror_msg_and_die(" Error writing to new file");37 bb_perror_msg_and_die("error writing to new file"); 41 38 } 42 39 free(line); … … 44 41 i++; 45 42 } 46 return (i);43 return i; 47 44 } 48 45 … … 59 56 60 57 /* Terminate string at end of source filename */ 61 temp = strchr(filename_start_ptr, '\t'); 62 if (temp) *temp = 0; 63 64 /* skip over (patch_level) number of leading directories */ 58 temp = strchrnul(filename_start_ptr, '\t'); 59 *temp = '\0'; 60 61 /* Skip over (patch_level) number of leading directories */ 62 if (patch_level == -1) 63 patch_level = INT_MAX; 65 64 for (i = 0; i < patch_level; i++) { 66 if(!(temp = strchr(filename_start_ptr, '/'))) break; 65 temp = strchr(filename_start_ptr, '/'); 66 if (!temp) 67 break; 67 68 filename_start_ptr = temp + 1; 68 69 } 69 70 70 return (bb_xstrdup(filename_start_ptr));71 return xstrdup(filename_start_ptr); 71 72 } 72 73 … … 74 75 { 75 76 struct stat statbuf; 76 return(stat(filename, &statbuf)); 77 } 78 77 return stat(filename, &statbuf); 78 } 79 80 int patch_main(int argc, char **argv); 79 81 int patch_main(int argc, char **argv) 80 82 { … … 86 88 { 87 89 char *p, *i; 88 ret = bb_getopt_ulflags(argc,argv, "p:i:", &p, &i);90 ret = getopt32(argv, "p:i:", &p, &i); 89 91 if (ret & 1) 90 patch_level = bb_xgetlarg(p, 10, -1, USHRT_MAX);92 patch_level = xatol_range(p, -1, USHRT_MAX); 91 93 if (ret & 2) { 92 patch_file = bb_xfopen(i, "r");94 patch_file = xfopen(i, "r"); 93 95 } else { 94 96 patch_file = stdin; … … 97 99 } 98 100 99 patch_line = bb_get_line_from_file(patch_file);101 patch_line = xmalloc_getline(patch_file); 100 102 while (patch_line) { 101 103 FILE *src_stream; … … 116 118 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) { 117 119 free(patch_line); 118 patch_line = bb_get_line_from_file(patch_file); 119 } 120 patch_line = xmalloc_getline(patch_file); 121 } 122 /* FIXME: patch_line NULL check?? */ 120 123 121 124 /* Extract the filename used before the patch was generated */ … … 123 126 free(patch_line); 124 127 125 patch_line = bb_get_line_from_file(patch_file); 128 patch_line = xmalloc_getline(patch_file); 129 /* FIXME: NULL check?? */ 126 130 if (strncmp(patch_line, "+++ ", 4) != 0) { 127 131 ret = 2; 128 bb_error_msg(" Invalid patch");132 bb_error_msg("invalid patch"); 129 133 continue; 130 134 } … … 141 145 *line_ptr = '/'; 142 146 } 143 dst_stream = bb_xfopen(new_filename, "w+");147 dst_stream = xfopen(new_filename, "w+"); 144 148 backup_filename = NULL; 145 149 } else { … … 148 152 strcat(backup_filename, ".orig"); 149 153 if (rename(new_filename, backup_filename) == -1) { 150 bb_perror_msg_and_die(" Couldnt create file %s",154 bb_perror_msg_and_die("cannot create file %s", 151 155 backup_filename); 152 156 } 153 dst_stream = bb_xfopen(new_filename, "w");157 dst_stream = xfopen(new_filename, "w"); 154 158 } 155 159 … … 158 162 } else { 159 163 if (strcmp(original_filename, new_filename) == 0) { 160 src_stream = bb_xfopen(backup_filename, "r");164 src_stream = xfopen(backup_filename, "r"); 161 165 } else { 162 src_stream = bb_xfopen(original_filename, "r");166 src_stream = xfopen(original_filename, "r"); 163 167 } 164 168 } … … 167 171 168 172 /* Handle each hunk */ 169 patch_line = bb_get_line_from_file(patch_file);173 patch_line = xmalloc_fgets(patch_file); 170 174 while (patch_line) { 171 175 unsigned int count; … … 190 194 count = src_beg_line - src_cur_line; 191 195 if (copy_lines(src_stream, dst_stream, count) != count) { 192 bb_error_msg_and_die(" Bad src file");196 bb_error_msg_and_die("bad src file"); 193 197 } 194 198 src_cur_line += count; … … 198 202 hunk_offset_start = src_cur_line; 199 203 200 while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {204 while ((patch_line = xmalloc_fgets(patch_file)) != NULL) { 201 205 if ((*patch_line == '-') || (*patch_line == ' ')) { 202 206 char *src_line = NULL; 203 207 if (src_stream) { 204 src_line = bb_get_line_from_file(src_stream);208 src_line = xmalloc_fgets(src_stream); 205 209 if (!src_line) { 206 210 hunk_error++; … … 210 214 } 211 215 if (strcmp(src_line, patch_line + 1) != 0) { 212 bb_error_msg(" Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);216 bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start); 213 217 hunk_error++; 214 218 free(patch_line); 219 /* Probably need to find next hunk, etc... */ 220 /* but for now we just bail out */ 221 patch_line = NULL; 215 222 break; 216 223 } … … 256 263 if ((dest_cur_line == 0) || (dest_beg_line == 0)) { 257 264 /* The new patched file is empty, remove it */ 258 if (unlink(new_filename) == -1) { 259 bb_perror_msg_and_die("Couldnt remove file %s", new_filename); 260 } 261 if (unlink(original_filename) == -1) { 262 bb_perror_msg_and_die("Couldnt remove original file %s", new_filename); 263 } 265 xunlink(new_filename); 266 if (strcmp(new_filename, original_filename) != 0) 267 xunlink(original_filename); 264 268 } 265 269 } … … 270 274 * 2 = More serious problems 271 275 */ 272 return (ret);273 } 276 return ret; 277 }
Note:
See TracChangeset
for help on using the changeset viewer.