wok-stable diff busybox/stuff/busybox-1.10.3-patch.u @ rev 1488

imlib2: typo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Oct 05 10:55:02 2008 +0000 (2008-10-05)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/busybox/stuff/busybox-1.10.3-patch.u	Sun Oct 05 10:55:02 2008 +0000
     1.3 @@ -0,0 +1,412 @@
     1.4 +--- busybox-1.10.3/editors/patch.c	2008-03-24 15:46:20.000000000 +0100
     1.5 ++++ busybox-1.10.3/editors/patch.c	2008-03-24 15:46:20.000000000 +0100
     1.6 +@@ -19,15 +19,11 @@
     1.7 +  *   - Reject file isnt saved
     1.8 +  */
     1.9 + 
    1.10 +-#include <getopt.h>
    1.11 +-
    1.12 + #include "libbb.h"
    1.13 + 
    1.14 +-static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
    1.15 ++static unsigned copy_lines(FILE *src_stream, FILE *dest_stream, unsigned lines_count)
    1.16 + {
    1.17 +-	unsigned int i = 0;
    1.18 +-
    1.19 +-	while (src_stream && (i < lines_count)) {
    1.20 ++	while (src_stream && lines_count) {
    1.21 + 		char *line;
    1.22 + 		line = xmalloc_fgets(src_stream);
    1.23 + 		if (line == NULL) {
    1.24 +@@ -37,60 +33,70 @@
    1.25 + 			bb_perror_msg_and_die("error writing to new file");
    1.26 + 		}
    1.27 + 		free(line);
    1.28 +-
    1.29 +-		i++;
    1.30 ++		lines_count--;
    1.31 + 	}
    1.32 +-	return i;
    1.33 ++	return lines_count;
    1.34 + }
    1.35 + 
    1.36 + /* If patch_level is -1 it will remove all directory names
    1.37 +  * char *line must be greater than 4 chars
    1.38 +  * returns NULL if the file doesnt exist or error
    1.39 +  * returns malloc'ed filename
    1.40 ++ * NB: frees 1st argument!
    1.41 +  */
    1.42 + 
    1.43 +-static char *extract_filename(char *line, int patch_level)
    1.44 ++static char *extract_filename(char *line, unsigned patch_level, const char *pat)
    1.45 + {
    1.46 +-	char *temp, *filename_start_ptr = line + 4;
    1.47 +-	int i;
    1.48 ++	char *temp = NULL, *filename_start_ptr = line + 4;
    1.49 + 
    1.50 +-	/* Terminate string at end of source filename */
    1.51 +-	temp = strchrnul(filename_start_ptr, '\t');
    1.52 +-	*temp = '\0';
    1.53 +-
    1.54 +-	/* Skip over (patch_level) number of leading directories */
    1.55 +-	if (patch_level == -1)
    1.56 +-		patch_level = INT_MAX;
    1.57 +-	for (i = 0; i < patch_level; i++) {
    1.58 +-		temp = strchr(filename_start_ptr, '/');
    1.59 +-		if (!temp)
    1.60 +-			break;
    1.61 +-		filename_start_ptr = temp + 1;
    1.62 ++	if (strncmp(line, pat, 4) == 0) {
    1.63 ++		/* Terminate string at end of source filename */
    1.64 ++		line[strcspn(line,"\t\n\r")] = '\0';
    1.65 ++
    1.66 ++		/* Skip over (patch_level) number of leading directories */
    1.67 ++		while (patch_level--) {
    1.68 ++			temp = strchr(filename_start_ptr, '/');
    1.69 ++			if (!temp)
    1.70 ++				break;
    1.71 ++			filename_start_ptr = temp + 1;
    1.72 ++		}
    1.73 ++		temp = xstrdup(filename_start_ptr);
    1.74 + 	}
    1.75 +-
    1.76 +-	return xstrdup(filename_start_ptr);
    1.77 ++	free(line);
    1.78 ++	return temp;
    1.79 + }
    1.80 + 
    1.81 + int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
    1.82 + int patch_main(int argc ATTRIBUTE_UNUSED, char **argv)
    1.83 + {
    1.84 +-	int patch_level = -1;
    1.85 +-	char *patch_line;
    1.86 +-	int ret;
    1.87 +-	FILE *patch_file = NULL;
    1.88 + 	struct stat saved_stat;
    1.89 +-	
    1.90 ++	char *patch_line;
    1.91 ++	FILE *patch_file;
    1.92 ++	int patch_level;
    1.93 ++	int ret = 0;
    1.94 ++#define ENABLE_FEATURE_PATCH_REVERSE 1
    1.95 ++#if ENABLE_FEATURE_PATCH_REVERSE
    1.96 ++	char minus = '-';
    1.97 ++	char plus = '+';
    1.98 ++#else
    1.99 ++	const char minus = '-';
   1.100 ++	const char plus = '+';
   1.101 ++#endif
   1.102 ++
   1.103 ++	xfunc_error_retval = 2;	
   1.104 + 	{
   1.105 +-		char *p, *i;
   1.106 +-		ret = getopt32(argv, "p:i:", &p, &i);
   1.107 +-		if (ret & 1)
   1.108 +-			patch_level = xatol_range(p, -1, USHRT_MAX);
   1.109 +-		if (ret & 2) {
   1.110 +-			patch_file = xfopen(i, "r");
   1.111 +-		} else {
   1.112 +-			patch_file = stdin;
   1.113 +-		}
   1.114 +-		ret = 0;
   1.115 ++		const char *p = "-1";
   1.116 ++		const char *i = "-"; /* compat */
   1.117 ++#if ENABLE_FEATURE_PATCH_REVERSE
   1.118 ++		if (getopt32(argv, "p:i:R", &p, &i) & 4) {
   1.119 ++			minus = '+';
   1.120 ++			plus = '-';
   1.121 ++		}
   1.122 ++#else
   1.123 ++		getopt32(argv, "p:i:", &p, &i);
   1.124 ++#endif
   1.125 ++		patch_level = xatoi(p); /* can be negative! */
   1.126 ++		patch_file = xfopen_stdin(i);
   1.127 + 	}
   1.128 + 
   1.129 + 	patch_line = xmalloc_getline(patch_file);
   1.130 +@@ -100,38 +106,38 @@
   1.131 + 		char *original_filename;
   1.132 + 		char *new_filename;
   1.133 + 		char *backup_filename;
   1.134 +-		unsigned int src_cur_line = 1;
   1.135 +-		unsigned int dest_cur_line = 0;
   1.136 +-		unsigned int dest_beg_line;
   1.137 +-		unsigned int bad_hunk_count = 0;
   1.138 +-		unsigned int hunk_count = 0;
   1.139 +-		char copy_trailing_lines_flag = 0;
   1.140 ++		unsigned src_cur_line = 1;
   1.141 ++		unsigned dest_cur_line = 0;
   1.142 ++		unsigned dest_beg_line;
   1.143 ++		unsigned bad_hunk_count = 0;
   1.144 ++		unsigned hunk_count = 0;
   1.145 ++		smallint copy_trailing_lines_flag = 0;
   1.146 + 
   1.147 + 		/* Skip everything upto the "---" marker
   1.148 + 		 * No need to parse the lines "Only in <dir>", and "diff <args>"
   1.149 + 		 */
   1.150 +-		while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
   1.151 +-			free(patch_line);
   1.152 ++		do {
   1.153 ++			/* Extract the filename used before the patch was generated */
   1.154 ++			original_filename = extract_filename(patch_line, patch_level, "--- ");
   1.155 + 			patch_line = xmalloc_getline(patch_file);
   1.156 +-		}
   1.157 +-		/* FIXME: patch_line NULL check?? */
   1.158 ++			if (!patch_line) goto quit;
   1.159 ++		} while (!original_filename);
   1.160 + 
   1.161 +-		/* Extract the filename used before the patch was generated */
   1.162 +-		original_filename = extract_filename(patch_line, patch_level);
   1.163 +-		free(patch_line);
   1.164 +-
   1.165 +-		patch_line = xmalloc_getline(patch_file);
   1.166 +-		/* FIXME: NULL check?? */
   1.167 +-		if (strncmp(patch_line, "+++ ", 4) != 0) {
   1.168 +-			ret = 2;
   1.169 +-			bb_error_msg("invalid patch");
   1.170 +-			continue;
   1.171 ++		new_filename = extract_filename(patch_line, patch_level, "+++ ");
   1.172 ++		if (!new_filename) {
   1.173 ++			bb_error_msg_and_die("invalid patch");
   1.174 ++		}
   1.175 ++#if ENABLE_FEATURE_PATCH_REVERSE
   1.176 ++		if (plus != '+') {
   1.177 ++			/* reverse patch */
   1.178 ++			char *tmp = original_filename;
   1.179 ++			original_filename = new_filename;
   1.180 ++			new_filename = tmp;
   1.181 + 		}
   1.182 +-		new_filename = extract_filename(patch_line, patch_level);
   1.183 +-		free(patch_line);
   1.184 ++#endif
   1.185 + 		
   1.186 + 		/* Get access rights from the file to be patched, -1 file does not exist */
   1.187 +-		if (stat(new_filename, &saved_stat)) {
   1.188 ++		if (stat(new_filename, &saved_stat) != 0) {
   1.189 + 			char *line_ptr;
   1.190 + 			/* Create leading directories */
   1.191 + 			line_ptr = strrchr(new_filename, '/');
   1.192 +@@ -140,132 +146,137 @@
   1.193 + 				bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
   1.194 + 				*line_ptr = '/';
   1.195 + 			}
   1.196 +-			dst_stream = xfopen(new_filename, "w+");
   1.197 + 			backup_filename = NULL;
   1.198 ++			saved_stat.st_mode = 0644;
   1.199 + 		} else {
   1.200 +-			backup_filename = xmalloc(strlen(new_filename) + 6);
   1.201 +-			strcpy(backup_filename, new_filename);
   1.202 +-			strcat(backup_filename, ".orig");
   1.203 ++			backup_filename = xasprintf("%s.orig", new_filename);
   1.204 + 			xrename(new_filename, backup_filename);
   1.205 +-			dst_stream = xfopen(new_filename, "w");
   1.206 +-			fchmod(fileno(dst_stream), saved_stat.st_mode);
   1.207 + 		}
   1.208 +-
   1.209 +-		if ((backup_filename == NULL) || stat(original_filename, &saved_stat)) {
   1.210 +-			src_stream = NULL;
   1.211 +-		} else {
   1.212 +-			if (strcmp(original_filename, new_filename) == 0) {
   1.213 +-				src_stream = xfopen(backup_filename, "r");
   1.214 +-			} else {
   1.215 +-				src_stream = xfopen(original_filename, "r");
   1.216 +-			}
   1.217 ++		dst_stream = xfopen(new_filename, "w");
   1.218 ++		fchmod(fileno(dst_stream), saved_stat.st_mode);
   1.219 ++		src_stream = NULL;
   1.220 ++
   1.221 ++		if (backup_filename && !stat(original_filename, &saved_stat)) {
   1.222 ++			src_stream = xfopen((strcmp(original_filename, new_filename)) ? 
   1.223 ++							    original_filename : backup_filename, "r");
   1.224 + 		}
   1.225 + 
   1.226 + 		printf("patching file %s\n", new_filename);
   1.227 + 
   1.228 +-		/* Handle each hunk */
   1.229 ++		/* Handle all hunks for this file */
   1.230 + 		patch_line = xmalloc_fgets(patch_file);
   1.231 + 		while (patch_line) {
   1.232 +-			unsigned int count;
   1.233 +-			unsigned int src_beg_line;
   1.234 +-			unsigned int unused;
   1.235 +-			unsigned int hunk_offset_start = 0;
   1.236 +-			int hunk_error = 0;
   1.237 +-
   1.238 +-			/* This bit should be improved */
   1.239 +-			if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
   1.240 +-				(sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
   1.241 +-				(sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
   1.242 ++			unsigned count;
   1.243 ++			unsigned src_beg_line;
   1.244 ++			unsigned hunk_offset_start;
   1.245 ++			unsigned src_last_line = 1;
   1.246 ++#if ENABLE_FEATURE_PATCH_REVERSE
   1.247 ++			unsigned dst_last_line = 1;
   1.248 ++
   1.249 ++			if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dest_beg_line, &dst_last_line) < 3) &&
   1.250 ++				(sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dest_beg_line, &dst_last_line) < 2)) {
   1.251 + 				/* No more hunks for this file */
   1.252 + 				break;
   1.253 + 			}
   1.254 +-			free(patch_line);
   1.255 ++			if (plus != '+') { 
   1.256 ++				/* reverse patch */
   1.257 ++				unsigned tmp = src_last_line;
   1.258 ++				src_last_line = dst_last_line;
   1.259 ++				dst_last_line = tmp;
   1.260 ++				tmp = src_beg_line;
   1.261 ++				src_beg_line = dest_beg_line;
   1.262 ++				dest_beg_line = tmp;
   1.263 ++			}
   1.264 ++#else
   1.265 ++
   1.266 ++			if ((sscanf(patch_line, "@@ -%d,%d +%d", &src_beg_line, &src_last_line, &dest_beg_line) != 3) &&
   1.267 ++				(sscanf(patch_line, "@@ -%d +%d", &src_beg_line, &dest_beg_line) != 2)) {
   1.268 ++				/* No more hunks for this file */
   1.269 ++				break;
   1.270 ++			}
   1.271 ++#endif
   1.272 + 			hunk_count++;
   1.273 + 
   1.274 + 			if (src_beg_line && dest_beg_line) {
   1.275 + 				/* Copy unmodified lines upto start of hunk */
   1.276 +-				/* src_beg_line will be 0 if its a new file */
   1.277 ++				/* src_beg_line will be 0 if it's a new file */
   1.278 + 				count = src_beg_line - src_cur_line;
   1.279 +-				if (copy_lines(src_stream, dst_stream, count) != count) {
   1.280 ++				if (copy_lines(src_stream, dst_stream, count)) {
   1.281 + 					bb_error_msg_and_die("bad src file");
   1.282 + 				}
   1.283 + 				src_cur_line += count;
   1.284 + 				dest_cur_line += count;
   1.285 + 				copy_trailing_lines_flag = 1;
   1.286 + 			}
   1.287 +-			hunk_offset_start = src_cur_line;
   1.288 +-
   1.289 +-			while ((patch_line = xmalloc_fgets(patch_file)) != NULL) {
   1.290 +-				if ((*patch_line == '-') || (*patch_line == ' ')) {
   1.291 ++			src_last_line += hunk_offset_start = src_cur_line;
   1.292 ++#if ENABLE_FEATURE_PATCH_REVERSE
   1.293 ++			dst_last_line += dest_cur_line;
   1.294 ++#endif
   1.295 ++			while (1) {
   1.296 ++				free(patch_line);
   1.297 ++			        patch_line = xmalloc_fgets(patch_file);
   1.298 ++				if (patch_line == NULL) break;
   1.299 ++				if ((*patch_line == minus) || (*patch_line == ' ')) {
   1.300 + 					char *src_line = NULL;
   1.301 ++					if (src_cur_line == src_last_line) break;
   1.302 + 					if (src_stream) {
   1.303 + 						src_line = xmalloc_fgets(src_stream);
   1.304 +-						if (!src_line) {
   1.305 +-							hunk_error++;
   1.306 +-							break;
   1.307 +-						} else {
   1.308 ++						if (src_line) {
   1.309 ++							int diff = strcmp(src_line, patch_line + 1);
   1.310 + 							src_cur_line++;
   1.311 ++							free(src_line);
   1.312 ++							if (diff) src_line = NULL;
   1.313 + 						}
   1.314 +-						if (strcmp(src_line, patch_line + 1) != 0) {
   1.315 +-							bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start);
   1.316 +-							hunk_error++;
   1.317 +-							free(patch_line);
   1.318 +-							/* Probably need to find next hunk, etc... */
   1.319 +-							/* but for now we just bail out */
   1.320 +-							patch_line = NULL;
   1.321 +-							break;
   1.322 +-						}
   1.323 +-						free(src_line);
   1.324 + 					}
   1.325 +-					if (*patch_line == ' ') {
   1.326 +-						fputs(patch_line + 1, dst_stream);
   1.327 +-						dest_cur_line++;
   1.328 ++					if (!src_line) {
   1.329 ++						bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
   1.330 ++						bad_hunk_count++;
   1.331 ++						break;
   1.332 + 					}
   1.333 +-				} else if (*patch_line == '+') {
   1.334 +-					fputs(patch_line + 1, dst_stream);
   1.335 +-					dest_cur_line++;
   1.336 +-				} else {
   1.337 ++					if (*patch_line != ' ') {
   1.338 ++						continue;
   1.339 ++					}
   1.340 ++				} else if (*patch_line != plus) {
   1.341 + 					break;
   1.342 + 				}
   1.343 +-				free(patch_line);
   1.344 +-			}
   1.345 +-			if (hunk_error) {
   1.346 +-				bad_hunk_count++;
   1.347 +-			}
   1.348 +-		}
   1.349 ++#if ENABLE_FEATURE_PATCH_REVERSE
   1.350 ++				if (dest_cur_line == dst_last_line) break;
   1.351 ++#endif
   1.352 ++				fputs(patch_line + 1, dst_stream);
   1.353 ++				dest_cur_line++;
   1.354 ++			} /* end of while loop handling one hunk */
   1.355 ++		} /* end of while loop handling one file */
   1.356 + 
   1.357 + 		/* Cleanup last patched file */
   1.358 + 		if (copy_trailing_lines_flag) {
   1.359 +-			copy_lines(src_stream, dst_stream, -1);
   1.360 ++			copy_lines(src_stream, dst_stream, (unsigned)(-1));
   1.361 + 		}
   1.362 + 		if (src_stream) {
   1.363 + 			fclose(src_stream);
   1.364 + 		}
   1.365 +-		if (dst_stream) {
   1.366 +-			fclose(dst_stream);
   1.367 +-		}
   1.368 ++		fclose(dst_stream);
   1.369 + 		if (bad_hunk_count) {
   1.370 +-			if (!ret) {
   1.371 +-				ret = 1;
   1.372 +-			}
   1.373 +-			bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
   1.374 ++			ret = 1;
   1.375 ++			bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
   1.376 + 		} else {
   1.377 + 			/* It worked, we can remove the backup */
   1.378 + 			if (backup_filename) {
   1.379 + 				unlink(backup_filename);
   1.380 ++				free(backup_filename);
   1.381 + 			}
   1.382 + 			if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
   1.383 + 				/* The new patched file is empty, remove it */
   1.384 + 				xunlink(new_filename);
   1.385 +-				if (strcmp(new_filename, original_filename) != 0)
   1.386 +-					xunlink(original_filename);
   1.387 ++				/* original_filename and new_filename may be the same file */
   1.388 ++				unlink(original_filename);
   1.389 + 			}
   1.390 + 		}
   1.391 +-	}
   1.392 ++	} /* end of "while there are patch lines" */
   1.393 ++quit:
   1.394 + 
   1.395 + 	/* 0 = SUCCESS
   1.396 + 	 * 1 = Some hunks failed
   1.397 +-	 * 2 = More serious problems
   1.398 ++	 * 2 = More serious problems (exited earlier)
   1.399 + 	 */
   1.400 + 	return ret;
   1.401 + }
   1.402 +
   1.403 +--- busybox-1.10.3/include/usage.h	2008-03-24 16:20:43.000000000 +0100
   1.404 ++++ busybox-1.10.3/include/usage.h	2008-03-24 16:22:06.000000000 +0100
   1.405 +@@ -2837,8 +2837,9 @@
   1.406 + 	)
   1.407 + 
   1.408 + #define patch_trivial_usage \
   1.409 +-       "[-p NUM] [-i DIFF]"
   1.410 ++       "[-R] [-p NUM] [-i DIFF]"
   1.411 + #define patch_full_usage \
   1.412 ++       "	-R    	Reverse patch\n" \
   1.413 +        "	-p NUM	Strip NUM leading components from file names" \
   1.414 +      "\n	-i DIFF	Read DIFF instead of stdin" \
   1.415 +