]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
Various pages: EXAMPLES: Use unsigned types for loop iterators
authorAlex Colomar <alx.manpages@gmail.com>
Thu, 15 Sep 2022 14:40:27 +0000 (16:40 +0200)
committerAlex Colomar <alx.manpages@gmail.com>
Thu, 15 Sep 2022 16:07:48 +0000 (18:07 +0200)
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>
46 files changed:
man1/sprof.1
man2/close_range.2
man2/eventfd.2
man2/execve.2
man2/futex.2
man2/open_by_handle_at.2
man2/poll.2
man2/recvmmsg.2
man2/sched_setaffinity.2
man2/timerfd_create.2
man2/userfaultfd.2
man3/CPU_SET.3
man3/backtrace.3
man3/bsearch.3
man3/dl_iterate_phdr.3
man3/dlinfo.3
man3/encrypt.3
man3/envz_add.3
man3/fopencookie.3
man3/getaddrinfo.3
man3/getaddrinfo_a.3
man3/getdate.3
man3/getgrent_r.3
man3/getgrouplist.3
man3/hsearch.3
man3/mallinfo.3
man3/malloc_info.3
man3/mtrace.3
man3/pthread_create.3
man3/pthread_getcpuclockid.3
man3/pthread_setaffinity_np.3
man3/qsort.3
man3/rand.3
man3/regex.3
man3/shm_open.3
man3/slist.3
man3/stailq.3
man3/strcat.3
man3/strsep.3
man3/tsearch.3
man3/wordexp.3
man5/core.5
man7/aio.7
man7/inotify.7
man7/unix.7
man7/user_namespaces.7

index 35bd42c3d4b2a15b45bccba42c57b9ba51f56e65..0acb51b4605516cf96dd8524f9dbe4ab0545a8c5 100644 (file)
@@ -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
index 386f829c956d61ee8610d4b8ac39925defff57bf..054c91c706bdee9199471d0a2084cfd8c8c6f7fd 100644 (file)
@@ -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]);
index 71517ad3a44953fff1fdd9072dfe6e12d3abbf8b..8315f852c1a4d11e528aaf32f29b2dac8b2d9e20 100644 (file)
@@ -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 */
index 744d06c273f020705f2d1986d83812676cb26fb3..4855d7c1e921ca7256d8edff217c85788ff98826 100644 (file)
@@ -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);
 }
index 1332ae852262269af403ce5b3cd221aa7000b15b..53caab621fbc4596b76a8d589b28cdbbe2a574b8 100644 (file)
@@ -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);
     }
 
index de9c3b745e29210d1545ebd43a73961433e40db1..48d07fde777f086dd5a6bef0344daa686496659a 100644 (file)
@@ -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
index e8be13543c1c16de32691eeb5cd693d6482b28d7..8d2b08d63c6cc53e39644bedcbb7fd1dc08c57ad 100644 (file)
@@ -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 "  : "",
index 4a76273a105b1604181ef583dfc8d3e9e7c80953..0e411729783746ecba6352dcc09b050b70e7e1de 100644 (file)
@@ -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);
 }
index 553ccf36d91464e006055cd00290a75e888f2d72..b4963e853e9b956eb442b384b25afee04ef27b1a 100644 (file)
@@ -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 */
index 9850f14c9e33d20f46df0661140d44d84ea29745..c53c97e2483690e3ae285e923c2b1d51ab8910cc 100644 (file)
@@ -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) {
index c91a83132a185df148364706b61620fd83834ee1..b2ee1ed3b02c4694431af9c22f26c2af45dafd65 100644 (file)
@@ -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;
index 968da2b39f8d4756da01a4559dabf71c7b6b6e98..c043ecc6dd0d14f463cbb2f7c6b65a90ec05d4cb 100644 (file)
@@ -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));
index 8b91cb1258fb58a84fe51bcaa51b0fb2c478350c..9d62a7f78aab0bf9c0f8ec60d53e562febf7d40f 100644 (file)
@@ -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);
index d6f5ff1d83faacaf0b476103b3c0089d58ec290e..b46b7e433ba8d44211a4b195a950240dd5a1fd1f 100644 (file)
@@ -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;
 
index 717f25027da5908f511bd562e81999492151dac4..f0a552abca8087ab1e6e12ddbfe51c851e06a770 100644 (file)
@@ -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);
index 05cba804634d9d4c67e425bca95687c313987a01..cac9b48246fa7f4aa2a5cc62e1f3ce0a38cfa2f6 100644 (file)
@@ -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);
index 671be82a25301ff8146a12372c798b397f7f32e1..6970e2f70f522420251c38b571360bb0e24d4a7b 100644 (file)
@@ -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;
index c983c070267d84d23922cba8a9931770f4e6e23d..08782cf0f11d543a5dc0c25f791547d56b747f0e 100644 (file)
@@ -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");
index 86b25cb83a43f2eaa627e50ebe0a14a9406dcebb..0d05fa13a85cd74f51a4fad8ff06f53152606d0d 100644 (file)
@@ -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);
index d4d7afa79529fb9c7a29cd59f2fb0c1f81aca3a0..51adb3be93788f5451e629893923f3ed4f1a15b1 100644 (file)
@@ -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;
         }
 
index a45179d16a9f7f92cdc825c62f4a43b60ac8b471..b6d9bd3edd0513ee68e8cf853bb83a3b49431d09 100644 (file)
@@ -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) {
index cc65ea3a2b10a7e90597689951f5e559e3c45f6e..871475ef756cef971b1798921bd3ff01c410de0e 100644 (file)
@@ -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);
index 0f734d59522007cc4095fc7457ab33857cb77c4e..46112794f4ad48587d9bb48dfb2d6fcc4e675f79 100644 (file)
@@ -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]);
index efba0eab429a44976b1e4538c5aafb7e1edbabdf..4f0d686cc8e4ff11986015a7376389746e12fa0a 100644 (file)
@@ -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)
index 1b35a61c1f78fafa330c36228bae39805aa93376..e3856aca6db1c682623c79d7a151d3ef9fce7ebc 100644 (file)
@@ -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];
index a1d13a38fd8fc676b24cad7d9b068083868ebdca..66cee7c7a52c4ae8f810939011801145321333f1 100644 (file)
@@ -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");
index dbeb0ee9263f2a583633fec7eef145a56cf36157..f212630752493b6d054b500247dabadde7f70698 100644 (file)
@@ -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");
 
index a7cb6176b83e6144c9283164b679fb2f8e1b167c..d03e925341fb5ec26eb9260dc9a9a39fa381c536 100644 (file)
@@ -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 */
index 7c95b3ab7ece2c3d5908b58ed2d4f592412e7f34..46b1b23b5edd90f71e7a4c8779ead7eac5c63147 100644 (file)
@@ -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");
index 669ce2255c8b301112a6cae4fa3db594714868c8..1c2549e485cf59ee86ebea8914b5467c2e8bed16 100644 (file)
@@ -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);
index c86db65ba7ef78377c926d2720414ed938855a44..8a4fdc6bf280f5e20499d5ee962099779351e14c 100644 (file)
@@ -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);
 }
index 1007653df504d1553a568dacb8d9e2a20516d867..fb9c2381dd1672c314e9b78aca34eefc61341753 100644 (file)
@@ -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);
 }
index c235170aee9862fc73fb33a9eded0f2bcf19e2b7..25e715dd32c5be9f7c5e387ef5a2a248ffe5e68a 100644 (file)
@@ -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);
     }
index e423e442d8333cb4dd83c6f58bd301523de1ec7c..c86628e9a2fbdad6ead3a99a46880238f79773c8 100644 (file)
@@ -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);
index ea429ddd44a7fbf7f664804fffae956e30da4f68..672fa5a88ef32bf215e8ac78f82972db03c22498 100644 (file)
@@ -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
index 6e32dba90dfed78830018de8ac15f245b2a574d0..bd0a09602e0d805f24fed462671c71b3bf605d10 100644 (file)
@@ -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;
index 359c1342c2b2cbd9a0d0a69bd239844f7ae7fe6f..6a3d361ba9b12f353a06ac8a688b76038a56564e 100644 (file)
@@ -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;
index b44208a45bf10f067a6076dc1aada52f2b232059..632cc2cc852d59f174187efbb7bbf02c600133b9 100644 (file)
@@ -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");
     }
 }
index 7a7a7f59881d0d5a8412d70a43c52829fea6c571..adf3c7a6b29d761834fd5d36052e9556bc922a34 100644 (file)
@@ -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);
index 05380ed4b3d98cb3e7711013709ff6bf112334fd..b2515e147f9eaab66954cb37a89256f0914581c7 100644 (file)
@@ -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);
index ea3c631ecc05f2a7b6bd8b1b339c273b408d94d6..262126ec66b8698e086907aecf61bc484f768f0b 100644 (file)
@@ -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);
index 04e34bbf9f998f9677cd2f4097455d7541750e95..4df94d3c1376eebe0394f3f6461206fde8e2652a 100644 (file)
@@ -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). */
 
index 73ffbc5dd82b5effd8f658617b3142ecadc6dd84..130b83d68ac2e20316632e92f13fa1c8d90c90df 100644 (file)
@@ -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);
     }
 
index dd6bc37d4c0e3faa30c440dd02eebc443e69baee..77c79ab628d18eeecdcca4252bf2feb3d5749a36 100644 (file)
@@ -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;
index 37f22320f60167c2e284d5d8658cd3000f6d5081..f44ffbe3094c77050bab5ba6976f8ba9e9dfa55d 100644 (file)
@@ -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");
index 74a0669666df273a8e7ff39f352b5a6c1d82571c..7ed3b026f17c15efb047d1a72081c765a6a04da8 100644 (file)
@@ -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;