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.
fsdg20230625
Leah Rowe 2022-11-26 11:02:28 +00:00
parent b0f9f47e9a
commit 0bbd4f1f26
1 changed files with 24 additions and 18 deletions

View File

@ -42,6 +42,7 @@ int validChecksum(int partnum);
uint16_t word(int pos16, int partnum); uint16_t word(int pos16, int partnum);
void setWord(int pos16, int partnum, uint16_t val); void setWord(int pos16, int partnum, uint16_t val);
void byteswap(uint8_t *byte); void byteswap(uint8_t *byte);
void writeGbeFile(int *fd, const char *filename);
#define FILENAME argv[1] #define FILENAME argv[1]
#define COMMAND argv[2] #define COMMAND argv[2]
@ -60,7 +61,7 @@ uint8_t little_endian;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd, partnum; int fd;
int flags = O_RDWR; int flags = O_RDWR;
char *strMac = NULL; char *strMac = NULL;
char *strRMac = "??:??:??:??:??:??"; char *strRMac = "??:??:??:??:??:??";
@ -104,23 +105,8 @@ main(int argc, char *argv[])
else else
cmd(COMMAND); cmd(COMMAND);
if (gbeFileModified) { if (gbeFileModified)
errno = 0; writeGbeFile(&fd, FILENAME);
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);
}
}
} }
nvmutil_exit: nvmutil_exit:
@ -132,6 +118,26 @@ nvmutil_exit:
return errno; 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 ssize_t
readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size) readFromFile(int *fd, uint8_t *buf, const char *path, int flags, size_t size)
{ {