From d5928d56bc383eac6427f63d2c8f8f1819281b1b Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 8 Apr 2008 22:59:18 -0500 Subject: Teach patch that a file dated 1969-12-31 means doesn't exist, and to fail if a create isn't exclusive. --- toys/patch.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'toys/patch.c') 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); -- cgit v1.2.3