summaryrefslogtreecommitdiff
path: root/lib/canonicalize.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2020-12-02 14:25:43 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2020-12-02 14:39:30 -0800
commitfdc6f3f80ed6b2eb2777bfca11e94e19d639ed21 (patch)
tree6d8486bcbfbab1e2a351b89592cfcf67b873b3d0 /lib/canonicalize.c
parentabbbc51d6334e3459bb7ef55a219a0883034f62d (diff)
downloadgnulib-fdc6f3f80ed6b2eb2777bfca11e94e19d639ed21.tar.gz
canonicalize: refactor can_mode flag
* lib/canonicalize.c (MULTIPLE_BITS_SET): Remove, replacing with ... (multiple_bits_set): ... this new static function. Uses changed. (canonicalize_filename_mode): Refactor for clarity to avoid modifying the CAN_MODE argument.
Diffstat (limited to 'lib/canonicalize.c')
-rw-r--r--lib/canonicalize.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/canonicalize.c b/lib/canonicalize.c
index e363cfef33..d12fc6b8ff 100644
--- a/lib/canonicalize.c
+++ b/lib/canonicalize.c
@@ -33,8 +33,6 @@
#include "xgetcwd.h"
#include "filename.h"
-#define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
-
/* In this file, we cannot handle file names longer than PATH_MAX.
On systems with no file name length limit, use a fallback. */
#ifndef PATH_MAX
@@ -65,6 +63,12 @@ canonicalize_file_name (const char *name)
}
#endif /* !HAVE_CANONICALIZE_FILE_NAME */
+static bool
+multiple_bits_set (canonicalize_mode_t i)
+{
+ return (i & (i - 1)) != 0;
+}
+
/* Return true if we've already seen the triple, <FILENAME, dev, ino>.
If *HT is not initialized, initialize it. */
static bool
@@ -106,14 +110,12 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
ptrdiff_t extra_len = 0;
Hash_table *ht = NULL;
int saved_errno;
- int can_flags = can_mode & ~CAN_MODE_MASK;
- bool logical = can_flags & CAN_NOLINKS;
+ bool logical = (can_mode & CAN_NOLINKS) != 0;
int num_links = 0;
ptrdiff_t prefix_len;
- can_mode &= CAN_MODE_MASK;
-
- if (MULTIPLE_BITS_SET (can_mode))
+ canonicalize_mode_t can_exist = can_mode & CAN_MODE_MASK;
+ if (multiple_bits_set (can_exist))
{
errno = EINVAL;
return NULL;
@@ -276,7 +278,7 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
indicate a loop. */
if (seen_triple (&ht, start, &st))
{
- if (can_mode == CAN_MISSING)
+ if (can_exist == CAN_MISSING)
continue;
saved_errno = ELOOP;
free (buf);
@@ -334,7 +336,7 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
free (buf);
}
- else if (can_mode != CAN_MISSING
+ else if (can_exist != CAN_MISSING
&& (!logical || readlink (rname, &discard, 1) < 0))
{
saved_errno = errno;
@@ -346,8 +348,8 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode)
case ENOENT:
/* RNAME does not exist. */
- if (can_mode == CAN_EXISTING
- || (can_mode == CAN_ALL_BUT_LAST
+ if (can_exist == CAN_EXISTING
+ || (can_exist == CAN_ALL_BUT_LAST
&& end[strspn (end, SLASHES)]))
goto error;
break;