- patch from Denis Vlasenko to add and use bb_xchdir()
[oweals/busybox.git] / editors / patch.c
index e3745ef39763d69a213307a134ae2e1cd2f86015..9a3740882410474165bf4ea765366dc83b798b97 100644 (file)
@@ -1,23 +1,9 @@
 /* vi: set sw=4 ts=4: */
 /*
  *  busybox patch applet to handle the unified diff format.
- *  Copyright (C) 2003 Glenn McGrath <bug1@optushome.com.au>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
+ *  Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
  *
+ *  Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  *
  *  This applet is written to work with patches generated by GNU diff,
  *  where there is equivalent functionality busybox patch shall behave
 #include <stdlib.h>
 #include <unistd.h>
 #include "busybox.h"
-#include "libbb.h"
 
-static 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, const unsigned int lines_count)
 {
-       int i = 0;
+       unsigned int i = 0;
 
        while (src_stream && (i < lines_count)) {
                char *line;
@@ -67,7 +52,7 @@ static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int li
  * returns malloc'ed filename
  */
 
-static unsigned char *extract_filename(char *line, unsigned short patch_level)
+static char *extract_filename(char *line, int patch_level)
 {
        char *filename_start_ptr = line + 4;
        int i;
@@ -103,18 +88,27 @@ static int file_doesnt_exist(const char *filename)
        return(stat(filename, &statbuf));
 }
 
-extern int patch_main(int argc, char **argv)
+int patch_main(int argc, char **argv)
 {
-       unsigned int patch_level = -1;
+       int patch_level = -1;
        char *patch_line;
-       int ret = 0;
+       int ret;
+       FILE *patch_file = NULL;
 
-       /* Handle 'p' option */
-       if (argv[1] && (argv[1][0] == '-') && (argv[1][1] == 'p')) {
-               patch_level = atoi(&argv[1][2]);
+       {
+               char *p, *i;
+               ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
+               if (ret & 1)
+                       patch_level = bb_xgetlarg(p, 10, -1, USHRT_MAX);
+               if (ret & 2) {
+                       patch_file = bb_xfopen(i, "r");
+               } else {
+                       patch_file = stdin;
+               }
+               ret = 0;
        }
 
-       patch_line = bb_get_line_from_file(stdin);
+       patch_line = bb_get_line_from_file(patch_file);
        while (patch_line) {
                FILE *src_stream;
                FILE *dst_stream;
@@ -133,14 +127,14 @@ extern int patch_main(int argc, char **argv)
                 */
                while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
                        free(patch_line);
-                       patch_line = bb_get_line_from_file(stdin);
+                       patch_line = bb_get_line_from_file(patch_file);
                }
 
                /* Extract the filename used before the patch was generated */
                original_filename = extract_filename(patch_line, patch_level);
                free(patch_line);
 
-               patch_line = bb_get_line_from_file(stdin);
+               patch_line = bb_get_line_from_file(patch_file);
                if (strncmp(patch_line, "+++ ", 4) != 0) {
                        ret = 2;
                        bb_error_msg("Invalid patch");
@@ -165,7 +159,8 @@ extern int patch_main(int argc, char **argv)
                        strcpy(backup_filename, new_filename);
                        strcat(backup_filename, ".orig");
                        if (rename(new_filename, backup_filename) == -1) {
-                               bb_perror_msg_and_die("Couldnt create file %s", backup_filename);
+                               bb_perror_msg_and_die("Couldnt create file %s",
+                                               backup_filename);
                        }
                        dst_stream = bb_xfopen(new_filename, "w");
                }
@@ -183,7 +178,7 @@ extern int patch_main(int argc, char **argv)
                printf("patching file %s\n", new_filename);
 
                /* Handle each hunk */
-               patch_line = bb_get_line_from_file(stdin);
+               patch_line = bb_get_line_from_file(patch_file);
                while (patch_line) {
                        unsigned int count;
                        unsigned int src_beg_line;
@@ -214,7 +209,7 @@ extern int patch_main(int argc, char **argv)
                        }
                        hunk_offset_start = src_cur_line;
 
-                       while ((patch_line = bb_get_line_from_file(stdin)) != NULL) {
+                       while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
                                if ((*patch_line == '-') || (*patch_line == ' ')) {
                                        char *src_line = NULL;
                                        if (src_stream) {