mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-07 23:51:14 +02:00
52153: mapfile without HAVE_MMAP should not trim newlines
This commit is contained in:
parent
aecef41f2e
commit
355cfc1b95
3 changed files with 46 additions and 13 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
2023-09-16 Bart Schaefer <schaefer@zsh.org>
|
||||||
|
|
||||||
|
* 52153: Src/input.c, Src/Modules/mapfile.c: $mapfile[fname]
|
||||||
|
should not trim newlines (only applies when not HAVE_MMAP)
|
||||||
|
|
||||||
2023-09-15 Mikael Magnusson <mikachu@gmail.com>
|
2023-09-15 Mikael Magnusson <mikachu@gmail.com>
|
||||||
|
|
||||||
* 52142: Completion/Zsh/Context/_parameter,
|
* 52142: Completion/Zsh/Context/_parameter,
|
||||||
|
|
|
@ -170,6 +170,8 @@ get_contents(char *fname)
|
||||||
#ifdef USE_MMAP
|
#ifdef USE_MMAP
|
||||||
caddr_t mmptr;
|
caddr_t mmptr;
|
||||||
struct stat sbuf;
|
struct stat sbuf;
|
||||||
|
#else
|
||||||
|
off_t size;
|
||||||
#endif
|
#endif
|
||||||
char *val;
|
char *val;
|
||||||
unmetafy(fname = ztrdup(fname), &fd);
|
unmetafy(fname = ztrdup(fname), &fd);
|
||||||
|
@ -196,12 +198,8 @@ get_contents(char *fname)
|
||||||
close(fd);
|
close(fd);
|
||||||
#else /* don't USE_MMAP */
|
#else /* don't USE_MMAP */
|
||||||
val = NULL;
|
val = NULL;
|
||||||
if ((fd = open(fname, O_RDONLY | O_NOCTTY)) >= 0) {
|
if ((size = zstuff(&val, fname)) > 0)
|
||||||
LinkList ll;
|
val = metafy(val, size, META_HEAPDUP);
|
||||||
|
|
||||||
if ((ll = readoutput(fd, 1, 0)))
|
|
||||||
val = peekfirst(ll);
|
|
||||||
}
|
|
||||||
#endif /* USE_MMAP */
|
#endif /* USE_MMAP */
|
||||||
free(fname);
|
free(fname);
|
||||||
return val;
|
return val;
|
||||||
|
|
44
Src/input.c
44
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
|
off_t
|
||||||
stuff(char *fn)
|
zstuff(char **out, const char *fn)
|
||||||
{
|
{
|
||||||
FILE *in;
|
FILE *in;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
@ -622,20 +622,50 @@ stuff(char *fn)
|
||||||
|
|
||||||
if (!(in = fopen(unmeta(fn), "r"))) {
|
if (!(in = fopen(unmeta(fn), "r"))) {
|
||||||
zerr("can't open %s", fn);
|
zerr("can't open %s", fn);
|
||||||
return 1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
queue_signals();
|
||||||
fseek(in, 0, SEEK_END);
|
fseek(in, 0, SEEK_END);
|
||||||
len = ftell(in);
|
len = ftell(in);
|
||||||
fseek(in, 0, SEEK_SET);
|
fseek(in, 0, SEEK_SET);
|
||||||
buf = (char *)zalloc(len + 1);
|
buf = (char *)zalloc(len + 1);
|
||||||
if (!(fread(buf, len, 1, in))) {
|
if (len && !(fread(buf, len, 1, in))) {
|
||||||
zerr("read error on %s", fn);
|
zerr("read error on %s", fn);
|
||||||
fclose(in);
|
fclose(in);
|
||||||
zfree(buf, len + 1);
|
unqueue_signals();
|
||||||
return 1;
|
return -1;
|
||||||
}
|
}
|
||||||
fclose(in);
|
fclose(in);
|
||||||
buf[len] = '\0';
|
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);
|
fwrite(buf, len, 1, stderr);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
inputsetline(metafy(buf, len, META_REALLOC), INP_FREE);
|
inputsetline(metafy(buf, len, META_REALLOC), INP_FREE);
|
||||||
|
|
Loading…
Reference in a new issue