1 /* $NetBSD: inp.c,v 1.17 2003/07/30 08:51:04 itojun 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.17 2003/07/30 08:51:04 itojun 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 if (verbose) 92 say("(Creating file %s...)\n",filename); 93 makedirs(filename, TRUE); 94 close(creat(filename, 0666)); 95 statfailed = stat(filename, &filestat); 96 } 97 /* 98 * For nonexistent or read-only files, look for RCS or SCCS 99 * versions. 100 */ 101 if (statfailed || 102 /* No one can write to it. */ 103 (filestat.st_mode & 0222) == 0 || 104 /* I can't write to it. */ 105 ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) { 106 struct stat cstat; 107 const char *cs = NULL; 108 char *filebase; 109 size_t pathlen; 110 111 filebase = basename(filename); 112 pathlen = filebase - filename; 113 114 /* 115 * Put any leading path into `s'. 116 * Leave room in lbuf for the diff command. 117 */ 118 s = lbuf + 20; 119 if (pathlen >= sizeof(lbuf) - 20) 120 fatal("file name too long\n"); 121 strncpy(s, filename, pathlen); 122 s[pathlen] = '\0'; 123 124 #define try(f, a1, a2) (snprintf(s + pathlen, sizeof(lbuf) - (s + pathlen - lbuf), f, a1, a2), stat(s, &cstat) == 0) 125 #define try1(f, a1) (snprintf(s + pathlen, sizeof(lbuf) - (s + pathlen - lbuf), f, a1), stat(s, &cstat) == 0) 126 if (try("RCS/%s%s", filebase, RCSSUFFIX) || 127 try1("RCS/%s" , filebase) || 128 try("%s%s", filebase, RCSSUFFIX)) { 129 snprintf(buf, sizeof(buf), CHECKOUT, filename); 130 snprintf(lbuf, sizeof(lbuf), RCSDIFF, filename); 131 cs = "RCS"; 132 } else if (try("SCCS/%s%s", SCCSPREFIX, filebase) || 133 try("%s%s", SCCSPREFIX, filebase)) { 134 snprintf(buf, sizeof(buf), GET, s); 135 snprintf(lbuf, sizeof(lbuf), SCCSDIFF, s, filename); 136 cs = "SCCS"; 137 } else if (statfailed) 138 fatal("can't find %s\n", filename); 139 /* 140 * else we can't write to it but it's not under a version 141 * control system, so just proceed. 142 */ 143 if (cs) { 144 if (!statfailed) { 145 if ((filestat.st_mode & 0222) != 0) 146 /* The owner can write to it. */ 147 fatal( 148 "file %s seems to be locked by somebody else under %s\n", 149 filename, cs); 150 /* 151 * It might be checked out unlocked. See if 152 * it's safe to check out the default version 153 * locked. 154 */ 155 if (verbose) 156 say( 157 "Comparing file %s to default %s version...\n", 158 filename, cs); 159 if (system(lbuf)) 160 fatal( 161 "can't check out file %s: differs from default %s version\n", 162 filename, cs); 163 } 164 if (verbose) 165 say("Checking out file %s from %s...\n", 166 filename, cs); 167 if (system(buf) || stat(filename, &filestat)) 168 fatal("can't check out file %s from %s\n", 169 filename, cs); 170 } 171 } 172 if (old_file_is_dev_null && 173 ok_to_create_file && 174 (filestat.st_size != 0)) { 175 fatal( 176 "patch creates new file but existing file %s not empty\n", 177 filename); 178 } 179 180 filemode = filestat.st_mode; 181 if (!S_ISREG(filemode)) 182 fatal("%s is not a normal file--can't patch\n", filename); 183 i_size = filestat.st_size; 184 185 i_womp = xmalloc(i_size + 2); 186 if ((ifd = open(filename, 0)) < 0) 187 pfatal("can't open file %s", filename); 188 if (read(ifd, i_womp, i_size) != i_size) 189 pfatal("read error"); 190 Close(ifd); 191 if (i_size && i_womp[i_size - 1] != '\n') 192 i_womp[i_size++] = '\n'; 193 i_womp[i_size] = '\0'; 194 195 /* 196 * Count the lines in the buffer so we know how many pointers we 197 * need. 198 */ 199 iline = 0; 200 for (s = i_womp; *s; s++) { 201 if (*s == '\n') 202 iline++; 203 } 204 i_ptr = xmalloc((iline + 2) * sizeof(char *)); 205 206 /* Now scan the buffer and build pointer array. */ 207 iline = 1; 208 i_ptr[iline] = i_womp; 209 for (s = i_womp; *s; s++) { 210 if (*s == '\n') { 211 /* These are NOT null terminated. */ 212 i_ptr[++iline] = s + 1; 213 } 214 } 215 input_lines = iline - 1; 216 217 /* Now check for revision, if any. */ 218 if (revision != NULL) { 219 if (!rev_in_string(i_womp)) { 220 if (force) { 221 if (verbose) 222 say( 223 "Warning: this file doesn't appear to be the %s version--patching anyway.\n", 224 revision); 225 } else if (batch) { 226 fatal( 227 "this file doesn't appear to be the %s version--aborting.\n", revision); 228 } else { 229 ask( 230 "This file doesn't appear to be the %s version--patch anyway? [n] ", 231 revision); 232 if (*buf != 'y') 233 fatal("aborted\n"); 234 } 235 } else if (verbose) 236 say("Good. This file appears to be the %s version.\n", 237 revision); 238 } 239 } 240 241 /* 242 * Fetch a line from the input file, \n terminated, not necessarily \0. 243 */ 244 const char * 245 ifetch(LINENUM line) 246 { 247 if (line < 1 || line > input_lines) 248 return ""; 249 250 return i_ptr[line]; 251 } 252 253 /* 254 * True if the string argument contains the revision number we want. 255 */ 256 static bool 257 rev_in_string(char *string) 258 { 259 char *s; 260 size_t patlen; 261 262 if (revision == NULL) 263 return TRUE; 264 patlen = strlen(revision); 265 if (strnEQ(string,revision,patlen) && 266 isspace((unsigned char)string[patlen])) 267 return TRUE; 268 for (s = string; *s; s++) { 269 if (isspace((unsigned char)*s) && 270 strnEQ(s + 1, revision, patlen) && 271 isspace((unsigned char)s[patlen + 1] )) { 272 return TRUE; 273 } 274 } 275 return FALSE; 276 } 277