whitespace cleanup
[oweals/busybox.git] / editors / patch.c
index f283953974730210e4e71d819f5bffc507c34648..a710d8224432bcd9d83b6f95a9de655e36decab8 100644 (file)
@@ -3,7 +3,7 @@
  *  busybox patch applet to handle the unified diff format.
  *  Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
  *
- *  Licensed under the GPL v2, see the file LICENSE in this tarball.
+ *  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
@@ -26,9 +26,9 @@
 #include <unistd.h>
 #include "busybox.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;
@@ -52,34 +52,22 @@ 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;
+       char *temp, *filename_start_ptr = line + 4;
        int i;
 
        /* Terminate string at end of source filename */
-       {
-               char *line_ptr;
-               line_ptr = strchr(filename_start_ptr, '\t');
-               if (!line_ptr) {
-                       bb_perror_msg("Malformed line %s", line);
-                       return(NULL);
-               }
-               *line_ptr = '\0';
-       }
+       temp = strchr(filename_start_ptr, '\t');
+       if (temp) *temp = 0;
 
-       /* Skip over (patch_level) number of leading directories */
+       /* skip over (patch_level) number of leading directories */
        for (i = 0; i < patch_level; i++) {
-               char *dirname_ptr;
-
-               dirname_ptr = strchr(filename_start_ptr, '/');
-               if (!dirname_ptr) {
-                       break;
-               }
-               filename_start_ptr = dirname_ptr + 1;
+               if(!(temp = strchr(filename_start_ptr, '/'))) break;
+               filename_start_ptr = temp + 1;
        }
 
-       return(bb_xstrdup(filename_start_ptr));
+       return(xstrdup(filename_start_ptr));
 }
 
 static int file_doesnt_exist(const char *filename)
@@ -88,9 +76,9 @@ 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;
        FILE *patch_file = NULL;
@@ -99,9 +87,9 @@ extern int patch_main(int argc, char **argv)
                char *p, *i;
                ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
                if (ret & 1)
-                       patch_level = atoi(p);
+                       patch_level = bb_xgetlarg(p, 10, -1, USHRT_MAX);
                if (ret & 2) {
-                       patch_file = bb_xfopen(i, "r");
+                       patch_file = xfopen(i, "r");
                } else {
                        patch_file = stdin;
                }
@@ -152,7 +140,7 @@ extern int patch_main(int argc, char **argv)
                                bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
                                *line_ptr = '/';
                        }
-                       dst_stream = bb_xfopen(new_filename, "w+");
+                       dst_stream = xfopen(new_filename, "w+");
                        backup_filename = NULL;
                } else {
                        backup_filename = xmalloc(strlen(new_filename) + 6);
@@ -162,16 +150,16 @@ extern int patch_main(int argc, char **argv)
                                bb_perror_msg_and_die("Couldnt create file %s",
                                                backup_filename);
                        }
-                       dst_stream = bb_xfopen(new_filename, "w");
+                       dst_stream = xfopen(new_filename, "w");
                }
 
                if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
                        src_stream = NULL;
                } else {
                        if (strcmp(original_filename, new_filename) == 0) {
-                               src_stream = bb_xfopen(backup_filename, "r");
+                               src_stream = xfopen(backup_filename, "r");
                        } else {
-                               src_stream = bb_xfopen(original_filename, "r");
+                               src_stream = xfopen(original_filename, "r");
                        }
                }