~emersion/mrsh-dev

make read return 1 when an EOF is encountered v1 PROPOSED

Stone Tickle: 1
 make read return 1 when an EOF is encountered

 3 files changed, 24 insertions(+), 1 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/patches/9717/mbox | git am -3
Learn more about email & git

[PATCH] make read return 1 when an EOF is encountered Export this patch

For instance:
echo "hi" | read arg
should have an exit code of >0 per the POSIX spec

- a test was also added
---
 builtin/read.c   |  7 ++++++-
 test/meson.build |  1 +
 test/read.sh     | 17 +++++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 test/read.sh

diff --git a/builtin/read.c b/builtin/read.c
index d014969..a0ce82e 100644
--- a/builtin/read.c
@@ -97,5 +97,10 @@ int builtin_read(struct mrsh_state *state, int argc, char *argv[]) {
		free(fields.data[i]);
	}
	mrsh_array_finish(&fields);
	return 0;

	if (c == EOF) {
		return 1;
	} else {
		return 0;
	}
}
diff --git a/test/meson.build b/test/meson.build
index fd8aefb..b58720a 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -12,6 +12,7 @@ test_files = [
	'function.sh',
	'loop.sh',
	'pipeline.sh',
	'read.sh',
	'readonly.sh',
	'redir.sh',
	'return.sh',
diff --git a/test/read.sh b/test/read.sh
new file mode 100644
index 0000000..e77e072
--- /dev/null
+++ b/test/read.sh
@@ -0,0 +1,17 @@
#!/bin/sh -eu


printf | read a
echo $?

i=0

printf "a\nb\nc\n" | while read line; do
  printf "%s\n" "${line:-blank}"

  i=$((i+1))

  [ $i -gt 10 ] && break
done

[ $i = 3 ] && echo "correct!"
-- 
2.24.1
LGTM. Slightly edited the commit message and pushed.

Thanks!
View this thread in the archives