annotate miscellany/patch-2.0.12u8/inp.c @ 1:848621fd0092 kono mh-6_8_4_JP3_05

MH pre UTF8
author kono
date Mon, 18 Apr 2005 23:46:02 +0900
parents bce86c4163a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
1 /* $Header$
bce86c4163a3 Initial revision
kono
parents:
diff changeset
2 *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
3 * $Log$
1
848621fd0092 MH pre UTF8
kono
parents: 0
diff changeset
4 * Revision 1.1.1.1 2005/04/18 14:46:05 kono
848621fd0092 MH pre UTF8
kono
parents: 0
diff changeset
5 * MH pre UTF8
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
6 *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
7 * Revision 2.0.1.1 88/06/03 15:06:13 lwall
bce86c4163a3 Initial revision
kono
parents:
diff changeset
8 * patch10: made a little smarter about sccs files
bce86c4163a3 Initial revision
kono
parents:
diff changeset
9 *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
10 * Revision 2.0 86/09/17 15:37:02 lwall
bce86c4163a3 Initial revision
kono
parents:
diff changeset
11 * Baseline for netwide release.
bce86c4163a3 Initial revision
kono
parents:
diff changeset
12 *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
13 */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
14
bce86c4163a3 Initial revision
kono
parents:
diff changeset
15 #include "EXTERN.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
16 #include "common.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
17 #include "util.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
18 #include "pch.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
19 #include "INTERN.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
20 #include "inp.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
21
bce86c4163a3 Initial revision
kono
parents:
diff changeset
22 /* Input-file-with-indexable-lines abstract type */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
23
bce86c4163a3 Initial revision
kono
parents:
diff changeset
24 static long i_size; /* size of the input file */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
25 static char *i_womp; /* plan a buffer for entire file */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
26 static char **i_ptr; /* pointers to lines in i_womp */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
27
bce86c4163a3 Initial revision
kono
parents:
diff changeset
28 static int tifd = -1; /* plan b virtual string array */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
29 static char *tibuf[2]; /* plan b buffers */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
30 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
31 static LINENUM lines_per_buf; /* how many lines per buffer */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
32 static int tireclen; /* length of records in tmp file */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
33
bce86c4163a3 Initial revision
kono
parents:
diff changeset
34 /* New patch--prepare to edit another file. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
35
bce86c4163a3 Initial revision
kono
parents:
diff changeset
36 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
37 re_input()
bce86c4163a3 Initial revision
kono
parents:
diff changeset
38 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
39 if (using_plan_a) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
40 i_size = 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
41 #ifndef lint
bce86c4163a3 Initial revision
kono
parents:
diff changeset
42 if (i_ptr != Null(char**))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
43 free((char *)i_ptr);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
44 #endif
bce86c4163a3 Initial revision
kono
parents:
diff changeset
45 if (i_womp != Nullch)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
46 free(i_womp);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
47 i_womp = Nullch;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
48 i_ptr = Null(char **);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
49 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
50 else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
51 using_plan_a = TRUE; /* maybe the next one is smaller */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
52 Close(tifd);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
53 tifd = -1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
54 free(tibuf[0]);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
55 free(tibuf[1]);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
56 tibuf[0] = tibuf[1] = Nullch;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
57 tiline[0] = tiline[1] = -1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
58 tireclen = 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
59 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
60 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
61
bce86c4163a3 Initial revision
kono
parents:
diff changeset
62 /* Constuct the line index, somehow or other. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
63
bce86c4163a3 Initial revision
kono
parents:
diff changeset
64 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
65 scan_input(filename)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
66 char *filename;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
67 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
68 if (!plan_a(filename))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
69 plan_b(filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
70 if (verbose) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
71 say3("Patching file %s using Plan %s...\n", filename,
bce86c4163a3 Initial revision
kono
parents:
diff changeset
72 (using_plan_a ? "A" : "B") );
bce86c4163a3 Initial revision
kono
parents:
diff changeset
73 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
74 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
75
bce86c4163a3 Initial revision
kono
parents:
diff changeset
76 /* Try keeping everything in memory. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
77
bce86c4163a3 Initial revision
kono
parents:
diff changeset
78 bool
bce86c4163a3 Initial revision
kono
parents:
diff changeset
79 plan_a(filename)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
80 char *filename;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
81 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
82 int ifd, statfailed;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
83 Reg1 char *s;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
84 Reg2 LINENUM iline;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
85 char lbuf[MAXLINELEN];
bce86c4163a3 Initial revision
kono
parents:
diff changeset
86
bce86c4163a3 Initial revision
kono
parents:
diff changeset
87 statfailed = stat(filename, &filestat);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
88 if (statfailed && ok_to_create_file) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
89 if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
90 say2("(Creating file %s...)\n",filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
91 makedirs(filename, TRUE);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
92 close(creat(filename, 0666));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
93 statfailed = stat(filename, &filestat);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
94 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
95 /* For nonexistent or read-only files, look for RCS or SCCS versions. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
96 if (statfailed
bce86c4163a3 Initial revision
kono
parents:
diff changeset
97 /* No one can write to it. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
98 || (filestat.st_mode & 0222) == 0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
99 /* I can't write to it. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
100 || ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
101 struct stat cstat;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
102 char *cs = Nullch;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
103 char *filebase;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
104 int pathlen;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
105
bce86c4163a3 Initial revision
kono
parents:
diff changeset
106 filebase = basename(filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
107 pathlen = filebase - filename;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
108
bce86c4163a3 Initial revision
kono
parents:
diff changeset
109 /* Put any leading path into `s'.
bce86c4163a3 Initial revision
kono
parents:
diff changeset
110 Leave room in lbuf for the diff command. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
111 s = lbuf + 20;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
112 strncpy(s, filename, pathlen);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
113
bce86c4163a3 Initial revision
kono
parents:
diff changeset
114 #define try(f, a1, a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
115 if ( try("RCS/%s%s", filebase, RCSSUFFIX)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
116 || try("RCS/%s" , filebase, 0)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
117 || try( "%s%s", filebase, RCSSUFFIX)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
118 Sprintf(buf, CHECKOUT, filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
119 Sprintf(lbuf, RCSDIFF, filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
120 cs = "RCS";
bce86c4163a3 Initial revision
kono
parents:
diff changeset
121 } else if ( try("SCCS/%s%s", SCCSPREFIX, filebase)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
122 || try( "%s%s", SCCSPREFIX, filebase)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
123 Sprintf(buf, GET, s);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
124 Sprintf(lbuf, SCCSDIFF, s, filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
125 cs = "SCCS";
bce86c4163a3 Initial revision
kono
parents:
diff changeset
126 } else if (statfailed)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
127 fatal2("can't find %s\n", filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
128 /* else we can't write to it but it's not under a version
bce86c4163a3 Initial revision
kono
parents:
diff changeset
129 control system, so just proceed. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
130 if (cs) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
131 if (!statfailed) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
132 if ((filestat.st_mode & 0222) != 0)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
133 /* The owner can write to it. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
134 fatal3("file %s seems to be locked by somebody else under %s\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
135 filename, cs);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
136 /* It might be checked out unlocked. See if it's safe to
bce86c4163a3 Initial revision
kono
parents:
diff changeset
137 check out the default version locked. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
138 if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
139 say3("Comparing file %s to default %s version...\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
140 filename, cs);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
141 if (system(lbuf))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
142 fatal3("can't check out file %s: differs from default %s version\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
143 filename, cs);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
144 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
145 if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
146 say3("Checking out file %s from %s...\n", filename, cs);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
147 if (system(buf) || stat(filename, &filestat))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
148 fatal3("can't check out file %s from %s\n", filename, cs);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
149 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
150 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
151 filemode = filestat.st_mode;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
152 if (!S_ISREG(filemode))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
153 fatal2("%s is not a normal file--can't patch\n", filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
154 i_size = filestat.st_size;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
155 if (out_of_mem) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
156 set_hunkmax(); /* make sure dynamic arrays are allocated */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
157 out_of_mem = FALSE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
158 return FALSE; /* force plan b because plan a bombed */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
159 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
160 #ifdef lint
bce86c4163a3 Initial revision
kono
parents:
diff changeset
161 i_womp = Nullch;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
162 #else
bce86c4163a3 Initial revision
kono
parents:
diff changeset
163 i_womp = malloc((MEM)(i_size+2)); /* lint says this may alloc less than */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
164 /* i_size, but that's okay, I think. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
165 #endif
bce86c4163a3 Initial revision
kono
parents:
diff changeset
166 if (i_womp == Nullch)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
167 return FALSE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
168 if ((ifd = open(filename, 0)) < 0)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
169 pfatal2("can't open file %s", filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
170 #ifndef lint
bce86c4163a3 Initial revision
kono
parents:
diff changeset
171 if (read(ifd, i_womp, (int)i_size) != i_size) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
172 Close(ifd); /* probably means i_size > 15 or 16 bits worth */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
173 free(i_womp); /* at this point it doesn't matter if i_womp was */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
174 return FALSE; /* undersized. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
175 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
176 #endif
bce86c4163a3 Initial revision
kono
parents:
diff changeset
177 Close(ifd);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
178 if (i_size && i_womp[i_size-1] != '\n')
bce86c4163a3 Initial revision
kono
parents:
diff changeset
179 i_womp[i_size++] = '\n';
bce86c4163a3 Initial revision
kono
parents:
diff changeset
180 i_womp[i_size] = '\0';
bce86c4163a3 Initial revision
kono
parents:
diff changeset
181
bce86c4163a3 Initial revision
kono
parents:
diff changeset
182 /* count the lines in the buffer so we know how many pointers we need */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
183
bce86c4163a3 Initial revision
kono
parents:
diff changeset
184 iline = 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
185 for (s=i_womp; *s; s++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
186 if (*s == '\n')
bce86c4163a3 Initial revision
kono
parents:
diff changeset
187 iline++;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
188 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
189 #ifdef lint
bce86c4163a3 Initial revision
kono
parents:
diff changeset
190 i_ptr = Null(char**);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
191 #else
bce86c4163a3 Initial revision
kono
parents:
diff changeset
192 i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
193 #endif
bce86c4163a3 Initial revision
kono
parents:
diff changeset
194 if (i_ptr == Null(char **)) { /* shucks, it was a near thing */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
195 free((char *)i_womp);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
196 return FALSE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
197 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
198
bce86c4163a3 Initial revision
kono
parents:
diff changeset
199 /* now scan the buffer and build pointer array */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
200
bce86c4163a3 Initial revision
kono
parents:
diff changeset
201 iline = 1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
202 i_ptr[iline] = i_womp;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
203 for (s=i_womp; *s; s++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
204 if (*s == '\n')
bce86c4163a3 Initial revision
kono
parents:
diff changeset
205 i_ptr[++iline] = s+1; /* these are NOT null terminated */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
206 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
207 input_lines = iline - 1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
208
bce86c4163a3 Initial revision
kono
parents:
diff changeset
209 /* now check for revision, if any */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
210
bce86c4163a3 Initial revision
kono
parents:
diff changeset
211 if (revision != Nullch) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
212 if (!rev_in_string(i_womp)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
213 if (force) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
214 if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
215 say2(
bce86c4163a3 Initial revision
kono
parents:
diff changeset
216 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
217 revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
218 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
219 else if (batch) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
220 fatal2(
bce86c4163a3 Initial revision
kono
parents:
diff changeset
221 "this file doesn't appear to be the %s version--aborting.\n", revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
222 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
223 else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
224 ask2(
bce86c4163a3 Initial revision
kono
parents:
diff changeset
225 "This file doesn't appear to be the %s version--patch anyway? [n] ",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
226 revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
227 if (*buf != 'y')
bce86c4163a3 Initial revision
kono
parents:
diff changeset
228 fatal1("aborted\n");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
229 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
230 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
231 else if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
232 say2("Good. This file appears to be the %s version.\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
233 revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
234 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
235 return TRUE; /* plan a will work */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
236 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
237
bce86c4163a3 Initial revision
kono
parents:
diff changeset
238 /* Keep (virtually) nothing in memory. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
239
bce86c4163a3 Initial revision
kono
parents:
diff changeset
240 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
241 plan_b(filename)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
242 char *filename;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
243 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
244 Reg3 FILE *ifp;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
245 Reg1 int i = 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
246 Reg2 int maxlen = 1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
247 Reg4 bool found_revision = (revision == Nullch);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
248
bce86c4163a3 Initial revision
kono
parents:
diff changeset
249 using_plan_a = FALSE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
250 if ((ifp = fopen(filename, "r")) == Nullfp)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
251 pfatal2("can't open file %s", filename);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
252 if ((tifd = creat(TMPINNAME, 0666)) < 0)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
253 pfatal2("can't open file %s", TMPINNAME);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
254 while (fgets(buf, sizeof buf, ifp) != Nullch) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
255 if (revision != Nullch && !found_revision && rev_in_string(buf))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
256 found_revision = TRUE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
257 if ((i = strlen(buf)) > maxlen)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
258 maxlen = i; /* find longest line */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
259 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
260 if (revision != Nullch) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
261 if (!found_revision) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
262 if (force) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
263 if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
264 say2(
bce86c4163a3 Initial revision
kono
parents:
diff changeset
265 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
266 revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
267 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
268 else if (batch) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
269 fatal2(
bce86c4163a3 Initial revision
kono
parents:
diff changeset
270 "this file doesn't appear to be the %s version--aborting.\n", revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
271 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
272 else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
273 ask2(
bce86c4163a3 Initial revision
kono
parents:
diff changeset
274 "This file doesn't appear to be the %s version--patch anyway? [n] ",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
275 revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
276 if (*buf != 'y')
bce86c4163a3 Initial revision
kono
parents:
diff changeset
277 fatal1("aborted\n");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
278 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
279 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
280 else if (verbose)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
281 say2("Good. This file appears to be the %s version.\n",
bce86c4163a3 Initial revision
kono
parents:
diff changeset
282 revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
283 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
284 Fseek(ifp, 0L, 0); /* rewind file */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
285 lines_per_buf = BUFFERSIZE / maxlen;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
286 tireclen = maxlen;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
287 tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
288 tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
289 if (tibuf[1] == Nullch)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
290 fatal1("out of memory\n");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
291 for (i=1; ; i++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
292 if (! (i % lines_per_buf)) /* new block */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
293 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
294 pfatal1("can't write temp file");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
295 if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
296 == Nullch) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
297 input_lines = i - 1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
298 if (i % lines_per_buf)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
299 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
300 pfatal1("can't write temp file");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
301 break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
302 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
303 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
304 Fclose(ifp);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
305 Close(tifd);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
306 if ((tifd = open(TMPINNAME, 0)) < 0) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
307 pfatal2("can't reopen file %s", TMPINNAME);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
308 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
309 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
310
bce86c4163a3 Initial revision
kono
parents:
diff changeset
311 /* Fetch a line from the input file, \n terminated, not necessarily \0. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
312
bce86c4163a3 Initial revision
kono
parents:
diff changeset
313 char *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
314 ifetch(line,whichbuf)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
315 Reg1 LINENUM line;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
316 int whichbuf; /* ignored when file in memory */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
317 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
318 if (line < 1 || line > input_lines)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
319 return "";
bce86c4163a3 Initial revision
kono
parents:
diff changeset
320 if (using_plan_a)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
321 return i_ptr[line];
bce86c4163a3 Initial revision
kono
parents:
diff changeset
322 else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
323 LINENUM offline = line % lines_per_buf;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
324 LINENUM baseline = line - offline;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
325
bce86c4163a3 Initial revision
kono
parents:
diff changeset
326 if (tiline[0] == baseline)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
327 whichbuf = 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
328 else if (tiline[1] == baseline)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
329 whichbuf = 1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
330 else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
331 tiline[whichbuf] = baseline;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
332 #ifndef lint /* complains of long accuracy */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
333 Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
334 #endif
bce86c4163a3 Initial revision
kono
parents:
diff changeset
335 if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
336 pfatal2("error reading tmp file %s", TMPINNAME);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
337 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
338 return tibuf[whichbuf] + (tireclen*offline);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
339 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
340 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
341
bce86c4163a3 Initial revision
kono
parents:
diff changeset
342 /* True if the string argument contains the revision number we want. */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
343
bce86c4163a3 Initial revision
kono
parents:
diff changeset
344 bool
bce86c4163a3 Initial revision
kono
parents:
diff changeset
345 rev_in_string(string)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
346 char *string;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
347 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
348 Reg1 char *s;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
349 Reg2 int patlen;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
350
bce86c4163a3 Initial revision
kono
parents:
diff changeset
351 if (revision == Nullch)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
352 return TRUE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
353 patlen = strlen(revision);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
354 if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
355 return TRUE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
356 for (s = string; *s; s++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
357 if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
bce86c4163a3 Initial revision
kono
parents:
diff changeset
358 isspace(s[patlen+1] )) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
359 return TRUE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
360 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
361 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
362 return FALSE;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
363 }