1 /* $FreeBSD: src/gnu/usr.bin/patch/inp.c,v 1.10.2.1 2002/04/30 20:40:02 gad Exp $
2 /* $DragonFly: src/gnu/usr.bin/patch/Attic/inp.c,v 1.2 2003/06/17 04:25:47 dillon Exp $
5 * Revision 2.0.1.1 88/06/03 15:06:13 lwall
6 * patch10: made a little smarter about sccs files
8 * Revision 2.0 86/09/17 15:37:02 lwall
9 * Baseline for netwide release.
20 /* Input-file-with-indexable-lines abstract type */
22 static long i_size; /* size of the input file */
23 static char *i_womp; /* plan a buffer for entire file */
24 static char **i_ptr; /* pointers to lines in i_womp */
26 static int tifd = -1; /* plan b virtual string array */
27 static char *tibuf[2]; /* plan b buffers */
28 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
29 static LINENUM lines_per_buf; /* how many lines per buffer */
30 static int tireclen; /* length of records in tmp file */
33 * New patch--prepare to edit another file.
41 if (i_ptr != Null(char**))
47 i_ptr = Null(char **);
50 using_plan_a = TRUE; /* maybe the next one is smaller */
55 tibuf[0] = tibuf[1] = Nullch;
56 tiline[0] = tiline[1] = -1;
62 * Constuct the line index, somehow or other.
65 scan_input(char *filename)
67 if (!plan_a(filename))
70 say3("Patching file %s using Plan %s...\n", filename,
71 (using_plan_a ? "A" : "B") );
76 * Try keeping everything in memory.
79 plan_a(char *filename)
84 char lbuf[MAXLINELEN];
85 int output_elsewhere = strcmp(filename, outname);
86 extern int check_patch;
88 statfailed = stat(filename, &filestat);
89 if (statfailed && ok_to_create_file) {
91 say2("(Creating file %s...)\n",filename);
94 makedirs(filename, TRUE);
95 close(creat(filename, 0666));
96 statfailed = stat(filename, &filestat);
98 if (statfailed && check_patch) {
99 fatal2("%s not found and in check_patch mode.", filename);
101 /* For nonexistent or read-only files, look for RCS or SCCS versions. */
103 || (! output_elsewhere
104 && (/* No one can write to it. */
105 (filestat.st_mode & 0222) == 0
106 /* I can't write to it. */
107 || ((filestat.st_mode & 0022) == 0
108 && filestat.st_uid != myuid)))) {
114 filebase = basename(filename);
115 pathlen = filebase - filename;
117 /* Put any leading path into `s'.
118 Leave room in lbuf for the diff command. */
120 strncpy(s, filename, pathlen);
122 #define try(f,a1,a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
123 if (( try("RCS/%s%s", filebase, RCSSUFFIX)
124 || try("RCS/%s%s", filebase, "")
125 || try( "%s%s", filebase, RCSSUFFIX))
127 /* Check that RCS file is not working file.
128 Some hosts don't report file name length errors. */
130 || ( (filestat.st_dev ^ cstat.st_dev)
131 | (filestat.st_ino ^ cstat.st_ino)))) {
132 Sprintf(buf, output_elsewhere?CHECKOUT:CHECKOUT_LOCKED, filename);
133 Sprintf(lbuf, RCSDIFF, filename);
135 } else if ( try("SCCS/%s%s", SCCSPREFIX, filebase)
136 || try( "%s%s", SCCSPREFIX, filebase)) {
137 Sprintf(buf, output_elsewhere?GET:GET_LOCKED, s);
138 Sprintf(lbuf, SCCSDIFF, s, filename);
140 } else if (statfailed)
141 fatal2("can't find %s\n", filename);
142 /* else we can't write to it but it's not under a version
143 control system, so just proceed. */
146 if ((filestat.st_mode & 0222) != 0)
147 /* The owner can write to it. */
148 fatal3("file %s seems to be locked by somebody else under %s\n",
150 /* It might be checked out unlocked. See if it's safe to
151 check out the default version locked. */
153 say3("Comparing file %s to default %s version...\n",
156 fatal3("can't check out file %s: differs from default %s version\n",
160 say3("Checking out file %s from %s...\n", filename, cs);
161 if (system(buf) || stat(filename, &filestat))
162 fatal3("can't check out file %s from %s\n", filename, cs);
165 filemode = filestat.st_mode;
166 if (!S_ISREG(filemode))
167 fatal2("%s is not a normal file--can't patch\n", filename);
168 i_size = filestat.st_size;
170 set_hunkmax(); /* make sure dynamic arrays are allocated */
172 return FALSE; /* force plan b because plan a bombed */
177 i_womp = malloc((MEM)(i_size+2)); /* lint says this may alloc less than */
178 /* i_size, but that's okay, I think. */
180 if (i_womp == Nullch)
182 if ((ifd = open(filename, 0)) < 0)
183 pfatal2("can't open file %s", filename);
185 if (read(ifd, i_womp, (int)i_size) != i_size) {
186 Close(ifd); /* probably means i_size > 15 or 16 bits worth */
187 free(i_womp); /* at this point it doesn't matter if i_womp was */
188 return FALSE; /* undersized. */
192 if (i_size && i_womp[i_size-1] != '\n')
193 i_womp[i_size++] = '\n';
194 i_womp[i_size] = '\0';
196 /* count the lines in the buffer so we know how many pointers we need */
199 for (s=i_womp; *s; s++) {
204 i_ptr = Null(char**);
206 i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
208 if (i_ptr == Null(char **)) { /* shucks, it was a near thing */
209 free((char *)i_womp);
213 /* now scan the buffer and build pointer array */
216 i_ptr[iline] = i_womp;
217 for (s=i_womp; *s; s++) {
219 i_ptr[++iline] = s+1; /* these are NOT null terminated */
221 input_lines = iline - 1;
223 /* now check for revision, if any */
225 if (revision != Nullch) {
226 if (!rev_in_string(i_womp)) {
230 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
235 "this file doesn't appear to be the %s version--aborting.\n", revision);
239 "This file doesn't appear to be the %s version--patch anyway? [n] ",
246 say2("Good. This file appears to be the %s version.\n",
249 return TRUE; /* plan a will work */
253 * Keep (virtually) nothing in memory.
256 plan_b(char *filename)
261 Reg4 bool found_revision = (revision == Nullch);
263 using_plan_a = FALSE;
264 if ((ifp = fopen(filename, "r")) == Nullfp)
265 pfatal2("can't open file %s", filename);
266 if ((tifd = creat(TMPINNAME, 0666)) < 0)
267 pfatal2("can't open file %s", TMPINNAME);
268 while (fgets(buf, sizeof buf, ifp) != Nullch) {
269 if (revision != Nullch && !found_revision && rev_in_string(buf))
270 found_revision = TRUE;
271 if ((i = strlen(buf)) > maxlen)
272 maxlen = i; /* find longest line */
274 if (revision != Nullch) {
275 if (!found_revision) {
279 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
284 "this file doesn't appear to be the %s version--aborting.\n", revision);
288 "This file doesn't appear to be the %s version--patch anyway? [n] ",
295 say2("Good. This file appears to be the %s version.\n",
298 Fseek(ifp, 0L, 0); /* rewind file */
299 lines_per_buf = BUFFERSIZE / maxlen;
301 tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
302 tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
303 if (tibuf[1] == Nullch)
304 fatal1("out of memory\n");
306 if (! (i % lines_per_buf)) /* new block */
307 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
308 pfatal1("can't write temp file");
309 if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
312 if (i % lines_per_buf)
313 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
314 pfatal1("can't write temp file");
320 if ((tifd = open(TMPINNAME, 0)) < 0) {
321 pfatal2("can't reopen file %s", TMPINNAME);
326 * Fetch a line from the input file, \n terminated, not necessarily \0.
329 ifetch(line,whichbuf)
331 int whichbuf; /* ignored when file in memory */
333 if (line < 1 || line > input_lines)
338 LINENUM offline = line % lines_per_buf;
339 LINENUM baseline = line - offline;
341 if (tiline[0] == baseline)
343 else if (tiline[1] == baseline)
346 tiline[whichbuf] = baseline;
347 #ifndef lint /* complains of long accuracy */
348 Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
350 if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
351 pfatal2("error reading tmp file %s", TMPINNAME);
353 return tibuf[whichbuf] + (tireclen*offline);
358 * True if the string argument contains the revision number we want.
361 rev_in_string(char *string)
366 if (revision == Nullch)
368 patlen = strlen(revision);
369 if (strnEQ(string,revision,patlen) && isspace((unsigned char)string[patlen]))
371 for (s = string; *s; s++) {
372 if (isspace((unsigned char)*s) && strnEQ(s+1, revision, patlen) &&
373 isspace((unsigned char)s[patlen+1] )) {