wok-6.x diff busybox/stuff/busybox-1.7.3-patch.u @ rev 287

Busybox: patch fix
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Feb 28 12:30:56 2008 +0000 (2008-02-28)
parents
children f6fa4501fd59
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/busybox/stuff/busybox-1.7.3-patch.u	Thu Feb 28 12:30:56 2008 +0000
     1.3 @@ -0,0 +1,258 @@
     1.4 +--- busybox-1.7.3/editors/patch.c
     1.5 ++++ busybox-1.7.3/editors/patch.c
     1.6 +@@ -23,11 +23,9 @@
     1.7 + 
     1.8 + #include "libbb.h"
     1.9 + 
    1.10 +-static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
    1.11 ++static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, unsigned int lines_count)
    1.12 + {
    1.13 +-	unsigned int i = 0;
    1.14 +-
    1.15 +-	while (src_stream && (i < lines_count)) {
    1.16 ++	while (src_stream && lines_count) {
    1.17 + 		char *line;
    1.18 + 		line = xmalloc_fgets(src_stream);
    1.19 + 		if (line == NULL) {
    1.20 +@@ -37,10 +35,9 @@
    1.21 + 			bb_perror_msg_and_die("error writing to new file");
    1.22 + 		}
    1.23 + 		free(line);
    1.24 +-
    1.25 +-		i++;
    1.26 ++		lines_count--;
    1.27 + 	}
    1.28 +-	return i;
    1.29 ++	return lines_count;
    1.30 + }
    1.31 + 
    1.32 + /* If patch_level is -1 it will remove all directory names
    1.33 +@@ -49,26 +46,24 @@
    1.34 +  * returns malloc'ed filename
    1.35 +  */
    1.36 + 
    1.37 +-static char *extract_filename(char *line, int patch_level)
    1.38 ++static char *extract_filename(char *line, unsigned int patch_level)
    1.39 + {
    1.40 + 	char *temp, *filename_start_ptr = line + 4;
    1.41 +-	int i;
    1.42 + 
    1.43 + 	/* Terminate string at end of source filename */
    1.44 +-	temp = strchrnul(filename_start_ptr, '\t');
    1.45 +-	*temp = '\0';
    1.46 ++	line[strcspn(line,"\t\n")] = '\0';
    1.47 + 
    1.48 + 	/* Skip over (patch_level) number of leading directories */
    1.49 +-	if (patch_level == -1)
    1.50 +-		patch_level = INT_MAX;
    1.51 +-	for (i = 0; i < patch_level; i++) {
    1.52 ++	while (patch_level--) {
    1.53 + 		temp = strchr(filename_start_ptr, '/');
    1.54 + 		if (!temp)
    1.55 + 			break;
    1.56 + 		filename_start_ptr = temp + 1;
    1.57 + 	}
    1.58 + 
    1.59 +-	return xstrdup(filename_start_ptr);
    1.60 ++	temp = xstrdup(filename_start_ptr);
    1.61 ++	free(line);
    1.62 ++	return temp;
    1.63 + }
    1.64 + 
    1.65 + static int file_doesnt_exist(const char *filename)
    1.66 +@@ -82,22 +77,19 @@
    1.67 + {
    1.68 + 	int patch_level = -1;
    1.69 + 	char *patch_line;
    1.70 +-	int ret;
    1.71 +-	FILE *patch_file = NULL;
    1.72 ++	FILE *patch_file = stdin;
    1.73 + 
    1.74 + 	{
    1.75 + 		char *p, *i;
    1.76 +-		ret = getopt32(argv, "p:i:", &p, &i);
    1.77 ++		int ret = getopt32(argv, "p:i:", &p, &i);
    1.78 + 		if (ret & 1)
    1.79 + 			patch_level = xatol_range(p, -1, USHRT_MAX);
    1.80 + 		if (ret & 2) {
    1.81 + 			patch_file = xfopen(i, "r");
    1.82 +-		} else {
    1.83 +-			patch_file = stdin;
    1.84 + 		}
    1.85 +-		ret = 0;
    1.86 + 	}
    1.87 + 
    1.88 ++	xfunc_error_retval = 2;
    1.89 + 	patch_line = xmalloc_getline(patch_file);
    1.90 + 	while (patch_line) {
    1.91 + 		FILE *src_stream;
    1.92 +@@ -122,18 +114,14 @@
    1.93 + 		/* FIXME: patch_line NULL check?? */
    1.94 + 
    1.95 + 		/* Extract the filename used before the patch was generated */
    1.96 +-		original_filename = extract_filename(patch_line, patch_level);
    1.97 +-		free(patch_line);
    1.98 ++		original_filename = extract_filename(patch_line, (unsigned int) patch_level);
    1.99 + 
   1.100 + 		patch_line = xmalloc_getline(patch_file);
   1.101 + 		/* FIXME: NULL check?? */
   1.102 + 		if (strncmp(patch_line, "+++ ", 4) != 0) {
   1.103 +-			ret = 2;
   1.104 +-			bb_error_msg("invalid patch");
   1.105 +-			continue;
   1.106 ++			bb_error_msg_and_die("invalid patch: %s\n",patch_line);
   1.107 + 		}
   1.108 +-		new_filename = extract_filename(patch_line, patch_level);
   1.109 +-		free(patch_line);
   1.110 ++		new_filename = extract_filename(patch_line, (unsigned int) patch_level);
   1.111 + 
   1.112 + 		if (file_doesnt_exist(new_filename)) {
   1.113 + 			char *line_ptr;
   1.114 +@@ -144,7 +132,6 @@
   1.115 + 				bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
   1.116 + 				*line_ptr = '/';
   1.117 + 			}
   1.118 +-			dst_stream = xfopen(new_filename, "w+");
   1.119 + 			backup_filename = NULL;
   1.120 + 		} else {
   1.121 + 			backup_filename = xmalloc(strlen(new_filename) + 6);
   1.122 +@@ -154,12 +141,11 @@
   1.123 + 				bb_perror_msg_and_die("cannot create file %s",
   1.124 + 						backup_filename);
   1.125 + 			}
   1.126 +-			dst_stream = xfopen(new_filename, "w");
   1.127 + 		}
   1.128 + 
   1.129 +-		if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
   1.130 +-			src_stream = NULL;
   1.131 +-		} else {
   1.132 ++ 		dst_stream = xfopen(new_filename, "w");
   1.133 ++ 		src_stream = NULL;
   1.134 ++ 		if (!file_doesnt_exist(original_filename)) {
   1.135 + 			if (strcmp(original_filename, new_filename) == 0) {
   1.136 + 				src_stream = xfopen(backup_filename, "r");
   1.137 + 			} else {
   1.138 +@@ -174,54 +160,59 @@
   1.139 + 		while (patch_line) {
   1.140 + 			unsigned int count;
   1.141 + 			unsigned int src_beg_line;
   1.142 ++			unsigned int src_last_line = 1;
   1.143 + 			unsigned int unused;
   1.144 + 			unsigned int hunk_offset_start = 0;
   1.145 +-			int hunk_error = 0;
   1.146 + 
   1.147 + 			/* This bit should be improved */
   1.148 +-			if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
   1.149 +-				(sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
   1.150 +-				(sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
   1.151 ++			if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, 
   1.152 ++						&src_last_line, &dest_beg_line, &unused) != 4) &&
   1.153 ++				(sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, 
   1.154 ++						&src_last_line, &dest_beg_line) != 3) &&
   1.155 ++				(sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line,
   1.156 ++						&dest_beg_line, &unused) != 3) &&
   1.157 ++				(sscanf(patch_line, "@@ -%d +%d @@", &src_beg_line,
   1.158 ++						&dest_beg_line) != 2)) {
   1.159 + 				/* No more hunks for this file */
   1.160 + 				break;
   1.161 + 			}
   1.162 +-			free(patch_line);
   1.163 + 			hunk_count++;
   1.164 + 
   1.165 + 			if (src_beg_line && dest_beg_line) {
   1.166 + 				/* Copy unmodified lines upto start of hunk */
   1.167 + 				/* src_beg_line will be 0 if its a new file */
   1.168 + 				count = src_beg_line - src_cur_line;
   1.169 +-				if (copy_lines(src_stream, dst_stream, count) != count) {
   1.170 ++				if (copy_lines(src_stream, dst_stream, count)) {
   1.171 + 					bb_error_msg_and_die("bad src file");
   1.172 + 				}
   1.173 + 				src_cur_line += count;
   1.174 + 				dest_cur_line += count;
   1.175 + 				copy_trailing_lines_flag = 1;
   1.176 + 			}
   1.177 +-			hunk_offset_start = src_cur_line;
   1.178 ++			src_last_line += hunk_offset_start = src_cur_line;
   1.179 + 
   1.180 +-			while ((patch_line = xmalloc_fgets(patch_file)) != NULL) {
   1.181 ++			for (free(patch_line); 
   1.182 ++				 (patch_line = xmalloc_fgets(patch_file)) != NULL;
   1.183 ++				 free(patch_line)) {
   1.184 + 				if ((*patch_line == '-') || (*patch_line == ' ')) {
   1.185 + 					char *src_line = NULL;
   1.186 ++					if (src_cur_line == src_last_line) break;
   1.187 + 					if (src_stream) {
   1.188 + 						src_line = xmalloc_fgets(src_stream);
   1.189 +-						if (!src_line) {
   1.190 +-							hunk_error++;
   1.191 +-							break;
   1.192 +-						} else {
   1.193 ++						if (src_line) {
   1.194 ++							int diff = strcmp(src_line, patch_line + 1);
   1.195 + 							src_cur_line++;
   1.196 ++							free(src_line);
   1.197 ++							if (diff) {
   1.198 ++								src_line = NULL;
   1.199 ++							}
   1.200 + 						}
   1.201 +-						if (strcmp(src_line, patch_line + 1) != 0) {
   1.202 +-							bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start);
   1.203 +-							hunk_error++;
   1.204 +-							free(patch_line);
   1.205 +-							/* Probably need to find next hunk, etc... */
   1.206 +-							/* but for now we just bail out */
   1.207 +-							patch_line = NULL;
   1.208 ++						if (!src_line) {
   1.209 ++							bb_error_msg("hunk #%d FAILED at %d",
   1.210 ++										 hunk_count, hunk_offset_start);
   1.211 ++							bad_hunk_count++;
   1.212 + 							break;
   1.213 + 						}
   1.214 +-						free(src_line);
   1.215 + 					}
   1.216 + 					if (*patch_line == ' ') {
   1.217 + 						fputs(patch_line + 1, dst_stream);
   1.218 +@@ -233,16 +224,12 @@
   1.219 + 				} else {
   1.220 + 					break;
   1.221 + 				}
   1.222 +-				free(patch_line);
   1.223 +-			}
   1.224 +-			if (hunk_error) {
   1.225 +-				bad_hunk_count++;
   1.226 + 			}
   1.227 + 		}
   1.228 + 
   1.229 + 		/* Cleanup last patched file */
   1.230 + 		if (copy_trailing_lines_flag) {
   1.231 +-			copy_lines(src_stream, dst_stream, -1);
   1.232 ++			copy_lines(src_stream, dst_stream, (unsigned int) -1);
   1.233 + 		}
   1.234 + 		if (src_stream) {
   1.235 + 			fclose(src_stream);
   1.236 +@@ -251,16 +238,14 @@
   1.237 + 			fclose(dst_stream);
   1.238 + 		}
   1.239 + 		if (bad_hunk_count) {
   1.240 +-			if (!ret) {
   1.241 +-				ret = 1;
   1.242 +-			}
   1.243 + 			bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
   1.244 ++			return 1;
   1.245 + 		} else {
   1.246 + 			/* It worked, we can remove the backup */
   1.247 + 			if (backup_filename) {
   1.248 + 				unlink(backup_filename);
   1.249 + 			}
   1.250 +-			if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
   1.251 ++			if (dest_cur_line == 0) {
   1.252 + 				/* The new patched file is empty, remove it */
   1.253 + 				xunlink(new_filename);
   1.254 + 				if (strcmp(new_filename, original_filename) != 0)
   1.255 +@@ -273,5 +258,5 @@
   1.256 + 	 * 1 = Some hunks failed
   1.257 + 	 * 2 = More serious problems
   1.258 + 	 */
   1.259 +-	return ret;
   1.260 ++	return 0;
   1.261 + }