aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpetershh <petershh@disroot.org>2022-08-02 23:44:07 +0300
committerPeter <santurysim@gmail.com>2022-08-03 00:37:07 +0300
commit83fe4566cdc49db506a0b84a0f1df01b03a14979 (patch)
tree1c90bdea714094b9a21b3c060a99588c958648bd
parent8f77364e08e6b2f6e75d455db81c437eff014d05 (diff)
downloadshh-portable-utils-83fe4566cdc49db506a0b84a0f1df01b03a14979.tar.xz
parse_mode_symbolic.c: modify error reporting
also bugfix
-rw-r--r--src/shh-portable-utils/chmod.c11
-rw-r--r--src/shh-portable-utils/parse_mode_symbolic.c49
2 files changed, 40 insertions, 20 deletions
diff --git a/src/shh-portable-utils/chmod.c b/src/shh-portable-utils/chmod.c
index 51a41fe..48a2305 100644
--- a/src/shh-portable-utils/chmod.c
+++ b/src/shh-portable-utils/chmod.c
@@ -56,11 +56,12 @@ int main(int argc, char const *const *argv)
if (parse_mode_octal(argv[0], &mode))
strerr_dief2sys(100, "invalid mode: ", argv[0]);
} else {
- int result = parse_mode_symbolic(argv[0], &directives);
- if (result == 1)
- strerr_dief2x(100, "invalid mode: ", argv[0]);
- else if (result == 2)
- strerr_diefu1sys(111, "parse mode");
+ if (parse_mode_symbolic(argv[0], &directives) == -1) {
+ if (errno == EINVAL)
+ strerr_diefu2sys(100, "invalid mode: ", argv[0]);
+ else
+ strerr_diefu1sys(111, "parse mode")
+ }
if (!genalloc_len(chmod_directive, &directives))
strerr_dieusage(100, USAGE);
}
diff --git a/src/shh-portable-utils/parse_mode_symbolic.c b/src/shh-portable-utils/parse_mode_symbolic.c
index 536757d..96cb94a 100644
--- a/src/shh-portable-utils/parse_mode_symbolic.c
+++ b/src/shh-portable-utils/parse_mode_symbolic.c
@@ -1,3 +1,5 @@
+#include <errno.h>
+
#include <skalibs/genalloc.h>
#include <skalibs/strerr2.h>
@@ -30,7 +32,8 @@ int parse_mode_symbolic(char const *raw, genalloc *directives)
d.action = *p;
break;
default:
- return 1;
+ errno = EINVAL;
+ return -1;
}
p++;
for (;;) {
@@ -56,19 +59,26 @@ int parse_mode_symbolic(char const *raw, genalloc *directives)
break;
}
else if (*p == '\0') {
- if (d.perm && d.permcopy)
- return 1;
+ if (d.perm && d.permcopy) {
+ errno = EINVAL;
+ return -1;
+ }
if (!genalloc_append(chmod_directive, directives, &d))
- return 2;
+ return -1; /* errno = ENOMEM */
return 0;
+ } else {
+ errno = EINVAL;
+ return -1;
}
p++;
}
- if (d.perm && d.permcopy)
- return 1;
+ if (d.perm && d.permcopy) {
+ errno = EINVAL;
+ return -1;
+ }
if (!genalloc_append(chmod_directive, directives, &d))
- return 2;
+ return -1; /* errno = ENOMEM */
for (;;) {
d.action = d.perm = d.permcopy = d.dir_x = 0;
@@ -80,8 +90,10 @@ int parse_mode_symbolic(char const *raw, genalloc *directives)
}
else if (*p == '\0')
return 0;
- else
- return 1;
+ else {
+ errno = EIVAL;
+ return -1;
+ }
p++;
@@ -109,19 +121,26 @@ int parse_mode_symbolic(char const *raw, genalloc *directives)
break;
}
else if (*p == '\0') {
- if (d.perm && d.permcopy)
- return 1;
+ if (d.perm && d.permcopy) {
+ errno = EINVAL;
+ return -1;
+ }
if (!genalloc_append(chmod_directive, directives, &d))
- return 2;
+ return -1; /* errno = ENOMEM */
return 0;
+ } else {
+ errno = EINVAL;
+ return -1;
}
p++;
}
- if (d.perm && d.permcopy)
- return 1;
+ if (d.perm && d.permcopy) {
+ errno = EINVAL;
+ return -1;
+ }
if (!genalloc_append(chmod_directive, directives, &d))
- return 2;
+ return -1; /* errno = ENOMEM */
}
}
return 0;