CVS rebase
[nedit-bw.git] / read_from_pipes.patch
blob73b56f3814038b59b24ac78b2c1de301d96c7834
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 @@ -494,29 +494,88 @@ static int doOpen(WindowInfo *window, co
11 return FALSE;
13 #endif
14 - fileLen = statbuf.st_size;
16 - /* Allocate space for the whole contents of the file (unfortunately) */
17 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
18 - if (fileString == NULL) {
19 - fclose(fp);
20 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
21 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
22 - "File is too large to edit", "OK");
23 - window->filenameSet = TRUE;
24 - return FALSE;
25 - }
27 - /* Read the file into fileString and terminate with a null */
28 - readLen = fread(fileString, sizeof(char), fileLen, fp);
29 - if (ferror(fp)) {
30 - fclose(fp);
31 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
32 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
33 - "Error reading %s:\n%s", "OK", name, errorString());
34 - window->filenameSet = TRUE;
35 - free(fileString);
36 - return FALSE;
37 + if (S_ISFIFO(statbuf.st_mode)) {
38 + int pipeBufLen, fileSize;
40 + window->fileMissing = TRUE;
41 + SET_PERM_LOCKED(window->lockReasons, TRUE);
43 + pipeBufLen = statbuf.st_blksize;
44 + if (pipeBufLen <= 0)
45 + pipeBufLen = 4096;
47 + fileSize = pipeBufLen << 2;
48 + fileString = malloc(fileSize + 1);
49 + if (fileString == NULL)
50 + {
51 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
52 + "File is too large to include", "OK");
53 + fclose(fp);
54 + return FALSE;
55 + }
57 + fileLen = 0;
58 + do
59 + {
60 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
62 + if (ferror(fp))
63 + {
64 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
65 + "Error reading %s:\n%s", "OK", name, errorString());
66 + fclose(fp);
67 + free(fileString);
68 + return FALSE;
69 + }
71 + fileLen += readLen;
72 + if (fileSize - fileLen < pipeBufLen)
73 + {
74 + char *tmpFileString;
76 + fileSize += pipeBufLen << 2;
77 + tmpFileString = realloc(fileString, fileSize + 1);
78 + if (tmpFileString == NULL)
79 + {
80 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
81 + "File is too large to include", "OK");
82 + fclose(fp);
83 + free(fileString);
84 + return FALSE;
85 + }
86 + fileString = tmpFileString;
87 + }
89 + }
90 + while (!feof(fp));
92 + readLen = fileLen;
93 + } else {
94 + fileLen = statbuf.st_size;
96 + /* Allocate space for the whole contents of the file (unfortunately) */
97 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
98 + if (fileString == NULL) {
99 + fclose(fp);
100 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
101 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
102 + "File is too large to edit", "OK");
103 + window->filenameSet = TRUE;
104 + return FALSE;
107 + /* Read the file into fileString and terminate with a null */
108 + readLen = fread(fileString, sizeof(char), fileLen, fp);
109 + if (ferror(fp)) {
110 + fclose(fp);
111 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
112 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
113 + "Error reading %s:\n%s", "OK", name, errorString());
114 + window->filenameSet = TRUE;
115 + free(fileString);
116 + return FALSE;
119 fileString[readLen] = 0;
121 @@ -634,27 +693,83 @@ int IncludeFile(WindowInfo *window, cons
122 fclose(fp);
123 return FALSE;
125 - fileLen = statbuf.st_size;
127 - /* allocate space for the whole contents of the file */
128 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
129 - if (fileString == NULL)
131 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
132 - "File is too large to include", "OK");
133 - fclose(fp);
134 - return FALSE;
137 - /* read the file into fileString and terminate with a null */
138 - readLen = fread(fileString, sizeof(char), fileLen, fp);
139 - if (ferror(fp))
141 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
142 - "Error reading %s:\n%s", "OK", name, errorString());
143 - fclose(fp);
144 - free(fileString);
145 - return FALSE;
146 + if (S_ISFIFO(statbuf.st_mode)) {
147 + int pipeBufLen, fileSize;
149 + pipeBufLen = statbuf.st_blksize;
150 + if (pipeBufLen <= 0)
151 + pipeBufLen = 4096;
153 + fileSize = pipeBufLen << 2;
154 + fileString = malloc(fileSize + 1);
155 + if (fileString == NULL)
157 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
158 + "File is too large to include", "OK");
159 + fclose(fp);
160 + return FALSE;
163 + fileLen = 0;
164 + do
166 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
168 + if (ferror(fp))
170 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
171 + "Error reading %s:\n%s", "OK", name, errorString());
172 + fclose(fp);
173 + free(fileString);
174 + return FALSE;
177 + fileLen += readLen;
178 + if (fileSize - fileLen < pipeBufLen)
180 + char *tmpFileString;
182 + fileSize += pipeBufLen << 2;
183 + tmpFileString = realloc(fileString, fileSize + 1);
184 + if (tmpFileString == NULL)
186 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
187 + "File is too large to include", "OK");
188 + fclose(fp);
189 + free(fileString);
190 + return FALSE;
192 + fileString = tmpFileString;
196 + while(!feof(fp));
198 + readLen = fileLen;
199 + } else {
200 + fileLen = statbuf.st_size;
202 + /* allocate space for the whole contents of the file */
203 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
204 + if (fileString == NULL)
206 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
207 + "File is too large to include", "OK");
208 + fclose(fp);
209 + return FALSE;
212 + /* read the file into fileString and terminate with a null */
213 + readLen = fread(fileString, sizeof(char), fileLen, fp);
214 + if (ferror(fp))
216 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
217 + "Error reading %s:\n%s", "OK", name, errorString());
218 + fclose(fp);
219 + free(fileString);
220 + return FALSE;
223 fileString[readLen] = 0;
225 diff --quilt old/util/fileUtils.c new/util/fileUtils.c
226 --- old/util/fileUtils.c
227 +++ new/util/fileUtils.c
228 @@ -679,14 +679,70 @@ char *ReadAnyTextFile(const char *fileNa
229 fclose(fp);
230 return NULL;
232 - fileLen = statbuf.st_size;
233 - fileString = XtMalloc(fileLen+1); /* +1 = space for null */
234 - readLen = fread(fileString, sizeof(char), fileLen, fp);
235 - if (ferror(fp)) {
236 - XtFree(fileString);
237 + if (S_ISDIR(statbuf.st_mode)) {
238 fclose(fp);
239 return NULL;
242 + if (S_ISFIFO(statbuf.st_mode)) {
243 + int pipeBufLen, fileBufSize;
244 + char *fileBuf;
246 + pipeBufLen = statbuf.st_blksize;
247 + if (pipeBufLen <= 0)
248 + pipeBufLen = 4096;
250 + fileBufSize = pipeBufLen << 2;
251 + fileBuf = malloc(fileBufSize);
252 + if (fileBuf == NULL) {
253 + fclose(fp);
254 + return NULL;
257 + fileLen = 0;
258 + do {
259 + readLen = fread(&fileBuf[fileLen], sizeof(char), pipeBufLen, fp);
261 + if (ferror(fp)) {
262 + fclose(fp);
263 + free(fileBuf);
264 + return NULL;
267 + fileLen += readLen;
268 + if (fileBufSize - fileLen < pipeBufLen) {
269 + char *tmpFileBuf;
271 + fileBufSize += pipeBufLen << 2;
272 + tmpFileBuf = realloc(fileBuf, fileBufSize);
273 + if (tmpFileBuf == NULL) {
274 + fclose(fp);
275 + free(fileBuf);
276 + return NULL;
278 + fileBuf = tmpFileBuf;
281 + } while (!feof(fp));
283 + readLen = fileLen;
284 + fileString = XtMalloc(readLen+1); /* +1 = space for null */
285 + memcpy(fileString, fileBuf, readLen * sizeof(char));
286 + free(fileBuf);
287 + } else {
288 + fileLen = statbuf.st_size;
289 + fileString = XtMalloc(fileLen+1); /* +1 = space for null */
290 + if (fileString == NULL) {
291 + fclose(fp);
292 + return NULL;
294 + readLen = fread(fileString, sizeof(char), fileLen, fp);
295 + if (ferror(fp)) {
296 + XtFree(fileString);
297 + fclose(fp);
298 + return NULL;
301 fclose(fp);
302 fileString[readLen] = 0;
304 @@ -701,3 +757,4 @@ char *ReadAnyTextFile(const char *fileNa
306 return fileString;