From 1379bdc65b76471a344b2affc8f1e9b6188d1092 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?=
Date: Sat, 17 Jun 2017 14:54:23 -0700 Subject: [PATCH] maint: use C99 for loop initial declarations where possible This results in a net reduction of about 120 lines. --- src/base64.c | 13 +++++++------ src/cksum.c | 11 +++-------- src/copy.c | 3 +-- src/cp.c | 3 +-- src/csplit.c | 11 +++-------- src/dd.c | 29 +++++++++-------------------- src/df.c | 8 ++------ src/du.c | 3 +-- src/expand-common.c | 3 +-- src/group-list.c | 3 +-- src/id.c | 3 +-- src/join.c | 13 ++++--------- src/ln.c | 4 +--- src/ls.c | 20 ++++++-------------- src/md5sum.c | 7 ++----- src/mv.c | 4 +--- src/od.c | 10 +++------- src/paste.c | 3 +-- src/pinky.c | 8 ++------ src/pr.c | 31 ++++++++++++------------------- src/pwd.c | 3 +-- src/rm.c | 3 +-- src/set-fields.c | 6 ++---- src/shuf.c | 22 ++++++---------------- src/sleep.c | 3 +-- src/sort.c | 19 ++++++------------- src/stat.c | 6 ++---- src/stdbuf.c | 3 +-- src/stty.c | 15 ++++----------- src/sum.c | 3 +-- src/tac-pipe.c | 6 ++---- src/tac.c | 3 +-- src/tail.c | 27 +++++++-------------------- src/tr.c | 3 +-- src/wc.c | 10 +++------- 35 files changed, 101 insertions(+), 221 deletions(-) diff --git a/src/base64.c b/src/base64.c index d005c262ac..6e1855641b 100644 --- a/src/base64.c +++ b/src/base64.c @@ -224,12 +224,13 @@ do_decode (FILE *in, FILE *out, bool ignore_garbage) if (ignore_garbage) { - size_t i; - for (i = 0; n > 0 && i < n;) - if (isbase (inbuf[sum + i]) || inbuf[sum + i] == '=') - i++; - else - memmove (inbuf + sum + i, inbuf + sum + i + 1, --n - i); + for (size_t i = 0; n > 0 && i < n;) + { + if (isbase (inbuf[sum + i]) || inbuf[sum + i] == '=') + i++; + else + memmove (inbuf + sum + i, inbuf + sum + i + 1, --n - i); + } } sum += n; diff --git a/src/cksum.c b/src/cksum.c index b1a8f0c5fc..e0929dafe7 100644 --- a/src/cksum.c +++ b/src/cksum.c @@ -67,10 +67,8 @@ static uint_fast32_t r[8]; static void fill_r (void) { - int i; - r[0] = GEN; - for (i = 1; i < 8; i++) + for (int i = 1; i < 8; i++) r[i] = (r[i - 1] << 1) ^ ((r[i - 1] & SBIT) ? GEN : 0); } @@ -78,9 +76,8 @@ static uint_fast32_t crc_remainder (int m) { uint_fast32_t rem = 0; - int i; - for (i = 0; i < 8; i++) + for (int i = 0; i < 8; i++) if (BIT (i) & m) rem ^= r[i]; @@ -90,11 +87,9 @@ crc_remainder (int m) int main (void) { - int i; - fill_r (); printf ("static uint_fast32_t const crctab[256] =\n{\n 0x00000000"); - for (i = 0; i < 51; i++) + for (int i = 0; i < 51; i++) { printf (",\n 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x", crc_remainder (i * 5 + 1), crc_remainder (i * 5 + 2), diff --git a/src/copy.c b/src/copy.c index 965e8190db..a2eee1be52 100644 --- a/src/copy.c +++ b/src/copy.c @@ -420,9 +420,8 @@ extent_copy (int src_fd, int dest_fd, char *buf, size_t buf_size, return false; } - unsigned int i; bool empty_extent = false; - for (i = 0; i < scan.ei_count || empty_extent; i++) + for (unsigned int i = 0; i < scan.ei_count || empty_extent; i++) { off_t ext_start; off_t ext_len; diff --git a/src/cp.c b/src/cp.c index ec09ed5ddc..a6f0c64a8f 100644 --- a/src/cp.c +++ b/src/cp.c @@ -640,7 +640,6 @@ do_copy (int n_files, char **file, const char *target_directory, /* cp file1...filen edir Copy the files 'file1' through 'filen' to the existing directory 'edir'. */ - int i; /* Initialize these hash tables only if we'll need them. The problems they're used to detect can arise only if @@ -651,7 +650,7 @@ do_copy (int n_files, char **file, const char *target_directory, src_info_init (x); } - for (i = 0; i < n_files; i++) + for (int i = 0; i < n_files; i++) { char *dst_name; bool parent_exists = true; /* True if dir_name (dst_name) exists. */ diff --git a/src/csplit.c b/src/csplit.c index a3f30e8745..a7c6cc1602 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -900,9 +900,7 @@ process_regexp (struct control *p, uintmax_t repetition) static void split_file (void) { - size_t i; - - for (i = 0; i < control_used; i++) + for (size_t i = 0; i < control_used; i++) { uintmax_t j; if (controls[i].regexpr) @@ -983,12 +981,10 @@ create_output_file (void) static void delete_all_files (bool in_signal_handler) { - unsigned int i; - if (! remove_files) return; - for (i = 0; i < files_created; i++) + for (unsigned int i = 0; i < files_created; i++) { const char *name = make_filename (i); if (unlink (name) != 0 && !in_signal_handler) @@ -1178,12 +1174,11 @@ extract_regexp (int argnum, bool ignore, char const *str) static void parse_patterns (int argc, int start, char **argv) { - int i; /* Index into ARGV. */ struct control *p; /* New control record created. */ uintmax_t val; static uintmax_t last_val = 0; - for (i = start; i < argc; i++) + for (int i = start; i < argc; i++) { if (*argv[i] == '/' || *argv[i] == '%') { diff --git a/src/dd.c b/src/dd.c index 99ca1fa664..ddeeb4f582 100644 --- a/src/dd.c +++ b/src/dd.c @@ -741,9 +741,7 @@ alloc_obuf (void) static void translate_charset (char const *new_trans) { - int i; - - for (i = 0; i < 256; i++) + for (int i = 0; i < 256; i++) trans_table[i] = new_trans[trans_table[i]]; translation_needed = true; } @@ -1382,13 +1380,12 @@ operand_is (char const *operand, char const *name) static void scanargs (int argc, char *const *argv) { - int i; size_t blocksize = 0; uintmax_t count = (uintmax_t) -1; uintmax_t skip = 0; uintmax_t seek = 0; - for (i = optind; i < argc; i++) + for (int i = optind; i < argc; i++) { char const *name = argv[i]; char const *val = strchr (name, '='); @@ -1617,9 +1614,8 @@ apply_translations (void) static void translate_buffer (char *buf, size_t nread) { - char *cp; size_t i; - + char *cp; for (i = nread, cp = buf; i; i--, cp++) *cp = trans_table[to_uchar (*cp)]; } @@ -1639,8 +1635,6 @@ static char * swab_buffer (char *buf, size_t *nread) { char *bufstart = buf; - char *cp; - size_t i; /* Is a char left from last time? */ if (char_is_saved) @@ -1661,8 +1655,8 @@ swab_buffer (char *buf, size_t *nread) positions toward the end, working from the end of the buffer toward the beginning. This way we only move half of the data. */ - cp = bufstart + *nread; /* Start one char past the last. */ - for (i = *nread / 2; i; i--, cp -= 2) + char *cp = bufstart + *nread; /* Start one char past the last. */ + for (size_t i = *nread / 2; i; i--, cp -= 2) *cp = *(cp - 2); return ++bufstart; @@ -1933,9 +1927,7 @@ copy_simple (char const *buf, size_t nread) static void copy_with_block (char const *buf, size_t nread) { - size_t i; - - for (i = nread; i; i--, buf++) + for (size_t i = nread; i; i--, buf++) { if (*buf == newline_character) { @@ -1965,13 +1957,11 @@ copy_with_block (char const *buf, size_t nread) static void copy_with_unblock (char const *buf, size_t nread) { - size_t i; - char c; static size_t pending_spaces = 0; - for (i = 0; i < nread; i++) + for (size_t i = 0; i < nread; i++) { - c = buf[i]; + char c = buf[i]; if (col++ >= conversion_blocksize) { @@ -2275,8 +2265,7 @@ dd_copy (void) { /* If the final input line didn't end with a '\n', pad the output block to 'conversion_blocksize' chars. */ - size_t i; - for (i = col; i < conversion_blocksize; i++) + for (size_t i = col; i < conversion_blocksize; i++) output_char (space_character); } diff --git a/src/df.c b/src/df.c index 5788d216f4..678aa47fd2 100644 --- a/src/df.c +++ b/src/df.c @@ -1701,13 +1701,11 @@ main (int argc, char **argv) if (optind < argc) { - int i; - /* Open each of the given entries to make sure any corresponding partition is automounted. This must be done before reading the file system table. */ stats = xnmalloc (argc - optind, sizeof *stats); - for (i = optind; i < argc; ++i) + for (int i = optind; i < argc; ++i) { /* Prefer to open with O_NOCTTY and use fstat, but fall back on using "stat", in case the file is unreadable. */ @@ -1759,12 +1757,10 @@ main (int argc, char **argv) if (optind < argc) { - int i; - /* Display explicitly requested empty file systems. */ show_listed_fs = true; - for (i = optind; i < argc; ++i) + for (int i = optind; i < argc; ++i) if (argv[i]) get_entry (argv[i], &stats[i - optind]); diff --git a/src/du.c b/src/du.c index 8e88b56217..95797ff085 100644 --- a/src/du.c +++ b/src/du.c @@ -613,7 +613,6 @@ process_file (FTS *fts, FTSENT *ent) Clear the accumulators for *all* levels between prev_level and the current one. The depth may change dramatically, e.g., from 1 to 10. */ - size_t i; if (n_alloc <= level) { @@ -621,7 +620,7 @@ process_file (FTS *fts, FTSENT *ent) n_alloc = level * 2; } - for (i = prev_level + 1; i <= level; i++) + for (size_t i = prev_level + 1; i <= level; i++) { duinfo_init (&dulvl[i].ent); duinfo_init (&dulvl[i].subdir); diff --git a/src/expand-common.c b/src/expand-common.c index 8c83fb3652..53a7710197 100644 --- a/src/expand-common.c +++ b/src/expand-common.c @@ -237,9 +237,8 @@ static void validate_tab_stops (uintmax_t const *tabs, size_t entries) { uintmax_t prev_tab = 0; - size_t i; - for (i = 0; i < entries; i++) + for (size_t i = 0; i < entries; i++) { if (tabs[i] == 0) die (EXIT_FAILURE, 0, _("tab size cannot be 0")); diff --git a/src/group-list.c b/src/group-list.c index 274c5e6c1c..36b6144674 100644 --- a/src/group-list.c +++ b/src/group-list.c @@ -59,7 +59,6 @@ print_group_list (const char *username, { gid_t *groups; - int i; int n_groups = xgetgroups (username, (pwd ? pwd->pw_gid : egid), &groups); if (n_groups < 0) @@ -76,7 +75,7 @@ print_group_list (const char *username, return false; } - for (i = 0; i < n_groups; i++) + for (int i = 0; i < n_groups; i++) if (groups[i] != rgid && groups[i] != egid) { putchar (delim); diff --git a/src/id.c b/src/id.c index ab7d53ef2d..2bd417d773 100644 --- a/src/id.c +++ b/src/id.c @@ -400,7 +400,6 @@ print_full_info (const char *username) { gid_t *groups; - int i; gid_t primary_group; if (username) @@ -422,7 +421,7 @@ print_full_info (const char *username) if (n_groups > 0) fputs (_(" groups="), stdout); - for (i = 0; i < n_groups; i++) + for (int i = 0; i < n_groups; i++) { if (i > 0) putchar (','); diff --git a/src/join.c b/src/join.c index 93570adeb4..ae10101fc6 100644 --- a/src/join.c +++ b/src/join.c @@ -480,9 +480,7 @@ get_line (FILE *fp, struct line **linep, int which) static void free_spareline (void) { - size_t i; - - for (i = 0; i < ARRAY_CARDINALITY (spareline); i++) + for (size_t i = 0; i < ARRAY_CARDINALITY (spareline); i++) { if (spareline[i]) { @@ -507,9 +505,8 @@ getseq (FILE *fp, struct seq *seq, int whichfile) { if (seq->count == seq->alloc) { - size_t i; seq->lines = X2NREALLOC (seq->lines, &seq->alloc); - for (i = seq->count; i < seq->alloc; i++) + for (size_t i = seq->count; i < seq->alloc; i++) seq->lines[i] = NULL; } @@ -535,8 +532,7 @@ advance_seq (FILE *fp, struct seq *seq, bool first, int whichfile) static void delseq (struct seq *seq) { - size_t i; - for (i = 0; i < seq->alloc; i++) + for (size_t i = 0; i < seq->alloc; i++) { freeline (seq->lines[i]); free (seq->lines[i]); @@ -693,7 +689,6 @@ join (FILE *fp1, FILE *fp2) while (seq1.count && seq2.count) { - size_t i; diff = keycmp (seq1.lines[0], seq2.lines[0], join_field_1, join_field_2); if (diff < 0) @@ -741,7 +736,7 @@ join (FILE *fp1, FILE *fp2) if (print_pairables) { - for (i = 0; i < seq1.count - 1; ++i) + for (size_t i = 0; i < seq1.count - 1; ++i) { size_t j; for (j = 0; j < seq2.count - 1; ++j) diff --git a/src/ln.c b/src/ln.c index 539d238c84..a70b7d50fe 100644 --- a/src/ln.c +++ b/src/ln.c @@ -575,8 +575,6 @@ main (int argc, char **argv) if (target_directory) { - int i; - /* Create the data structure we'll use to record which hard links we create. Used to ensure that ln detects an obscure corner case that might result in user data loss. Create it only if needed. */ @@ -600,7 +598,7 @@ main (int argc, char **argv) } ok = true; - for (i = 0; i < n_files; ++i) + for (int i = 0; i < n_files; ++i) { char *dest_base; char *dest = file_name_concat (target_directory, diff --git a/src/ls.c b/src/ls.c index 9477d31be3..4802d9218c 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1041,12 +1041,9 @@ dired_dump_obstack (const char *prefix, struct obstack *os) n_pos = obstack_object_size (os) / sizeof (dired_pos); if (n_pos > 0) { - size_t i; - size_t *pos; - - pos = (size_t *) obstack_finish (os); + size_t *pos = (size_t *) obstack_finish (os); fputs (prefix, stdout); - for (i = 0; i < n_pos; i++) + for (size_t i = 0; i < n_pos; i++) printf (" %lu", (unsigned long int) pos[i]); putchar ('\n'); } @@ -2198,8 +2195,7 @@ decode_switches (int argc, char **argv) case locale_time_style: if (hard_locale (LC_TIME)) { - int i; - for (i = 0; i < 2; i++) + for (int i = 0; i < 2; i++) long_time_format[i] = dcgettext (NULL, long_time_format[i], LC_TIME); } @@ -2926,9 +2922,7 @@ free_ent (struct fileinfo *f) static void clear_files (void) { - size_t i; - - for (i = 0; i < cwd_n_used; i++) + for (size_t i = 0; i < cwd_n_used; i++) { struct fileinfo *f = sorted_file[i]; free_ent (f); @@ -3708,8 +3702,7 @@ verify (ARRAY_CARDINALITY (sort_functions) static void initialize_ordering_vector (void) { - size_t i; - for (i = 0; i < cwd_n_used; i++) + for (size_t i = 0; i < cwd_n_used; i++) sorted_file[i] = &cwd_file[i]; } @@ -4965,9 +4958,8 @@ calculate_columns (bool by_columns) { struct fileinfo const *f = sorted_file[filesno]; size_t name_length = length_of_file_name_and_frills (f); - size_t i; - for (i = 0; i < max_cols; ++i) + for (size_t i = 0; i < max_cols; ++i) { if (column_info[i].valid_len) { diff --git a/src/md5sum.c b/src/md5sum.c index 9de0eb9217..93db658e61 100644 --- a/src/md5sum.c +++ b/src/md5sum.c @@ -347,8 +347,7 @@ filename_unescape (char *s, size_t s_len) static bool _GL_ATTRIBUTE_PURE hex_digits (unsigned char const *s) { - unsigned int i; - for (i = 0; i < digest_hex_bytes; i++) + for (unsigned int i = 0; i < digest_hex_bytes; i++) { if (!isxdigit (*s)) return false; @@ -1062,14 +1061,12 @@ main (int argc, char **argv) fputs (") = ", stdout); } - size_t i; - /* Output a leading backslash if the file name contains a newline or backslash. */ if (!prefix_tag && needs_escape) putchar ('\\'); - for (i = 0; i < (digest_hex_bytes / 2); ++i) + for (size_t i = 0; i < (digest_hex_bytes / 2); ++i) printf ("%02x", bin_buffer[i]); if (!prefix_tag) diff --git a/src/mv.c b/src/mv.c index d64786b9c3..a6c6e3978f 100644 --- a/src/mv.c +++ b/src/mv.c @@ -474,8 +474,6 @@ main (int argc, char **argv) if (target_directory) { - int i; - /* Initialize the hash table only if we'll need it. The problem it is used to detect can arise only if there are two or more files to move. */ @@ -483,7 +481,7 @@ main (int argc, char **argv) dest_info_init (&x); ok = true; - for (i = 0; i < n_files; ++i) + for (int i = 0; i < n_files; ++i) ok &= movefile (file[i], target_directory, true, &x); } else diff --git a/src/od.c b/src/od.c index f469326ffe..52df7ae08f 100644 --- a/src/od.c +++ b/src/od.c @@ -484,9 +484,8 @@ PRINT_FLOATTYPE (print_long_double, long double, ldtoastr, LDBL_BUFSIZE_BOUND) static void dump_hexl_mode_trailer (size_t n_bytes, const char *block) { - size_t i; fputs (" >", stdout); - for (i = n_bytes; i > 0; i--) + for (size_t i = n_bytes; i > 0; i--) { unsigned char c = *block++; unsigned char c2 = (isprint (c) ? c : '.'); @@ -1205,10 +1204,8 @@ write_block (uintmax_t current_offset, size_t n_bytes, } else { - size_t i; - prev_pair_equal = false; - for (i = 0; i < n_specs; i++) + for (size_t i = 0; i < n_specs; i++) { int datum_width = width_bytes[spec[i].size]; int fields_per_block = bytes_per_block / datum_width; @@ -1318,10 +1315,9 @@ read_block (size_t n, char *block, size_t *n_bytes_in_buffer) static int _GL_ATTRIBUTE_PURE get_lcm (void) { - size_t i; int l_c_m = 1; - for (i = 0; i < n_specs; i++) + for (size_t i = 0; i < n_specs; i++) l_c_m = lcm (l_c_m, width_bytes[spec[i].size]); return l_c_m; } diff --git a/src/paste.c b/src/paste.c index 31ba4ca6b8..17ee393d87 100644 --- a/src/paste.c +++ b/src/paste.c @@ -231,9 +231,8 @@ paste_parallel (size_t nfiles, char **fnamptr) bool somedone = false; char const *delimptr = delims; size_t delims_saved = 0; /* Number of delims saved in 'delbuf'. */ - size_t i; - for (i = 0; i < nfiles && files_open; i++) + for (size_t i = 0; i < nfiles && files_open; i++) { int chr IF_LINT ( = 0); /* Input character. */ int err IF_LINT ( = 0); /* Input errno value. */ diff --git a/src/pinky.c b/src/pinky.c index 6db565bb72..44dae9afdd 100644 --- a/src/pinky.c +++ b/src/pinky.c @@ -441,9 +441,7 @@ scan_entries (size_t n, const STRUCT_UTMP *utmp_buf, { if (argc_names) { - int i; - - for (i = 0; i < argc_names; i++) + for (int i = 0; i < argc_names; i++) if (STREQ_LEN (UT_USER (utmp_buf), argv_names[i], UT_USER_SIZE)) { print_entry (utmp_buf); @@ -477,9 +475,7 @@ short_pinky (const char *filename, static void long_pinky (const int argc_names, char *const argv_names[]) { - int i; - - for (i = 0; i < argc_names; i++) + for (int i = 0; i < argc_names; i++) print_long_entry (argv_names[i]); } diff --git a/src/pr.c b/src/pr.c index 4580c5c780..9455781d93 100644 --- a/src/pr.c +++ b/src/pr.c @@ -1136,8 +1136,7 @@ main (int argc, char **argv) print_files (n_files, file_names); else { - unsigned int i; - for (i = 0; i < n_files; i++) + for (unsigned int i = 0; i < n_files; i++) print_files (1, &file_names[i]); } } @@ -1311,10 +1310,7 @@ init_parameters (int number_of_files) static bool init_fps (int number_of_files, char **av) { - int i, files_left; COLUMN *p; - FILE *firstfp; - char const *firstname; total_files = 0; @@ -1323,7 +1319,7 @@ init_fps (int number_of_files, char **av) if (parallel_files) { - files_left = number_of_files; + int files_left = number_of_files; for (p = column_vector; files_left--; ++p, ++av) { if (! open_file (*av, p)) @@ -1358,8 +1354,9 @@ init_fps (int number_of_files, char **av) p->lines_stored = 0; } - firstname = p->name; - firstfp = p->fp; + char const *firstname = p->name; + FILE *firstfp = p->fp; + int i; for (i = columns - 1, ++p; i; --i, ++p) { p->name = firstname; @@ -2077,12 +2074,10 @@ pad_across_to (int position) static void pad_down (unsigned int lines) { - unsigned int i; - if (use_form_feed) putchar ('\f'); else - for (i = lines; i; --i) + for (unsigned int i = lines; i; --i) putchar ('\n'); } @@ -2310,14 +2305,12 @@ print_char (char c) static bool skip_to_page (uintmax_t page) { - uintmax_t n; - int i; - int j; - COLUMN *p; - - for (n = 1; n < page; ++n) + for (uintmax_t n = 1; n < page; ++n) { - for (i = 1; i < lines_per_body; ++i) + COLUMN *p; + int j; + + for (int i = 1; i < lines_per_body; ++i) { for (j = 1, p = column_vector; j <= columns; ++j, ++p) if (p->status == OPEN) @@ -2552,7 +2545,6 @@ static bool print_stored (COLUMN *p) { COLUMN *q; - int i; int line = p->current_line++; char *first = &buff[line_vector[line]]; @@ -2576,6 +2568,7 @@ print_stored (COLUMN *p) if (p->status == FF_FOUND) { + int i; for (i = 1, q = column_vector; i <= columns; ++i, ++q) q->status = ON_HOLD; if (column_vector->lines_to_print <= 0) diff --git a/src/pwd.c b/src/pwd.c index 472e3a878b..8852ab4841 100644 --- a/src/pwd.c +++ b/src/pwd.c @@ -128,9 +128,8 @@ nth_parent (size_t n) { char *buf = xnmalloc (3, n); char *p = buf; - size_t i; - for (i = 0; i < n; i++) + for (size_t i = 0; i < n; i++) { memcpy (p, "../", 3); p += 3; diff --git a/src/rm.c b/src/rm.c index 0dff18a4cd..51b80facf8 100644 --- a/src/rm.c +++ b/src/rm.c @@ -105,9 +105,8 @@ diagnose_leading_hyphen (int argc, char **argv) { /* OPTIND is unreliable, so iterate through the arguments looking for a file name that looks like an option. */ - int i; - for (i = 1; i < argc; i++) + for (int i = 1; i < argc; i++) { char const *arg = argv[i]; struct stat st; diff --git a/src/set-fields.c b/src/set-fields.c index 076a77666b..2b3edccc23 100644 --- a/src/set-fields.c +++ b/src/set-fields.c @@ -73,7 +73,6 @@ complement_rp (void) { struct field_range_pair *c = frp; size_t n = n_frp; - size_t i; frp = NULL; n_frp = 0; @@ -82,7 +81,7 @@ complement_rp (void) if (c[0].lo > 1) add_range_pair (1, c[0].lo - 1); - for (i = 1; i < n; ++i) + for (size_t i = 1; i < n; ++i) { if (c[i-1].hi + 1 == c[i].lo) continue; @@ -144,7 +143,6 @@ set_fields (const char *fieldstr, unsigned int options) bool rhs_specified = false; bool dash_found = false; /* True if a '-' is found in this field. */ - size_t i; bool in_digits = false; /* Collect and store in RP the range end points. */ @@ -285,7 +283,7 @@ set_fields (const char *fieldstr, unsigned int options) qsort (frp, n_frp, sizeof (frp[0]), compare_ranges); /* Merge range pairs (e.g. `2-5,3-4' becomes `2-5'). */ - for (i = 0; i < n_frp; ++i) + for (size_t i = 0; i < n_frp; ++i) { for (size_t j = i + 1; j < n_frp; ++j) { diff --git a/src/shuf.c b/src/shuf.c index 282cfd5c24..46556a89be 100644 --- a/src/shuf.c +++ b/src/shuf.c @@ -240,9 +240,7 @@ static int write_permuted_output_reservoir (size_t n_lines, struct linebuffer *lines, size_t const *permutation) { - size_t i; - - for (i = 0; i < n_lines; i++) + for (size_t i = 0; i < n_lines; i++) { const struct linebuffer *p = &lines[permutation[i]]; if (fwrite (p->buffer, sizeof (char), p->length, stdout) != p->length) @@ -266,7 +264,6 @@ read_input (FILE *in, char eolbyte, char ***pline) size_t used; char *lim; char **line; - size_t i; size_t n_lines; /* TODO: We should limit the amount of data read here, @@ -293,7 +290,7 @@ read_input (FILE *in, char eolbyte, char ***pline) *pline = line = xnmalloc (n_lines + 1, sizeof *line); line[0] = p = buf; - for (i = 1; i <= n_lines; i++) + for (size_t i = 1; i <= n_lines; i++) line[i] = p = next_line (p, eolbyte, lim - p); return n_lines; @@ -307,9 +304,7 @@ static int write_permuted_lines (size_t n_lines, char *const *line, size_t const *permutation) { - size_t i; - - for (i = 0; i < n_lines; i++) + for (size_t i = 0; i < n_lines; i++) { char *const *p = line + permutation[i]; size_t len = p[1] - p[0]; @@ -326,9 +321,7 @@ static int write_permuted_numbers (size_t n_lines, size_t lo_input, size_t const *permutation, char eolbyte) { - size_t i; - - for (i = 0; i < n_lines; i++) + for (size_t i = 0; i < n_lines; i++) { unsigned long int n = lo_input + permutation[i]; if (printf ("%lu%c", n, eolbyte) < 0) @@ -344,10 +337,9 @@ static int write_random_numbers (struct randint_source *s, size_t count, size_t lo_input, size_t hi_input, char eolbyte) { - size_t i; const randint range = hi_input - lo_input + 1; - for (i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) { unsigned long int j = lo_input + randint_choose (s, range); if (printf ("%lu%c", j, eolbyte) < 0) @@ -364,9 +356,7 @@ static int write_random_lines (struct randint_source *s, size_t count, char *const *lines, size_t n_lines) { - size_t i; - - for (i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) { const randint j = randint_choose (s, n_lines); char *const *p = lines + j; diff --git a/src/sleep.c b/src/sleep.c index 403485ed75..26e3fe22d5 100644 --- a/src/sleep.c +++ b/src/sleep.c @@ -98,7 +98,6 @@ apply_suffix (double *x, char suffix_char) int main (int argc, char **argv) { - int i; double seconds = 0.0; bool ok = true; @@ -121,7 +120,7 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } - for (i = optind; i < argc; i++) + for (int i = optind; i < argc; i++) { double s; const char *p; diff --git a/src/sort.c b/src/sort.c index d9a2edbfde..015e40e3a2 100644 --- a/src/sort.c +++ b/src/sort.c @@ -1511,9 +1511,7 @@ sort_buffer_size (FILE *const *fps, size_t nfps, This extra room might be needed when preparing to read EOF. */ size_t size = worst_case_per_input_byte + 1; - size_t i; - - for (i = 0; i < nfiles; i++) + for (size_t i = 0; i < nfiles; i++) { struct stat st; off_t file_size; @@ -3692,12 +3690,11 @@ static void avoid_trashing_input (struct sortfile *files, size_t ntemps, size_t nfiles, char const *outfile) { - size_t i; bool got_outstat = false; struct stat outstat; struct tempnode *tempcopy = NULL; - for (i = ntemps; i < nfiles; i++) + for (size_t i = ntemps; i < nfiles; i++) { bool is_stdin = STREQ (files[i].name, "-"); bool same; @@ -3748,8 +3745,7 @@ avoid_trashing_input (struct sortfile *files, size_t ntemps, static void check_inputs (char *const *files, size_t nfiles) { - size_t i; - for (i = 0; i < nfiles; i++) + for (size_t i = 0; i < nfiles; i++) { if (STREQ (files[i], "-")) continue; @@ -3997,10 +3993,9 @@ sort (char *const *files, size_t nfiles, char const *output_file, if (! output_file_created) { - size_t i; struct tempnode *node = temphead; struct sortfile *tempfiles = xnmalloc (ntemps, sizeof *tempfiles); - for (i = 0; node; i++) + for (size_t i = 0; node; i++) { tempfiles[i].name = node->name; tempfiles[i].temp = node; @@ -4613,11 +4608,10 @@ main (int argc, char **argv) if (tok.n_tok) { - size_t i; free (files); files = tok.tok; nfiles = tok.n_tok; - for (i = 0; i < nfiles; i++) + for (size_t i = 0; i < nfiles; i++) { if (STREQ (files[i], "-")) die (SORT_FAILURE, 0, _("when reading file names from stdin, " @@ -4747,9 +4741,8 @@ main (int argc, char **argv) if (mergeonly) { struct sortfile *sortfiles = xcalloc (nfiles, sizeof *sortfiles); - size_t i; - for (i = 0; i < nfiles; ++i) + for (size_t i = 0; i < nfiles; ++i) sortfiles[i].name = files[i]; merge (sortfiles, 0, nfiles, outfile); diff --git a/src/stat.c b/src/stat.c index f5e930b1be..dcaa580dc8 100644 --- a/src/stat.c +++ b/src/stat.c @@ -820,8 +820,7 @@ print_statfs (char *pformat, size_t prefix_len, unsigned int m, with glibc's statvfs implementation. */ uintmax_t fsid = 0; int words = sizeof statfsbuf->f_fsid / sizeof *p; - int i; - for (i = 0; i < words && i * sizeof *p < sizeof fsid; i++) + for (int i = 0; i < words && i * sizeof *p < sizeof fsid; i++) { uintmax_t u = p[words - 1 - i]; fsid |= u << (i * CHAR_BIT * sizeof *p); @@ -1577,7 +1576,6 @@ int main (int argc, char *argv[]) { int c; - int i; bool fs = false; bool terse = false; char *format = NULL; @@ -1651,7 +1649,7 @@ main (int argc, char *argv[]) format2 = default_format (fs, terse, /* device= */ true); } - for (i = optind; i < argc; i++) + for (int i = optind; i < argc; i++) ok &= (fs ? do_statfs (argv[i], format) : do_stat (argv[i], format, format2)); diff --git a/src/stdbuf.c b/src/stdbuf.c index 5342a26a13..c81519db5c 100644 --- a/src/stdbuf.c +++ b/src/stdbuf.c @@ -275,9 +275,8 @@ static bool set_libstdbuf_options (void) { bool env_set = false; - size_t i; - for (i = 0; i < ARRAY_CARDINALITY (stdbuf); i++) + for (size_t i = 0; i < ARRAY_CARDINALITY (stdbuf); i++) { if (stdbuf[i].optarg) { diff --git a/src/stty.c b/src/stty.c index 32007e6df7..528f167823 100644 --- a/src/stty.c +++ b/src/stty.c @@ -1461,9 +1461,8 @@ main (int argc, char **argv) quotef (device_name)); #ifdef TESTING { - size_t i; printf ("new_mode: mode\n"); - for (i = 0; i < sizeof (new_mode); i++) + for (size_t i = 0; i < sizeof (new_mode); i++) printf ("0x%02x: 0x%02x\n", *(((unsigned char *) &new_mode) + i), *(((unsigned char *) &mode) + i)); @@ -2076,14 +2075,12 @@ display_speed (struct termios *mode, bool fancy) static void display_recoverable (struct termios *mode) { - size_t i; - printf ("%lx:%lx:%lx:%lx", (unsigned long int) mode->c_iflag, (unsigned long int) mode->c_oflag, (unsigned long int) mode->c_cflag, (unsigned long int) mode->c_lflag); - for (i = 0; i < NCCS; ++i) + for (size_t i = 0; i < NCCS; ++i) printf (":%lx", (unsigned long int) mode->c_cc[i]); putchar ('\n'); } @@ -2226,9 +2223,7 @@ static struct speed_map const speeds[] = static speed_t _GL_ATTRIBUTE_PURE string_to_baud (const char *arg) { - int i; - - for (i = 0; speeds[i].string != NULL; ++i) + for (int i = 0; speeds[i].string != NULL; ++i) if (STREQ (arg, speeds[i].string)) return speeds[i].speed; return (speed_t) -1; @@ -2237,9 +2232,7 @@ string_to_baud (const char *arg) static unsigned long int _GL_ATTRIBUTE_PURE baud_to_value (speed_t speed) { - int i; - - for (i = 0; speeds[i].string != NULL; ++i) + for (int i = 0; speeds[i].string != NULL; ++i) if (speed == speeds[i].speed) return speeds[i].value; return 0; diff --git a/src/sum.c b/src/sum.c index cae5d37054..412a9a8d89 100644 --- a/src/sum.c +++ b/src/sum.c @@ -181,7 +181,6 @@ sysv_sum_file (const char *file, int print_name) while (1) { - size_t i; size_t bytes_read = safe_read (fd, buf, sizeof buf); if (bytes_read == 0) @@ -195,7 +194,7 @@ sysv_sum_file (const char *file, int print_name) return false; } - for (i = 0; i < bytes_read; i++) + for (size_t i = 0; i < bytes_read; i++) s += buf[i]; total_bytes += bytes_read; } diff --git a/src/tac-pipe.c b/src/tac-pipe.c index d31df43e44..bdc003e11f 100644 --- a/src/tac-pipe.c +++ b/src/tac-pipe.c @@ -127,8 +127,7 @@ buf_init_from_stdin (Buf *x, char eol_byte) static void buf_free (Buf *x) { - size_t i; - for (i = 0; i < x->n_bufs; i++) + for (size_t i = 0; i < x->n_bufs; i++) free (x->p[i].start); obstack_free (OBS, NULL); } @@ -222,8 +221,7 @@ static void print_line (FILE *out_stream, const Buf *x, const Line_ptr *bol, const Line_ptr *bol_next) { - size_t i; - for (i = bol->i; i <= bol_next->i; i++) + for (size_t i = bol->i; i <= bol_next->i; i++) { char *a = (i == bol->i ? bol->ptr : x->p[i].start); char *b = (i == bol_next->i ? bol_next->ptr : ONE_PAST_END (x, i)); diff --git a/src/tac.c b/src/tac.c index 4e06b7623a..3c6c52d673 100644 --- a/src/tac.c +++ b/src/tac.c @@ -690,9 +690,8 @@ main (int argc, char **argv) xset_binary_mode (STDOUT_FILENO, O_BINARY); { - size_t i; ok = true; - for (i = 0; file[i]; ++i) + for (size_t i = 0; file[i]; ++i) ok &= tac_file (file[i]); } diff --git a/src/tail.c b/src/tail.c index b8be1d21df..ce34886cde 100644 --- a/src/tail.c +++ b/src/tail.c @@ -1099,15 +1099,13 @@ recheck (struct File_spec *f, bool blocking) static bool any_live_files (const struct File_spec *f, size_t n_files) { - size_t i; - /* In inotify mode, ignore may be set for files which may later be replaced with new files. So always consider files live in -F mode. */ if (reopen_inaccessible_files && follow_mode == Follow_name) return true; - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) { if (0 <= f[i].fd) return true; @@ -1300,9 +1298,7 @@ tail_forever (struct File_spec *f, size_t n_files, double sleep_interval) static bool any_remote_file (const struct File_spec *f, size_t n_files) { - size_t i; - - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) if (0 <= f[i].fd && f[i].remote) return true; return false; @@ -1314,9 +1310,7 @@ any_remote_file (const struct File_spec *f, size_t n_files) static bool any_non_remote_file (const struct File_spec *f, size_t n_files) { - size_t i; - - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) if (0 <= f[i].fd && ! f[i].remote) return true; return false; @@ -1330,10 +1324,8 @@ any_non_remote_file (const struct File_spec *f, size_t n_files) static bool any_symlinks (const struct File_spec *f, size_t n_files) { - size_t i; - struct stat st; - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode)) return true; return false; @@ -1347,9 +1339,7 @@ any_symlinks (const struct File_spec *f, size_t n_files) static bool any_non_regular (const struct File_spec *f, size_t n_files) { - size_t i; - - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) if (0 <= f[i].fd && ! S_ISREG (f[i].mode)) return true; return false; @@ -1361,9 +1351,7 @@ any_non_regular (const struct File_spec *f, size_t n_files) static bool tailable_stdin (const struct File_spec *f, size_t n_files) { - size_t i; - - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) if (!f[i].ignore && STREQ (f[i].name, "-")) return true; return false; @@ -2288,8 +2276,7 @@ ignore_fifo_and_pipe (struct File_spec *f, size_t n_files) ignore any "-" operand that corresponds to a pipe or FIFO. */ size_t n_viable = 0; - size_t i; - for (i = 0; i < n_files; i++) + for (size_t i = 0; i < n_files; i++) { bool is_a_fifo_or_pipe = (STREQ (f[i].name, "-") diff --git a/src/tr.c b/src/tr.c index 0c770d7a9b..333aed04d0 100644 --- a/src/tr.c +++ b/src/tr.c @@ -1167,7 +1167,6 @@ validate_case_classes (struct Spec_list *s1, struct Spec_list *s2) { size_t n_upper = 0; size_t n_lower = 0; - unsigned int i; int c1 = 0; int c2 = 0; count old_s1_len = s1->length; @@ -1180,7 +1179,7 @@ validate_case_classes (struct Spec_list *s1, struct Spec_list *s2) if (!s2->has_char_class) return; - for (i = 0; i < N_CHARS; i++) + for (int i = 0; i < N_CHARS; i++) { if (isupper (i)) n_upper++; diff --git a/src/wc.c b/src/wc.c index 8a656c3d0b..299a9665f6 100644 --- a/src/wc.c +++ b/src/wc.c @@ -599,9 +599,7 @@ get_input_fstatus (size_t nfiles, char *const *file) fstatus[0].failed = 1; else { - size_t i; - - for (i = 0; i < nfiles; i++) + for (size_t i = 0; i < nfiles; i++) fstatus[i].failed = (! file[i] || STREQ (file[i], "-") ? fstat (STDIN_FILENO, &fstatus[i].st) : stat (file[i], &fstatus[i].st)); @@ -623,9 +621,8 @@ compute_number_width (size_t nfiles, struct fstatus const *fstatus) { int minimum_width = 1; uintmax_t regular_total = 0; - size_t i; - for (i = 0; i < nfiles; i++) + for (size_t i = 0; i < nfiles; i++) if (! fstatus[i].failed) { if (S_ISREG (fstatus[i].st.st_mode)) @@ -774,9 +771,8 @@ main (int argc, char **argv) fstatus = get_input_fstatus (nfiles, files); number_width = compute_number_width (nfiles, fstatus); - int i; ok = true; - for (i = 0; /* */; i++) + for (int i = 0; /* */; i++) { bool skip_file = false; enum argv_iter_err ai_err; -- 2.47.2