# HG changeset patch # User Pascal Bellard # Date 1204201856 0 # Node ID 4b37234fc0be7695dec0db94b8f4a3f931c72aae # Parent 83a60b321c15f6a229c43659ed52a893aae729b7 Busybox: patch fix diff -r 83a60b321c15 -r 4b37234fc0be busybox/receipt --- a/busybox/receipt Wed Feb 27 16:38:56 2008 +0100 +++ b/busybox/receipt Thu Feb 28 12:30:56 2008 +0000 @@ -16,6 +16,7 @@ { patch -p0 < stuff/$PACKAGE-$VERSION-hexdump.u patch -p0 < stuff/$PACKAGE-$VERSION-df.u + patch -p0 < stuff/$PACKAGE-$VERSION-patch.u patch -p0 < stuff/$PACKAGE-$VERSION-script.u cp stuff/$PACKAGE-$VERSION.config $PACKAGE-$VERSION/.config cd $PACKAGE-$VERSION diff -r 83a60b321c15 -r 4b37234fc0be busybox/stuff/busybox-1.7.3-patch.u --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/busybox/stuff/busybox-1.7.3-patch.u Thu Feb 28 12:30:56 2008 +0000 @@ -0,0 +1,258 @@ +--- busybox-1.7.3/editors/patch.c ++++ busybox-1.7.3/editors/patch.c +@@ -23,11 +23,9 @@ + + #include "libbb.h" + +-static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count) ++static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, unsigned int lines_count) + { +- unsigned int i = 0; +- +- while (src_stream && (i < lines_count)) { ++ while (src_stream && lines_count) { + char *line; + line = xmalloc_fgets(src_stream); + if (line == NULL) { +@@ -37,10 +35,9 @@ + bb_perror_msg_and_die("error writing to new file"); + } + free(line); +- +- i++; ++ lines_count--; + } +- return i; ++ return lines_count; + } + + /* If patch_level is -1 it will remove all directory names +@@ -49,26 +46,24 @@ + * returns malloc'ed filename + */ + +-static char *extract_filename(char *line, int patch_level) ++static char *extract_filename(char *line, unsigned int patch_level) + { + char *temp, *filename_start_ptr = line + 4; +- int i; + + /* Terminate string at end of source filename */ +- temp = strchrnul(filename_start_ptr, '\t'); +- *temp = '\0'; ++ line[strcspn(line,"\t\n")] = '\0'; + + /* Skip over (patch_level) number of leading directories */ +- if (patch_level == -1) +- patch_level = INT_MAX; +- for (i = 0; i < patch_level; i++) { ++ while (patch_level--) { + temp = strchr(filename_start_ptr, '/'); + if (!temp) + break; + filename_start_ptr = temp + 1; + } + +- return xstrdup(filename_start_ptr); ++ temp = xstrdup(filename_start_ptr); ++ free(line); ++ return temp; + } + + static int file_doesnt_exist(const char *filename) +@@ -82,22 +77,19 @@ + { + int patch_level = -1; + char *patch_line; +- int ret; +- FILE *patch_file = NULL; ++ FILE *patch_file = stdin; + + { + char *p, *i; +- ret = getopt32(argv, "p:i:", &p, &i); ++ int ret = getopt32(argv, "p:i:", &p, &i); + if (ret & 1) + patch_level = xatol_range(p, -1, USHRT_MAX); + if (ret & 2) { + patch_file = xfopen(i, "r"); +- } else { +- patch_file = stdin; + } +- ret = 0; + } + ++ xfunc_error_retval = 2; + patch_line = xmalloc_getline(patch_file); + while (patch_line) { + FILE *src_stream; +@@ -122,18 +114,14 @@ + /* FIXME: patch_line NULL check?? */ + + /* Extract the filename used before the patch was generated */ +- original_filename = extract_filename(patch_line, patch_level); +- free(patch_line); ++ original_filename = extract_filename(patch_line, (unsigned int) patch_level); + + patch_line = xmalloc_getline(patch_file); + /* FIXME: NULL check?? */ + if (strncmp(patch_line, "+++ ", 4) != 0) { +- ret = 2; +- bb_error_msg("invalid patch"); +- continue; ++ bb_error_msg_and_die("invalid patch: %s\n",patch_line); + } +- new_filename = extract_filename(patch_line, patch_level); +- free(patch_line); ++ new_filename = extract_filename(patch_line, (unsigned int) patch_level); + + if (file_doesnt_exist(new_filename)) { + char *line_ptr; +@@ -144,7 +132,6 @@ + bb_make_directory(new_filename, -1, FILEUTILS_RECUR); + *line_ptr = '/'; + } +- dst_stream = xfopen(new_filename, "w+"); + backup_filename = NULL; + } else { + backup_filename = xmalloc(strlen(new_filename) + 6); +@@ -154,12 +141,11 @@ + bb_perror_msg_and_die("cannot create file %s", + backup_filename); + } +- dst_stream = xfopen(new_filename, "w"); + } + +- if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) { +- src_stream = NULL; +- } else { ++ dst_stream = xfopen(new_filename, "w"); ++ src_stream = NULL; ++ if (!file_doesnt_exist(original_filename)) { + if (strcmp(original_filename, new_filename) == 0) { + src_stream = xfopen(backup_filename, "r"); + } else { +@@ -174,54 +160,59 @@ + while (patch_line) { + unsigned int count; + unsigned int src_beg_line; ++ unsigned int src_last_line = 1; + unsigned int unused; + unsigned int hunk_offset_start = 0; +- int hunk_error = 0; + + /* This bit should be improved */ +- if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) && +- (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) && +- (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) { ++ if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, ++ &src_last_line, &dest_beg_line, &unused) != 4) && ++ (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, ++ &src_last_line, &dest_beg_line) != 3) && ++ (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, ++ &dest_beg_line, &unused) != 3) && ++ (sscanf(patch_line, "@@ -%d +%d @@", &src_beg_line, ++ &dest_beg_line) != 2)) { + /* No more hunks for this file */ + break; + } +- free(patch_line); + hunk_count++; + + if (src_beg_line && dest_beg_line) { + /* Copy unmodified lines upto start of hunk */ + /* src_beg_line will be 0 if its a new file */ + count = src_beg_line - src_cur_line; +- if (copy_lines(src_stream, dst_stream, count) != count) { ++ if (copy_lines(src_stream, dst_stream, count)) { + bb_error_msg_and_die("bad src file"); + } + src_cur_line += count; + dest_cur_line += count; + copy_trailing_lines_flag = 1; + } +- hunk_offset_start = src_cur_line; ++ src_last_line += hunk_offset_start = src_cur_line; + +- while ((patch_line = xmalloc_fgets(patch_file)) != NULL) { ++ for (free(patch_line); ++ (patch_line = xmalloc_fgets(patch_file)) != NULL; ++ free(patch_line)) { + if ((*patch_line == '-') || (*patch_line == ' ')) { + char *src_line = NULL; ++ if (src_cur_line == src_last_line) break; + if (src_stream) { + src_line = xmalloc_fgets(src_stream); +- if (!src_line) { +- hunk_error++; +- break; +- } else { ++ if (src_line) { ++ int diff = strcmp(src_line, patch_line + 1); + src_cur_line++; ++ free(src_line); ++ if (diff) { ++ src_line = NULL; ++ } + } +- if (strcmp(src_line, patch_line + 1) != 0) { +- bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start); +- hunk_error++; +- free(patch_line); +- /* Probably need to find next hunk, etc... */ +- /* but for now we just bail out */ +- patch_line = NULL; ++ if (!src_line) { ++ bb_error_msg("hunk #%d FAILED at %d", ++ hunk_count, hunk_offset_start); ++ bad_hunk_count++; + break; + } +- free(src_line); + } + if (*patch_line == ' ') { + fputs(patch_line + 1, dst_stream); +@@ -233,16 +224,12 @@ + } else { + break; + } +- free(patch_line); +- } +- if (hunk_error) { +- bad_hunk_count++; + } + } + + /* Cleanup last patched file */ + if (copy_trailing_lines_flag) { +- copy_lines(src_stream, dst_stream, -1); ++ copy_lines(src_stream, dst_stream, (unsigned int) -1); + } + if (src_stream) { + fclose(src_stream); +@@ -251,16 +238,14 @@ + fclose(dst_stream); + } + if (bad_hunk_count) { +- if (!ret) { +- ret = 1; +- } + bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count); ++ return 1; + } else { + /* It worked, we can remove the backup */ + if (backup_filename) { + unlink(backup_filename); + } +- if ((dest_cur_line == 0) || (dest_beg_line == 0)) { ++ if (dest_cur_line == 0) { + /* The new patched file is empty, remove it */ + xunlink(new_filename); + if (strcmp(new_filename, original_filename) != 0) +@@ -273,5 +258,5 @@ + * 1 = Some hunks failed + * 2 = More serious problems + */ +- return ret; ++ return 0; + } diff -r 83a60b321c15 -r 4b37234fc0be busybox/stuff/busybox-1.7.3-script.u --- a/busybox/stuff/busybox-1.7.3-script.u Wed Feb 27 16:38:56 2008 +0100 +++ b/busybox/stuff/busybox-1.7.3-script.u Thu Feb 28 12:30:56 2008 +0000 @@ -355,4 +355,3 @@ + #define sed_trivial_usage \ "[-efinr] pattern [files...]" -patch bug...