summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util/nvmutil/nvmutil.c38
1 files changed, 31 insertions, 7 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 719a4810..b40a0910 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -714,11 +714,25 @@ ino_t tmp_ino;
int tmp_fd = -1;
char *tname = NULL;
+/*
+ * Used for checking whether.
+ * a file is a file via stat().
+ *
+ * Portable macro for compatibility
+ * with older unix e.g. v7 unix (has S_IFREG),
+ * 4.2bsd (has S_IFMT) or POSIX (has S_ISREG)
+ *
+ * Fallback works where S_IFREG == 0100000
+ * (classic unix bitmask)
+ */
+
#ifndef S_ISREG
-#ifdef S_IFMT
-#define S_ISREG(m) (((m) & (S_IFMT)) == (S_IFREG))
+#if defined(S_IFMT) && defined(S_IFREG)
+#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
+#elif defined(S_IFREG)
+#define S_ISREG(m) (((m) & S_IFREG) != 0)
#else
-#define S_ISREG(m) (((m) & (S_IFREG)) == (S_IFREG))
+#error "can't determine types with stat()"
#endif
#endif
@@ -3039,10 +3053,9 @@ x_i_mkstemp(char *template)
char *p;
char ch[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- unsigned long chlen;
+ unsigned long r = rlong();
len = xstrxlen(template, PATH_LEN);
- chlen = xstrxlen(ch, 100);
/* find trailing XXXXXX */
if (len < 6)
@@ -3053,7 +3066,7 @@ x_i_mkstemp(char *template)
for (i = 0; i < 100; i++) {
for (j = 0; j < 6; j++)
- p[j] = ch[rlong() % chlen];
+ p[j] = ch[r % (sizeof(ch) - 1)];
fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
@@ -3191,6 +3204,12 @@ x_i_memcmp(const void *a, const void *b, unsigned long n)
return 0;
}
+/*
+ * emulate fchmod() using file descriptor
+ * paths, for old unix portability. should
+ * work on e.g. BSD/MacOS (/dev/fd/N),
+ * Linux (/proc/self/fd/N) and others
+ */
int
x_i_fchmod(int fd, mode_t mode)
{
@@ -3213,13 +3232,18 @@ x_try_fdpath(const char *prefix, int fd, mode_t mode)
unsigned long j;
while (prefix[i]) {
+ if (i >= PATH_LEN - 1)
+ return -1;
path[i] = prefix[i];
i++;
}
j = x_conv_fd(path + i, (unsigned long)fd);
- i += j;
+ if (i + j >= PATH_LEN)
+ return -1;
+
+ i += j;
path[i] = '\0';
return chmod(path, mode);