zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] mapfile without HAVE_MMAP should not trim newlines
Date: Sat, 16 Sep 2023 12:31:25 -0700	[thread overview]
Message-ID: <CAH+w=7YgTUV_WDDRBp+Meu26H4yEM2P0HvK=XSk9NnEvjeJfvg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 486 bytes --]

When memory mapping is not available, the mapfile module was using
readoutput() to slurp the file contents.  This metafies the contents
(which is necessary for return as a parameter value) but has the
side-effect of dropping trailing newlines.

Src/input.c has another file-slurping function stuff() which has the
side-effect of loading the input buffer.  This patch separates the
guts of stuff() into a separately-callable function and uses that in
mapfile.c to preserve the newlines.

[-- Attachment #2: mapfile-nl.txt --]
[-- Type: text/plain, Size: 2525 bytes --]

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 4ed8f2ff2..8d7f44d7c 100644
--- a/Src/input.c
+++ b/Src/input.c
@@ -613,8 +613,8 @@ inungetc(int c)
 /* stuff a whole file into memory and return it */
 
 /**/
-char *
-ztuff(const char *fn)
+off_t
+zstuff(char **out, const char *fn)
 {
     FILE *in;
     char *buf;
@@ -622,20 +622,36 @@ ztuff(const char *fn)
 
     if (!(in = fopen(unmeta(fn), "r"))) {
 	zerr("can't open %s", fn);
-	return NULL;
+	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);
-	return NULL;
+	unqueue_signals();
+	return -1;
     }
     fclose(in);
     buf[len] = '\0';
-    return buf;
+    *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 */
@@ -644,26 +660,12 @@ ztuff(const char *fn)
 int
 stuff(char *fn)
 {
-    FILE *in;
     char *buf;
-    off_t len;
+    off_t len = zstuff(&buf, fn);
 
-    if (!(in = fopen(unmeta(fn), "r"))) {
-	zerr("can't open %s", fn);
+    if (len < 0)
 	return 1;
-    }
-    fseek(in, 0, SEEK_END);
-    len = ftell(in);
-    fseek(in, 0, SEEK_SET);
-    buf = (char *)zalloc(len + 1);
-    if (!(fread(buf, len, 1, in))) {
-	zerr("read error on %s", fn);
-	fclose(in);
-	zfree(buf, len + 1);
-	return 1;
-    }
-    fclose(in);
-    buf[len] = '\0';
+    
     fwrite(buf, len, 1, stderr);
     fflush(stderr);
     inputsetline(metafy(buf, len, META_REALLOC), INP_FREE);

             reply	other threads:[~2023-09-16 19:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-16 19:31 Bart Schaefer [this message]
2023-09-16 20:04 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7YgTUV_WDDRBp+Meu26H4yEM2P0HvK=XSk9NnEvjeJfvg@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).