- stty's visible() function and catv's guts are identical. Merge them into
[oweals/busybox.git] / editors / patch.c
index 9336b275a09ea401ed545448c8c52c21d671b552..f8d7605c0d4ab707766a9dedd223b7546e035c8d 100644 (file)
@@ -15,9 +15,8 @@
  *
  *  Issues
  *   - Non-interactive
- *   - Patches must apply cleanly or the hunk will fail.
+ *   - Patches must apply cleanly or patch (not just one hunk) will fail.
  *   - Reject file isnt saved
- *   -
  */
 
 #include <getopt.h>
@@ -32,18 +31,18 @@ static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsign
 
        while (src_stream && (i < lines_count)) {
                char *line;
-               line = bb_get_line_from_file(src_stream);
+               line = xmalloc_fgets(src_stream);
                if (line == NULL) {
                        break;
                }
                if (fputs(line, dest_stream) == EOF) {
-                       bb_perror_msg_and_die("Error writing to new file");
+                       bb_perror_msg_and_die("error writing to new file");
                }
                free(line);
 
                i++;
        }
-       return(i);
+       return i;
 }
 
 /* If patch_level is -1 it will remove all directory names
@@ -67,13 +66,13 @@ static char *extract_filename(char *line, int patch_level)
                filename_start_ptr = temp + 1;
        }
 
-       return(xstrdup(filename_start_ptr));
+       return xstrdup(filename_start_ptr);
 }
 
 static int file_doesnt_exist(const char *filename)
 {
        struct stat statbuf;
-       return(stat(filename, &statbuf));
+       return stat(filename, &statbuf);
 }
 
 int patch_main(int argc, char **argv)
@@ -96,7 +95,7 @@ int patch_main(int argc, char **argv)
                ret = 0;
        }
 
-       patch_line = bb_get_line_from_file(patch_file);
+       patch_line = xmalloc_fgets(patch_file);
        while (patch_line) {
                FILE *src_stream;
                FILE *dst_stream;
@@ -115,17 +114,19 @@ 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(patch_file);
+                       patch_line = xmalloc_fgets(patch_file);
                }
+               /* 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);
 
-               patch_line = bb_get_line_from_file(patch_file);
+               patch_line = xmalloc_fgets(patch_file);
+               /* FIXME: NULL check?? */
                if (strncmp(patch_line, "+++ ", 4) != 0) {
                        ret = 2;
-                       bb_error_msg("Invalid patch");
+                       bb_error_msg("invalid patch");
                        continue;
                }
                new_filename = extract_filename(patch_line, patch_level);
@@ -166,7 +167,7 @@ int patch_main(int argc, char **argv)
                printf("patching file %s\n", new_filename);
 
                /* Handle each hunk */
-               patch_line = bb_get_line_from_file(patch_file);
+               patch_line = xmalloc_fgets(patch_file);
                while (patch_line) {
                        unsigned int count;
                        unsigned int src_beg_line;
@@ -189,7 +190,7 @@ int patch_main(int argc, char **argv)
                                /* 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) {
-                                       bb_error_msg_and_die("Bad src file");
+                                       bb_error_msg_and_die("bad src file");
                                }
                                src_cur_line += count;
                                dest_cur_line += count;
@@ -197,11 +198,11 @@ int patch_main(int argc, char **argv)
                        }
                        hunk_offset_start = src_cur_line;
 
-                       while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
+                       while ((patch_line = xmalloc_fgets(patch_file)) != NULL) {
                                if ((*patch_line == '-') || (*patch_line == ' ')) {
                                        char *src_line = NULL;
                                        if (src_stream) {
-                                               src_line = bb_get_line_from_file(src_stream);
+                                               src_line = xmalloc_fgets(src_stream);
                                                if (!src_line) {
                                                        hunk_error++;
                                                        break;
@@ -209,9 +210,12 @@ int patch_main(int argc, char **argv)
                                                        src_cur_line++;
                                                }
                                                if (strcmp(src_line, patch_line + 1) != 0) {
-                                                       bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
+                                                       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;
                                                        break;
                                                }
                                                free(src_line);
@@ -269,5 +273,5 @@ int patch_main(int argc, char **argv)
         * 1 = Some hunks failed
         * 2 = More serious problems
         */
-       return(ret);
+       return ret;
 }