commit: 90c05aa4a2f433260329a6b65a52a51f52dc1048
parent 1691a635e66f4cfeab54e893b4ef220a64b03002
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 11 Feb 2022 21:17:24 +0100
bin/xcd: Handle permission errors
Diffstat:
2 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/bin/xcd.c b/bin/xcd.c
@@ -130,7 +130,11 @@ main(int argc, char *argv[])
if(argc <= 1)
{
- err += concat(stdin);
+ if(concat(stdin) != 0)
+ {
+ err = 1;
+ goto cleanup;
+ }
}
else
{
@@ -138,14 +142,20 @@ main(int argc, char *argv[])
{
if(strncmp(argv[argi], "-", 2) == 0)
{
- err += concat(stdin);
+ if(concat(stdin) != 0)
+ {
+ err = 1;
+ goto cleanup;
+ }
}
else
{
FILE *file = fopen(argv[argi], "r");
if(!file)
{
- printf("\n[0mError opening ‘%s’: %s\n", argv[argi], strerror(errno));
+ fprintf(stderr, "\n[0mError opening ‘%s’: %s\n", argv[argi], strerror(errno));
+ err = 1;
+ goto cleanup;
}
else
{
@@ -153,13 +163,16 @@ main(int argc, char *argv[])
if((err += fclose(file)) != 0)
{
- printf("\n[0mError closing ‘%s’: %s\n", argv[argi], strerror(errno));
+ fprintf(stderr, "\n[0mError closing ‘%s’: %s\n", argv[argi], strerror(errno));
+ err = 1;
+ goto cleanup;
}
}
}
}
}
+cleanup:
printf("[0m");
return err;
diff --git a/test-bin/xcd b/test-bin/xcd
@@ -19,10 +19,22 @@ nullinput_body() {
atf_check -o file:outputs/xcd/null ../bin/xcd </dev/null
}
+atf_test_case noperm cleanup
+noperm_body() {
+ touch inputs/chmod_000 || atf_fail "touching chmod_000"
+ chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
+ atf_check -s exit:1 -e 'inline:\n[0mError opening ‘inputs/chmod_000’: Permission denied\n' -o 'inline:[0m' ../bin/xcd inputs/chmod_000
+}
+noperm_cleanup() {
+ chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
+ rm inputs/chmod_000 || atf_fail "rm chmod_000"
+}
+
atf_init_test_cases() {
cd "$(atf_get_srcdir)"
atf_add_test_case openfile
atf_add_test_case stdinput
atf_add_test_case nullfile
atf_add_test_case nullinput
+ atf_add_test_case noperm
}