Skip to content

Commit

Permalink
reduce scope of variables
Browse files Browse the repository at this point in the history
C99 allows a 'for' loop to define its variable.
This reduces the variable's scope making its use clearer.
  • Loading branch information
flatcap committed Oct 14, 2017
1 parent 1d83dcf commit da2ecbc
Show file tree
Hide file tree
Showing 47 changed files with 257 additions and 349 deletions.
13 changes: 4 additions & 9 deletions browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,10 @@ void _mutt_select_file(char *f, size_t flen, int flags, char ***files, int *numf
else
{
struct NntpServer *nserv = CurrentNewsSrv;
unsigned int j;

/* default state for news reader mode is browse subscribed newsgroups */
buffy = 0;
for (j = 0; j < nserv->groups_num; j++)
for (unsigned int j = 0; j < nserv->groups_num; j++)
{
struct NntpData *nntp_data = nserv->groups_list[j];
if (nntp_data && nntp_data->subscribed)
Expand Down Expand Up @@ -1468,13 +1467,12 @@ void _mutt_select_file(char *f, size_t flen, int flags, char ***files, int *numf
if (multiple)
{
char **tfiles = NULL;
int j, k;

if (menu->tagged)
{
*numfiles = menu->tagged;
tfiles = safe_calloc(*numfiles, sizeof(char *));
for (j = 0, k = 0; j < state.entrylen; j++)
for (int j = 0, k = 0; j < state.entrylen; j++)
{
struct FolderFile ff = state.entry[j];
char full[_POSIX_PATH_MAX];
Expand Down Expand Up @@ -1947,12 +1945,11 @@ void _mutt_select_file(char *f, size_t flen, int flags, char ***files, int *numf
if (option(OPT_NEWS))
{
struct NntpServer *nserv = CurrentNewsSrv;
unsigned int j;

if (nntp_newsrc_parse(nserv) < 0)
break;

for (j = 0; j < nserv->groups_num; j++)
for (unsigned int j = 0; j < nserv->groups_num; j++)
{
struct NntpData *nntp_data = nserv->groups_list[j];
if (nntp_data)
Expand Down Expand Up @@ -2046,9 +2043,7 @@ void _mutt_select_file(char *f, size_t flen, int flags, char ***files, int *numf
}
if (i == OP_SUBSCRIBE_PATTERN)
{
unsigned int k;

for (k = 0; nserv && k < nserv->groups_num; k++)
for (unsigned int k = 0; nserv && (k < nserv->groups_num); k++)
{
struct NntpData *nntp_data = nserv->groups_list[k];
if (nntp_data && nntp_data->group && !nntp_data->subscribed)
Expand Down
3 changes: 1 addition & 2 deletions charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,13 @@ void fgetconv_close(FGETCONV **_fc)

bool mutt_check_charset(const char *s, bool strict)
{
int i;
iconv_t cd;

if (mutt_is_utf8(s))
return true;

if (!strict)
for (i = 0; PreferredMIMENames[i].key; i++)
for (int i = 0; PreferredMIMENames[i].key; i++)
{
if ((mutt_strcasecmp(PreferredMIMENames[i].key, s) == 0) ||
(mutt_strcasecmp(PreferredMIMENames[i].pref, s) == 0))
Expand Down
3 changes: 1 addition & 2 deletions color.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ void ci_start_color(void)
static char *get_color_name(char *dest, size_t destlen, int val)
{
static const char *const missing[3] = { "brown", "lightgray", "default" };
int i;

switch (val)
{
Expand All @@ -224,7 +223,7 @@ static char *get_color_name(char *dest, size_t destlen, int val)
return dest;
}

for (i = 0; Colors[i].name; i++)
for (int i = 0; Colors[i].name; i++)
{
if (Colors[i].value == val)
{
Expand Down
17 changes: 8 additions & 9 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ static void pipe_msg(struct Header *h, FILE *fp, int decode, int print)
static int _mutt_pipe_message(struct Header *h, char *cmd, int decode,
int print, int split, char *sep)
{
int i, rc = 0;
int rc = 0;
pid_t thepid;
FILE *fpout = NULL;

Expand Down Expand Up @@ -425,7 +425,7 @@ static int _mutt_pipe_message(struct Header *h, char *cmd, int decode,

if (WithCrypto && decode)
{
for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
if (Context->hdrs[Context->v2r[i]]->tagged)
{
mutt_message_hook(Context, Context->hdrs[Context->v2r[i]], MUTT_MESSAGEHOOK);
Expand All @@ -438,7 +438,7 @@ static int _mutt_pipe_message(struct Header *h, char *cmd, int decode,

if (split)
{
for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
{
if (Context->hdrs[Context->v2r[i]]->tagged)
{
Expand Down Expand Up @@ -470,7 +470,7 @@ static int _mutt_pipe_message(struct Header *h, char *cmd, int decode,
return 1;
}
set_option(OPT_KEEP_QUIET);
for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
{
if (Context->hdrs[Context->v2r[i]]->tagged)
{
Expand Down Expand Up @@ -746,7 +746,7 @@ int _mutt_save_message(struct Header *h, struct Context *ctx, int delete, int de
*/
int mutt_save_message(struct Header *h, int delete, int decode, int decrypt)
{
int i, need_buffy_cleanup;
int need_buffy_cleanup;
int need_passphrase = 0, app = 0;
char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
struct Context ctx;
Expand Down Expand Up @@ -775,7 +775,7 @@ int mutt_save_message(struct Header *h, int delete, int decode, int decrypt)
{
/* look for the first tagged message */

for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
{
if (Context->hdrs[Context->v2r[i]]->tagged)
{
Expand Down Expand Up @@ -885,7 +885,7 @@ int mutt_save_message(struct Header *h, int delete, int decode, int decrypt)
if (Context->magic == MUTT_NOTMUCH)
nm_longrun_init(Context, true);
#endif
for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
{
if (Context->hdrs[Context->v2r[i]]->tagged)
{
Expand Down Expand Up @@ -1067,13 +1067,12 @@ static int _mutt_check_traditional_pgp(struct Header *h, int *redraw)

int mutt_check_traditional_pgp(struct Header *h, int *redraw)
{
int i;
int rv = 0;
if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
rv = _mutt_check_traditional_pgp(h, redraw);
else
{
for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
if (Context->hdrs[Context->v2r[i]]->tagged &&
!(Context->hdrs[Context->v2r[i]]->security & PGP_TRADITIONAL_CHECKED))
{
Expand Down
10 changes: 4 additions & 6 deletions compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,20 @@ static void calc_header_width_padding(int idx, const char *header, int calc_max)
static void init_header_padding(void)
{
static short done = 0;
int i;

if (done)
return;
done = 1;

for (i = 0; i <= HDR_XCOMMENTTO; i++)
for (int i = 0; i <= HDR_XCOMMENTTO; i++)
calc_header_width_padding(i, _(Prompts[i]), 1);

/* Don't include "Sign as: " in the MaxHeaderWidth calculation. It
* doesn't show up by default, and so can make the indentation of
* the other fields look funny. */
calc_header_width_padding(HDR_CRYPTINFO, _(Prompts[HDR_CRYPTINFO]), 0);

for (i = 0; i <= HDR_XCOMMENTTO; i++)
for (int i = 0; i <= HDR_XCOMMENTTO; i++)
{
HeaderPadding[i] += MaxHeaderWidth;
if (HeaderPadding[i] < 0)
Expand Down Expand Up @@ -648,14 +647,13 @@ static void compose_menu_redraw(struct Menu *menu)
*/
static unsigned long cum_attachs_size(struct Menu *menu)
{
size_t s;
unsigned short i;
size_t s = 0;
struct AttachCtx *actx = menu->data;
struct AttachPtr **idx = actx->idx;
struct Content *info = NULL;
struct Body *b = NULL;

for (i = 0, s = 0; i < actx->idxlen; i++)
for (unsigned short i = 0; i < actx->idxlen; i++)
{
b = idx[i]->content;

Expand Down
5 changes: 2 additions & 3 deletions copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,12 @@ int mutt_copy_header(FILE *in, struct Header *h, FILE *out, int flags, const cha
static int count_delete_lines(FILE *fp, struct Body *b, LOFF_T *length, size_t datelen)
{
int dellines = 0;
long l;
int ch;

if (b->deleted)
{
fseeko(fp, b->offset, SEEK_SET);
for (l = b->length; l; l--)
for (long l = b->length; l; l--)
{
ch = getc(fp);
if (ch == EOF)
Expand All @@ -513,7 +512,7 @@ static int count_delete_lines(FILE *fp, struct Body *b, LOFF_T *length, size_t d
dellines -= 3;
*length -= b->length - (84 + datelen);
/* Count the number of digits exceeding the first one to write the size */
for (l = 10; b->length >= l; l *= 10)
for (long l = 10; b->length >= l; l *= 10)
(*length)++;
}
else
Expand Down
54 changes: 22 additions & 32 deletions curs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ static void collapse_all(struct Menu *menu, int toggle)

/* Restore the cursor */
mutt_set_virtual(Context);
int j;
for (j = 0; j < Context->vcount; j++)
for (int j = 0; j < Context->vcount; j++)
{
if (Context->hdrs[Context->v2r[j]]->index == base->index)
{
Expand All @@ -220,19 +219,15 @@ static void collapse_all(struct Menu *menu, int toggle)

static int ci_next_undeleted(int msgno)
{
int i;

for (i = msgno + 1; i < Context->vcount; i++)
for (int i = msgno + 1; i < Context->vcount; i++)
if (!Context->hdrs[Context->v2r[i]]->deleted)
return i;
return -1;
}

static int ci_previous_undeleted(int msgno)
{
int i;

for (i = msgno - 1; i >= 0; i--)
for (int i = msgno - 1; i >= 0; i--)
if (!Context->hdrs[Context->v2r[i]]->deleted)
return i;
return -1;
Expand All @@ -246,11 +241,11 @@ static int ci_previous_undeleted(int msgno)
*/
static int ci_first_message(void)
{
int old = -1, i;
int old = -1;

if (Context && Context->msgcount)
{
for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
{
if (!Context->hdrs[Context->v2r[i]]->read &&
!Context->hdrs[Context->v2r[i]]->deleted)
Expand Down Expand Up @@ -309,14 +304,13 @@ static int mx_toggle_write(struct Context *ctx)

static void resort_index(struct Menu *menu)
{
int i;
struct Header *current = CURHDR;

menu->current = -1;
mutt_sort_headers(Context, 0);
/* Restore the current message */

for (i = 0; i < Context->vcount; i++)
for (int i = 0; i < Context->vcount; i++)
{
if (Context->hdrs[Context->v2r[i]] == current)
{
Expand All @@ -338,7 +332,6 @@ void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcoun
{
/* store pointers to the newly added messages */
struct Header **save_new = NULL;
int j;

if (!menu || !ctx)
return;
Expand All @@ -357,20 +350,20 @@ void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcoun
* they will be visible in the limited view */
if (ctx->pattern)
{
for (j = (check == MUTT_REOPENED) ? 0 : oldcount; j < ctx->msgcount; j++)
for (int i = (check == MUTT_REOPENED) ? 0 : oldcount; i < ctx->msgcount; i++)
{
if (!j)
if (!i)
ctx->vcount = 0;

if (mutt_pattern_exec(ctx->limit_pattern, MUTT_MATCH_FULL_ADDRESS, ctx,
ctx->hdrs[j], NULL))
ctx->hdrs[i], NULL))
{
assert(ctx->vcount < ctx->msgcount);
ctx->hdrs[j]->virtual = ctx->vcount;
ctx->v2r[ctx->vcount] = j;
ctx->hdrs[j]->limited = true;
ctx->hdrs[i]->virtual = ctx->vcount;
ctx->v2r[ctx->vcount] = i;
ctx->hdrs[i]->limited = true;
ctx->vcount++;
struct Body *b = ctx->hdrs[j]->content;
struct Body *b = ctx->hdrs[i]->content;
ctx->vsize += b->length + b->offset - b->hdr_offset;
}
}
Expand All @@ -381,8 +374,8 @@ void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcoun
((Sort & SORT_MASK) == SORT_THREADS))
{
save_new = safe_malloc(sizeof(struct Header *) * (ctx->msgcount - oldcount));
for (j = oldcount; j < ctx->msgcount; j++)
save_new[j - oldcount] = ctx->hdrs[j];
for (int i = oldcount; i < ctx->msgcount; i++)
save_new[i - oldcount] = ctx->hdrs[i];
}

/* if the mailbox was reopened, need to rethread from scratch */
Expand All @@ -407,14 +400,12 @@ void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcoun
}
else if (oldcount)
{
for (j = 0; j < ctx->msgcount - oldcount; j++)
for (int i = 0; i < ctx->msgcount - oldcount; i++)
{
int k;

for (k = 0; k < ctx->msgcount; k++)
for (int k = 0; k < ctx->msgcount; k++)
{
struct Header *h = ctx->hdrs[k];
if (h == save_new[j] && (!ctx->pattern || h->limited))
if (h == save_new[i] && (!ctx->pattern || h->limited))
mutt_uncollapse_thread(ctx, h);
}
}
Expand All @@ -427,11 +418,11 @@ void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcoun
if (oldcount)
{
/* restore the current message to the message it was pointing to */
for (j = 0; j < ctx->vcount; j++)
for (int i = 0; i < ctx->vcount; i++)
{
if (ctx->hdrs[ctx->v2r[j]]->index == menu->oldcurrent)
if (ctx->hdrs[ctx->v2r[i]]->index == menu->oldcurrent)
{
menu->current = j;
menu->current = i;
break;
}
}
Expand Down Expand Up @@ -1348,7 +1339,6 @@ int mutt_index_menu(void)
{
struct Header *oldcur = CURHDR;
struct Header *hdr = NULL;
int k;
bool quiet = Context->quiet;

if (rc2 < 0)
Expand All @@ -1374,7 +1364,7 @@ int mutt_index_menu(void)
/* try to restore old position */
else
{
for (k = 0; k < Context->msgcount; k++)
for (int k = 0; k < Context->msgcount; k++)
{
if (Context->hdrs[k]->index == oldindex)
{
Expand Down
Loading

0 comments on commit da2ecbc

Please sign in to comment.