summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2025-11-20 08:46:44 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2025-11-20 09:11:59 -0800
commit2cc6c812788281c05ca9687afd7bedf597b1e942 (patch)
tree2d998ed896f38fe92ba260240075d1860709aba4 /lib
parentcada1c3192902136214e60b82fd7f9116bc88792 (diff)
Update from Gnulib by running admin/merge-gnulib
Diffstat (limited to 'lib')
-rw-r--r--lib/acl-internal.c33
-rw-r--r--lib/diffseq.h238
-rw-r--r--lib/fcntl.c3
-rw-r--r--lib/fcntl.in.h9
-rw-r--r--lib/file-has-acl.c8
-rw-r--r--lib/ftoastr.c3
-rw-r--r--lib/getdtablesize.c11
-rw-r--r--lib/getloadavg.c8
-rw-r--r--lib/group-member.c7
-rw-r--r--lib/intprops-internal.h8
-rw-r--r--lib/memrchr.c14
-rw-r--r--lib/mktime.c6
-rw-r--r--lib/nproc.c16
-rw-r--r--lib/pselect.c4
-rw-r--r--lib/set-permissions.c8
-rw-r--r--lib/sha1.c3
-rw-r--r--lib/sha256.c9
-rw-r--r--lib/sha512.c9
-rw-r--r--lib/sig2str.c6
-rw-r--r--lib/stat-time.h3
-rw-r--r--lib/str-two-way.h9
-rw-r--r--lib/strftime.c65
-rw-r--r--lib/strnlen.c3
-rw-r--r--lib/strtol.c5
-rw-r--r--lib/sys_select.in.h3
-rw-r--r--lib/tempname.c3
26 files changed, 237 insertions, 257 deletions
diff --git a/lib/acl-internal.c b/lib/acl-internal.c
index b8a6ed06c31..7d7cbb79818 100644
--- a/lib/acl-internal.c
+++ b/lib/acl-internal.c
@@ -161,9 +161,7 @@ acl_default_nontrivial (acl_t acl)
int
acl_nontrivial (int count, aclent_t *entries)
{
- int i;
-
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
aclent_t *ace = &entries[i];
@@ -192,8 +190,6 @@ acl_nontrivial (int count, aclent_t *entries)
int
acl_ace_nontrivial (int count, ace_t *entries)
{
- int i;
-
/* The flags in the ace_t structure changed in a binary incompatible way
when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
How to distinguish the two conventions at runtime?
@@ -202,7 +198,7 @@ acl_ace_nontrivial (int count, ace_t *entries)
convention, these values are not used. */
int old_convention = 0;
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
{
old_convention = 1;
@@ -211,7 +207,7 @@ acl_ace_nontrivial (int count, ace_t *entries)
if (old_convention)
/* Running on Solaris 10. */
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
ace_t *ace = &entries[i];
@@ -238,7 +234,7 @@ acl_ace_nontrivial (int count, ace_t *entries)
0 /* everyone@ allow */
};
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
ace_t *ace = &entries[i];
unsigned int index1;
@@ -374,12 +370,10 @@ acl_ace_nontrivial (int count, ace_t *entries)
int
acl_nontrivial (int count, struct acl_entry *entries)
{
- int i;
-
if (count > 3)
return 1;
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
struct acl_entry *ace = &entries[i];
@@ -396,9 +390,7 @@ acl_nontrivial (int count, struct acl_entry *entries)
int
aclv_nontrivial (int count, struct acl *entries)
{
- int i;
-
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
struct acl *ace = &entries[i];
@@ -424,10 +416,8 @@ int
acl_nontrivial (struct acl *a)
{
/* The normal way to iterate through an ACL is like this:
- struct acl_entry *ace;
- for (ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
+ for (struct acl_entry *ace = a->acl_ext; ace != acl_last (a); ace = acl_nxt (ace))
{
- struct ace_id *aei;
switch (ace->ace_type)
{
case ACC_PERMIT:
@@ -435,7 +425,7 @@ acl_nontrivial (struct acl *a)
case ACC_SPECIFY:
...;
}
- for (aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
+ for (struct ace_id *aei = ace->ace_id; aei != id_last (ace); aei = id_nxt (aei))
...
}
*/
@@ -453,9 +443,8 @@ acl_nfs4_nontrivial (nfs4_acl_int_t *a)
return (a->aclEntryN > 0 ? 1 : 0);
# else
int count = a->aclEntryN;
- int i;
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
nfs4_ace_int_t *ace = &a->aclEntry[i];
@@ -484,9 +473,7 @@ acl_nfs4_nontrivial (nfs4_acl_int_t *a)
int
acl_nontrivial (int count, struct acl *entries)
{
- int i;
-
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
struct acl *ace = &entries[i];
diff --git a/lib/diffseq.h b/lib/diffseq.h
index 914bc643bb4..b4959813d7f 100644
--- a/lib/diffseq.h
+++ b/lib/diffseq.h
@@ -286,160 +286,156 @@ diag (OFFSET xoff, OFFSET xlim, OFFSET yoff, OFFSET ylim, bool find_minimal,
}
}
- if (find_minimal)
- continue;
-
+ if (!find_minimal)
+ {
#ifdef USE_HEURISTIC
- bool heuristic = ctxt->heuristic;
+ bool heuristic = ctxt->heuristic;
#else
- bool heuristic = false;
+ bool heuristic = false;
#endif
- /* Heuristic: check occasionally for a diagonal that has made lots
- of progress compared with the edit distance. If we have any
- such, find the one that has made the most progress and return it
- as if it had succeeded.
+ /* Heuristic: check occasionally for a diagonal that has made lots
+ of progress compared with the edit distance. If we have any
+ such, find the one that has made the most progress and return it
+ as if it had succeeded.
- With this heuristic, for vectors with a constant small density
- of changes, the algorithm is linear in the vector size. */
-
- if (200 < c && big_snake && heuristic)
- {
- {
- OFFSET best = 0;
+ With this heuristic, for vectors with a constant small density
+ of changes, the algorithm is linear in the vector size. */
- for (d = fmax; d >= fmin; d -= 2)
+ if (200 < c && big_snake && heuristic)
+ {
{
- OFFSET dd = d - fmid;
- OFFSET x = fd[d];
- OFFSET y = x - d;
- OFFSET v = (x - xoff) * 2 - dd;
+ OFFSET best = 0;
- if (v > 12 * (c + (dd < 0 ? -dd : dd)))
+ for (d = fmax; d >= fmin; d -= 2)
{
- if (v > best
- && xoff + SNAKE_LIMIT <= x && x < xlim
- && yoff + SNAKE_LIMIT <= y && y < ylim)
- {
- /* We have a good enough best diagonal; now insist
- that it end with a significant snake. */
- int k;
+ OFFSET dd = d - fmid;
+ OFFSET x = fd[d];
+ OFFSET y = x - d;
+ OFFSET v = (x - xoff) * 2 - dd;
- for (k = 1; XREF_YREF_EQUAL (x - k, y - k); k++)
- if (k == SNAKE_LIMIT)
- {
- best = v;
- part->xmid = x;
- part->ymid = y;
- break;
- }
+ if (v > 12 * (c + (dd < 0 ? -dd : dd)))
+ {
+ if (v > best
+ && xoff + SNAKE_LIMIT <= x && x < xlim
+ && yoff + SNAKE_LIMIT <= y && y < ylim)
+ {
+ /* We have a good enough best diagonal; now insist
+ that it end with a significant snake. */
+ for (int k = 1; XREF_YREF_EQUAL (x - k, y - k); k++)
+ if (k == SNAKE_LIMIT)
+ {
+ best = v;
+ part->xmid = x;
+ part->ymid = y;
+ break;
+ }
+ }
}
}
+ if (best > 0)
+ {
+ part->lo_minimal = true;
+ part->hi_minimal = false;
+ return;
+ }
}
- if (best > 0)
- {
- part->lo_minimal = true;
- part->hi_minimal = false;
- return;
- }
- }
-
- {
- OFFSET best = 0;
- for (d = bmax; d >= bmin; d -= 2)
{
- OFFSET dd = d - bmid;
- OFFSET x = bd[d];
- OFFSET y = x - d;
- OFFSET v = (xlim - x) * 2 + dd;
+ OFFSET best = 0;
- if (v > 12 * (c + (dd < 0 ? -dd : dd)))
+ for (d = bmax; d >= bmin; d -= 2)
{
- if (v > best
- && xoff < x && x <= xlim - SNAKE_LIMIT
- && yoff < y && y <= ylim - SNAKE_LIMIT)
- {
- /* We have a good enough best diagonal; now insist
- that it end with a significant snake. */
- int k;
+ OFFSET dd = d - bmid;
+ OFFSET x = bd[d];
+ OFFSET y = x - d;
+ OFFSET v = (xlim - x) * 2 + dd;
- for (k = 0; XREF_YREF_EQUAL (x + k, y + k); k++)
- if (k == SNAKE_LIMIT - 1)
- {
- best = v;
- part->xmid = x;
- part->ymid = y;
- break;
- }
+ if (v > 12 * (c + (dd < 0 ? -dd : dd)))
+ {
+ if (v > best
+ && xoff < x && x <= xlim - SNAKE_LIMIT
+ && yoff < y && y <= ylim - SNAKE_LIMIT)
+ {
+ /* We have a good enough best diagonal; now insist
+ that it end with a significant snake. */
+ for (int k = 0; XREF_YREF_EQUAL (x + k, y + k); k++)
+ if (k == SNAKE_LIMIT - 1)
+ {
+ best = v;
+ part->xmid = x;
+ part->ymid = y;
+ break;
+ }
+ }
}
}
+ if (best > 0)
+ {
+ part->lo_minimal = false;
+ part->hi_minimal = true;
+ return;
+ }
}
- if (best > 0)
- {
- part->lo_minimal = false;
- part->hi_minimal = true;
- return;
- }
- }
- }
+ }
- /* Heuristic: if we've gone well beyond the call of duty, give up
- and report halfway between our best results so far. */
- if (c >= ctxt->too_expensive)
- {
- /* Find forward diagonal that maximizes X + Y. */
- OFFSET fxybest = -1, fxbest;
- for (d = fmax; d >= fmin; d -= 2)
+ /* Heuristic: if we've gone well beyond the call of duty, give up
+ and report halfway between our best results so far. */
+ if (c >= ctxt->too_expensive)
{
- OFFSET x = MIN (fd[d], xlim);
- OFFSET y = x - d;
- if (ylim < y)
+ /* Find forward diagonal that maximizes X + Y. */
+ OFFSET fxybest = -1, fxbest;
+ for (d = fmax; d >= fmin; d -= 2)
{
- x = ylim + d;
- y = ylim;
+ OFFSET x = MIN (fd[d], xlim);
+ OFFSET y = x - d;
+ if (ylim < y)
+ {
+ x = ylim + d;
+ y = ylim;
+ }
+ if (fxybest < x + y)
+ {
+ fxybest = x + y;
+ fxbest = x;
+ }
}
- if (fxybest < x + y)
+
+ /* Find backward diagonal that minimizes X + Y. */
+ OFFSET bxybest = OFFSET_MAX, bxbest;
+ for (d = bmax; d >= bmin; d -= 2)
{
- fxybest = x + y;
- fxbest = x;
+ OFFSET x = MAX (xoff, bd[d]);
+ OFFSET y = x - d;
+ if (y < yoff)
+ {
+ x = yoff + d;
+ y = yoff;
+ }
+ if (x + y < bxybest)
+ {
+ bxybest = x + y;
+ bxbest = x;
+ }
}
- }
- /* Find backward diagonal that minimizes X + Y. */
- OFFSET bxybest = OFFSET_MAX, bxbest;
- for (d = bmax; d >= bmin; d -= 2)
- {
- OFFSET x = MAX (xoff, bd[d]);
- OFFSET y = x - d;
- if (y < yoff)
+ /* Use the better of the two diagonals. */
+ if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
{
- x = yoff + d;
- y = yoff;
+ part->xmid = fxbest;
+ part->ymid = fxybest - fxbest;
+ part->lo_minimal = true;
+ part->hi_minimal = false;
}
- if (x + y < bxybest)
+ else
{
- bxybest = x + y;
- bxbest = x;
+ part->xmid = bxbest;
+ part->ymid = bxybest - bxbest;
+ part->lo_minimal = false;
+ part->hi_minimal = true;
}
+ return;
}
-
- /* Use the better of the two diagonals. */
- if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
- {
- part->xmid = fxbest;
- part->ymid = fxybest - fxbest;
- part->lo_minimal = true;
- part->hi_minimal = false;
- }
- else
- {
- part->xmid = bxbest;
- part->ymid = bxybest - bxbest;
- part->lo_minimal = false;
- part->hi_minimal = true;
- }
- return;
}
}
#undef XREF_YREF_EQUAL
diff --git a/lib/fcntl.c b/lib/fcntl.c
index f47ebde105c..29cb4f9eb84 100644
--- a/lib/fcntl.c
+++ b/lib/fcntl.c
@@ -146,9 +146,8 @@ dupfd (int oldfd, int newfd, int flags)
/* Close the previous fds that turned out to be too small. */
{
int saved_errno = errno;
- unsigned int duplicated_fd;
- for (duplicated_fd = 0;
+ for (unsigned int duplicated_fd = 0;
duplicated_fd < fds_to_close_bound * CHAR_BIT;
duplicated_fd++)
if ((fds_to_close[duplicated_fd / CHAR_BIT]
diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h
index 3fbbf4b0e4a..bcb5606b13e 100644
--- a/lib/fcntl.in.h
+++ b/lib/fcntl.in.h
@@ -495,6 +495,15 @@ _GL_WARN_ON_USE (openat2, "openat2 is not portable - "
# define AT_NO_AUTOMOUNT 0
#endif
+/* errno when openat+O_NOFOLLOW fails because the file is a symlink. */
+#if defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __DragonFly__
+# define _GL_OPENAT_ESYMLINK EMLINK
+#elif defined __NetBSD__
+# define _GL_OPENAT_ESYMLINK EFTYPE
+#else
+# define _GL_OPENAT_ESYMLINK ELOOP
+#endif
+
#endif /* _@GUARD_PREFIX@_FCNTL_H */
#endif /* _@GUARD_PREFIX@_FCNTL_H */
#endif
diff --git a/lib/file-has-acl.c b/lib/file-has-acl.c
index d2fa69a2834..b1eb1f2e8e7 100644
--- a/lib/file-has-acl.c
+++ b/lib/file-has-acl.c
@@ -660,9 +660,9 @@ fdfile_has_aclinfo (MAYBE_UNUSED int fd,
(aclent_t *) malloc (alloc * sizeof (aclent_t));
if (entries == NULL)
return -1;
- continue;
}
- break;
+ else
+ break;
}
if (count < 0)
{
@@ -732,9 +732,9 @@ fdfile_has_aclinfo (MAYBE_UNUSED int fd,
entries = malloced = (ace_t *) malloc (alloc * sizeof (ace_t));
if (entries == NULL)
return -1;
- continue;
}
- break;
+ else
+ break;
}
if (count < 0)
{
diff --git a/lib/ftoastr.c b/lib/ftoastr.c
index 2438ffe837f..03252f83d38 100644
--- a/lib/ftoastr.c
+++ b/lib/ftoastr.c
@@ -124,7 +124,6 @@ FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x)
PROMOTED_FLOAT promoted_x = x;
char format[sizeof "%-+ 0*.*Lg"];
FLOAT abs_x = x < 0 ? -x : x;
- int prec;
char *p = format;
*p++ = '%';
@@ -142,7 +141,7 @@ FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x)
*p++ = flags & FTOASTR_UPPER_E ? 'G' : 'g';
*p = '\0';
- for (prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++)
+ for (int prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++)
{
int n = snprintf (buf, bufsize, format, width, prec, promoted_x);
if (n < 0
diff --git a/lib/getdtablesize.c b/lib/getdtablesize.c
index b98fbb70add..396993821cd 100644
--- a/lib/getdtablesize.c
+++ b/lib/getdtablesize.c
@@ -61,13 +61,10 @@ getdtablesize (void)
{
/* We are looking for the number N such that the valid file descriptors
are 0..N-1. It can be obtained through a loop as follows:
- {
- int fd;
- for (fd = 3; fd < 65536; fd++)
- if (dup2 (0, fd) == -1)
- break;
- return fd;
- }
+ for (int fd = 3; fd < 65536; fd++)
+ if (dup2 (0, fd) == -1)
+ break;
+ return fd;
On Windows XP, the result is 2048.
The drawback of this loop is that it allocates memory for a libc
internal array that is never freed.
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 1e3e215360b..f9ed97b04bd 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -623,11 +623,11 @@ getloadavg (double loadavg[], int nelem)
struct proc_summary proc_sum_data;
struct stat_descr proc_info;
double load;
- register unsigned int i, j;
+ register unsigned int j;
if (cpus == 0)
{
- register unsigned int c, i;
+ register unsigned int c;
struct cpu_config conf;
struct stat_descr desc;
@@ -641,7 +641,7 @@ getloadavg (double loadavg[], int nelem)
return -1;
c = 0;
- for (i = 0; i < conf.config_maxclass; ++i)
+ for (unsigned int i = 0; i < conf.config_maxclass; ++i)
{
struct class_stats stats;
memset (&stats, 0, sizeof stats);
@@ -672,7 +672,7 @@ getloadavg (double loadavg[], int nelem)
load = proc_sum_data.ps_nrunnable;
j = 0;
- for (i = samples - 1; i > 0; --i)
+ for (unsigned int i = samples - 1; i > 0; --i)
{
load += proc_sum_data.ps_nrun[j];
if (j++ == PS_NRUNSIZE)
diff --git a/lib/group-member.c b/lib/group-member.c
index a26eebe37d2..34938a1b05e 100644
--- a/lib/group-member.c
+++ b/lib/group-member.c
@@ -73,14 +73,13 @@ get_group_info (struct group_info *gi)
int
group_member (gid_t gid)
{
- int i;
int found;
struct group_info gi;
int n_groups = get_group_info (&gi);
/* Search through the list looking for GID. */
found = 0;
- for (i = 0; i < n_groups; i++)
+ for (int i = 0; i < n_groups; i++)
{
if (gid == gi.group[i])
{
@@ -99,9 +98,7 @@ group_member (gid_t gid)
int
main (int argc, char **argv)
{
- int i;
-
- for (i = 1; i < argc; i++)
+ for (int i = 1; i < argc; i++)
{
gid_t gid;
diff --git a/lib/intprops-internal.h b/lib/intprops-internal.h
index 2609803094b..856cfa534f2 100644
--- a/lib/intprops-internal.h
+++ b/lib/intprops-internal.h
@@ -116,8 +116,8 @@
#endif
/* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
-#if defined __clang_major__ && __clang_major__ < 14
-/* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
+#if defined __clang_major__ && __clang_major__ < 21
+/* Work around Clang bug <https://github.com/llvm/llvm-project/issues/16778>. */
# define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
#else
# define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
@@ -179,9 +179,9 @@
_GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
#endif
-/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
+/* Nonzero if this compiler has GCC bug 68193 or Clang bug 25764. See:
https://gcc.gnu.org/PR68193
- https://llvm.org/bugs/show_bug.cgi?id=25390
+ https://github.com/llvm/llvm-project/issues/25764
For now, assume GCC < 14 and all Clang versions generate bogus
warnings for _Generic. This matters only for compilers that
lack relevant builtins. */
diff --git a/lib/memrchr.c b/lib/memrchr.c
index b143fb0a3a5..0c4022caff1 100644
--- a/lib/memrchr.c
+++ b/lib/memrchr.c
@@ -84,15 +84,11 @@ __memrchr (void const *s, int c_in, size_t n)
repeated_one |= repeated_one << 31 << 1;
repeated_c |= repeated_c << 31 << 1;
if (8 < sizeof (longword))
- {
- size_t i;
-
- for (i = 64; i < sizeof (longword) * 8; i *= 2)
- {
- repeated_one |= repeated_one << i;
- repeated_c |= repeated_c << i;
- }
- }
+ for (size_t i = 64; i < sizeof (longword) * 8; i *= 2)
+ {
+ repeated_one |= repeated_one << i;
+ repeated_c |= repeated_c << i;
+ }
}
/* Instead of the traditional loop which tests each byte, we will test a
diff --git a/lib/mktime.c b/lib/mktime.c
index 4218fca69b1..90d2002f27e 100644
--- a/lib/mktime.c
+++ b/lib/mktime.c
@@ -468,11 +468,9 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset)
int year_seconds_bound = 366 * 24 * 60 * 60 + 1;
int delta_bound = year_seconds_bound + stride;
- int delta, direction;
-
/* Search in both directions, closest first. */
- for (delta = stride; delta < delta_bound; delta += stride)
- for (direction = -1; direction <= 1; direction += 2)
+ for (int delta = stride; delta < delta_bound; delta += stride)
+ for (int direction = -1; direction <= 1; direction += 2)
{
long_int ot;
if (! ckd_add (&ot, t, delta * direction))
diff --git a/lib/nproc.c b/lib/nproc.c
index 9404be1d384..1cccfc56065 100644
--- a/lib/nproc.c
+++ b/lib/nproc.c
@@ -89,10 +89,8 @@ num_processors_via_affinity_mask (void)
/* glibc >= 2.6 has the CPU_COUNT macro. */
count = CPU_COUNT (&set);
# else
- size_t i;
-
count = 0;
- for (i = 0; i < CPU_SETSIZE; i++)
+ for (size_t i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET (i, &set))
count++;
# endif
@@ -112,9 +110,7 @@ num_processors_via_affinity_mask (void)
if (pthread_getaffinity_np (pthread_self (), cpuset_size (set), set)
== 0)
{
- cpuid_t i;
-
- for (i = 0;; i++)
+ for (cpuid_t i = 0;; i++)
{
int ret = cpuset_isset (i, set);
if (ret < 0)
@@ -180,10 +176,8 @@ num_processors_via_affinity_mask (void)
/* glibc >= 2.6 has the CPU_COUNT macro. */
count = CPU_COUNT (&set);
# else
- size_t i;
-
count = 0;
- for (i = 0; i < CPU_SETSIZE; i++)
+ for (size_t i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET (i, &set))
count++;
# endif
@@ -202,9 +196,7 @@ num_processors_via_affinity_mask (void)
if (sched_getaffinity_np (getpid (), cpuset_size (set), set) == 0)
{
- cpuid_t i;
-
- for (i = 0;; i++)
+ for (cpuid_t i = 0;; i++)
{
int ret = cpuset_isset (i, set);
if (ret < 0)
diff --git a/lib/pselect.c b/lib/pselect.c
index 78f0446fee9..f6599f5f23b 100644
--- a/lib/pselect.c
+++ b/lib/pselect.c
@@ -95,15 +95,13 @@ rpl_pselect (int nfds, fd_set *restrict rfds,
struct timespec const *restrict timeout,
sigset_t const *restrict sigmask)
{
- int i;
-
/* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */
if (nfds < 0 || nfds > FD_SETSIZE)
{
errno = EINVAL;
return -1;
}
- for (i = 0; i < nfds; i++)
+ for (int i = 0; i < nfds; i++)
{
if (((rfds && FD_ISSET (i, rfds))
|| (wfds && FD_ISSET (i, wfds))
diff --git a/lib/set-permissions.c b/lib/set-permissions.c
index af0b5de228d..27f41fde514 100644
--- a/lib/set-permissions.c
+++ b/lib/set-permissions.c
@@ -99,19 +99,17 @@ set_acls_from_mode (const char *name, int desc, mode_t mode, bool *must_chmod)
errno = ENOMEM;
return -1;
}
- continue;
}
- break;
+ else
+ break;
}
if (count <= 0)
convention = -1;
else
{
- int i;
-
convention = 0;
- for (i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
{
convention = 1;
diff --git a/lib/sha1.c b/lib/sha1.c
index d10c159af6c..fd72199d2fc 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -254,8 +254,7 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
while (words < endp)
{
uint32_t tm;
- int t;
- for (t = 0; t < 16; t++)
+ for (int t = 0; t < 16; t++)
{
x[t] = SWAP (*words);
words++;
diff --git a/lib/sha256.c b/lib/sha256.c
index 9358faff3e1..27c4f0c76f3 100644
--- a/lib/sha256.c
+++ b/lib/sha256.c
@@ -96,10 +96,9 @@ set_uint32 (char *cp, uint32_t v)
void *
sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
{
- int i;
char *r = resbuf;
- for (i = 0; i < 8; i++)
+ for (int i = 0; i < 8; i++)
set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
return resbuf;
@@ -108,10 +107,9 @@ sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
void *
sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf)
{
- int i;
char *r = resbuf;
- for (i = 0; i < 7; i++)
+ for (int i = 0; i < 7; i++)
set_uint32 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
return resbuf;
@@ -338,9 +336,8 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
{
uint32_t tm;
uint32_t t0, t1;
- int t;
/* FIXME: see sha1.c for a better implementation. */
- for (t = 0; t < 16; t++)
+ for (int t = 0; t < 16; t++)
{
x[t] = SWAP (*words);
words++;
diff --git a/lib/sha512.c b/lib/sha512.c
index 904a8912270..76deb731008 100644
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -96,10 +96,9 @@ set_uint64 (char *cp, u64 v)
void *
sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
{
- int i;
char *r = resbuf;
- for (i = 0; i < 8; i++)
+ for (int i = 0; i < 8; i++)
set_uint64 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
return resbuf;
@@ -108,10 +107,9 @@ sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
void *
sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf)
{
- int i;
char *r = resbuf;
- for (i = 0; i < 6; i++)
+ for (int i = 0; i < 6; i++)
set_uint64 (r + i * sizeof ctx->state[0], SWAP (ctx->state[i]));
return resbuf;
@@ -367,9 +365,8 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
while (words < endp)
{
- int t;
/* FIXME: see sha1.c for a better implementation. */
- for (t = 0; t < 16; t++)
+ for (int t = 0; t < 16; t++)
{
x[t] = SWAP (*words);
words++;
diff --git a/lib/sig2str.c b/lib/sig2str.c
index bf4d009578d..d21aac1c643 100644
--- a/lib/sig2str.c
+++ b/lib/sig2str.c
@@ -286,8 +286,7 @@ str2signum (char const *signame)
}
else
{
- unsigned int i;
- for (i = 0; i < NUMNAME_ENTRIES; i++)
+ for (unsigned int i = 0; i < NUMNAME_ENTRIES; i++)
if (streq (numname_table[i].name, signame))
return numname_table[i].num;
@@ -331,8 +330,7 @@ str2sig (char const *signame, int *signum)
int
sig2str (int signum, char *signame)
{
- unsigned int i;
- for (i = 0; i < NUMNAME_ENTRIES; i++)
+ for (unsigned int i = 0; i < NUMNAME_ENTRIES; i++)
if (numname_table[i].num == signum)
{
strcpy (signame, numname_table[i].name);
diff --git a/lib/stat-time.h b/lib/stat-time.h
index 38315b9f569..3fab4db6923 100644
--- a/lib/stat-time.h
+++ b/lib/stat-time.h
@@ -232,8 +232,7 @@ stat_time_normalize (int result, _GL_UNUSED struct stat *st)
short int const ts_off[] = { STAT_TIMESPEC_OFFSETOF (st_atim),
STAT_TIMESPEC_OFFSETOF (st_mtim),
STAT_TIMESPEC_OFFSETOF (st_ctim) };
- int i;
- for (i = 0; i < sizeof ts_off / sizeof *ts_off; i++)
+ for (int i = 0; i < sizeof ts_off / sizeof *ts_off; i++)
{
struct timespec *ts = (struct timespec *) ((char *) st + ts_off[i]);
long int q = ts->tv_nsec / timespec_hz;
diff --git a/lib/str-two-way.h b/lib/str-two-way.h
index d13fb298fd6..852e5d078f2 100644
--- a/lib/str-two-way.h
+++ b/lib/str-two-way.h
@@ -329,7 +329,6 @@ static RETURN_TYPE _GL_ATTRIBUTE_PURE
two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
const unsigned char *needle, size_t needle_len)
{
- size_t i; /* Index into current byte of NEEDLE. */
size_t j; /* Index into current window of HAYSTACK. */
size_t period; /* The period of the right half of needle. */
size_t suffix; /* The index of the right half of needle. */
@@ -344,9 +343,9 @@ two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
shift_table[c] is the distance from the last occurrence of c to
the end of NEEDLE, or NEEDLE_LEN if c is absent from the NEEDLE.
shift_table[NEEDLE[NEEDLE_LEN - 1]] contains the only 0. */
- for (i = 0; i < 1U << CHAR_BIT; i++)
+ for (size_t i = 0; i < 1U << CHAR_BIT; i++)
shift_table[i] = needle_len;
- for (i = 0; i < needle_len; i++)
+ for (size_t i = 0; i < needle_len; i++)
shift_table[CANON_ELEMENT (needle[i])] = needle_len - i - 1;
/* Perform the search. Each iteration compares the right half
@@ -379,7 +378,7 @@ two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
}
/* Scan for matches in right half. The last byte has
already been matched, by virtue of the shift table. */
- i = MAX (suffix, memory);
+ size_t i = MAX (suffix, memory);
while (i < needle_len - 1 && (CANON_ELEMENT (needle[i])
== CANON_ELEMENT (haystack[i + j])))
++i;
@@ -423,7 +422,7 @@ two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
}
/* Scan for matches in right half. The last byte has
already been matched, by virtue of the shift table. */
- i = suffix;
+ size_t i = suffix;
while (i < needle_len - 1 && (CANON_ELEMENT (needle[i])
== CANON_ELEMENT (haystack[i + j])))
++i;
diff --git a/lib/strftime.c b/lib/strftime.c
index 6445d6e3d28..291c650b970 100644
--- a/lib/strftime.c
+++ b/lib/strftime.c
@@ -181,7 +181,9 @@ enum pad_style
#if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME
/* Support for non-Gregorian calendars. */
# include "localcharset.h"
-# include "localename.h"
+# if !(defined __APPLE__ && defined __MACH__)
+# include "localename.h"
+# endif
# include "calendars.h"
# define CAL_ARGS(x,y) x, y,
#else
@@ -1020,8 +1022,7 @@ underlying_strftime (timezone_t tz, char *ubuf, size_t ubufsize,
{
/* The last word has length >= 3. */
bool found_letter = false;
- const char *p;
- for (p = space + 1; *p != '\0'; p++)
+ for (const char *p = space + 1; *p != '\0'; p++)
if ((*p >= 'A' && *p <= 'Z')
|| (*p >= 'a' && *p <= 'z'))
{
@@ -1138,9 +1139,9 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
#if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME
/* Recognize whether to use a non-Gregorian calendar. */
const struct calendar *cal = NULL;
- struct calendar_date caldate;
if (strcmp (locale_charset (), "UTF-8") == 0)
{
+# if !(defined __APPLE__ && defined __MACH__)
const char *loc = gl_locale_name_unsafe (LC_TIME, "LC_TIME");
if (strlen (loc) >= 5 && !(loc[5] >= 'A' && loc[5] <= 'Z'))
{
@@ -1150,15 +1151,45 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
cal = &persian_calendar;
else if (memcmp (loc, "am_ET", 5) == 0)
cal = &ethiopian_calendar;
- if (cal != NULL)
- {
- if (cal->from_gregorian (&caldate,
- tp->tm_year + 1900,
- tp->tm_mon,
- tp->tm_mday) < 0)
- cal = NULL;
- }
}
+# else /* defined __APPLE__ && defined __MACH__ */
+ /* Nearly equivalent code for macOS, that avoids the need to link with
+ CoreFoundation.
+ It's not entirely equivalent, because it tests only for the language
+ (Thai, Farsi, Amharic) instead of also for the territory (Thailand,
+ Iran, Ethiopia). */
+ /* Get the translation of "Monday" in the LC_TIME locale, by calling
+ the underlying strftime function. */
+ struct tm some_monday; /* 2024-01-01 12:00:00 */
+ memset (&some_monday, '\0', sizeof (struct tm));
+ some_monday.tm_year = 2024 - 1900;
+ some_monday.tm_mon = 1 - 1;
+ some_monday.tm_mday = 1;
+ some_monday.tm_wday = 1; /* Monday */
+ some_monday.tm_hour = 12;
+ some_monday.tm_min = 0;
+ some_monday.tm_sec = 0;
+ char weekday_buf[32];
+ if (strftime (weekday_buf, sizeof (weekday_buf), "%A", &some_monday) > 0)
+ {
+ /* Test for the Thai / Farsi / Amharic translation of "Monday". */
+ if (streq (weekday_buf, "จันทร์") || streq (weekday_buf, "วันจันทร์"))
+ cal = &thai_calendar;
+ else if (streq (weekday_buf, "ﺩﻮﺸﻨﺒﻫ") || streq (weekday_buf, "دوشنبه"))
+ cal = &persian_calendar;
+ else if (streq (weekday_buf, "ሰኞ"))
+ cal = &ethiopian_calendar;
+ }
+# endif
+ }
+ struct calendar_date caldate;
+ if (cal != NULL)
+ {
+ if (cal->from_gregorian (&caldate,
+ tp->tm_year + 1900,
+ tp->tm_mon,
+ tp->tm_mday) < 0)
+ cal = NULL;
}
#endif
bool tzset_called = false;
@@ -1248,7 +1279,6 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
#endif
retval_t i = 0;
STREAM_OR_CHAR_T *p = s;
- const CHAR_T *f;
#if DO_MULTIBYTE && !defined COMPILE_WIDE
const char *format_end = NULL;
#endif
@@ -1260,7 +1290,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
if (hour12 == 0)
hour12 = 12;
- for (f = format; *f != '\0'; width = -1, f++)
+ for (const CHAR_T *f = format; *f != '\0'; width = -1, f++)
{
enum pad_style pad = ZERO_PAD;
int modifier; /* Field modifier ('E', 'O', or 0). */
@@ -1659,10 +1689,9 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
{
/* The last word has length >= 3. */
bool found_letter = false;
- const char *p;
- for (p = space + 1; *p != '\0'; p++)
- if ((*p >= 'A' && *p <= 'Z')
- || (*p >= 'a' && *p <= 'z'))
+ for (const char *wp = space + 1; *wp != '\0'; wp++)
+ if ((*wp >= 'A' && *wp <= 'Z')
+ || (*wp >= 'a' && *wp <= 'z'))
{
found_letter = true;
break;
diff --git a/lib/strnlen.c b/lib/strnlen.c
index f6990415762..5a03d7ec942 100644
--- a/lib/strnlen.c
+++ b/lib/strnlen.c
@@ -27,8 +27,7 @@ strnlen (const char *s, size_t maxlen)
{
/* Do not use memchr, because on some platforms memchr has
undefined behavior if MAXLEN exceeds the number of bytes in S. */
- size_t i;
- for (i = 0; i < maxlen && s[i]; i++)
+ for (size_t i = 0; i < maxlen && s[i]; i++)
continue;
return i;
}
diff --git a/lib/strtol.c b/lib/strtol.c
index 0bcc6f40eca..954ba67a4a7 100644
--- a/lib/strtol.c
+++ b/lib/strtol.c
@@ -217,7 +217,6 @@ INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
register unsigned int cutlim;
register unsigned LONG int i;
register const STRING_TYPE *s;
- register UCHAR_TYPE c;
const STRING_TYPE *save, *end;
int overflow;
@@ -307,7 +306,7 @@ INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
{
/* Find the end of the digit string and check its grouping. */
end = s;
- for (c = *end; c != L_('\0'); c = *++end)
+ for (UCHAR_TYPE c = *end; c != L_('\0'); c = *++end)
if ((wchar_t) c != thousands
&& ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
&& (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
@@ -326,7 +325,7 @@ INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
overflow = 0;
i = 0;
- for (c = *s; c != L_('\0'); c = *++s)
+ for (UCHAR_TYPE c = *s; c != L_('\0'); c = *++s)
{
if (s == end)
break;
diff --git a/lib/sys_select.in.h b/lib/sys_select.in.h
index 69c2fa9fc4e..9106702deb1 100644
--- a/lib/sys_select.in.h
+++ b/lib/sys_select.in.h
@@ -152,11 +152,10 @@ typedef int suseconds_t;
static int
rpl_fd_isset (SOCKET fd, const fd_set * set)
{
- u_int i;
if (set == NULL)
return 0;
- for (i = 0; i < set->fd_count; i++)
+ for (u_int i = 0; i < set->fd_count; i++)
if (set->fd_array[i] == fd)
return 1;
diff --git a/lib/tempname.c b/lib/tempname.c
index cd6020c0f22..cd16daf3335 100644
--- a/lib/tempname.c
+++ b/lib/tempname.c
@@ -191,7 +191,6 @@ try_tempname_len (char *tmpl, int suffixlen, void *args,
{
size_t len;
char *XXXXXX;
- unsigned int count;
int fd = -1;
int saved_errno = errno;
@@ -236,7 +235,7 @@ try_tempname_len (char *tmpl, int suffixlen, void *args,
/* This is where the Xs start. */
XXXXXX = &tmpl[len - x_suffix_len - suffixlen];
- for (count = 0; count < attempts; ++count)
+ for (unsigned int count = 0; count < attempts; ++count)
{
for (size_t i = 0; i < x_suffix_len; i++)
{