aboutsummaryrefslogtreecommitdiff
path: root/toys/patch.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2008-04-08 22:59:18 -0500
committerRob Landley <rob@landley.net>2008-04-08 22:59:18 -0500
commitd5928d56bc383eac6427f63d2c8f8f1819281b1b (patch)
tree873da371789dcc3eb7d8f33a25d0c83d11fbf4cb /toys/patch.c
parent52b499c816e77aa4ebb66ab959ccd8aaa0982ccb (diff)
downloadtoybox-d5928d56bc383eac6427f63d2c8f8f1819281b1b.tar.gz
Teach patch that a file dated 1969-12-31 means doesn't exist, and to fail
if a create isn't exclusive.
Diffstat (limited to 'toys/patch.c')
-rw-r--r--toys/patch.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/toys/patch.c b/toys/patch.c
index 612b572b..10615e3e 100644
--- a/toys/patch.c
+++ b/toys/patch.c
@@ -40,7 +40,8 @@ config PATCH
a file when all all hunks to that file apply. Patch prints failed
hunks to stderr, and exits with nonzero status if any hunks fail.
- A file compared against /dev/null is created/deleted as appropriate.
+ A file compared against /dev/null (or with a date in 1969) is
+ created/deleted as appropriate.
*/
#include "toys.h"
@@ -211,9 +212,12 @@ void patch_main(void)
// Trim date from end of filename (if any). We don't care.
for (s = patchline+4; *s && *s!='\t'; s++)
if (*s=='\\' && s[1]) s++;
- *s = 0;
-
- TT.oldname = xstrdup(patchline+4);
+ if (!strncmp(s, "\t1969-12-31", 10))
+ TT.oldname = xstrdup("/dev/null");
+ else {
+ *s = 0;
+ TT.oldname = xstrdup(patchline+4);
+ }
} else if (!strncmp("+++ ", patchline, 4)) {
int i = 0, del = 0;
char *s, *start;
@@ -221,13 +225,12 @@ void patch_main(void)
finish_oldfile();
// Trim date from end of filename (if any). We don't care.
- for (s = patchline+4; *s && *s!='\t'; s++)
+ for (s = start = patchline+4; *s && *s!='\t'; s++)
if (*s=='\\' && s[1]) s++;
+ if (!strncmp(s, "\t1969-12-31", 10)) start = "/dev/null";
*s = 0;
-
// If new file is /dev/null (before -p), we're deleting oldname
- start = patchline+4;
if (!strcmp(start, "/dev/null")) {
start = TT.oldname;
del++;
@@ -254,7 +257,7 @@ void patch_main(void)
xmkpath(start, -1);
*s = '/';
}
- TT.filein = xcreate(start, O_CREAT|O_RDWR, 0666);
+ TT.filein = xcreate(start, O_CREAT|O_EXCL|O_RDWR, 0666);
} else {
printf("patching %s\n", start);
TT.filein = xopen(start, O_RDWR);