summaryrefslogtreecommitdiff
path: root/util/nvmutil/nvmutil.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r--util/nvmutil/nvmutil.c55
1 files changed, 31 insertions, 24 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 256c3b88..e60501ab 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -20,8 +20,9 @@ void cmd_setchecksum(void), cmd_brick(void), swap(int partnum), writeGbe(void),
parseMacString(const char *strMac, uint16_t *mac), cmd_swap(void),
openFiles(void), cmd_copy(void), writeGbe_part(int), readGbe_part(int),
set_cmd(int, char **), setWord(int, int, uint16_t), check_bounds(int, int),
- xopen (int *, const char *, int p, struct stat *), checkMacSeparator(int),
- set_mac_byte(int, uint64_t *), usage(char*), set_io_flags(int, char **);
+ xopen(int *, const char *, int p, struct stat *), checkMacSeparator(int),
+ set_mac_byte(int, uint64_t *), usage(char*), set_io_flags(int, char **),
+ err_if(int);
int goodChecksum(int partnum), write_mac_part(int), set_err(int);
uint8_t hextonum(char chs), rhex(void);
uint16_t word(int, int);
@@ -43,7 +44,7 @@ uint16_t mac[3] = {0, 0, 0};
size_t partsize;
int flags, rfd, fd, part, e = 1;
-const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *fname = NULL;
+const char *strMac = NULL, *strRMac = "xx:xx:xx:xx:xx:xx", *fname = "";
typedef struct op {
char *str;
@@ -60,8 +61,6 @@ op_t op[] = {
};
void (*cmd)(void) = NULL;
-#define err_if(x) if (x) err(set_err(ECANCELED), "%s", fname)
-
int
main(int argc, char *argv[])
{
@@ -93,7 +92,7 @@ main(int argc, char *argv[])
writeGbe();
err_if((errno != 0) && (cmd != cmd_dump));
- return errno ? 1 : 0;
+ return errno ? EXIT_FAILURE : EXIT_SUCCESS;
}
void
@@ -140,24 +139,15 @@ set_io_flags(int argc, char *argv[])
}
void
-checkdir(const char *path)
-{
- if (opendir(path) != NULL)
- err(set_err(EISDIR), "%s", path);
- if (errno == ENOTDIR)
- errno = 0;
- err_if(errno);
-}
-
-void
openFiles(void)
{
struct stat st;
+ struct stat st_rfd;
checkdir("/dev/urandom");
checkdir(fname);
- xopen(&rfd, "/dev/urandom", O_RDONLY, &st);
+ xopen(&rfd, "/dev/urandom", O_RDONLY, &st_rfd);
xopen(&fd, fname, flags, &st);
switch(st.st_size) {
@@ -173,12 +163,22 @@ openFiles(void)
}
void
+checkdir(const char *path)
+{
+ struct stat st;
+ if (stat(path, &st) == -1)
+ err(set_err(ECANCELED), "%s", path);
+ if (S_ISDIR(st.st_mode))
+ err(set_err(EISDIR), "%s", path);
+}
+
+void
xopen(int *f, const char *l, int p, struct stat *st)
{
- if ((*f = open(l, p)) == -1) \
- err(set_err(ECANCELED), "%s", l); \
- if (fstat(*f, st) == -1) \
- err(set_err(ECANCELED), "%s", l);
+ if ((*f = open(l, p)) == -1)
+ err(set_err(ECANCELED), "%s", l);
+ if (fstat(*f, st) == -1)
+ err(set_err(ECANCELED), "%s", l);
}
void
@@ -453,8 +453,8 @@ swap(int partnum)
for (size_t w = NVM_SIZE * ((uint8_t *) &e)[0], x = 1;
w < NVM_SIZE; w += 2, x += 2) {
uint8_t chg = n[w];
- n[w] ^= n[x];
- n[x] ^= chg;
+ n[w] = n[x];
+ n[x] = chg;
}
}
@@ -478,9 +478,16 @@ usage(char *util)
err(set_err(ECANCELED), "Too few arguments");
}
+void
+err_if(int x)
+{
+ if (x)
+ err(set_err(ECANCELED), "%s", fname);
+}
+
int
set_err(int x)
{
errno = errno ? errno : x;
- return 1;
+ return EXIT_FAILURE;
}