From 849a73012eea818e05d6b03ecf427dcdcf8a5867 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Mon, 16 Mar 2026 16:52:28 +0000 Subject: util/nvmutil: portable fchmod and with that, now the code compiles on gcc with -std=c90 -pedantic with -Werror and -Wall -Wextra Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'util/nvmutil/nvmutil.c') diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 3cea175d..7731d31f 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -491,6 +491,7 @@ static void *x_v_memcpy(void *dst, const void *src, unsigned long n); static int x_i_memcmp(const void *a, const void *b, unsigned long n); +static int x_i_fchmod(int fd, mode_t mode); /* * Sizes in bytes: @@ -2954,7 +2955,7 @@ new_tmpfile(int *fd, int local, const char *path) if (fd_tmp == -1) goto err_new_tmpfile; - if (fchmod(fd_tmp, 0600) == -1) + if (x_i_fchmod(fd_tmp, 0600) == -1) goto err_new_tmpfile; if (lock_file(fd_tmp) == -1) @@ -3175,3 +3176,42 @@ x_i_memcmp(const void *a, const void *b, unsigned long n) return 0; } + +static int +x_i_fchmod(int fd, mode_t mode) +{ + char path[32]; + char tmp[16]; + int i = 0; + int j = 0; + int n; + + /* build "/dev/fd/" */ + path[i++] = '/'; + path[i++] = 'd'; + path[i++] = 'e'; + path[i++] = 'v'; + path[i++] = '/'; + path[i++] = 'f'; + path[i++] = 'd'; + path[i++] = '/'; + + /* convert fd to decimal */ + n = fd; + + if (n == 0) { + path[i++] = '0'; + } else { + while (n > 0) { + tmp[j++] = (char)('0' + (n % 10)); + n /= 10; + } + + while (j > 0) + path[i++] = tmp[--j]; + } + + path[i] = '\0'; + + return chmod(path, mode); +} -- cgit v1.2.1