inp.c (286617) | inp.c (286795) |
---|---|
1/*- 2 * Copyright 1986, Larry Wall 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following condition is met: 6 * 1. Redistributions of source code must retain the above copyright notice, 7 * this condition and the following disclaimer. 8 * --- 10 unchanged lines hidden (view full) --- 19 * SUCH DAMAGE. 20 * 21 * patch - a program to apply diffs to original files 22 * 23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD 24 * behaviour 25 * 26 * $OpenBSD: inp.c,v 1.36 2012/04/10 14:46:34 ajacoutot Exp $ | 1/*- 2 * Copyright 1986, Larry Wall 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following condition is met: 6 * 1. Redistributions of source code must retain the above copyright notice, 7 * this condition and the following disclaimer. 8 * --- 10 unchanged lines hidden (view full) --- 19 * SUCH DAMAGE. 20 * 21 * patch - a program to apply diffs to original files 22 * 23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD 24 * behaviour 25 * 26 * $OpenBSD: inp.c,v 1.36 2012/04/10 14:46:34 ajacoutot Exp $ |
27 * $FreeBSD: head/usr.bin/patch/inp.c 286617 2015-08-11 05:58:33Z delphij $ | 27 * $FreeBSD: head/usr.bin/patch/inp.c 286795 2015-08-15 00:42:33Z delphij $ |
28 */ 29 30#include <sys/types.h> 31#include <sys/file.h> 32#include <sys/stat.h> 33#include <sys/mman.h> 34#include <sys/wait.h> 35 --- 96 unchanged lines hidden (view full) --- 132 return true; 133} 134 135/* Try keeping everything in memory. */ 136 137static bool 138plan_a(const char *filename) 139{ | 28 */ 29 30#include <sys/types.h> 31#include <sys/file.h> 32#include <sys/stat.h> 33#include <sys/mman.h> 34#include <sys/wait.h> 35 --- 96 unchanged lines hidden (view full) --- 132 return true; 133} 134 135/* Try keeping everything in memory. */ 136 137static bool 138plan_a(const char *filename) 139{ |
140 int ifd, statfailed, pstat; 141 char *p, *s, lbuf[INITLINELEN]; | 140 int ifd, statfailed; 141 char *p, *s; |
142 struct stat filestat; 143 ptrdiff_t sz; 144 size_t i; 145 size_t iline, lines_allocated; | 142 struct stat filestat; 143 ptrdiff_t sz; 144 size_t i; 145 size_t iline, lines_allocated; |
146 pid_t pid; | |
147 148#ifdef DEBUGGING 149 if (debug & 8) 150 return false; 151#endif 152 153 if (filename == NULL || *filename == '\0') 154 return false; --- 9 unchanged lines hidden (view full) --- 164 * to normal patch behavior as possible 165 */ 166 if (check_only) 167 return true; 168 makedirs(filename, true); 169 close(creat(filename, 0666)); 170 statfailed = stat(filename, &filestat); 171 } | 146 147#ifdef DEBUGGING 148 if (debug & 8) 149 return false; 150#endif 151 152 if (filename == NULL || *filename == '\0') 153 return false; --- 9 unchanged lines hidden (view full) --- 163 * to normal patch behavior as possible 164 */ 165 if (check_only) 166 return true; 167 makedirs(filename, true); 168 close(creat(filename, 0666)); 169 statfailed = stat(filename, &filestat); 170 } |
172 if (statfailed && check_only) 173 fatal("%s not found, -C mode, can't probe further\n", filename); 174 /* For nonexistent or read-only files, look for RCS versions. */ 175 176 if (statfailed || 177 /* No one can write to it. */ 178 (filestat.st_mode & 0222) == 0 || 179 /* I can't write to it. */ 180 ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) { 181 char *filebase, *filedir; 182 struct stat cstat; 183 char *tmp_filename1, *tmp_filename2; 184 char *argp[4] = { NULL }; 185 posix_spawn_file_actions_t file_actions; 186 187 tmp_filename1 = strdup(filename); 188 tmp_filename2 = strdup(filename); 189 if (tmp_filename1 == NULL || tmp_filename2 == NULL) 190 fatal("strdupping filename"); 191 192 filebase = basename(tmp_filename1); 193 filedir = dirname(tmp_filename2); 194 195 memset(argp, 0, sizeof(argp)); 196 197#define try(f, a1, a2, a3) \ 198 (snprintf(lbuf, sizeof(lbuf), f, a1, a2, a3), stat(lbuf, &cstat) == 0) 199 200 /* 201 * else we can't write to it but it's not under a version 202 * control system, so just proceed. 203 */ 204 if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) || 205 try("%s/RCS/%s%s", filedir, filebase, "") || 206 try("%s/%s%s", filedir, filebase, RCSSUFFIX)) { 207 if (!statfailed) { 208 if ((filestat.st_mode & 0222) != 0) 209 /* The owner can write to it. */ 210 fatal("file %s seems to be locked " 211 "by somebody else under RCS\n", 212 filename); 213 /* 214 * It might be checked out unlocked. See if 215 * it's safe to check out the default version 216 * locked. 217 */ 218 if (verbose) 219 say("Comparing file %s to default " 220 "RCS version...\n", filename); 221 222 argp[0] = __DECONST(char *, RCSDIFF); 223 argp[1] = __DECONST(char *, filename); 224 posix_spawn_file_actions_init(&file_actions); 225 posix_spawn_file_actions_addopen(&file_actions, 226 STDOUT_FILENO, _PATH_DEVNULL, O_WRONLY, 0); 227 if (posix_spawn(&pid, RCSDIFF, &file_actions, 228 NULL, argp, NULL) == 0) { 229 pid = waitpid(pid, &pstat, 0); 230 if (pid == -1 || WEXITSTATUS(pstat) != 0) 231 fatal("can't check out file %s: " 232 "differs from default RCS version\n", 233 filename); 234 } else 235 fatal("posix_spawn: %s\n", strerror(errno)); 236 posix_spawn_file_actions_destroy(&file_actions); 237 } 238 239 if (verbose) 240 say("Checking out file %s from RCS...\n", 241 filename); 242 243 argp[0] = __DECONST(char *, CHECKOUT); 244 argp[1] = __DECONST(char *, "-l"); 245 argp[2] = __DECONST(char *, filename); 246 if (posix_spawn(&pid, CHECKOUT, NULL, NULL, argp, 247 NULL) == 0) { 248 pid = waitpid(pid, &pstat, 0); 249 if (pid == -1 || WEXITSTATUS(pstat) != 0 || 250 stat(filename, &filestat)) 251 fatal("can't check out file %s from RCS\n", 252 filename); 253 } else 254 fatal("posix_spawn: %s\n", strerror(errno)); 255 } else if (statfailed) { 256 fatal("can't find %s\n", filename); 257 } 258 free(tmp_filename1); 259 free(tmp_filename2); 260 } 261 | 171 if (statfailed) 172 fatal("can't find %s\n", filename); |
262 filemode = filestat.st_mode; 263 if (!S_ISREG(filemode)) 264 fatal("%s is not a normal file--can't patch\n", filename); 265 if ((uint64_t)filestat.st_size > SIZE_MAX) { 266 say("block too large to mmap\n"); 267 return false; 268 } 269 i_size = (size_t)filestat.st_size; --- 253 unchanged lines hidden --- | 173 filemode = filestat.st_mode; 174 if (!S_ISREG(filemode)) 175 fatal("%s is not a normal file--can't patch\n", filename); 176 if ((uint64_t)filestat.st_size > SIZE_MAX) { 177 say("block too large to mmap\n"); 178 return false; 179 } 180 i_size = (size_t)filestat.st_size; --- 253 unchanged lines hidden --- |