diff options
author | Leah Rowe <leah@libreboot.org> | 2023-05-31 09:30:13 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2023-05-31 09:30:13 +0100 |
commit | 8f1e6d792f9abfe7fa5e9865f894cdcbefc5e394 (patch) | |
tree | ee4694bc988069bf9f57b515c45912328bb889cb /util/nvmutil/nvmutil.c | |
parent | 78fc89352b23571f6bf4d7ef7e9f4a464dfe373c (diff) |
util/nvmutil: simplified error handling in main
This change also reduces code indentation.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r-- | util/nvmutil/nvmutil.c | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 26c31d2c..e75ba650 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -58,6 +58,7 @@ void byteswap(int n, int partnum); void writeGbeFile(int *fd, const char *filename, size_t nw); void xpledge(const char *promises, const char *execpromises); void xunveil(const char *path, const char *permissions); +void err_if(int condition); #define FILENAME argv[1] #define COMMAND argv[2] @@ -118,31 +119,27 @@ main(int argc, char *argv[]) } } - if ((strMac == NULL) && (cmd == NULL)) - errno = EINVAL; - - if (errno == 0) { - skipread[part ^ 1] = (cmd == &cmd_copy) | - (cmd == &cmd_setchecksum) | (cmd == &cmd_brick); - readGbeFile(&fd, FILENAME, flags, nr); - (void)rhex(); - xunveil("/dev/urandom", "r"); - if (flags == O_RDONLY) { - xpledge("stdio", NULL); - } else { - xpledge("stdio wpath unveil", NULL); - xunveil(FILENAME, "w"); - } - if (strMac != NULL) - cmd_setmac(strMac); /* nvm gbe.bin setmac */ - else if (cmd != NULL) - (*cmd)(); /* all other commands except setmac */ - writeGbeFile(&fd, FILENAME, nr); - } else + err_if(errno = ((strMac == NULL) && (cmd == NULL)) ? EINVAL : errno); + + skipread[part ^ 1] = (cmd == &cmd_copy) | + (cmd == &cmd_setchecksum) | (cmd == &cmd_brick); + readGbeFile(&fd, FILENAME, flags, nr); + (void)rhex(); + xunveil("/dev/urandom", "r"); + if (flags == O_RDONLY) { xpledge("stdio", NULL); + } else { + xpledge("stdio wpath unveil", NULL); + xunveil(FILENAME, "w"); + } - if ((errno != 0) && (cmd != &cmd_dump)) - err(errno, NULL); + if (strMac != NULL) + cmd_setmac(strMac); /* nvm gbe.bin setmac */ + else if (cmd != NULL) + (*cmd)(); /* all other commands except setmac */ + writeGbeFile(&fd, FILENAME, nr); + + err_if((errno != 0) && (cmd != &cmd_dump)); return errno; } @@ -396,3 +393,13 @@ xunveil(const char *path, const char *permissions) err(errno, NULL); #endif } + +void +err_if(int condition) +{ + if (!condition) + return; + if (!errno) + errno = ECANCELED; + err(errno, NULL); +} |