sprof.1, eventfd.2, execve.2, futex.2, getdents.2, mprotect.2, open_by_handle_at.2, recvmmsg.2, sched_setaffinity.2, CPU_SET.3, backtrace.3, bsearch.3, dl_iterate_phdr.3, dlinfo.3, duplocale.3, encrypt.3, envz_add.3, fopencookie.3, getaddrinfo.3, getaddrinfo_a.3, getdate.3, getgrent_r.3, getgrouplist.3, getifaddrs.3, getprotoent_r.3, getservent_r.3, hsearch.3, mallinfo.3, malloc_info.3, mbstowcs.3, mtrace.3, pthread_create.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, qsort.3, rand.3, strcat.3, strtok.3, tsearch.3, wordexp.3, core.5, aio.7, inotify.7, sock_diag.7, unix.7, user_namespaces.7: Use C99 style to declare loop counter variables

Rather than:

    sometype x;

    for (x = ....; ...)

use

    for (sometype x = ...; ...)

This brings the declaration and use closer together (thus aiding
readability) and also clearly indicates the scope of the loop
counter variable.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2020-09-05 17:18:10 +02:00
parent 3b27ce1573
commit 88893a773c
46 changed files with 99 additions and 135 deletions

View File

@ -116,35 +116,27 @@ $ \fBcat libdemo.c\fP
void void
consumeCpu1(int lim) consumeCpu1(int lim)
{ {
int j; for (int j = 0; j < lim; j++)
for (j = 0; j < lim; j++)
getppid(); getppid();
} }
void void
x1(void) { x1(void) {
int j; for (int j = 0; j < 100; j++)
for (j = 0; j < 100; j++)
consumeCpu1(200000); consumeCpu1(200000);
} }
void void
consumeCpu2(int lim) consumeCpu2(int lim)
{ {
int j; for (int j = 0; j < lim; j++)
for (j = 0; j < lim; j++)
getppid(); getppid();
} }
void void
x2(void) x2(void)
{ {
int j; for (int j = 0; j < 1000; j++)
for (j = 0; j < 1000; j++)
consumeCpu2(10000); consumeCpu2(10000);
} }
.EE .EE

View File

@ -396,7 +396,7 @@ Parent read 28 (0x1c) from efd
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int efd, j; int efd;
uint64_t u; uint64_t u;
ssize_t s; ssize_t s;
@ -411,7 +411,7 @@ main(int argc, char *argv[])
switch (fork()) { switch (fork()) {
case 0: case 0:
for (j = 1; j < argc; j++) { for (int j = 1; j < argc; j++) {
printf("Child writing %s to efd\en", argv[j]); printf("Child writing %s to efd\en", argv[j]);
u = strtoull(argv[j], NULL, 0); u = strtoull(argv[j], NULL, 0);
/* strtoull() allows various bases */ /* strtoull() allows various bases */

View File

@ -794,9 +794,7 @@ It just echoes its command-line arguments, one per line.
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int j; for (int j = 0; j < argc; j++)
for (j = 0; j < argc; j++)
printf("argv[%d]: %s\en", j, argv[j]); printf("argv[%d]: %s\en", j, argv[j]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -1828,7 +1828,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
pid_t childPid; pid_t childPid;
int j, nloops; int nloops;
setbuf(stdout, NULL); setbuf(stdout, NULL);
@ -1858,7 +1858,7 @@ main(int argc, char *argv[])
errExit("fork"); errExit("fork");
if (childPid == 0) { /* Child */ if (childPid == 0) { /* Child */
for (j = 0; j < nloops; j++) { for (int j = 0; j < nloops; j++) {
fwait(futex1); fwait(futex1);
printf("Child (%ld) %d\en", (long) getpid(), j); printf("Child (%ld) %d\en", (long) getpid(), j);
fpost(futex2); fpost(futex2);
@ -1869,7 +1869,7 @@ main(int argc, char *argv[])
/* Parent falls through to here */ /* Parent falls through to here */
for (j = 0; j < nloops; j++) { for (int j = 0; j < nloops; j++) {
fwait(futex2); fwait(futex2);
printf("Parent (%ld) %d\en", (long) getpid(), j); printf("Parent (%ld) %d\en", (long) getpid(), j);
fpost(futex1); fpost(futex1);

View File

@ -280,7 +280,6 @@ main(int argc, char *argv[])
int fd, nread; int fd, nread;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
struct linux_dirent *d; struct linux_dirent *d;
int bpos;
char d_type; char d_type;
fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY); fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
@ -297,7 +296,7 @@ main(int argc, char *argv[])
printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread); printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
printf("inode# file type d_reclen d_off d_name\en"); printf("inode# file type d_reclen d_off d_name\en");
for (bpos = 0; bpos < nread;) { for (int bpos = 0; bpos < nread;) {
d = (struct linux_dirent *) (buf + bpos); d = (struct linux_dirent *) (buf + bpos);
printf("%8ld ", d\->d_ino); printf("%8ld ", d\->d_ino);
d_type = *(buf + bpos + d\->d_reclen \- 1); d_type = *(buf + bpos + d\->d_reclen \- 1);

View File

@ -334,7 +334,6 @@ handler(int sig, siginfo_t *si, void *unused)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *p;
int pagesize; int pagesize;
struct sigaction sa; struct sigaction sa;
@ -361,7 +360,7 @@ main(int argc, char *argv[])
PROT_READ) == \-1) PROT_READ) == \-1)
handle_error("mprotect"); handle_error("mprotect");
for (p = buffer ; ; ) for (char *p = buffer ; ; )
*(p++) = \(aqa\(aq; *(p++) = \(aqa\(aq;
printf("Loop completed\en"); /* Should never happen */ printf("Loop completed\en"); /* Should never happen */

View File

@ -555,7 +555,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct file_handle *fhp; struct file_handle *fhp;
int mount_id, fhsize, flags, dirfd, j; int mount_id, fhsize, flags, dirfd;
char *pathname; char *pathname;
if (argc != 2) { if (argc != 2) {
@ -601,7 +601,7 @@ main(int argc, char *argv[])
printf("%d\en", mount_id); printf("%d\en", mount_id);
printf("%d %d ", fhp\->handle_bytes, fhp\->handle_type); printf("%d %d ", fhp\->handle_bytes, fhp\->handle_type);
for (j = 0; j < fhp\->handle_bytes; j++) for (int j = 0; j < fhp\->handle_bytes; j++)
printf(" %02x", fhp\->f_handle[j]); printf(" %02x", fhp\->f_handle[j]);
printf("\en"); printf("\en");
@ -677,7 +677,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct file_handle *fhp; struct file_handle *fhp;
int mount_id, fd, mount_fd, handle_bytes, j; int mount_id, fd, mount_fd, handle_bytes;
ssize_t nread; ssize_t nread;
char buf[1000]; char buf[1000];
#define LINE_SIZE 100 #define LINE_SIZE 100
@ -715,7 +715,7 @@ main(int argc, char *argv[])
fhp\->handle_type = strtoul(nextp, &nextp, 0); fhp\->handle_type = strtoul(nextp, &nextp, 0);
for (j = 0; j < fhp\->handle_bytes; j++) for (int j = 0; j < fhp\->handle_bytes; j++)
fhp\->f_handle[j] = strtoul(nextp, &nextp, 16); fhp\->f_handle[j] = strtoul(nextp, &nextp, 16);
/* Obtain file descriptor for mount point, either by opening /* Obtain file descriptor for mount point, either by opening

View File

@ -237,7 +237,7 @@ main(void)
#define VLEN 10 #define VLEN 10
#define BUFSIZE 200 #define BUFSIZE 200
#define TIMEOUT 1 #define TIMEOUT 1
int sockfd, retval, i; int sockfd, retval;
struct sockaddr_in addr; struct sockaddr_in addr;
struct mmsghdr msgs[VLEN]; struct mmsghdr msgs[VLEN];
struct iovec iovecs[VLEN]; struct iovec iovecs[VLEN];
@ -259,7 +259,7 @@ main(void)
} }
memset(msgs, 0, sizeof(msgs)); memset(msgs, 0, sizeof(msgs));
for (i = 0; i < VLEN; i++) { for (int i = 0; i < VLEN; i++) {
iovecs[i].iov_base = bufs[i]; iovecs[i].iov_base = bufs[i];
iovecs[i].iov_len = BUFSIZE; iovecs[i].iov_len = BUFSIZE;
msgs[i].msg_hdr.msg_iov = &iovecs[i]; msgs[i].msg_hdr.msg_iov = &iovecs[i];
@ -276,7 +276,7 @@ main(void)
} }
printf("%d messages received\en", retval); printf("%d messages received\en", retval);
for (i = 0; i < retval; i++) { for (int i = 0; i < retval; i++) {
bufs[i][msgs[i].msg_len] = 0; bufs[i][msgs[i].msg_len] = 0;
printf("%d %s", i+1, bufs[i]); printf("%d %s", i+1, bufs[i]);
} }

View File

@ -370,7 +370,7 @@ main(int argc, char *argv[])
{ {
cpu_set_t set; cpu_set_t set;
int parentCPU, childCPU; int parentCPU, childCPU;
int nloops, j; int nloops;
if (argc != 4) { if (argc != 4) {
fprintf(stderr, "Usage: %s parent\-cpu child\-cpu num\-loops\en", fprintf(stderr, "Usage: %s parent\-cpu child\-cpu num\-loops\en",
@ -394,7 +394,7 @@ main(int argc, char *argv[])
if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1) if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
errExit("sched_setaffinity"); errExit("sched_setaffinity");
for (j = 0; j < nloops; j++) for (int j = 0; j < nloops; j++)
getppid(); getppid();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -405,7 +405,7 @@ main(int argc, char *argv[])
if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1) if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
errExit("sched_setaffinity"); errExit("sched_setaffinity");
for (j = 0; j < nloops; j++) for (int j = 0; j < nloops; j++)
getppid(); getppid();
wait(NULL); /* Wait for child to terminate */ wait(NULL); /* Wait for child to terminate */

View File

@ -329,7 +329,7 @@ main(int argc, char *argv[])
{ {
cpu_set_t *cpusetp; cpu_set_t *cpusetp;
size_t size; size_t size;
int num_cpus, cpu; int num_cpus;
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Usage: %s <num\-cpus>\en", argv[0]); fprintf(stderr, "Usage: %s <num\-cpus>\en", argv[0]);
@ -347,7 +347,7 @@ main(int argc, char *argv[])
size = CPU_ALLOC_SIZE(num_cpus); size = CPU_ALLOC_SIZE(num_cpus);
CPU_ZERO_S(size, cpusetp); CPU_ZERO_S(size, cpusetp);
for (cpu = 0; cpu < num_cpus; cpu += 2) for (int cpu = 0; cpu < num_cpus; cpu += 2)
CPU_SET_S(cpu, size, cpusetp); CPU_SET_S(cpu, size, cpusetp);
printf("CPU_COUNT() of set: %d\en", CPU_COUNT_S(size, cpusetp)); printf("CPU_COUNT() of set: %d\en", CPU_COUNT_S(size, cpusetp));

View File

@ -232,7 +232,7 @@ backtrace() returned 8 addresses
void void
myfunc3(void) myfunc3(void)
{ {
int j, nptrs; int nptrs;
void *buffer[BT_BUF_SIZE]; void *buffer[BT_BUF_SIZE];
char **strings; char **strings;
@ -248,7 +248,7 @@ myfunc3(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (j = 0; j < nptrs; j++) for (int j = 0; j < nptrs; j++)
printf("%s\en", strings[j]); printf("%s\en", strings[j]);
free(strings); free(strings);

View File

@ -122,10 +122,8 @@ compmi(const void *m1, const void *m2)
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int i;
qsort(months, nr_of_months, sizeof(months[0]), compmi); qsort(months, nr_of_months, sizeof(months[0]), compmi);
for (i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
struct mi key, *res; struct mi key, *res;
key.name = argv[i]; key.name = argv[i];
res = bsearch(&key, months, nr_of_months, res = bsearch(&key, months, nr_of_months,

View File

@ -307,12 +307,12 @@ static int
callback(struct dl_phdr_info *info, size_t size, void *data) callback(struct dl_phdr_info *info, size_t size, void *data)
{ {
char *type; char *type;
int p_type, j; int p_type;
printf("Name: \e"%s\e" (%d segments)\en", info\->dlpi_name, printf("Name: \e"%s\e" (%d segments)\en", info\->dlpi_name,
info\->dlpi_phnum); info\->dlpi_phnum);
for (j = 0; j < info\->dlpi_phnum; j++) { for (int j = 0; j < info\->dlpi_phnum; j++) {
p_type = info\->dlpi_phdr[j].p_type; p_type = info\->dlpi_phdr[j].p_type;
type = (p_type == PT_LOAD) ? "PT_LOAD" : type = (p_type == PT_LOAD) ? "PT_LOAD" :
(p_type == PT_DYNAMIC) ? "PT_DYNAMIC" : (p_type == PT_DYNAMIC) ? "PT_DYNAMIC" :

View File

@ -271,7 +271,6 @@ main(int argc, char *argv[])
void *handle; void *handle;
Dl_serinfo serinfo; Dl_serinfo serinfo;
Dl_serinfo *sip; Dl_serinfo *sip;
int j;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s <libpath>\en", argv[0]); fprintf(stderr, "Usage: %s <libpath>\en", argv[0]);
@ -317,7 +316,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (j = 0; j < serinfo.dls_cnt; j++) for (int j = 0; j < serinfo.dls_cnt; j++)
printf("dls_serpath[%d].dls_name = %s\en", printf("dls_serpath[%d].dls_name = %s\en",
j, sip\->dls_serpath[j].dls_name); j, sip\->dls_serpath[j].dls_name);

View File

@ -148,7 +148,6 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
locale_t loc, nloc; locale_t loc, nloc;
char *p;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s string\en", argv[0]); fprintf(stderr, "Usage: %s string\en", argv[0]);
@ -167,7 +166,7 @@ main(int argc, char *argv[])
if (nloc == (locale_t) 0) if (nloc == (locale_t) 0)
errExit("duplocale"); errExit("duplocale");
for (p = argv[1]; *p; p++) for (char *p = argv[1]; *p; p++)
putchar(toupper_l(*p, nloc)); putchar(toupper_l(*p, nloc));
printf("\en"); printf("\en");

View File

@ -178,14 +178,13 @@ main(void)
char orig[9] = "eggplant"; char orig[9] = "eggplant";
char buf[64]; char buf[64];
char txt[9]; char txt[9];
int i, j;
for (i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
key[i] = rand() & 1; key[i] = rand() & 1;
} }
for (i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
buf[i * 8 + j] = orig[i] >> j & 1; buf[i * 8 + j] = orig[i] >> j & 1;
} }
setkey(key); setkey(key);
@ -193,8 +192,8 @@ main(void)
printf("Before encrypting: %s\en", orig); printf("Before encrypting: %s\en", orig);
encrypt(buf, 0); encrypt(buf, 0);
for (i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
for (j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) { for (int j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) {
txt[i] |= buf[i * 8 + j] << j; txt[i] |= buf[i * 8 + j] << j;
} }
txt[8] = \(aq\e0\(aq; txt[8] = \(aq\e0\(aq;
@ -202,8 +201,8 @@ main(void)
printf("After encrypting: %s\en", txt); printf("After encrypting: %s\en", txt);
encrypt(buf, 1); encrypt(buf, 1);
for (i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
for (j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) { for (int j = 0, txt[i] = \(aq\e0\(aq; j < 8; j++) {
txt[i] |= buf[i * 8 + j] << j; txt[i] |= buf[i * 8 + j] << j;
} }
txt[8] = \(aq\e0\(aq; txt[8] = \(aq\e0\(aq;

View File

@ -147,10 +147,10 @@ Handle with care.
int int
main(int argc, char *argv[], char *envp[]) main(int argc, char *argv[], char *envp[])
{ {
int i, e_len = 0; int e_len = 0;
char *str; char *str;
for (i = 0; envp[i] != NULL; i++) for (int i = 0; envp[i] != NULL; i++)
e_len += strlen(envp[i]) + 1; e_len += strlen(envp[i]) + 1;
str = envz_entry(*envp, e_len, "HOME"); str = envz_entry(*envp, e_len, "HOME");

View File

@ -393,8 +393,6 @@ main(int argc, char *argv[])
FILE *stream; FILE *stream;
struct memfile_cookie mycookie; struct memfile_cookie mycookie;
ssize_t nread; ssize_t nread;
long p;
int j;
char buf[1000]; char buf[1000];
/* Set up the cookie before calling fopencookie() */ /* Set up the cookie before calling fopencookie() */
@ -417,7 +415,7 @@ main(int argc, char *argv[])
/* Write command\-line arguments to our file */ /* Write command\-line arguments to our file */
for (j = 1; j < argc; j++) for (int j = 1; j < argc; j++)
if (fputs(argv[j], stream) == EOF) { if (fputs(argv[j], stream) == EOF) {
perror("fputs"); perror("fputs");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -425,7 +423,7 @@ main(int argc, char *argv[])
/* Read two bytes out of every five, until EOF */ /* Read two bytes out of every five, until EOF */
for (p = 0; ; p += 5) { for (long p = 0; ; p += 5) {
if (fseek(stream, p, SEEK_SET) == \-1) { if (fseek(stream, p, SEEK_SET) == \-1) {
perror("fseek"); perror("fseek");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -763,7 +763,7 @@ main(int argc, char *argv[])
{ {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *result, *rp; struct addrinfo *result, *rp;
int sfd, s, j; int sfd, s;
size_t len; size_t len;
ssize_t nread; ssize_t nread;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
@ -814,7 +814,7 @@ main(int argc, char *argv[])
/* Send remaining command\-line arguments as separate /* Send remaining command\-line arguments as separate
datagrams, and read responses from server */ datagrams, and read responses from server */
for (j = 3; j < argc; j++) { for (int j = 3; j < argc; j++) {
len = strlen(argv[j]) + 1; len = strlen(argv[j]) + 1;
/* +1 for terminating null byte */ /* +1 for terminating null byte */

View File

@ -359,7 +359,7 @@ Here is the program source code
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, ret; int ret;
struct gaicb *reqs[argc \- 1]; struct gaicb *reqs[argc \- 1];
char host[NI_MAXHOST]; char host[NI_MAXHOST];
struct addrinfo *res; struct addrinfo *res;
@ -369,7 +369,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (i = 0; i < argc \- 1; i++) { for (int i = 0; i < argc \- 1; i++) {
reqs[i] = malloc(sizeof(*reqs[0])); reqs[i] = malloc(sizeof(*reqs[0]));
if (reqs[i] == NULL) { if (reqs[i] == NULL) {
perror("malloc"); perror("malloc");
@ -386,7 +386,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (i = 0; i < argc \- 1; i++) { for (int i = 0; i < argc \- 1; i++) {
printf("%s: ", reqs[i]\->ar_name); printf("%s: ", reqs[i]\->ar_name);
ret = gai_error(reqs[i]); ret = gai_error(reqs[i]);
if (ret == 0) { if (ret == 0) {

View File

@ -290,9 +290,8 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct tm *tmp; struct tm *tmp;
int j;
for (j = 1; j < argc; j++) { for (int j = 1; j < argc; j++) {
tmp = getdate(argv[j]); tmp = getdate(argv[j]);
if (tmp == NULL) { if (tmp == NULL) {

View File

@ -190,10 +190,10 @@ main(void)
if (i) if (i)
break; break;
printf("%s (%d):", grpp\->gr_name, grpp\->gr_gid); printf("%s (%d):", grpp\->gr_name, grpp\->gr_gid);
for (i = 0; ; i++) { for (int j = 0; ; j++) {
if (grpp\->gr_mem[i] == NULL) if (grpp\->gr_mem[j] == NULL)
break; break;
printf(" %s", grpp\->gr_mem[i]); printf(" %s", grpp\->gr_mem[j]);
} }
printf("\en"); printf("\en");
} }

View File

@ -152,7 +152,7 @@ ngroups = 3
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int j, ngroups; int ngroups;
struct passwd *pw; struct passwd *pw;
struct group *gr; struct group *gr;
@ -188,7 +188,7 @@ main(int argc, char *argv[])
/* Display list of retrieved groups, along with group names */ /* Display list of retrieved groups, along with group names */
fprintf(stderr, "ngroups = %d\en", ngroups); fprintf(stderr, "ngroups = %d\en", ngroups);
for (j = 0; j < ngroups; j++) { for (int j = 0; j < ngroups; j++) {
printf("%d", groups[j]); printf("%d", groups[j]);
gr = getgrgid(groups[j]); gr = getgrgid(groups[j]);
if (gr != NULL) if (gr != NULL)

View File

@ -263,7 +263,7 @@ wlp3s0 AF_INET6 (10)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr;
int family, s; int family, s;
char host[NI_MAXHOST]; char host[NI_MAXHOST];
@ -275,7 +275,8 @@ int main(int argc, char *argv[])
/* Walk through linked list, maintaining head pointer so we /* Walk through linked list, maintaining head pointer so we
can free list later */ can free list later */
for (ifa = ifaddr; ifa != NULL; ifa = ifa\->ifa_next) { for (struct ifaddrs *ifa = ifaddr; ifa != NULL;
ifa = ifa\->ifa_next) {
if (ifa\->ifa_addr == NULL) if (ifa\->ifa_addr == NULL)
continue; continue;

View File

@ -197,7 +197,6 @@ main(int argc, char *argv[])
struct protoent result_buf; struct protoent result_buf;
struct protoent *result; struct protoent *result;
char buf[MAX_BUF]; char buf[MAX_BUF];
char **p;
if (argc < 2) { if (argc < 2) {
printf("Usage: %s proto\-name [buflen]\en", argv[0]); printf("Usage: %s proto\-name [buflen]\en", argv[0]);
@ -245,7 +244,7 @@ main(int argc, char *argv[])
printf("p_name=%s; p_proto=%d; aliases=", printf("p_name=%s; p_proto=%d; aliases=",
result_buf.p_name, result_buf.p_proto); result_buf.p_name, result_buf.p_proto);
for (p = result_buf.p_aliases; *p != NULL; p++) for (char **p = result_buf.p_aliases; *p != NULL; p++)
printf("%s ", *p); printf("%s ", *p);
printf("\en"); printf("\en");

View File

@ -195,7 +195,6 @@ main(int argc, char *argv[])
struct servent *result; struct servent *result;
char buf[MAX_BUF]; char buf[MAX_BUF];
char *protop; char *protop;
char **p;
if (argc < 3) { if (argc < 3) {
printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]); printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
@ -248,7 +247,7 @@ main(int argc, char *argv[])
printf("s_name=%s; s_proto=%s; s_port=%d; aliases=", printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
result_buf.s_name, result_buf.s_proto, result_buf.s_name, result_buf.s_proto,
ntohs(result_buf.s_port)); ntohs(result_buf.s_port));
for (p = result_buf.s_aliases; *p != NULL; p++) for (char **p = result_buf.s_aliases; *p != NULL; p++)
printf("%s ", *p); printf("%s ", *p);
printf("\en"); printf("\en");

View File

@ -316,11 +316,10 @@ int
main(void) main(void)
{ {
ENTRY e, *ep; ENTRY e, *ep;
int i;
hcreate(30); hcreate(30);
for (i = 0; i < 24; i++) { for (int i = 0; i < 24; i++) {
e.key = data[i]; e.key = data[i];
/* data is just an integer, instead of a /* data is just an integer, instead of a
pointer to something */ pointer to something */
@ -333,7 +332,7 @@ main(void)
} }
} }
for (i = 22; i < 26; i++) { for (int i = 22; i < 26; i++) {
/* print two entries from the table, and /* print two entries from the table, and
show that two are not in the table */ show that two are not in the table */
e.key = data[i]; e.key = data[i];

View File

@ -263,7 +263,7 @@ main(int argc, char *argv[])
{ {
#define MAX_ALLOCS 2000000 #define MAX_ALLOCS 2000000
char *alloc[MAX_ALLOCS]; char *alloc[MAX_ALLOCS];
int numBlocks, j, freeBegin, freeEnd, freeStep; int numBlocks, freeBegin, freeEnd, freeStep;
size_t blockSize; size_t blockSize;
if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) { if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) {
@ -281,7 +281,7 @@ main(int argc, char *argv[])
printf("============== Before allocating blocks ==============\en"); printf("============== Before allocating blocks ==============\en");
display_mallinfo(); display_mallinfo();
for (j = 0; j < numBlocks; j++) { for (int j = 0; j < numBlocks; j++) {
if (numBlocks >= MAX_ALLOCS) { if (numBlocks >= MAX_ALLOCS) {
fprintf(stderr, "Too many allocations\en"); fprintf(stderr, "Too many allocations\en");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -297,7 +297,7 @@ main(int argc, char *argv[])
printf("\en============== After allocating blocks ==============\en"); printf("\en============== After allocating blocks ==============\en");
display_mallinfo(); display_mallinfo();
for (j = freeBegin; j < freeEnd; j += freeStep) for (int j = freeBegin; j < freeEnd; j += freeStep)
free(alloc[j]); free(alloc[j]);
printf("\en============== After freeing blocks ==============\en"); printf("\en============== After freeing blocks ==============\en");

View File

@ -194,13 +194,12 @@ static int numThreads, numBlocks;
static void * static void *
thread_func(void *arg) thread_func(void *arg)
{ {
int j;
int tn = (int) arg; int tn = (int) arg;
/* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
the main thread) allocates a different amount of memory */ the main thread) allocates a different amount of memory */
for (j = 0; j < numBlocks; j++) for (int j = 0; j < numBlocks; j++)
if (malloc(blockSize * (2 + tn)) == NULL) if (malloc(blockSize * (2 + tn)) == NULL)
errExit("malloc\-thread"); errExit("malloc\-thread");
@ -211,7 +210,7 @@ thread_func(void *arg)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int j, tn, sleepTime; int sleepTime;
if (argc < 4) { if (argc < 4) {
fprintf(stderr, fprintf(stderr,
@ -234,7 +233,7 @@ main(int argc, char *argv[])
/* Create threads that allocate different amounts of memory */ /* Create threads that allocate different amounts of memory */
for (tn = 0; tn < numThreads; tn++) { for (int tn = 0; tn < numThreads; tn++) {
errno = pthread_create(&thr[tn], NULL, thread_func, errno = pthread_create(&thr[tn], NULL, thread_func,
(void *) tn); (void *) tn);
if (errno != 0) if (errno != 0)
@ -251,7 +250,7 @@ main(int argc, char *argv[])
/* The main thread also allocates some memory */ /* The main thread also allocates some memory */
for (j = 0; j < numBlocks; j++) for (int j = 0; j < numBlocks; j++)
if (malloc(blockSize) == NULL) if (malloc(blockSize) == NULL)
errExit("malloc"); errExit("malloc");

View File

@ -154,7 +154,6 @@ main(int argc, char *argv[])
{ {
size_t mbslen; /* Number of multibyte characters in source */ size_t mbslen; /* Number of multibyte characters in source */
wchar_t *wcs; /* Pointer to converted wide character string */ wchar_t *wcs; /* Pointer to converted wide character string */
wchar_t *wp;
if (argc < 3) { if (argc < 3) {
fprintf(stderr, "Usage: %s <locale> <string>\en", argv[0]); fprintf(stderr, "Usage: %s <locale> <string>\en", argv[0]);
@ -206,7 +205,7 @@ main(int argc, char *argv[])
/* Now do some inspection of the classes of the characters in /* Now do some inspection of the classes of the characters in
the wide character string */ the wide character string */
for (wp = wcs; *wp != 0; wp++) { for (wchar_t *wp = wcs; *wp != 0; wp++) {
printf(" %lc ", (wint_t) *wp); printf(" %lc ", (wint_t) *wp);
if (!iswalpha(*wp)) if (!iswalpha(*wp))

View File

@ -145,11 +145,9 @@ The demonstration uses the following program:
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int j;
mtrace(); mtrace();
for (j = 0; j < 2; j++) for (int j = 0; j < 2; j++)
malloc(100); /* Never freed\-\-a memory leak */ malloc(100); /* Never freed\-\-a memory leak */
calloc(16, 16); /* Never freed\-\-a memory leak */ calloc(16, 16); /* Never freed\-\-a memory leak */

View File

@ -305,7 +305,7 @@ static void *
thread_start(void *arg) thread_start(void *arg)
{ {
struct thread_info *tinfo = arg; struct thread_info *tinfo = arg;
char *uargv, *p; char *uargv;
printf("Thread %d: top of stack near %p; argv_string=%s\en", printf("Thread %d: top of stack near %p; argv_string=%s\en",
tinfo\->thread_num, &p, tinfo\->argv_string); tinfo\->thread_num, &p, tinfo\->argv_string);
@ -314,7 +314,7 @@ thread_start(void *arg)
if (uargv == NULL) if (uargv == NULL)
handle_error("strdup"); handle_error("strdup");
for (p = uargv; *p != \(aq\e0\(aq; p++) for (char *p = uargv; *p != \(aq\e0\(aq; p++)
*p = toupper(*p); *p = toupper(*p);
return uargv; return uargv;
@ -323,7 +323,7 @@ thread_start(void *arg)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int s, tnum, opt, num_threads; int s, opt, num_threads;
pthread_attr_t attr; pthread_attr_t attr;
int stack_size; int stack_size;
void *res; void *res;
@ -366,7 +366,7 @@ main(int argc, char *argv[])
/* Create one thread for each command\-line argument */ /* Create one thread for each command\-line argument */
for (tnum = 0; tnum < num_threads; tnum++) { for (int tnum = 0; tnum < num_threads; tnum++) {
tinfo[tnum].thread_num = tnum + 1; tinfo[tnum].thread_num = tnum + 1;
tinfo[tnum].argv_string = argv[optind + tnum]; tinfo[tnum].argv_string = argv[optind + tnum];
@ -388,7 +388,7 @@ main(int argc, char *argv[])
/* Now join with each thread, and display its returned value */ /* Now join with each thread, and display its returned value */
for (tnum = 0; tnum < num_threads; tnum++) { for (int tnum = 0; tnum < num_threads; tnum++) {
s = pthread_join(tinfo[tnum].thread_id, &res); s = pthread_join(tinfo[tnum].thread_id, &res);
if (s != 0) if (s != 0)
handle_error_en(s, "pthread_join"); handle_error_en(s, "pthread_join");

View File

@ -151,7 +151,7 @@ main(int argc, char *argv[])
{ {
pthread_t thread; pthread_t thread;
clockid_t cid; clockid_t cid;
int j, s; int s;
s = pthread_create(&thread, NULL, thread_start, NULL); s = pthread_create(&thread, NULL, thread_start, NULL);
if (s != 0) if (s != 0)
@ -161,7 +161,7 @@ main(int argc, char *argv[])
sleep(1); sleep(1);
printf("Main thread consuming some CPU time...\en"); printf("Main thread consuming some CPU time...\en");
for (j = 0; j < 2000000; j++) for (int j = 0; j < 2000000; j++)
getppid(); getppid();
pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID); pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);

View File

@ -182,7 +182,7 @@ to check the resulting CPU affinity mask of the thread.
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int s, j; int s;
cpu_set_t cpuset; cpu_set_t cpuset;
pthread_t thread; pthread_t thread;
@ -191,7 +191,7 @@ main(int argc, char *argv[])
/* Set affinity mask to include CPUs 0 to 7 */ /* Set affinity mask to include CPUs 0 to 7 */
CPU_ZERO(&cpuset); CPU_ZERO(&cpuset);
for (j = 0; j < 8; j++) for (int j = 0; j < 8; j++)
CPU_SET(j, &cpuset); CPU_SET(j, &cpuset);
s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
@ -205,7 +205,7 @@ main(int argc, char *argv[])
handle_error_en(s, "pthread_getaffinity_np"); handle_error_en(s, "pthread_getaffinity_np");
printf("Set returned by pthread_getaffinity_np() contained:\en"); printf("Set returned by pthread_getaffinity_np() contained:\en");
for (j = 0; j < CPU_SETSIZE; j++) for (int j = 0; j < CPU_SETSIZE; j++)
if (CPU_ISSET(j, &cpuset)) if (CPU_ISSET(j, &cpuset))
printf(" CPU %d\en", j); printf(" CPU %d\en", j);

View File

@ -143,8 +143,6 @@ cmpstringp(const void *p1, const void *p2)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int j;
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Usage: %s <string>...\en", argv[0]); fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -152,7 +150,7 @@ main(int argc, char *argv[])
qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++) for (int j = 1; j < argc; j++)
puts(argv[j]); puts(argv[j]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -213,7 +213,7 @@ when given a particular seed.
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int j, r, nloops; int r, nloops;
unsigned int seed; unsigned int seed;
if (argc != 3) { if (argc != 3) {
@ -225,7 +225,7 @@ main(int argc, char *argv[])
nloops = atoi(argv[2]); nloops = atoi(argv[2]);
srand(seed); srand(seed);
for (j = 0; j < nloops; j++) { for (int j = 0; j < nloops; j++) {
r = rand(); r = rand();
printf("%d\en", r); printf("%d\en", r);
} }

View File

@ -208,14 +208,13 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
#define LIM 4000000 #define LIM 4000000
int j;
char p[LIM + 1]; /* +1 for terminating null byte */ char p[LIM + 1]; /* +1 for terminating null byte */
time_t base; time_t base;
base = time(NULL); base = time(NULL);
p[0] = \(aq\e0\(aq; p[0] = \(aq\e0\(aq;
for (j = 0; j < LIM; j++) { for (int j = 0; j < LIM; j++) {
if ((j % 10000) == 0) if ((j % 10000) == 0)
printf("%d %ld\en", j, (long) (time(NULL) \- base)); printf("%d %ld\en", j, (long) (time(NULL) \- base));
strcat(p, "a"); strcat(p, "a");

View File

@ -250,7 +250,6 @@ main(int argc, char *argv[])
{ {
char *str1, *str2, *token, *subtoken; char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2; char *saveptr1, *saveptr2;
int j;
if (argc != 4) { if (argc != 4) {
fprintf(stderr, "Usage: %s string delim subdelim\en", fprintf(stderr, "Usage: %s string delim subdelim\en",
@ -258,7 +257,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) { for (int j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
token = strtok_r(str1, argv[2], &saveptr1); token = strtok_r(str1, argv[2], &saveptr1);
if (token == NULL) if (token == NULL)
break; break;

View File

@ -322,11 +322,10 @@ action(const void *nodep, VISIT which, int depth)
int int
main(void) main(void)
{ {
int i;
void *val; void *val;
srand(time(NULL)); srand(time(NULL));
for (i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {
int *ptr = xmalloc(sizeof(*ptr)); int *ptr = xmalloc(sizeof(*ptr));
*ptr = rand() & 0xff; *ptr = rand() & 0xff;
val = tsearch((void *) ptr, &root, compare); val = tsearch((void *) ptr, &root, compare);

View File

@ -234,11 +234,10 @@ main(int argc, char **argv)
{ {
wordexp_t p; wordexp_t p;
char **w; char **w;
int i;
wordexp("[a\-c]*.c", &p, 0); wordexp("[a\-c]*.c", &p, 0);
w = p.we_wordv; w = p.we_wordv;
for (i = 0; i < p.we_wordc; i++) for (int i = 0; i < p.we_wordc; i++)
printf("%s\en", w[i]); printf("%s\en", w[i]);
wordfree(&p); wordfree(&p);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -654,7 +654,7 @@ Total bytes in core dump: 282624
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int tot, j; int tot;
ssize_t nread; ssize_t nread;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
FILE *fp; FILE *fp;
@ -676,7 +676,7 @@ main(int argc, char *argv[])
pipe program */ pipe program */
fprintf(fp, "argc=%d\en", argc); fprintf(fp, "argc=%d\en", argc);
for (j = 0; j < argc; j++) for (int j = 0; j < argc; j++)
fprintf(fp, "argc[%d]=<%s>\en", j, argv[j]); fprintf(fp, "argc[%d]=<%s>\en", j, argv[j]);
/* Count bytes in standard input (the core dump) */ /* Count bytes in standard input (the core dump) */

View File

@ -295,7 +295,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct sigaction sa; struct sigaction sa;
int s, j; int s;
int numReqs; /* Total number of queued I/O requests */ int numReqs; /* Total number of queued I/O requests */
int openReqs; /* Number of I/O requests still in progress */ int openReqs; /* Number of I/O requests still in progress */
@ -334,7 +334,7 @@ main(int argc, char *argv[])
/* Open each file specified on the command line, and queue /* Open each file specified on the command line, and queue
a read request on the resulting file descriptor */ a read request on the resulting file descriptor */
for (j = 0; j < numReqs; j++) { for (int j = 0; j < numReqs; j++) {
ioList[j].reqNum = j; ioList[j].reqNum = j;
ioList[j].status = EINPROGRESS; ioList[j].status = EINPROGRESS;
ioList[j].aiocbp = &aiocbList[j]; ioList[j].aiocbp = &aiocbList[j];
@ -377,7 +377,7 @@ main(int argc, char *argv[])
printf("got SIGQUIT; canceling I/O requests: \en"); printf("got SIGQUIT; canceling I/O requests: \en");
for (j = 0; j < numReqs; j++) { for (int j = 0; j < numReqs; j++) {
if (ioList[j].status == EINPROGRESS) { if (ioList[j].status == EINPROGRESS) {
printf(" Request %d on descriptor %d:", j, printf(" Request %d on descriptor %d:", j,
ioList[j].aiocbp\->aio_fildes); ioList[j].aiocbp\->aio_fildes);
@ -401,7 +401,7 @@ main(int argc, char *argv[])
in progress */ in progress */
printf("aio_error():\en"); printf("aio_error():\en");
for (j = 0; j < numReqs; j++) { for (int j = 0; j < numReqs; j++) {
if (ioList[j].status == EINPROGRESS) { if (ioList[j].status == EINPROGRESS) {
printf(" for request %d (descriptor %d): ", printf(" for request %d (descriptor %d): ",
j, ioList[j].aiocbp\->aio_fildes); j, ioList[j].aiocbp\->aio_fildes);
@ -433,7 +433,7 @@ main(int argc, char *argv[])
/* Check status return of all I/O requests */ /* Check status return of all I/O requests */
printf("aio_return():\en"); printf("aio_return():\en");
for (j = 0; j < numReqs; j++) { for (int j = 0; j < numReqs; j++) {
ssize_t s; ssize_t s;
s = aio_return(ioList[j].aiocbp); s = aio_return(ioList[j].aiocbp);

View File

@ -942,9 +942,7 @@ handle_events(int fd, int *wd, int argc, char* argv[])
char buf[4096] char buf[4096]
__attribute__ ((aligned(__alignof__(struct inotify_event)))); __attribute__ ((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event; const struct inotify_event *event;
int i;
ssize_t len; ssize_t len;
char *ptr;
/* Loop while events can be read from inotify file descriptor. */ /* Loop while events can be read from inotify file descriptor. */
@ -967,7 +965,7 @@ handle_events(int fd, int *wd, int argc, char* argv[])
/* Loop over all events in the buffer */ /* Loop over all events in the buffer */
for (ptr = buf; ptr < buf + len; for (char *ptr = buf; ptr < buf + len;
ptr += sizeof(struct inotify_event) + event\->len) { ptr += sizeof(struct inotify_event) + event\->len) {
event = (const struct inotify_event *) ptr; event = (const struct inotify_event *) ptr;
@ -983,7 +981,7 @@ handle_events(int fd, int *wd, int argc, char* argv[])
/* Print the name of the watched directory */ /* Print the name of the watched directory */
for (i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if (wd[i] == event\->wd) { if (wd[i] == event\->wd) {
printf("%s/", argv[i]); printf("%s/", argv[i]);
break; break;

View File

@ -711,13 +711,12 @@ print_diag(const struct unix_diag_msg *diag, unsigned int len)
return \-1; return \-1;
} }
struct rtattr *attr;
unsigned int rta_len = len \- NLMSG_LENGTH(sizeof(*diag)); unsigned int rta_len = len \- NLMSG_LENGTH(sizeof(*diag));
unsigned int peer = 0; unsigned int peer = 0;
size_t path_len = 0; size_t path_len = 0;
char path[sizeof(((struct sockaddr_un *) 0)\->sun_path) + 1]; char path[sizeof(((struct sockaddr_un *) 0)\->sun_path) + 1];
for (attr = (struct rtattr *) (diag + 1); for (struct rtattr *attr = (struct rtattr *) (diag + 1);
RTA_OK(attr, rta_len); attr = RTA_NEXT(attr, rta_len)) { RTA_OK(attr, rta_len); attr = RTA_NEXT(attr, rta_len)) {
switch (attr\->rta_type) { switch (attr\->rta_type) {
case UNIX_DIAG_NAME: case UNIX_DIAG_NAME:

View File

@ -1063,7 +1063,6 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct sockaddr_un addr; struct sockaddr_un addr;
int i;
int ret; int ret;
int data_socket; int data_socket;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
@ -1098,7 +1097,7 @@ main(int argc, char *argv[])
/* Send arguments. */ /* Send arguments. */
for (i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
ret = write(data_socket, argv[i], strlen(argv[i]) + 1); ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
if (ret == \-1) { if (ret == \-1) {
perror("write"); perror("write");

View File

@ -1149,13 +1149,13 @@ usage(char *pname)
static void static void
update_map(char *mapping, char *map_file) update_map(char *mapping, char *map_file)
{ {
int fd, j; int fd;
size_t map_len; /* Length of \(aqmapping\(aq */ size_t map_len; /* Length of \(aqmapping\(aq */
/* Replace commas in mapping string with newlines */ /* Replace commas in mapping string with newlines */
map_len = strlen(mapping); map_len = strlen(mapping);
for (j = 0; j < map_len; j++) for (int j = 0; j < map_len; j++)
if (mapping[j] == \(aq,\(aq) if (mapping[j] == \(aq,\(aq)
mapping[j] = \(aq\en\(aq; mapping[j] = \(aq\en\(aq;