1 Subject: handle reading from pipes
6 source/file.c | 199 +++++++++++++++++++++++++++++++++++++++++++------------
7 util/fileUtils.c | 87 ++++++++++++++++++++----
8 2 files changed, 231 insertions(+), 55 deletions(-)
10 diff --quilt old/source/file.c new/source/file.c
13 @@ -495,29 +495,88 @@ static int doOpen(WindowInfo *window, co
17 - fileLen = statbuf.st_size;
19 - /* Allocate space for the whole contents of the file (unfortunately) */
20 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
21 - if (fileString == NULL) {
23 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
24 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
25 - "File is too large to edit", "OK");
26 - window->filenameSet = TRUE;
30 - /* Read the file into fileString and terminate with a null */
31 - readLen = fread(fileString, sizeof(char), fileLen, fp);
34 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
35 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
36 - "Error reading %s:\n%s", "OK", name, errorString());
37 - window->filenameSet = TRUE;
40 + if (S_ISFIFO(statbuf.st_mode)) {
41 + int pipeBufLen, fileSize;
43 + window->fileMissing = TRUE;
44 + SET_PERM_LOCKED(window->lockReasons, TRUE);
46 + pipeBufLen = statbuf.st_blksize;
47 + if (pipeBufLen <= 0)
50 + fileSize = pipeBufLen << 2;
51 + fileString = malloc(fileSize + 1);
52 + if (fileString == NULL)
54 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
55 + "File is too large to include", "OK");
63 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
67 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
68 + "Error reading %s:\n%s", "OK", name, errorString());
75 + if (fileSize - fileLen < pipeBufLen)
77 + char *tmpFileString;
79 + fileSize += pipeBufLen << 2;
80 + tmpFileString = realloc(fileString, fileSize + 1);
81 + if (tmpFileString == NULL)
83 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
84 + "File is too large to include", "OK");
89 + fileString = tmpFileString;
97 + fileLen = statbuf.st_size;
99 + /* Allocate space for the whole contents of the file (unfortunately) */
100 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
101 + if (fileString == NULL) {
103 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
104 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
105 + "File is too large to edit", "OK");
106 + window->filenameSet = TRUE;
110 + /* Read the file into fileString and terminate with a null */
111 + readLen = fread(fileString, sizeof(char), fileLen, fp);
114 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
115 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
116 + "Error reading %s:\n%s", "OK", name, errorString());
117 + window->filenameSet = TRUE;
122 fileString[readLen] = 0;
124 @@ -635,27 +694,83 @@ int IncludeFile(WindowInfo *window, cons
128 - fileLen = statbuf.st_size;
130 - /* allocate space for the whole contents of the file */
131 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
132 - if (fileString == NULL)
134 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
135 - "File is too large to include", "OK");
140 - /* read the file into fileString and terminate with a null */
141 - readLen = fread(fileString, sizeof(char), fileLen, fp);
144 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
145 - "Error reading %s:\n%s", "OK", name, errorString());
149 + if (S_ISFIFO(statbuf.st_mode)) {
150 + int pipeBufLen, fileSize;
152 + pipeBufLen = statbuf.st_blksize;
153 + if (pipeBufLen <= 0)
156 + fileSize = pipeBufLen << 2;
157 + fileString = malloc(fileSize + 1);
158 + if (fileString == NULL)
160 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
161 + "File is too large to include", "OK");
169 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
173 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
174 + "Error reading %s:\n%s", "OK", name, errorString());
180 + fileLen += readLen;
181 + if (fileSize - fileLen < pipeBufLen)
183 + char *tmpFileString;
185 + fileSize += pipeBufLen << 2;
186 + tmpFileString = realloc(fileString, fileSize + 1);
187 + if (tmpFileString == NULL)
189 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
190 + "File is too large to include", "OK");
195 + fileString = tmpFileString;
203 + fileLen = statbuf.st_size;
205 + /* allocate space for the whole contents of the file */
206 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
207 + if (fileString == NULL)
209 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
210 + "File is too large to include", "OK");
215 + /* read the file into fileString and terminate with a null */
216 + readLen = fread(fileString, sizeof(char), fileLen, fp);
219 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
220 + "Error reading %s:\n%s", "OK", name, errorString());
226 fileString[readLen] = 0;
228 diff --quilt old/util/fileUtils.c new/util/fileUtils.c
229 --- old/util/fileUtils.c
230 +++ new/util/fileUtils.c
231 @@ -675,22 +675,82 @@ char *ReadAnyTextFile(const char *fileNa
233 /* Read the whole file into fileString */
234 if ((fp = fopen(fileName, "r")) == NULL) {
238 if (fstat(fileno(fp), &statbuf) != 0) {
244 - fileLen = statbuf.st_size;
245 - /* +1 = space for null
246 - ** +1 = possible additional \n
248 - fileString = XtMalloc(fileLen + 2);
249 - readLen = fread(fileString, sizeof(char), fileLen, fp);
251 - XtFree(fileString);
254 + if (S_ISDIR(statbuf.st_mode)) {
259 + if (S_ISFIFO(statbuf.st_mode)) {
260 + int pipeBufLen, fileBufSize;
263 + pipeBufLen = statbuf.st_blksize;
264 + if (pipeBufLen <= 0) {
268 + fileBufSize = pipeBufLen << 2;
269 + fileBuf = malloc(fileBufSize);
270 + if (fileBuf == NULL) {
277 + readLen = fread(&fileBuf[fileLen], sizeof(char), pipeBufLen, fp);
285 + fileLen += readLen;
286 + if (fileBufSize - fileLen < pipeBufLen) {
289 + fileBufSize += pipeBufLen << 2;
290 + tmpFileBuf = realloc(fileBuf, fileBufSize);
291 + if (tmpFileBuf == NULL) {
296 + fileBuf = tmpFileBuf;
299 + } while (!feof(fp));
302 + /* +1 = space for null
303 + ** +1 = possible additional \n
305 + fileString = XtMalloc(readLen + 2);
306 + memcpy(fileString, fileBuf, readLen * sizeof(char));
309 + fileLen = statbuf.st_size;
310 + /* +1 = space for null
311 + ** +1 = possible additional \n
313 + fileString = XtMalloc(fileLen + 2);
314 + if (fileString == NULL) {
318 + readLen = fread(fileString, sizeof(char), fileLen, fp);
320 + XtFree(fileString);
326 fileString[readLen] = 0;
327 @@ -712,3 +772,4 @@ char *ReadAnyTextFile(const char *fileNa