Mercurial > hg > toybox
comparison toys/patch.c @ 377:15ca7e49b6ff
The @@ -1,2 +3,4 @@ lines treat ,1 as implied, so the format isn't regular.
(Yes, this was designed by the FSF, what gave it away?)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 21 May 2011 22:54:21 -0500 |
parents | 35c8beb54800 |
children | 56d07d82e691 |
comparison
equal
deleted
inserted
replaced
376:35c8beb54800 | 377:15ca7e49b6ff |
---|---|
49 DEFINE_GLOBALS( | 49 DEFINE_GLOBALS( |
50 char *infile; | 50 char *infile; |
51 long prefix; | 51 long prefix; |
52 | 52 |
53 struct double_list *current_hunk; | 53 struct double_list *current_hunk; |
54 long oldline, oldlen, newline, newlen, linenum; | 54 long oldline, oldlen, newline, newlen; |
55 long linenum; | |
55 int context, state, filein, fileout, filepatch, hunknum; | 56 int context, state, filein, fileout, filepatch, hunknum; |
56 char *tempname; | 57 char *tempname; |
57 ) | 58 ) |
58 | 59 |
59 #define TT this.patch | 60 #define TT this.patch |
306 // We defer actually opening the file because svn produces broken | 307 // We defer actually opening the file because svn produces broken |
307 // patches that don't signal they want to create a new file the | 308 // patches that don't signal they want to create a new file the |
308 // way the patch man page says, so you have to read the first hunk | 309 // way the patch man page says, so you have to read the first hunk |
309 // and _guess_. | 310 // and _guess_. |
310 | 311 |
311 // Start a new hunk? | 312 // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@ |
313 // but a missing ,value means the value is 1. | |
312 } else if (state == 1 && !strncmp("@@ -", patchline, 4)) { | 314 } else if (state == 1 && !strncmp("@@ -", patchline, 4)) { |
313 int i; | 315 int i; |
314 | 316 char *s = patchline+4; |
315 i = sscanf(patchline+4, "%ld,%ld +%ld,%ld", &TT.oldline, | 317 |
316 &TT.oldlen, &TT.newline, &TT.newlen); | 318 // Read oldline[,oldlen] +newline[,newlen] |
317 if (i != 4) | 319 |
318 error_exit("Corrupt hunk %d at %ld\n", TT.hunknum, TT.linenum); | 320 TT.oldlen = TT.newlen = 1; |
321 TT.oldline = strtol(s, &s, 10); | |
322 if (*s == ',') TT.oldlen=strtol(s+1, &s, 10); | |
323 TT.newline = strtol(s+2, &s, 10); | |
324 if (*s == ',') TT.newlen = strtol(s+1, &s, 10); | |
319 | 325 |
320 TT.context = 0; | 326 TT.context = 0; |
321 state = 2; | 327 state = 2; |
322 | 328 |
323 // If this is the first hunk, open the file. | 329 // If this is the first hunk, open the file. |
324 if (TT.filein == -1) { | 330 if (TT.filein == -1) { |
325 int oldsum, newsum, del = 0; | 331 int oldsum, newsum, del = 0; |
326 char *s, *name; | 332 char *name; |
327 | 333 |
328 oldsum = TT.oldline + TT.oldlen; | 334 oldsum = TT.oldline + TT.oldlen; |
329 newsum = TT.newline + TT.newlen; | 335 newsum = TT.newline + TT.newlen; |
330 | 336 |
331 name = reverse ? oldname : newname; | 337 name = reverse ? oldname : newname; |