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 @@ -492,33 +492,92 @@ static int doOpen(WindowInfo *window, co
14 "Can't open block device %s", "OK", name);
15 window->filenameSet = TRUE;
19 - fileLen = statbuf.st_size;
21 - /* Allocate space for the whole contents of the file (unfortunately) */
22 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
23 - if (fileString == NULL) {
25 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
26 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
27 - "File is too large to edit", "OK");
28 - window->filenameSet = TRUE;
32 - /* Read the file into fileString and terminate with a null */
33 - readLen = fread(fileString, sizeof(char), fileLen, fp);
36 - window->filenameSet = FALSE; /* Temp. prevent check for changes. */
37 - DialogF(DF_ERR, window->shell, 1, "Error while opening File",
38 - "Error reading %s:\n%s", "OK", name, errorString());
39 - window->filenameSet = TRUE;
42 + if (S_ISFIFO(statbuf.st_mode)) {
43 + int pipeBufLen, fileSize;
45 + window->fileMissing = TRUE;
46 + SET_PERM_LOCKED(window->lockReasons, TRUE);
48 + pipeBufLen = statbuf.st_blksize;
49 + if (pipeBufLen <= 0)
52 + fileSize = pipeBufLen << 2;
53 + fileString = malloc(fileSize + 1);
54 + if (fileString == NULL)
56 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
57 + "File is too large to include", "OK");
65 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
69 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
70 + "Error reading %s:\n%s", "OK", name, errorString());
77 + if (fileSize - fileLen < pipeBufLen)
79 + char *tmpFileString;
81 + fileSize += pipeBufLen << 2;
82 + tmpFileString = realloc(fileString, fileSize + 1);
83 + if (tmpFileString == NULL)
85 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
86 + "File is too large to include", "OK");
91 + fileString = tmpFileString;
99 + fileLen = statbuf.st_size;
101 + /* Allocate space for the whole contents of the file (unfortunately) */
102 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
103 + if (fileString == NULL) {
105 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
106 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
107 + "File is too large to edit", "OK");
108 + window->filenameSet = TRUE;
112 + /* Read the file into fileString and terminate with a null */
113 + readLen = fread(fileString, sizeof(char), fileLen, fp);
116 + window->filenameSet = FALSE; /* Temp. prevent check for changes. */
117 + DialogF(DF_ERR, window->shell, 1, "Error while opening File",
118 + "Error reading %s:\n%s", "OK", name, errorString());
119 + window->filenameSet = TRUE;
124 fileString[readLen] = 0;
127 if (fclose(fp) != 0) {
128 @@ -632,31 +691,87 @@ int IncludeFile(WindowInfo *window, cons
129 DialogF(DF_ERR, window->shell, 1, "Error opening File",
130 "Can't open directory %s", "OK", name);
134 - fileLen = statbuf.st_size;
136 - /* allocate space for the whole contents of the file */
137 - fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
138 - if (fileString == NULL)
140 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
141 - "File is too large to include", "OK");
146 - /* read the file into fileString and terminate with a null */
147 - readLen = fread(fileString, sizeof(char), fileLen, fp);
150 - DialogF(DF_ERR, window->shell, 1, "Error opening File",
151 - "Error reading %s:\n%s", "OK", name, errorString());
155 + if (S_ISFIFO(statbuf.st_mode)) {
156 + int pipeBufLen, fileSize;
158 + pipeBufLen = statbuf.st_blksize;
159 + if (pipeBufLen <= 0)
162 + fileSize = pipeBufLen << 2;
163 + fileString = malloc(fileSize + 1);
164 + if (fileString == NULL)
166 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
167 + "File is too large to include", "OK");
175 + readLen = fread(&fileString[fileLen], sizeof(char), pipeBufLen, fp);
179 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
180 + "Error reading %s:\n%s", "OK", name, errorString());
186 + fileLen += readLen;
187 + if (fileSize - fileLen < pipeBufLen)
189 + char *tmpFileString;
191 + fileSize += pipeBufLen << 2;
192 + tmpFileString = realloc(fileString, fileSize + 1);
193 + if (tmpFileString == NULL)
195 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
196 + "File is too large to include", "OK");
201 + fileString = tmpFileString;
209 + fileLen = statbuf.st_size;
211 + /* allocate space for the whole contents of the file */
212 + fileString = (char *)malloc(fileLen+1); /* +1 = space for null */
213 + if (fileString == NULL)
215 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
216 + "File is too large to include", "OK");
221 + /* read the file into fileString and terminate with a null */
222 + readLen = fread(fileString, sizeof(char), fileLen, fp);
225 + DialogF(DF_ERR, window->shell, 1, "Error opening File",
226 + "Error reading %s:\n%s", "OK", name, errorString());
232 fileString[readLen] = 0;
234 /* Detect and convert DOS and Macintosh format files */
235 switch (FormatOfFile(fileString)) {
236 diff --quilt old/util/fileUtils.c new/util/fileUtils.c
237 --- old/util/fileUtils.c
238 +++ new/util/fileUtils.c
239 @@ -673,26 +673,86 @@ char *ReadAnyTextFile(const char *fileNa
243 /* Read the whole file into fileString */
244 if ((fp = fopen(fileName, "r")) == NULL) {
248 if (fstat(fileno(fp), &statbuf) != 0) {
254 - fileLen = statbuf.st_size;
255 - /* +1 = space for null
256 - ** +1 = possible additional \n
258 - fileString = XtMalloc(fileLen + 2);
259 - readLen = fread(fileString, sizeof(char), fileLen, fp);
261 - XtFree(fileString);
264 + if (S_ISDIR(statbuf.st_mode)) {
269 + if (S_ISFIFO(statbuf.st_mode)) {
270 + int pipeBufLen, fileBufSize;
273 + pipeBufLen = statbuf.st_blksize;
274 + if (pipeBufLen <= 0) {
278 + fileBufSize = pipeBufLen << 2;
279 + fileBuf = malloc(fileBufSize);
280 + if (fileBuf == NULL) {
287 + readLen = fread(&fileBuf[fileLen], sizeof(char), pipeBufLen, fp);
295 + fileLen += readLen;
296 + if (fileBufSize - fileLen < pipeBufLen) {
299 + fileBufSize += pipeBufLen << 2;
300 + tmpFileBuf = realloc(fileBuf, fileBufSize);
301 + if (tmpFileBuf == NULL) {
306 + fileBuf = tmpFileBuf;
309 + } while (!feof(fp));
312 + /* +1 = space for null
313 + ** +1 = possible additional \n
315 + fileString = XtMalloc(readLen + 2);
316 + memcpy(fileString, fileBuf, readLen * sizeof(char));
319 + fileLen = statbuf.st_size;
320 + /* +1 = space for null
321 + ** +1 = possible additional \n
323 + fileString = XtMalloc(fileLen + 2);
324 + if (fileString == NULL) {
328 + readLen = fread(fileString, sizeof(char), fileLen, fp);
330 + XtFree(fileString);
336 fileString[readLen] = 0;
338 /* Convert linebreaks? */
339 @@ -710,5 +770,6 @@ char *ReadAnyTextFile(const char *fileNa
340 fileString[readLen + 1] = '\0';