Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: $(<nofile) doesn't set $? to non-zero
- X-seq: zsh-workers 42465
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Peter Stephenson <p.stephenson@xxxxxxxxxxx>
- Subject: Re: $(<nofile) doesn't set $? to non-zero
- Date: Thu, 15 Mar 2018 07:12:04 +0000
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=780H0UwMW8G9owv5spe5EhKsR+CXJwlB1QZMQMvgdks=; b=QdSxU8VKvJUu5bTwtJIQrqH9pbxn6Ef0Ff808ttQ4UBRKjh2lOLkWT9iDLNZogt8Qa bX/EiGIAEeS6akcizblqHPuI0rF4M49xhHCWUIJX8Yv8VxHAr0ewUb3ZGw4mnzqtMqTz f+3H37FKZPVKC53IlVTA38TNBkS2kt9+HUpOr+fsWZ1hVXUhlsQDkVQHCOpk6bWWTXmo 8CKgRSXxE5P4VqJeLvyz6owk4rqlRWGDaxzFVSepSKwQKcgHDjRlj/5MWSMvq3Mt8eRu gXq+yUkHK2dPY1mcBkdkG9WWy9akYDe5f0RXUhft8SnpaFW4M/BfH8iu8pLZ/LRc5Dle 1rmQ==
- In-reply-to: <20180314145004.6e4b971b@camnpupstephen>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mail-followup-to: Peter Stephenson <p.stephenson@xxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CGME20180314103335epcas4p30222f0df02adda27cbddbe62075ff9ad@epcas4p3.samsung.com> <20180314103254.GA10404@chaz.gmail.com> <20180314105442.28c5554a@camnpupstephen> <20180314144248.GC10404@chaz.gmail.com> <20180314145004.6e4b971b@camnpupstephen>
2018-03-14 14:50:04 +0000, Peter Stephenson:
> On Wed, 14 Mar 2018 14:42:48 +0000
> Stephane Chazelas <stephane.chazelas@xxxxxxxxx> wrote:
> > Would it be worth doing some:
> >
> > ret = readoutput(stream, qt);
> > if (errno) {
> > zwarn("%e: %s", errno, s);
> > lastval = cmdoutval = 1;
> > }
> > return ret;
> >
> > there (or something cleaner to avoid relying on errno)?
>
> The return value is the linked list as we're in the context of
> substitution, not command execution, which isn't easy to change without
> a complete rewrite.
[...]
Why not just pass the error as a return argument like in the
patch below?
Then:
$ ./Src/zsh -c 'ERRNO=0; v=$(</x); echo $? $ERRNO $#v'
zsh:1: no such file or directory: /x
1 2 0
$ ./Src/zsh -c 'ERRNO=0; v=$(</); echo $? $ERRNO $#v'
zsh:1: error when reading /: is a directory
1 21 0
$ ./Src/zsh -c 'ERRNO=0; v=$(</dev/mem); echo $? $ERRNO $#v'
zsh:1: permission denied: /dev/mem
1 13 0
$ sudo ./Src/zsh -c 'ERRNO=0; v=$(</dev/mem); echo $? $ERRNO $#v'
zsh:1: error when reading /dev/mem: operation not permitted
1 1 1046296
We still expand to what could be read but signal the error like
when doing the unoptimised $(cat file).
diff --git a/Src/Modules/mapfile.c b/Src/Modules/mapfile.c
index 2503b36..7c5a1e3 100644
--- a/Src/Modules/mapfile.c
+++ b/Src/Modules/mapfile.c
@@ -197,8 +197,9 @@ get_contents(char *fname)
val = NULL;
if ((fd = open(fname, O_RDONLY | O_NOCTTY)) >= 0) {
LinkList ll;
+ int readerror;
- if ((ll = readoutput(fd, 1)))
+ if ((ll = readoutput(fd, 1, 0)))
val = peekfirst(ll);
}
#endif /* USE_MMAP */
diff --git a/Src/exec.c b/Src/exec.c
index e5c6455..32be683 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4507,6 +4507,8 @@ getoutput(char *cmd, int qt)
if ((s = simple_redir_name(prog, REDIR_READ))) {
/* $(< word) */
int stream;
+ LinkList retval;
+ int readerror;
singsub(&s);
if (errflag)
@@ -4514,9 +4516,15 @@ getoutput(char *cmd, int qt)
untokenize(s);
if ((stream = open(unmeta(s), O_RDONLY | O_NOCTTY)) == -1) {
zwarn("%e: %s", errno, s);
+ lastval = cmdoutval = 1;
return newlinklist();
}
- return readoutput(stream, qt);
+ retval = readoutput(stream, qt, &readerror);
+ if (readerror) {
+ zwarn("error when reading %s: %e", s, readerror);
+ lastval = cmdoutval = 1;
+ }
+ return retval;
}
if (mpipe(pipes) < 0) {
errflag |= ERRFLAG_ERROR;
@@ -4537,7 +4545,7 @@ getoutput(char *cmd, int qt)
LinkList retval;
zclose(pipes[1]);
- retval = readoutput(pipes[0], qt);
+ retval = readoutput(pipes[0], qt, 0);
fdtable[pipes[0]] = FDT_UNUSED;
waitforpid(pid, 0); /* unblocks */
lastval = cmdoutval;
@@ -4562,7 +4570,7 @@ getoutput(char *cmd, int qt)
/**/
mod_export LinkList
-readoutput(int in, int qt)
+readoutput(int in, int qt, int *readerror)
{
LinkList ret;
char *buf, *ptr;
@@ -4591,6 +4599,8 @@ readoutput(int in, int qt)
}
*ptr++ = c;
}
+ if (readerror && ferror(fin))
+ *readerror = errno;
fclose(fin);
while (cnt && ptr[-1] == '\n')
ptr--, cnt--;
Messages sorted by:
Reverse Date,
Date,
Thread,
Author