about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorBart Schaefer <schaefer@zsh.org>2023-09-16 13:08:59 -0700
committerBart Schaefer <schaefer@zsh.org>2023-09-16 13:08:59 -0700
commit355cfc1b95ca65e7ab9ada5b59a82ed4f651556f (patch)
tree72dbef24e8e0d70ee83d27eef191f690466a0af0 /Src
parentaecef41f2e47e738214cf6c55a383b4eae87902f (diff)
downloadzsh-355cfc1b95ca65e7ab9ada5b59a82ed4f651556f.tar.gz
zsh-355cfc1b95ca65e7ab9ada5b59a82ed4f651556f.tar.xz
zsh-355cfc1b95ca65e7ab9ada5b59a82ed4f651556f.zip
52153: mapfile without HAVE_MMAP should not trim newlines
Diffstat (limited to 'Src')
-rw-r--r--Src/Modules/mapfile.c10
-rw-r--r--Src/input.c44
2 files changed, 41 insertions, 13 deletions
diff --git a/Src/Modules/mapfile.c b/Src/Modules/mapfile.c
index dd86fb596..84cdfea18 100644
--- a/Src/Modules/mapfile.c
+++ b/Src/Modules/mapfile.c
@@ -170,6 +170,8 @@ get_contents(char *fname)
 #ifdef USE_MMAP
     caddr_t mmptr;
     struct stat sbuf;
+#else
+    off_t size;
 #endif
     char *val;
     unmetafy(fname = ztrdup(fname), &fd);
@@ -196,12 +198,8 @@ get_contents(char *fname)
     close(fd);
 #else /* don't USE_MMAP */
     val = NULL;
-    if ((fd = open(fname, O_RDONLY | O_NOCTTY)) >= 0) {
-	LinkList ll;
-
-	if ((ll = readoutput(fd, 1, 0)))
-	    val = peekfirst(ll);
-    }
+    if ((size = zstuff(&val, fname)) > 0)
+	val = metafy(val, size, META_HEAPDUP);
 #endif /* USE_MMAP */
     free(fname);
     return val;
diff --git a/Src/input.c b/Src/input.c
index 0da065e51..8d7f44d7c 100644
--- a/Src/input.c
+++ b/Src/input.c
@@ -610,11 +610,11 @@ inungetc(int c)
     }
 }
 
-/* stuff a whole file into the input queue and print it */
+/* stuff a whole file into memory and return it */
 
 /**/
-int
-stuff(char *fn)
+off_t
+zstuff(char **out, const char *fn)
 {
     FILE *in;
     char *buf;
@@ -622,20 +622,50 @@ stuff(char *fn)
 
     if (!(in = fopen(unmeta(fn), "r"))) {
 	zerr("can't open %s", fn);
-	return 1;
+	return -1;
     }
+    queue_signals();
     fseek(in, 0, SEEK_END);
     len = ftell(in);
     fseek(in, 0, SEEK_SET);
     buf = (char *)zalloc(len + 1);
-    if (!(fread(buf, len, 1, in))) {
+    if (len && !(fread(buf, len, 1, in))) {
 	zerr("read error on %s", fn);
 	fclose(in);
-	zfree(buf, len + 1);
-	return 1;
+	unqueue_signals();
+	return -1;
     }
     fclose(in);
     buf[len] = '\0';
+    *out = buf;
+    unqueue_signals();
+    return len;
+}
+
+/**/
+char *
+ztuff(const char *fn)
+{
+    char *buf;
+    off_t len = zstuff(&buf, fn);
+    if (len > 0)
+	return buf;
+    else
+	return NULL;
+}
+
+/* stuff a whole file into the input queue and print it */
+
+/**/
+int
+stuff(char *fn)
+{
+    char *buf;
+    off_t len = zstuff(&buf, fn);
+
+    if (len < 0)
+	return 1;
+    
     fwrite(buf, len, 1, stderr);
     fflush(stderr);
     inputsetline(metafy(buf, len, META_REALLOC), INP_FREE);