InterpretDebug-mods: calculate OpName length
[nedit-bw.git] / read_from_pipes.patch
blob2b20465e23edad03c852069138b0f41ff43a5482
1 ---
3 source/file.c | 199 +++++++++++++++++++++++++++++++++++++++++++------------
4 util/fileUtils.c | 67 +++++++++++++++++-
5 2 files changed, 219 insertions(+), 47 deletions(-)
7 diff --quilt old/source/file.c new/source/file.c
8 --- old/source/file.c
9 +++ new/source/file.c
10 @@ -478,33 +478,92 @@ static int doOpen(WindowInfo *window, co
11 "Can't open block device %s", "OK", name);
12 window->filenameSet = TRUE;
13 return FALSE;
15 #endif
16 - fileLen = statbuf.st_size;
18 - /* Allocate space for the whole contents of the file (unfortunately) */
19 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
20 - if (fileString == NULL) {
21 - fclose(fp);
22 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
23 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
24 - "File is too large to edit", "OK");
25 - window->filenameSet = TRUE;
26 - return FALSE;
27 - }
29 - /* Read the file into fileString and terminate with a null */
30 - readLen = fread(fileString, sizeof(char), fileLen, fp);
31 - if (ferror(fp)) {
32 - fclose(fp);
33 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
34 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
35 - "Error reading %s:\n%s", "OK", name, errorString());
36 - window->filenameSet = TRUE;
37 - free(fileString);
38 - return FALSE;
39 + if (S_ISFIFO(statbuf.st_mode)) {
40 + int pipeBufLen, fileSize;
42 + window->fileMissing = TRUE;
43 + SET_PERM_LOCKED(window->lockReasons, TRUE);
45 + pipeBufLen = statbuf.st_blksize;
46 + if (pipeBufLen <= 0)
47 + pipeBufLen = 4096;
49 + fileSize = pipeBufLen << 2;
50 + fileString = malloc(fileSize + 1);
51 + if (fileString == NULL)
52 + {
53 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
54 + "File is too large to include", "OK");
55 + fclose(fp);
56 + return FALSE;
57 + }
59 + fileLen = 0;
60 + do
61 + {
62 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
64 + if (ferror(fp))
65 + {
66 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
67 + "Error reading %s:\n%s", "OK", name, errorString());
68 + fclose(fp);
69 + free(fileString);
70 + return FALSE;
71 + }
73 + fileLen += readLen;
74 + if (fileSize - fileLen < pipeBufLen)
75 + {
76 + char *tmpFileString;
78 + fileSize += pipeBufLen << 2;
79 + tmpFileString = realloc(fileString, fileSize + 1);
80 + if (tmpFileString == NULL)
81 + {
82 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
83 + "File is too large to include", "OK");
84 + fclose(fp);
85 + free(fileString);
86 + return FALSE;
87 + }
88 + fileString = tmpFileString;
89 + }
91 + }
92 + while (!feof(fp));
94 + readLen = fileLen;
95 + } else {
96 + fileLen = statbuf.st_size;
98 + /* Allocate space for the whole contents of the file (unfortunately) */
99 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
100 + if (fileString == NULL) {
101 + fclose(fp);
102 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
103 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
104 + "File is too large to edit", "OK");
105 + window->filenameSet = TRUE;
106 + return FALSE;
109 + /* Read the file into fileString and terminate with a null */
110 + readLen = fread(fileString, sizeof(char), fileLen, fp);
111 + if (ferror(fp)) {
112 + fclose(fp);
113 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
114 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
115 + "Error reading %s:\n%s", "OK", name, errorString());
116 + window->filenameSet = TRUE;
117 + free(fileString);
118 + return FALSE;
121 fileString[readLen] = 0;
123 /* Close the file */
124 if (fclose(fp) != 0) {
125 @@ -622,31 +681,87 @@ int IncludeFile(WindowInfo *window, cons
126 DialogF(DF_ERR, window->shell, 1, "Error opening File",
127 "Can't open directory %s", "OK", name);
128 fclose(fp);
129 return FALSE;
131 - fileLen = statbuf.st_size;
133 - /* allocate space for the whole contents of the file */
134 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
135 - if (fileString == NULL)
137 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
138 - "File is too large to include", "OK");
139 - fclose(fp);
140 - return FALSE;
143 - /* read the file into fileString and terminate with a null */
144 - readLen = fread(fileString, sizeof(char), fileLen, fp);
145 - if (ferror(fp))
147 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
148 - "Error reading %s:\n%s", "OK", name, errorString());
149 - fclose(fp);
150 - free(fileString);
151 - return FALSE;
152 + if (S_ISFIFO(statbuf.st_mode)) {
153 + int pipeBufLen, fileSize;
155 + pipeBufLen = statbuf.st_blksize;
156 + if (pipeBufLen <= 0)
157 + pipeBufLen = 4096;
159 + fileSize = pipeBufLen << 2;
160 + fileString = malloc(fileSize + 1);
161 + if (fileString == NULL)
163 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
164 + "File is too large to include", "OK");
165 + fclose(fp);
166 + return FALSE;
169 + fileLen = 0;
170 + do
172 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
174 + if (ferror(fp))
176 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
177 + "Error reading %s:\n%s", "OK", name, errorString());
178 + fclose(fp);
179 + free(fileString);
180 + return FALSE;
183 + fileLen += readLen;
184 + if (fileSize - fileLen < pipeBufLen)
186 + char *tmpFileString;
188 + fileSize += pipeBufLen << 2;
189 + tmpFileString = realloc(fileString, fileSize + 1);
190 + if (tmpFileString == NULL)
192 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
193 + "File is too large to include", "OK");
194 + fclose(fp);
195 + free(fileString);
196 + return FALSE;
198 + fileString = tmpFileString;
202 + while(!feof(fp));
204 + readLen = fileLen;
205 + } else {
206 + fileLen = statbuf.st_size;
208 + /* allocate space for the whole contents of the file */
209 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
210 + if (fileString == NULL)
212 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
213 + "File is too large to include", "OK");
214 + fclose(fp);
215 + return FALSE;
218 + /* read the file into fileString and terminate with a null */
219 + readLen = fread(fileString, sizeof(char), fileLen, fp);
220 + if (ferror(fp))
222 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
223 + "Error reading %s:\n%s", "OK", name, errorString());
224 + fclose(fp);
225 + free(fileString);
226 + return FALSE;
229 fileString[readLen] = 0;
231 /* Detect and convert DOS and Macintosh format files */
232 switch (FormatOfFile(fileString)) {
233 diff --quilt old/util/fileUtils.c new/util/fileUtils.c
234 --- old/util/fileUtils.c
235 +++ new/util/fileUtils.c
236 @@ -677,18 +677,74 @@ char *ReadAnyTextFile(const char *fileNa
238 if (fstat(fileno(fp), &statbuf) != 0) {
239 fclose(fp);
240 return NULL;
242 - fileLen = statbuf.st_size;
243 - fileString = XtMalloc(fileLen+1); /* +1 = space for null */
244 - readLen = fread(fileString, sizeof(char), fileLen, fp);
245 - if (ferror(fp)) {
246 - XtFree(fileString);
247 + if (S_ISDIR(statbuf.st_mode)) {
248 fclose(fp);
249 return NULL;
252 + if (S_ISFIFO(statbuf.st_mode)) {
253 + int pipeBufLen, fileBufSize;
254 + char *fileBuf;
256 + pipeBufLen = statbuf.st_blksize;
257 + if (pipeBufLen <= 0)
258 + pipeBufLen = 4096;
260 + fileBufSize = pipeBufLen << 2;
261 + fileBuf = malloc(fileBufSize);
262 + if (fileBuf == NULL) {
263 + fclose(fp);
264 + return NULL;
267 + fileLen = 0;
268 + do {
269 + readLen = fread(&fileBuf[fileLen], sizeof(char), pipeBufLen, fp);
271 + if (ferror(fp)) {
272 + fclose(fp);
273 + free(fileBuf);
274 + return NULL;
277 + fileLen += readLen;
278 + if (fileBufSize - fileLen < pipeBufLen) {
279 + char *tmpFileBuf;
281 + fileBufSize += pipeBufLen << 2;
282 + tmpFileBuf = realloc(fileBuf, fileBufSize);
283 + if (tmpFileBuf == NULL) {
284 + fclose(fp);
285 + free(fileBuf);
286 + return NULL;
288 + fileBuf = tmpFileBuf;
291 + } while (!feof(fp));
293 + readLen = fileLen;
294 + fileString = XtMalloc(readLen+1); /* +1 = space for null */
295 + memcpy(fileString, fileBuf, readLen * sizeof(char));
296 + free(fileBuf);
297 + } else {
298 + fileLen = statbuf.st_size;
299 + fileString = XtMalloc(fileLen+1); /* +1 = space for null */
300 + if (fileString == NULL) {
301 + fclose(fp);
302 + return NULL;
304 + readLen = fread(fileString, sizeof(char), fileLen, fp);
305 + if (ferror(fp)) {
306 + XtFree(fileString);
307 + fclose(fp);
308 + return NULL;
311 fclose(fp);
312 fileString[readLen] = 0;
314 /* Convert linebreaks? */
315 format = FormatOfFile(fileString);
316 @@ -699,5 +755,6 @@ char *ReadAnyTextFile(const char *fileNa
317 ConvertFromMacFileString(fileString, readLen);
320 return fileString;