From a07a25a0eb4d171078f949ab26749b1eee82b4d9 Mon Sep 17 00:00:00 2001 From: petershh Date: Mon, 1 Aug 2022 11:51:36 +0300 Subject: Move error handling back to chmod.c --- src/shh-portable-utils/chmod.c | 13 +++++++---- src/shh-portable-utils/parse_mode_octal.c | 32 ++++++++++++++-------------- src/shh-portable-utils/parse_mode_symbolic.c | 25 +++++++++++----------- src/shh-portable-utils/shhfuncs.h | 4 ++-- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/shh-portable-utils/chmod.c b/src/shh-portable-utils/chmod.c index 47e91ba..e661746 100644 --- a/src/shh-portable-utils/chmod.c +++ b/src/shh-portable-utils/chmod.c @@ -48,10 +48,15 @@ int main(int argc, char const *const *argv) mask = umask(0); umask(mask); - if ('0' <= argv[0][0] && argv[0][0] <= '7') - mode = parse_mode_octal(argv[0]); - else { - parse_mode_symbolic(argv[0], &directives); + if ('0' <= argv[0][0] && argv[0][0] <= '7') { + 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 (!genalloc_len(chmod_directive, &directives)) strerr_dieusage(100, USAGE); } diff --git a/src/shh-portable-utils/parse_mode_octal.c b/src/shh-portable-utils/parse_mode_octal.c index 8de58bd..e678727 100644 --- a/src/shh-portable-utils/parse_mode_octal.c +++ b/src/shh-portable-utils/parse_mode_octal.c @@ -3,37 +3,37 @@ #include "shhfuncs.h" -mode_t parse_mode_octal(char const *raw_mode) +int parse_mode_octal(char const *raw_mode, mode_t *mode) { - mode_t mode = 0; + *mode = 0; unsigned int m; if (!uint0_oscan(raw_mode, &m)) - strerr_dief2x(100, "invalid mode: ", raw_mode); + return 1; if (m & 0001) - mode |= S_IXOTH; + *mode |= S_IXOTH; if (m & 0002) - mode |= S_IWOTH; + *mode |= S_IWOTH; if (m & 0004) - mode |= S_IROTH; + *mode |= S_IROTH; if (m & 0010) - mode |= S_IXGRP; + *mode |= S_IXGRP; if (m & 0020) - mode |= S_IWGRP; + *mode |= S_IWGRP; if (m & 0040) - mode |= S_IRGRP; + *mode |= S_IRGRP; if (m & 0100) - mode |= S_IXUSR; + *mode |= S_IXUSR; if (m & 0200) - mode |= S_IWUSR; + *mode |= S_IWUSR; if (m & 0400) - mode |= S_IRUSR; + *mode |= S_IRUSR; if (m & 01000) - mode |= S_ISVTX; + *mode |= S_ISVTX; if (m & 02000) - mode |= S_ISGID; + *mode |= S_ISGID; if (m & 04000) - mode |= S_ISUID; + *mode |= S_ISUID; - return mode; + return 0; } diff --git a/src/shh-portable-utils/parse_mode_symbolic.c b/src/shh-portable-utils/parse_mode_symbolic.c index 04f3817..4f70dbb 100644 --- a/src/shh-portable-utils/parse_mode_symbolic.c +++ b/src/shh-portable-utils/parse_mode_symbolic.c @@ -3,7 +3,7 @@ #include "shhfuncs.h" -void parse_mode_symbolic(char const *raw, genalloc *directives) +int parse_mode_symbolic(char const *raw, genalloc *directives) { char const *p = raw; for (;;) { @@ -30,7 +30,7 @@ void parse_mode_symbolic(char const *raw, genalloc *directives) d.action = *p; break; default: - strerr_dief2x(100, "invalid mode: ", raw); + return 1; } p++; for (;;) { @@ -58,18 +58,18 @@ void parse_mode_symbolic(char const *raw, genalloc *directives) } else if (*p == '\0') { if (d.perm && d.permcopy) - strerr_dief2x(100, "invalid mode: ", raw); + return 1; if (!genalloc_append(chmod_directive, directives, &d)) - strerr_diefu1sys(111, "parse mode"); - return; + return 2; + return 0; } p++; } if (d.perm && d.permcopy) - strerr_dief2x(100, "invalid mode: ", raw); + return 1; if (!genalloc_append(chmod_directive, directives, &d)) - strerr_diefu1sys(111, "parse mode"); + return 2; for (;;) { d.action = d.perm = d.permcopy = d.dir_x = 0; @@ -78,9 +78,9 @@ void parse_mode_symbolic(char const *raw, genalloc *directives) else if (*p == ',') break; else if (*p == '\0') - return; + return 0; else - strerr_dief2x(100, "invalid mode: ", raw); + return 1; p++; @@ -112,16 +112,17 @@ void parse_mode_symbolic(char const *raw, genalloc *directives) strerr_dief2x(100, "invalid mode: ", raw); if (!genalloc_append(chmod_directive, directives, &d)) strerr_diefu1sys(111, "parse mode"); - return; + return 0; } p++; } if (d.perm && d.permcopy) - strerr_dief2x(100, "invalid mode: ", raw); + return 1; if (!genalloc_append(chmod_directive, directives, &d)) - strerr_diefu1sys(111, "parse mode"); + return 2; } } + return 0; } diff --git a/src/shh-portable-utils/shhfuncs.h b/src/shh-portable-utils/shhfuncs.h index 1614eac..f02dcff 100644 --- a/src/shh-portable-utils/shhfuncs.h +++ b/src/shh-portable-utils/shhfuncs.h @@ -18,8 +18,8 @@ typedef struct chmod_directive_s chmod_directive; #define CHMOD_DIRECTIVE_ZERO { 0, 0, 0, 0, 0 } -mode_t parse_mode_octal(char const*); -void parse_mode_symbolic(char const*, genalloc*); +int parse_mode_octal(char const*, mode_t*); +int parse_mode_symbolic(char const*, genalloc*); /* * Algorithm for chmod mode parsing was inspired by sbase's chmod -- cgit v1.2.3