1 /* $NetBSD: inp.c,v 1.18 2006/04/09 19:06:34 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, Larry Wall 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following condition 8 * is met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this condition and the following disclaimer. 11 * 12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22 * SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 #ifndef lint 27 __RCSID("$NetBSD: inp.c,v 1.18 2006/04/09 19:06:34 christos Exp $"); 28 #endif /* not lint */ 29 30 #include "EXTERN.h" 31 #include "backupfile.h" 32 #include "common.h" 33 #include "util.h" 34 #include "pch.h" 35 #include "INTERN.h" 36 #include "inp.h" 37 38 #include <stdlib.h> 39 #include <unistd.h> 40 #include <fcntl.h> 41 42 static void plan_a(char *); 43 static bool rev_in_string(char *); 44 45 /* Input-file-with-indexable-lines abstract type. */ 46 47 static size_t i_size; /* Size of the input file */ 48 static char *i_womp; /* Plan a buffer for entire file */ 49 static char **i_ptr; /* Pointers to lines in i_womp */ 50 51 /* 52 * New patch -- prepare to edit another file. 53 */ 54 void 55 re_input(void) 56 { 57 i_size = 0; 58 59 if (i_ptr != NULL) 60 free(i_ptr); 61 if (i_womp != NULL) 62 free(i_womp); 63 i_womp = NULL; 64 i_ptr = NULL; 65 } 66 67 /* 68 * Construct the line index, somehow or other. 69 */ 70 void 71 scan_input(char *filename) 72 { 73 plan_a(filename); 74 if (verbose) 75 say("Patching file %s using Plan A...\n", filename); 76 } 77 78 /* 79 * Try keeping everything in memory. 80 */ 81 static void 82 plan_a(char *filename) 83 { 84 int ifd, statfailed; 85 char *s; 86 LINENUM iline; 87 char lbuf[MAXLINELEN]; 88 89 statfailed = stat(filename, &filestat); 90 if (statfailed && ok_to_create_file) { 91 int fd; 92 if (verbose) 93 say("(Creating file %s...)\n",filename); 94 makedirs(filename, TRUE); 95 if ((fd = open(filename, O_CREAT|O_TRUNC, 0666)) == -1) 96 pfatal("can't open file %s", filename); 97 statfailed = fstat(fd, &filestat); 98 (void)close(fd); 99 } 100 /* 101 * For nonexistent or read-only files, look for RCS or SCCS 102 * versions. 103 */ 104 if (statfailed || 105 /* No one can write to it. */ 106 (filestat.st_mode & 0222) == 0 || 107 /* I can't write to it. */ 108 ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) { 109 struct stat cstat; 110 const char *cs = NULL; 111 char *filebase; 112 size_t pathlen; 113 114 filebase = basename(filename); 115 pathlen = filebase - filename; 116 117 /* 118 * Put any leading path into `s'. 119 * Leave room in lbuf for the diff command. 120 */ 121 s = lbuf + 20; 122 if (pathlen >= sizeof(lbuf) - 20) 123 fatal("file name too long\n"); 124 strncpy(s, filename, pathlen); 125 s[pathlen] = '\0'; 126 127 #define try(f, a1, a2) (snprintf(s + pathlen, sizeof(lbuf) - (s + pathlen - lbuf), f, a1, a2), stat(s, &cstat) == 0) 128 #define try1(f, a1) (snprintf(s + pathlen, sizeof(lbuf) - (s + pathlen - lbuf), f, a1), stat(s, &cstat) == 0) 129 if (try("RCS/%s%s", filebase, RCSSUFFIX) || 130 try1("RCS/%s" , filebase) || 131 try("%s%s", filebase, RCSSUFFIX)) { 132 snprintf(buf, sizeof(buf), CHECKOUT, filename); 133 snprintf(lbuf, sizeof(lbuf), RCSDIFF, filename); 134 cs = "RCS"; 135 } else if (try("SCCS/%s%s", SCCSPREFIX, filebase) || 136 try("%s%s", SCCSPREFIX, filebase)) { 137 snprintf(buf, sizeof(buf), GET, s); 138 snprintf(lbuf, sizeof(lbuf), SCCSDIFF, s, filename); 139 cs = "SCCS"; 140 } else if (statfailed) 141 fatal("can't find %s\n", filename); 142 /* 143 * else we can't write to it but it's not under a version 144 * control system, so just proceed. 145 */ 146 if (cs) { 147 if (!statfailed) { 148 if ((filestat.st_mode & 0222) != 0) 149 /* The owner can write to it. */ 150 fatal( 151 "file %s seems to be locked by somebody else under %s\n", 152 filename, cs); 153 /* 154 * It might be checked out unlocked. See if 155 * it's safe to check out the default version 156 * locked. 157 */ 158 if (verbose) 159 say( 160 "Comparing file %s to default %s version...\n", 161 filename, cs); 162 if (system(lbuf)) 163 fatal( 164 "can't check out file %s: differs from default %s version\n", 165 filename, cs); 166 } 167 if (verbose) 168 say("Checking out file %s from %s...\n", 169 filename, cs); 170 if (system(buf) || stat(filename, &filestat)) 171 fatal("can't check out file %s from %s\n", 172 filename, cs); 173 } 174 } 175 if (old_file_is_dev_null && 176 ok_to_create_file && 177 (filestat.st_size != 0)) { 178 fatal( 179 "patch creates new file but existing file %s not empty\n", 180 filename); 181 } 182 183 filemode = filestat.st_mode; 184 if (!S_ISREG(filemode)) 185 fatal("%s is not a normal file--can't patch\n", filename); 186 i_size = filestat.st_size; 187 188 i_womp = xmalloc(i_size + 2); 189 if ((ifd = open(filename, 0)) < 0) 190 pfatal("can't open file %s", filename); 191 if (read(ifd, i_womp, i_size) != i_size) 192 pfatal("read error"); 193 Close(ifd); 194 if (i_size && i_womp[i_size - 1] != '\n') 195 i_womp[i_size++] = '\n'; 196 i_womp[i_size] = '\0'; 197 198 /* 199 * Count the lines in the buffer so we know how many pointers we 200 * need. 201 */ 202 iline = 0; 203 for (s = i_womp; *s; s++) { 204 if (*s == '\n') 205 iline++; 206 } 207 i_ptr = xmalloc((iline + 2) * sizeof(char *)); 208 209 /* Now scan the buffer and build pointer array. */ 210 iline = 1; 211 i_ptr[iline] = i_womp; 212 for (s = i_womp; *s; s++) { 213 if (*s == '\n') { 214 /* These are NOT null terminated. */ 215 i_ptr[++iline] = s + 1; 216 } 217 } 218 input_lines = iline - 1; 219 220 /* Now check for revision, if any. */ 221 if (revision != NULL) { 222 if (!rev_in_string(i_womp)) { 223 if (force) { 224 if (verbose) 225 say( 226 "Warning: this file doesn't appear to be the %s version--patching anyway.\n", 227 revision); 228 } else if (batch) { 229 fatal( 230 "this file doesn't appear to be the %s version--aborting.\n", revision); 231 } else { 232 ask( 233 "This file doesn't appear to be the %s version--patch anyway? [n] ", 234 revision); 235 if (*buf != 'y') 236 fatal("aborted\n"); 237 } 238 } else if (verbose) 239 say("Good. This file appears to be the %s version.\n", 240 revision); 241 } 242 } 243 244 /* 245 * Fetch a line from the input file, \n terminated, not necessarily \0. 246 */ 247 const char * 248 ifetch(LINENUM line) 249 { 250 if (line < 1 || line > input_lines) 251 return ""; 252 253 return i_ptr[line]; 254 } 255 256 /* 257 * True if the string argument contains the revision number we want. 258 */ 259 static bool 260 rev_in_string(char *string) 261 { 262 char *s; 263 size_t patlen; 264 265 if (revision == NULL) 266 return TRUE; 267 patlen = strlen(revision); 268 if (strnEQ(string,revision,patlen) && 269 isspace((unsigned char)string[patlen])) 270 return TRUE; 271 for (s = string; *s; s++) { 272 if (isspace((unsigned char)*s) && 273 strnEQ(s + 1, revision, patlen) && 274 isspace((unsigned char)s[patlen + 1] )) { 275 return TRUE; 276 } 277 } 278 return FALSE; 279 } 280