From 0bbd4f1f262ed6b8eba131b62c941a6b2400763f Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 26 Nov 2022 11:02:28 +0000 Subject: util/nvmutil: write gbe files in a function in any C program, main() should not contain detailed logic. ideally, the main() function should only be a skeleton, showing the overall logic flow of the program. split writing gbe files into a separate function, to satisfy this criteria. --- util/nvmutil/nvmutil.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'util/nvmutil') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index b3c9031b..f9e56090 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -42,6 +42,7 @@ int validChecksum(int partnum); uint16_t word(int pos16, int partnum); void setWord(int pos16, int partnum, uint16_t val); void byteswap(uint8_t *byte); +void writeGbeFile(int *fd, const char *filename); #define FILENAME argv[1] #define COMMAND argv[2] @@ -60,7 +61,7 @@ uint8_t little_endian; int main(int argc, char *argv[]) { - int fd, partnum; + int fd; int flags = O_RDWR; char *strMac = NULL; char *strRMac = "??:??:??:??:??:??"; @@ -104,23 +105,8 @@ main(int argc, char *argv[]) else cmd(COMMAND); - if (gbeFileModified) { - errno = 0; - if (pwrite(fd, gbe, SIZE_8KB, 0) == SIZE_8KB) - close(fd); - if (errno == 0) { - for (partnum = 0; partnum < 2; partnum++) { - if (nvmPartModified[partnum]) - printf("Part %d modified\n", - partnum); - else - fprintf (stderr, "Part %d NOT " - "modified\n", partnum); - } - printf("File `%s` successfully " - "modified\n", FILENAME); - } - } + if (gbeFileModified) + writeGbeFile(&fd, FILENAME); } nvmutil_exit: @@ -132,6 +118,26 @@ nvmutil_exit: return errno; } +void +writeGbeFile(int *fd, const char *filename) +{ + int partnum; + errno = 0; + + if (pwrite((*fd), gbe, SIZE_8KB, 0) == SIZE_8KB) + close((*fd)); + if (errno == 0) { + for (partnum = 0; partnum < 2; partnum++) { + if (nvmPartModified[partnum]) + printf("Part %d modified\n", partnum); + else + fprintf (stderr, + "Part %d NOT modified\n", partnum); + } + printf("File `%s` successfully modified\n", filename); + } +} + ssize_t readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size) { -- cgit v1.2.1