aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Colomar <alx.manpages@gmail.com>2022-09-15 16:40:27 +0200
committerAlex Colomar <alx.manpages@gmail.com>2022-09-15 18:07:48 +0200
commitb42296e4feaffd0ca4e5675fa181d19891f947ce (patch)
treeb16ebd13da8366c95ace85f538f32f3043dc12e3
parent489712257bb14b3674f9e9037c65b371b3f570c0 (diff)
downloadman-pages-b42296e4feaffd0ca4e5675fa181d19891f947ce.tar.gz
Various pages: EXAMPLES: Use unsigned types for loop iterators
Looping with unsigned types is safer. See the link below. When the iterators are used for accessing an array, use size_t; otherwise, use the most appropriate unsigned type, which in most cases is just 'unsigned int'. Also adjust other variables that have to interact with the iterators, to avoid comparison of integers of different signedness. Link: <https://gustedt.wordpress.com/2013/07/15/a-praise-of-size_t-and-other-unsigned-types/> Cc: Jens Gustedt <jens.gustedt@inria.fr> Signed-off-by: Alex Colomar <alx.manpages@gmail.com>
-rw-r--r--man1/sprof.18
-rw-r--r--man2/close_range.22
-rw-r--r--man2/eventfd.22
-rw-r--r--man2/execve.24
-rw-r--r--man2/futex.212
-rw-r--r--man2/open_by_handle_at.24
-rw-r--r--man2/poll.27
-rw-r--r--man2/recvmmsg.26
-rw-r--r--man2/sched_setaffinity.26
-rw-r--r--man2/timerfd_create.24
-rw-r--r--man2/userfaultfd.23
-rw-r--r--man3/CPU_SET.35
-rw-r--r--man3/backtrace.32
-rw-r--r--man3/bsearch.32
-rw-r--r--man3/dl_iterate_phdr.34
-rw-r--r--man3/dlinfo.34
-rw-r--r--man3/encrypt.314
-rw-r--r--man3/envz_add.32
-rw-r--r--man3/fopencookie.32
-rw-r--r--man3/getaddrinfo.34
-rw-r--r--man3/getaddrinfo_a.322
-rw-r--r--man3/getdate.34
-rw-r--r--man3/getgrent_r.32
-rw-r--r--man3/getgrouplist.32
-rw-r--r--man3/hsearch.34
-rw-r--r--man3/mallinfo.37
-rw-r--r--man3/malloc_info.311
-rw-r--r--man3/mtrace.32
-rw-r--r--man3/pthread_create.37
-rw-r--r--man3/pthread_getcpuclockid.32
-rw-r--r--man3/pthread_setaffinity_np.36
-rw-r--r--man3/qsort.32
-rw-r--r--man3/rand.36
-rw-r--r--man3/regex.34
-rw-r--r--man3/shm_open.32
-rw-r--r--man3/slist.32
-rw-r--r--man3/stailq.32
-rw-r--r--man3/strcat.34
-rw-r--r--man3/strsep.34
-rw-r--r--man3/tsearch.32
-rw-r--r--man3/wordexp.32
-rw-r--r--man5/core.54
-rw-r--r--man7/aio.714
-rw-r--r--man7/inotify.72
-rw-r--r--man7/unix.72
-rw-r--r--man7/user_namespaces.72
46 files changed, 111 insertions, 109 deletions
diff --git a/man1/sprof.1 b/man1/sprof.1
index 35bd42c3d4..0acb51b460 100644
--- a/man1/sprof.1
+++ b/man1/sprof.1
@@ -96,27 +96,27 @@ $ \fBcat libdemo.c\fP
void
consumeCpu1(int lim)
{
- for (int j = 0; j < lim; j++)
+ for (unsigned int j = 0; j < lim; j++)
getppid();
}
void
x1(void) {
- for (int j = 0; j < 100; j++)
+ for (unsigned int j = 0; j < 100; j++)
consumeCpu1(200000);
}
void
consumeCpu2(int lim)
{
- for (int j = 0; j < lim; j++)
+ for (unsigned int j = 0; j < lim; j++)
getppid();
}
void
x2(void)
{
- for (int j = 0; j < 1000; j++)
+ for (unsigned int j = 0; j < 1000; j++)
consumeCpu2(10000);
}
.EE
diff --git a/man2/close_range.2 b/man2/close_range.2
index 386f829c95..054c91c706 100644
--- a/man2/close_range.2
+++ b/man2/close_range.2
@@ -247,7 +247,7 @@ main(int argc, char *argv[])
{
int fd;
- for (int j = 1; j < argc; j++) {
+ for (size_t j = 1; j < argc; j++) {
fd = open(argv[j], O_RDONLY);
if (fd == \-1) {
perror(argv[j]);
diff --git a/man2/eventfd.2 b/man2/eventfd.2
index 71517ad3a4..8315f852c1 100644
--- a/man2/eventfd.2
+++ b/man2/eventfd.2
@@ -405,7 +405,7 @@ main(int argc, char *argv[])
switch (fork()) {
case 0:
- for (int j = 1; j < argc; j++) {
+ for (size_t j = 1; j < argc; j++) {
printf("Child writing %s to efd\en", argv[j]);
u = strtoull(argv[j], NULL, 0);
/* strtoull() allows various bases */
diff --git a/man2/execve.2 b/man2/execve.2
index 744d06c273..4855d7c1e9 100644
--- a/man2/execve.2
+++ b/man2/execve.2
@@ -790,8 +790,8 @@ It just echoes its command-line arguments, one per line.
int
main(int argc, char *argv[])
{
- for (int j = 0; j < argc; j++)
- printf("argv[%d]: %s\en", j, argv[j]);
+ for (size_t j = 0; j < argc; j++)
+ printf("argv[%zu]: %s\en", j, argv[j]);
exit(EXIT_SUCCESS);
}
diff --git a/man2/futex.2 b/man2/futex.2
index 1332ae8522..53caab621f 100644
--- a/man2/futex.2
+++ b/man2/futex.2
@@ -1882,8 +1882,8 @@ fpost(uint32_t *futexp)
int
main(int argc, char *argv[])
{
- pid_t childPid;
- int nloops;
+ pid_t childPid;
+ unsigned int nloops;
setbuf(stdout, NULL);
@@ -1913,9 +1913,9 @@ main(int argc, char *argv[])
err(EXIT_FAILURE, "fork");
if (childPid == 0) { /* Child */
- for (int j = 0; j < nloops; j++) {
+ for (unsigned int j = 0; j < nloops; j++) {
fwait(futex1);
- printf("Child (%jd) %d\en", (intmax_t) getpid(), j);
+ printf("Child (%jd) %u\en", (intmax_t) getpid(), j);
fpost(futex2);
}
@@ -1924,9 +1924,9 @@ main(int argc, char *argv[])
/* Parent falls through to here. */
- for (int j = 0; j < nloops; j++) {
+ for (unsigned int j = 0; j < nloops; j++) {
fwait(futex2);
- printf("Parent (%jd) %d\en", (intmax_t) getpid(), j);
+ printf("Parent (%jd) %u\en", (intmax_t) getpid(), j);
fpost(futex1);
}
diff --git a/man2/open_by_handle_at.2 b/man2/open_by_handle_at.2
index de9c3b745e..48d07fde77 100644
--- a/man2/open_by_handle_at.2
+++ b/man2/open_by_handle_at.2
@@ -586,7 +586,7 @@ main(int argc, char *argv[])
printf("%d\en", mount_id);
printf("%u %d ", fhp\->handle_bytes, fhp\->handle_type);
- for (int j = 0; j < fhp\->handle_bytes; j++)
+ for (size_t j = 0; j < fhp\->handle_bytes; j++)
printf(" %02x", fhp\->f_handle[j]);
printf("\en");
@@ -699,7 +699,7 @@ main(int argc, char *argv[])
fhp\->handle_type = strtoul(nextp, &nextp, 0);
- for (int j = 0; j < fhp\->handle_bytes; j++)
+ for (size_t j = 0; j < fhp\->handle_bytes; j++)
fhp\->f_handle[j] = strtoul(nextp, &nextp, 16);
/* Obtain file descriptor for mount point, either by opening
diff --git a/man2/poll.2 b/man2/poll.2
index e8be13543c..8d2b08d63c 100644
--- a/man2/poll.2
+++ b/man2/poll.2
@@ -561,8 +561,9 @@ at which point the file descriptor was closed and the program terminated.
int
main(int argc, char *argv[])
{
- int nfds, num_open_fds, ready;
+ int ready;
char buf[10];
+ nfds_t num_open_fds, nfds;
ssize_t s;
struct pollfd *pfds;
@@ -578,7 +579,7 @@ main(int argc, char *argv[])
/* Open each file on command line, and add it \(aqpfds\(aq array. */
- for (int j = 0; j < nfds; j++) {
+ for (nfds_t j = 0; j < nfds; j++) {
pfds[j].fd = open(argv[j + 1], O_RDONLY);
if (pfds[j].fd == \-1)
errExit("open");
@@ -601,7 +602,7 @@ main(int argc, char *argv[])
/* Deal with array returned by poll(). */
- for (int j = 0; j < nfds; j++) {
+ for (nfds_t j = 0; j < nfds; j++) {
if (pfds[j].revents != 0) {
printf(" fd=%d; events: %s%s%s\en", pfds[j].fd,
(pfds[j].revents & POLLIN) ? "POLLIN " : "",
diff --git a/man2/recvmmsg.2 b/man2/recvmmsg.2
index 4a76273a10..0e41172978 100644
--- a/man2/recvmmsg.2
+++ b/man2/recvmmsg.2
@@ -245,7 +245,7 @@ main(void)
}
memset(msgs, 0, sizeof(msgs));
- for (int i = 0; i < VLEN; i++) {
+ for (size_t i = 0; i < VLEN; i++) {
iovecs[i].iov_base = bufs[i];
iovecs[i].iov_len = BUFSIZE;
msgs[i].msg_hdr.msg_iov = &iovecs[i];
@@ -262,9 +262,9 @@ main(void)
}
printf("%d messages received\en", retval);
- for (int i = 0; i < retval; i++) {
+ for (size_t i = 0; i < retval; i++) {
bufs[i][msgs[i].msg_len] = 0;
- printf("%d %s", i+1, bufs[i]);
+ printf("%zu %s", i+1, bufs[i]);
}
exit(EXIT_SUCCESS);
}
diff --git a/man2/sched_setaffinity.2 b/man2/sched_setaffinity.2
index 553ccf36d9..b4963e853e 100644
--- a/man2/sched_setaffinity.2
+++ b/man2/sched_setaffinity.2
@@ -356,7 +356,7 @@ main(int argc, char *argv[])
{
cpu_set_t set;
int parentCPU, childCPU;
- int nloops;
+ unsigned int nloops;
if (argc != 4) {
fprintf(stderr, "Usage: %s parent\-cpu child\-cpu num\-loops\en",
@@ -380,7 +380,7 @@ main(int argc, char *argv[])
if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
err(EXIT_FAILURE, "sched_setaffinity");
- for (int j = 0; j < nloops; j++)
+ for (unsigned int j = 0; j < nloops; j++)
getppid();
exit(EXIT_SUCCESS);
@@ -391,7 +391,7 @@ main(int argc, char *argv[])
if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
err(EXIT_FAILURE, "sched_setaffinity");
- for (int j = 0; j < nloops; j++)
+ for (unsigned int j = 0; j < nloops; j++)
getppid();
wait(NULL); /* Wait for child to terminate */
diff --git a/man2/timerfd_create.2 b/man2/timerfd_create.2
index 9850f14c9e..c53c97e248 100644
--- a/man2/timerfd_create.2
+++ b/man2/timerfd_create.2
@@ -633,9 +633,9 @@ int
main(int argc, char *argv[])
{
struct itimerspec new_value;
- int max_exp, fd;
+ int fd;
struct timespec now;
- uint64_t exp, tot_exp;
+ uint64_t exp, tot_exp, max_exp;
ssize_t s;
if (argc != 2 && argc != 4) {
diff --git a/man2/userfaultfd.2 b/man2/userfaultfd.2
index c91a83132a..b2ee1ed3b0 100644
--- a/man2/userfaultfd.2
+++ b/man2/userfaultfd.2
@@ -859,11 +859,10 @@ fault_handler_thread(void *arg)
int
main(int argc, char *argv[])
{
- int l;
char c;
long uffd; /* userfaultfd file descriptor */
char *addr; /* Start of region handled by userfaultfd */
- uint64_t len; /* Length of region handled by userfaultfd */
+ size_t len, l; /* Length of region handled by userfaultfd */
pthread_t thr; /* ID of thread that handles page faults */
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;
diff --git a/man3/CPU_SET.3 b/man3/CPU_SET.3
index 968da2b39f..c043ecc6dd 100644
--- a/man3/CPU_SET.3
+++ b/man3/CPU_SET.3
@@ -313,8 +313,7 @@ int
main(int argc, char *argv[])
{
cpu_set_t *cpusetp;
- size_t size;
- int num_cpus;
+ size_t size, num_cpus;
if (argc < 2) {
fprintf(stderr, "Usage: %s <num\-cpus>\en", argv[0]);
@@ -332,7 +331,7 @@ main(int argc, char *argv[])
size = CPU_ALLOC_SIZE(num_cpus);
CPU_ZERO_S(size, cpusetp);
- for (int cpu = 0; cpu < num_cpus; cpu += 2)
+ for (size_t cpu = 0; cpu < num_cpus; cpu += 2)
CPU_SET_S(cpu, size, cpusetp);
printf("CPU_COUNT() of set: %d\en", CPU_COUNT_S(size, cpusetp));
diff --git a/man3/backtrace.3 b/man3/backtrace.3
index 8b91cb1258..9d62a7f78a 100644
--- a/man3/backtrace.3
+++ b/man3/backtrace.3
@@ -245,7 +245,7 @@ myfunc3(void)
exit(EXIT_FAILURE);
}
- for (int j = 0; j < nptrs; j++)
+ for (size_t j = 0; j < nptrs; j++)
printf("%s\en", strings[j]);
free(strings);
diff --git a/man3/bsearch.3 b/man3/bsearch.3
index d6f5ff1d83..b46b7e433b 100644
--- a/man3/bsearch.3
+++ b/man3/bsearch.3
@@ -114,7 +114,7 @@ int
main(int argc, char *argv[])
{
qsort(months, ARRAY_SIZE(months), sizeof(months[0]), compmi);
- for (int i = 1; i < argc; i++) {
+ for (size_t i = 1; i < argc; i++) {
struct mi key;
struct mi *res;
diff --git a/man3/dl_iterate_phdr.3 b/man3/dl_iterate_phdr.3
index 717f25027d..f0a552abca 100644
--- a/man3/dl_iterate_phdr.3
+++ b/man3/dl_iterate_phdr.3
@@ -301,7 +301,7 @@ callback(struct dl_phdr_info *info, size_t size, void *data)
printf("Name: \e"%s\e" (%d segments)\en", info\->dlpi_name,
info\->dlpi_phnum);
- for (int j = 0; j < info\->dlpi_phnum; j++) {
+ for (size_t j = 0; j < info\->dlpi_phnum; j++) {
p_type = info\->dlpi_phdr[j].p_type;
type = (p_type == PT_LOAD) ? "PT_LOAD" :
(p_type == PT_DYNAMIC) ? "PT_DYNAMIC" :
@@ -314,7 +314,7 @@ callback(struct dl_phdr_info *info, size_t size, void *data)
(p_type == PT_GNU_STACK) ? "PT_GNU_STACK" :
(p_type == PT_GNU_RELRO) ? "PT_GNU_RELRO" : NULL;
- printf(" %2d: [%14p; memsz:%7jx] flags: %#jx; ", j,
+ printf(" %2zu: [%14p; memsz:%7jx] flags: %#jx; ", j,
(void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr),
(uintmax_t) info\->dlpi_phdr[j].p_memsz,
(uintmax_t) info\->dlpi_phdr[j].p_flags);
diff --git a/man3/dlinfo.3 b/man3/dlinfo.3
index 05cba80463..cac9b48246 100644
--- a/man3/dlinfo.3
+++ b/man3/dlinfo.3
@@ -304,8 +304,8 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- for (int j = 0; j < serinfo.dls_cnt; j++)
- printf("dls_serpath[%d].dls_name = %s\en",
+ for (size_t j = 0; j < serinfo.dls_cnt; j++)
+ printf("dls_serpath[%zu].dls_name = %s\en",
j, sip\->dls_serpath[j].dls_name);
exit(EXIT_SUCCESS);
diff --git a/man3/encrypt.3 b/man3/encrypt.3
index 671be82a25..6970e2f70f 100644
--- a/man3/encrypt.3
+++ b/man3/encrypt.3
@@ -169,12 +169,12 @@ main(void)
char buf[64];
char txt[9];
- for (int i = 0; i < 64; i++) {
+ for (size_t i = 0; i < 64; i++) {
key[i] = rand() & 1;
}
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
+ for (size_t i = 0; i < 8; i++) {
+ for (size_t j = 0; j < 8; j++) {
buf[i * 8 + j] = orig[i] >> j & 1;
}
setkey(key);
@@ -182,8 +182,8 @@ main(void)
printf("Before encrypting: %s\en", orig);
encrypt(buf, 0);
- for (int i = 0; i < 8; i++) {
- for (int j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) {
+ for (size_t i = 0; i < 8; i++) {
+ for (size_t j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) {
txt[i] |= buf[i * 8 + j] << j;
}
txt[8] = \(aq\e0\(aq;
@@ -191,8 +191,8 @@ main(void)
printf("After encrypting: %s\en", txt);
encrypt(buf, 1);
- for (int i = 0; i < 8; i++) {
- for (int j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) {
+ for (size_t i = 0; i < 8; i++) {
+ for (size_t j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) {
txt[i] |= buf[i * 8 + j] << j;
}
txt[8] = \(aq\e0\(aq;
diff --git a/man3/envz_add.3 b/man3/envz_add.3
index c983c07026..08782cf0f1 100644
--- a/man3/envz_add.3
+++ b/man3/envz_add.3
@@ -155,7 +155,7 @@ main(int argc, char *argv[], char *envp[])
char *str;
size_t e_len = 0;
- for (int i = 0; envp[i] != NULL; i++)
+ for (size_t i = 0; envp[i] != NULL; i++)
e_len += strlen(envp[i]) + 1;
str = envz_entry(*envp, e_len, "HOME");
diff --git a/man3/fopencookie.3 b/man3/fopencookie.3
index 86b25cb83a..0d05fa13a8 100644
--- a/man3/fopencookie.3
+++ b/man3/fopencookie.3
@@ -418,7 +418,7 @@ main(int argc, char *argv[])
/* Write command\-line arguments to our file. */
- for (int j = 1; j < argc; j++)
+ for (size_t j = 1; j < argc; j++)
if (fputs(argv[j], stream) == EOF) {
perror("fputs");
exit(EXIT_FAILURE);
diff --git a/man3/getaddrinfo.3 b/man3/getaddrinfo.3
index d4d7afa795..51adb3be93 100644
--- a/man3/getaddrinfo.3
+++ b/man3/getaddrinfo.3
@@ -808,13 +808,13 @@ main(int argc, char *argv[])
/* Send remaining command\-line arguments as separate
datagrams, and read responses from server. */
- for (int j = 3; j < argc; j++) {
+ for (size_t j = 3; j < argc; j++) {
len = strlen(argv[j]) + 1;
/* +1 for terminating null byte */
if (len > BUF_SIZE) {
fprintf(stderr,
- "Ignoring long message in argument %d\en", j);
+ "Ignoring long message in argument %zu\en", j);
continue;
}
diff --git a/man3/getaddrinfo_a.3 b/man3/getaddrinfo_a.3
index a45179d16a..b6d9bd3edd 100644
--- a/man3/getaddrinfo_a.3
+++ b/man3/getaddrinfo_a.3
@@ -353,7 +353,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- for (int i = 0; i < argc \- 1; i++) {
+ for (size_t i = 0; i < argc \- 1; i++) {
reqs[i] = malloc(sizeof(*reqs[0]));
if (reqs[i] == NULL) {
perror("malloc");
@@ -370,7 +370,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- for (int i = 0; i < argc \- 1; i++) {
+ for (size_t i = 0; i < argc \- 1; i++) {
printf("%s: ", reqs[i]\->ar_name);
ret = gai_error(reqs[i]);
if (ret == 0) {
@@ -432,7 +432,7 @@ The program source is as follows:
#include <string.h>
static struct gaicb **reqs = NULL;
-static int nreqs = 0;
+static size_t nreqs = 0;
static char *
getcmd(void)
@@ -453,7 +453,7 @@ getcmd(void)
static void
add_requests(void)
{
- int nreqs_base = nreqs;
+ size_t nreqs_base = nreqs;
char *host;
int ret;
@@ -481,7 +481,8 @@ static void
wait_requests(void)
{
char *id;
- int ret, n;
+ int ret;
+ size_t n;
struct gaicb const **wait_reqs = calloc(nreqs, sizeof(*wait_reqs));
/* NULL elements are ignored by gai_suspend(). */
@@ -502,7 +503,7 @@ wait_requests(void)
return;
}
- for (int i = 0; i < nreqs; i++) {
+ for (size_t i = 0; i < nreqs; i++) {
if (wait_reqs[i] == NULL)
continue;
@@ -510,7 +511,7 @@ wait_requests(void)
if (ret == EAI_INPROGRESS)
continue;
- printf("[%02d] %s: %s\en", i, reqs[i]\->ar_name,
+ printf("[%02zu] %s: %s\en", i, reqs[i]\->ar_name,
ret == 0 ? "Finished" : gai_strerror(ret));
}
}
@@ -520,7 +521,8 @@ static void
cancel_requests(void)
{
char *id;
- int ret, n;
+ int ret;
+ size_t n;
while ((id = strtok(NULL, " ")) != NULL) {
n = atoi(id);
@@ -544,8 +546,8 @@ list_requests(void)
char host[NI_MAXHOST];
struct addrinfo *res;
- for (int i = 0; i < nreqs; i++) {
- printf("[%02d] %s: ", i, reqs[i]\->ar_name);
+ for (size_t i = 0; i < nreqs; i++) {
+ printf("[%02zu] %s: ", i, reqs[i]\->ar_name);
ret = gai_error(reqs[i]);
if (!ret) {
diff --git a/man3/getdate.3 b/man3/getdate.3
index cc65ea3a2b..871475ef75 100644
--- a/man3/getdate.3
+++ b/man3/getdate.3
@@ -281,7 +281,7 @@ main(int argc, char *argv[])
{
struct tm *tmp;
- for (int j = 1; j < argc; j++) {
+ for (size_t j = 1; j < argc; j++) {
tmp = getdate(argv[j]);
if (tmp == NULL) {
@@ -290,7 +290,7 @@ main(int argc, char *argv[])
continue;
}
- printf("Call %d (\e"%s\e") succeeded:\en", j, argv[j]);
+ printf("Call %zu (\e"%s\e") succeeded:\en", j, argv[j]);
printf(" tm_sec = %d\en", tmp\->tm_sec);
printf(" tm_min = %d\en", tmp\->tm_min);
printf(" tm_hour = %d\en", tmp\->tm_hour);
diff --git a/man3/getgrent_r.3 b/man3/getgrent_r.3
index 0f734d5952..46112794f4 100644
--- a/man3/getgrent_r.3
+++ b/man3/getgrent_r.3
@@ -190,7 +190,7 @@ main(void)
if (i)
break;
printf("%s (%jd):", grpp\->gr_name, (intmax_t) grpp\->gr_gid);
- for (int j = 0; ; j++) {
+ for (size_t j = 0; ; j++) {
if (grpp\->gr_mem[j] == NULL)
break;
printf(" %s", grpp\->gr_mem[j]);
diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
index efba0eab42..4f0d686cc8 100644
--- a/man3/getgrouplist.3
+++ b/man3/getgrouplist.3
@@ -181,7 +181,7 @@ main(int argc, char *argv[])
/* Display list of retrieved groups, along with group names. */
fprintf(stderr, "ngroups = %d\en", ngroups);
- for (int j = 0; j < ngroups; j++) {
+ for (size_t j = 0; j < ngroups; j++) {
printf("%d", groups[j]);
gr = getgrgid(groups[j]);
if (gr != NULL)
diff --git a/man3/hsearch.3 b/man3/hsearch.3
index 1b35a61c1f..e3856aca6d 100644
--- a/man3/hsearch.3
+++ b/man3/hsearch.3
@@ -306,7 +306,7 @@ main(void)
hcreate(30);
- for (int i = 0; i < 24; i++) {
+ for (size_t i = 0; i < 24; i++) {
e.key = data[i];
/* data is just an integer, instead of a
pointer to something */
@@ -319,7 +319,7 @@ main(void)
}
}
- for (int i = 22; i < 26; i++) {
+ for (size_t i = 22; i < 26; i++) {
/* print two entries from the table, and
show that two are not in the table */
e.key = data[i];
diff --git a/man3/mallinfo.3 b/man3/mallinfo.3
index a1d13a38fd..66cee7c7a5 100644
--- a/man3/mallinfo.3
+++ b/man3/mallinfo.3
@@ -285,8 +285,7 @@ main(int argc, char *argv[])
{
#define MAX_ALLOCS 2000000
char *alloc[MAX_ALLOCS];
- int numBlocks, freeBegin, freeEnd, freeStep;
- size_t blockSize;
+ size_t blockSize, numBlocks, freeBegin, freeEnd, freeStep;
if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) {
fprintf(stderr, "%s num\-blocks block\-size [free\-step "
@@ -303,7 +302,7 @@ main(int argc, char *argv[])
printf("============== Before allocating blocks ==============\en");
display_mallinfo2();
- for (int j = 0; j < numBlocks; j++) {
+ for (size_t j = 0; j < numBlocks; j++) {
if (numBlocks >= MAX_ALLOCS) {
fprintf(stderr, "Too many allocations\en");
exit(EXIT_FAILURE);
@@ -319,7 +318,7 @@ main(int argc, char *argv[])
printf("\en============== After allocating blocks ==============\en");
display_mallinfo2();
- for (int j = freeBegin; j < freeEnd; j += freeStep)
+ for (size_t j = freeBegin; j < freeEnd; j += freeStep)
free(alloc[j]);
printf("\en============== After freeing blocks ==============\en");
diff --git a/man3/malloc_info.3 b/man3/malloc_info.3
index dbeb0ee926..f212630752 100644
--- a/man3/malloc_info.3
+++ b/man3/malloc_info.3
@@ -174,8 +174,9 @@ glibc 2.13
#include <stdlib.h>
#include <unistd.h>
-static size_t blockSize;
-static int numThreads, numBlocks;
+static size_t blockSize;
+static size_t numThreads;
+static unsigned int numBlocks;
static void *
thread_func(void *arg)
@@ -185,7 +186,7 @@ thread_func(void *arg)
/* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
the main thread) allocates a different amount of memory. */
- for (int j = 0; j < numBlocks; j++)
+ for (unsigned int j = 0; j < numBlocks; j++)
if (malloc(blockSize * (2 + tn)) == NULL)
err(EXIT_FAILURE, "malloc\-thread");
@@ -220,7 +221,7 @@ main(int argc, char *argv[])
/* Create threads that allocate different amounts of memory. */
- for (int tn = 0; tn < numThreads; tn++) {
+ for (size_t tn = 0; tn < numThreads; tn++) {
errno = pthread_create(&thr[tn], NULL, thread_func,
(void *) tn);
if (errno != 0)
@@ -237,7 +238,7 @@ main(int argc, char *argv[])
/* The main thread also allocates some memory. */
- for (int j = 0; j < numBlocks; j++)
+ for (unsigned int j = 0; j < numBlocks; j++)
if (malloc(blockSize) == NULL)
err(EXIT_FAILURE, "malloc");
diff --git a/man3/mtrace.3 b/man3/mtrace.3
index a7cb6176b8..d03e925341 100644
--- a/man3/mtrace.3
+++ b/man3/mtrace.3
@@ -137,7 +137,7 @@ main(void)
{
mtrace();
- for (int j = 0; j < 2; j++)
+ for (unsigned int j = 0; j < 2; j++)
malloc(100); /* Never freed\-\-a memory leak */
calloc(16, 16); /* Never freed\-\-a memory leak */
diff --git a/man3/pthread_create.3 b/man3/pthread_create.3
index 7c95b3ab7e..46b1b23b5e 100644
--- a/man3/pthread_create.3
+++ b/man3/pthread_create.3
@@ -311,8 +311,9 @@ thread_start(void *arg)
int
main(int argc, char *argv[])
{
- int s, opt, num_threads;
+ int s, opt;
void *res;
+ size_t num_threads;
ssize_t stack_size;
pthread_attr_t attr;
struct thread_info *tinfo;
@@ -355,7 +356,7 @@ main(int argc, char *argv[])
/* Create one thread for each command\-line argument. */
- for (int tnum = 0; tnum < num_threads; tnum++) {
+ for (size_t tnum = 0; tnum < num_threads; tnum++) {
tinfo[tnum].thread_num = tnum + 1;
tinfo[tnum].argv_string = argv[optind + tnum];
@@ -377,7 +378,7 @@ main(int argc, char *argv[])
/* Now join with each thread, and display its returned value. */
- for (int tnum = 0; tnum < num_threads; tnum++) {
+ for (size_t tnum = 0; tnum < num_threads; tnum++) {
s = pthread_join(tinfo[tnum].thread_id, &res);
if (s != 0)
handle_error_en(s, "pthread_join");
diff --git a/man3/pthread_getcpuclockid.3 b/man3/pthread_getcpuclockid.3
index 669ce2255c..1c2549e485 100644
--- a/man3/pthread_getcpuclockid.3
+++ b/man3/pthread_getcpuclockid.3
@@ -149,7 +149,7 @@ main(void)
sleep(1);
printf("Main thread consuming some CPU time...\en");
- for (int j = 0; j < 2000000; j++)
+ for (unsigned int j = 0; j < 2000000; j++)
getppid();
pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
diff --git a/man3/pthread_setaffinity_np.3 b/man3/pthread_setaffinity_np.3
index c86db65ba7..8a4fdc6bf2 100644
--- a/man3/pthread_setaffinity_np.3
+++ b/man3/pthread_setaffinity_np.3
@@ -176,7 +176,7 @@ main(void)
/* Set affinity mask to include CPUs 0 to 7. */
CPU_ZERO(&cpuset);
- for (int j = 0; j < 8; j++)
+ for (size_t j = 0; j < 8; j++)
CPU_SET(j, &cpuset);
s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
@@ -190,9 +190,9 @@ main(void)
errc(EXIT_FAILURE, s, "pthread_getaffinity_np");
printf("Set returned by pthread_getaffinity_np() contained:\en");
- for (int j = 0; j < CPU_SETSIZE; j++)
+ for (size_t j = 0; j < CPU_SETSIZE; j++)
if (CPU_ISSET(j, &cpuset))
- printf(" CPU %d\en", j);
+ printf(" CPU %zu\en", j);
exit(EXIT_SUCCESS);
}
diff --git a/man3/qsort.3 b/man3/qsort.3
index 1007653df5..fb9c2381dd 100644
--- a/man3/qsort.3
+++ b/man3/qsort.3
@@ -137,7 +137,7 @@ main(int argc, char *argv[])
qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
- for (int j = 1; j < argc; j++)
+ for (size_t j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
diff --git a/man3/rand.3 b/man3/rand.3
index c235170aee..25e715dd32 100644
--- a/man3/rand.3
+++ b/man3/rand.3
@@ -199,8 +199,8 @@ when given a particular seed.
int
main(int argc, char *argv[])
{
- int r, nloops;
- unsigned int seed;
+ int r;
+ unsigned int seed, nloops;
if (argc != 3) {
fprintf(stderr, "Usage: %s <seed> <nloops>\en", argv[0]);
@@ -211,7 +211,7 @@ main(int argc, char *argv[])
nloops = atoi(argv[2]);
srand(seed);
- for (int j = 0; j < nloops; j++) {
+ for (unsigned int j = 0; j < nloops; j++) {
r = rand();
printf("%d\en", r);
}
diff --git a/man3/regex.3 b/man3/regex.3
index e423e442d8..c86628e9a2 100644
--- a/man3/regex.3
+++ b/man3/regex.3
@@ -351,13 +351,13 @@ int main(void)
printf("String = \e"%s\e"\en", str);
printf("Matches:\en");
- for (int i = 0; ; i++) {
+ for (unsigned int i = 0; ; i++) {
if (regexec(&regex, s, ARRAY_SIZE(pmatch), pmatch, 0))
break;
off = pmatch[0].rm_so + (s \- str);
len = pmatch[0].rm_eo \- pmatch[0].rm_so;
- printf("#%d:\en", i);
+ printf("#%zu:\en", i);
printf("offset = %jd; length = %jd\en", (intmax_t) off,
(intmax_t) len);
printf("substring = \e"%.*s\e"\en", len, s + pmatch[0].rm_so);
diff --git a/man3/shm_open.3 b/man3/shm_open.3
index ea429ddd44..672fa5a88e 100644
--- a/man3/shm_open.3
+++ b/man3/shm_open.3
@@ -388,7 +388,7 @@ main(int argc, char *argv[])
/* Convert data in shared memory into upper case. */
- for (int j = 0; j < shmp\->cnt; j++)
+ for (size_t j = 0; j < shmp\->cnt; j++)
shmp\->buf[j] = toupper((unsigned char) shmp\->buf[j]);
/* Post \(aqsem2\(aq to tell the peer that it can now
diff --git a/man3/slist.3 b/man3/slist.3
index 6e32dba90d..bd0a09602e 100644
--- a/man3/slist.3
+++ b/man3/slist.3
@@ -293,7 +293,7 @@ main(void)
SLIST_REMOVE_HEAD(&head, entries); /* Deletion from the head */
free(n3);
- for (int i = 0; i < 5; i++) {
+ for (unsigned int i = 0; i < 5; i++) {
n1 = malloc(sizeof(struct entry));
SLIST_INSERT_HEAD(&head, n1, entries);
n1\->data = i;
diff --git a/man3/stailq.3 b/man3/stailq.3
index 359c1342c2..6a3d361ba9 100644
--- a/man3/stailq.3
+++ b/man3/stailq.3
@@ -351,7 +351,7 @@ main(void)
n1 = STAILQ_FIRST(&head);
n1\->data = 0;
- for (int i = 1; i < 5; i++) {
+ for (unsigned int i = 1; i < 5; i++) {
n1 = malloc(sizeof(struct entry));
STAILQ_INSERT_HEAD(&head, n1, entries);
n1\->data = i;
diff --git a/man3/strcat.3 b/man3/strcat.3
index b44208a45b..632cc2cc85 100644
--- a/man3/strcat.3
+++ b/man3/strcat.3
@@ -208,9 +208,9 @@ main(void)
base = time(NULL);
p[0] = \(aq\e0\(aq;
- for (int j = 0; j < LIM; j++) {
+ for (unsigned int j = 0; j < LIM; j++) {
if ((j % 10000) == 0)
- printf("%d %jd\en", j, (intmax_t) (time(NULL) \- base));
+ printf("%u %jd\en", j, (intmax_t) (time(NULL) \- base));
strcat(p, "a");
}
}
diff --git a/man3/strsep.3 b/man3/strsep.3
index 7a7a7f5988..adf3c7a6b2 100644
--- a/man3/strsep.3
+++ b/man3/strsep.3
@@ -141,8 +141,8 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- for (int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
- printf("%d: %s\en", j, token);
+ for (unsigned int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
+ printf("%u: %s\en", j, token);
while ((subtoken = strsep(&token, argv[3])))
printf("\et \-\-> %s\en", subtoken);
diff --git a/man3/tsearch.3 b/man3/tsearch.3
index 05380ed4b3..b2515e147f 100644
--- a/man3/tsearch.3
+++ b/man3/tsearch.3
@@ -311,7 +311,7 @@ main(void)
int **val;
srand(time(NULL));
- for (int i = 0; i < 12; i++) {
+ for (unsigned int i = 0; i < 12; i++) {
int *ptr = xmalloc(sizeof(*ptr));
*ptr = rand() & 0xff;
val = tsearch(ptr, &root, compare);
diff --git a/man3/wordexp.3 b/man3/wordexp.3
index ea3c631ecc..262126ec66 100644
--- a/man3/wordexp.3
+++ b/man3/wordexp.3
@@ -232,7 +232,7 @@ main(int argc, char *argv[])
wordexp("[a\-c]*.c", &p, 0);
w = p.we_wordv;
- for (int i = 0; i < p.we_wordc; i++)
+ for (size_t i = 0; i < p.we_wordc; i++)
printf("%s\en", w[i]);
wordfree(&p);
exit(EXIT_SUCCESS);
diff --git a/man5/core.5 b/man5/core.5
index 04e34bbf9f..4df94d3c13 100644
--- a/man5/core.5
+++ b/man5/core.5
@@ -652,8 +652,8 @@ main(int argc, char *argv[])
pipe program. */
fprintf(fp, "argc=%d\en", argc);
- for (int j = 0; j < argc; j++)
- fprintf(fp, "argc[%d]=<%s>\en", j, argv[j]);
+ for (size_t j = 0; j < argc; j++)
+ fprintf(fp, "argc[%zu]=<%s>\en", j, argv[j]);
/* Count bytes in standard input (the core dump). */
diff --git a/man7/aio.7 b/man7/aio.7
index 73ffbc5dd8..130b83d68a 100644
--- a/man7/aio.7
+++ b/man7/aio.7
@@ -312,7 +312,7 @@ main(int argc, char *argv[])
/* Open each file specified on the command line, and queue
a read request on the resulting file descriptor. */
- for (int j = 0; j < numReqs; j++) {
+ for (size_t j = 0; j < numReqs; j++) {
ioList[j].reqNum = j;
ioList[j].status = EINPROGRESS;
ioList[j].aiocbp = &aiocbList[j];
@@ -355,9 +355,9 @@ main(int argc, char *argv[])
printf("got SIGQUIT; canceling I/O requests: \en");
- for (int j = 0; j < numReqs; j++) {
+ for (size_t j = 0; j < numReqs; j++) {
if (ioList[j].status == EINPROGRESS) {
- printf(" Request %d on descriptor %d:", j,
+ printf(" Request %zu on descriptor %d:", j,
ioList[j].aiocbp\->aio_fildes);
s = aio_cancel(ioList[j].aiocbp\->aio_fildes,
ioList[j].aiocbp);
@@ -379,9 +379,9 @@ main(int argc, char *argv[])
in progress. */
printf("aio_error():\en");
- for (int j = 0; j < numReqs; j++) {
+ for (size_t j = 0; j < numReqs; j++) {
if (ioList[j].status == EINPROGRESS) {
- printf(" for request %d (descriptor %d): ",
+ printf(" for request %zu (descriptor %d): ",
j, ioList[j].aiocbp\->aio_fildes);
ioList[j].status = aio_error(ioList[j].aiocbp);
@@ -411,11 +411,11 @@ main(int argc, char *argv[])
/* Check status return of all I/O requests. */
printf("aio_return():\en");
- for (int j = 0; j < numReqs; j++) {
+ for (size_t j = 0; j < numReqs; j++) {
ssize_t s;
s = aio_return(ioList[j].aiocbp);
- printf(" for request %d (descriptor %d): %zd\en",
+ printf(" for request %zu (descriptor %d): %zd\en",
j, ioList[j].aiocbp\->aio_fildes, s);
}
diff --git a/man7/inotify.7 b/man7/inotify.7
index dd6bc37d4c..77c79ab628 100644
--- a/man7/inotify.7
+++ b/man7/inotify.7
@@ -965,7 +965,7 @@ handle_events(int fd, int *wd, int argc, char* argv[])
/* Print the name of the watched directory. */
- for (int i = 1; i < argc; ++i) {
+ for (size_t i = 1; i < argc; ++i) {
if (wd[i] == event\->wd) {
printf("%s/", argv[i]);
break;
diff --git a/man7/unix.7 b/man7/unix.7
index 37f22320f6..f44ffbe309 100644
--- a/man7/unix.7
+++ b/man7/unix.7
@@ -1154,7 +1154,7 @@ main(int argc, char *argv[])
/* Send arguments. */
- for (int i = 1; i < argc; ++i) {
+ for (size_t i = 1; i < argc; ++i) {
ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
if (ret == \-1) {
perror("write");
diff --git a/man7/user_namespaces.7 b/man7/user_namespaces.7
index 74a0669666..7ed3b026f1 100644
--- a/man7/user_namespaces.7
+++ b/man7/user_namespaces.7
@@ -1220,7 +1220,7 @@ update_map(char *mapping, char *map_file)
/* Replace commas in mapping string with newlines. */
map_len = strlen(mapping);
- for (int j = 0; j < map_len; j++)
+ for (size_t j = 0; j < map_len; j++)
if (mapping[j] == \(aq,\(aq)
mapping[j] = \(aq\en\(aq;