commit: 60f4d370a4ea9482b66176f1b2e8a7f5d504c7c6
parent 958f574988efd83006fa746ec77c38c673e58579
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 1 Jan 2025 09:56:53 +0100
Use `for(int c; c = getopt;)` instead of `while((c = getopt))`
This way the return variable is scoped.
Diffstat:
46 files changed, 64 insertions(+), 99 deletions(-)
diff --git a/cmd/base64.c b/cmd/base64.c
@@ -257,9 +257,9 @@ main(int argc, char *argv[])
{
int (*process)(FILE *, const char *) = &encode;
- int c = 0, ret = 0;
+ int ret = 0;
- while((c = getopt(argc, argv, ":dw:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":dw:")) != -1;)
{
switch(c)
{
diff --git a/cmd/basename.c b/cmd/basename.c
@@ -49,8 +49,7 @@ main(int argc, char *argv[])
char *suffix = NULL;
char delim = '\n';
- int c = -1;
- while((c = getopt(argc, argv, ":as:z")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":as:z")) != -1;)
{
switch(c)
{
diff --git a/cmd/cat.c b/cmd/cat.c
@@ -24,8 +24,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int c = -1;
- while((c = getopt(argc, argv, ":u")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":u")) != -1;)
{
switch(c)
{
diff --git a/cmd/chmod.c b/cmd/chmod.c
@@ -183,7 +183,6 @@ main(int argc, char *argv[])
{
bool opt_R = false;
- int c = -1;
#ifdef HAS_GETOPT_LONG
// Strictly for GNUisms compatibility so no long-only options
// clang-format off
@@ -196,7 +195,7 @@ main(int argc, char *argv[])
// clang-format on
#endif
- while(true)
+ for(int c = -1;;)
{
if(optind >= argc || !argv[optind]) break;
diff --git a/cmd/chown.c b/cmd/chown.c
@@ -205,7 +205,6 @@ main(int argc, char *argv[])
enum chown_follow_symlinks follow_symlinks = CHOWN_FOLLOW_UNK_SYMLINKS;
- int c = -1;
#ifdef HAS_GETOPT_LONG
// Strictly for GNUisms compatibility so no long-only options
// clang-format off
@@ -218,9 +217,9 @@ main(int argc, char *argv[])
// clang-format on
// Need + as first character to get POSIX-style option parsing
- while((c = getopt_long(argc, argv, "+:hRHLPv", opts, NULL)) != -1)
+ for(int c = -1; (c = getopt_long(argc, argv, "+:hRHLPv", opts, NULL)) != -1;)
#else
- while((c = getopt(argc, argv, ":hRHLPv")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":hRHLPv")) != -1;)
#endif
{
switch(c)
diff --git a/cmd/cmp.c b/cmd/cmp.c
@@ -97,8 +97,7 @@ main(int argc, char *argv[])
{
char *endptr = NULL;
- int c = -1;
- while((c = getopt(argc, argv, ":ln:s")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":ln:s")) != -1;)
{
switch(c)
{
diff --git a/cmd/cut.c b/cmd/cut.c
@@ -369,8 +369,7 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
errno = 0;
- int c = -1;
- while((c = getopt(argc, argv, ":b:c:d:f:ns")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":b:c:d:f:ns")) != -1;)
{
switch(c)
{
diff --git a/cmd/date.c b/cmd/date.c
@@ -67,7 +67,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- while((c = getopt(argc, argv, ":d:f:jr:Ru")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":d:f:jr:Ru")) != -1;)
{
const char *errstr = NULL;
switch(c)
diff --git a/cmd/df.c b/cmd/df.c
@@ -50,8 +50,7 @@ main(int argc, char *argv[])
size_t only_count = 0;
char *only[4096];
- int c = -1;
- while((c = getopt(argc, argv, ":ahilPkTt:x:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":ahilPkTt:x:")) != -1;)
{
switch(c)
{
diff --git a/cmd/env.c b/cmd/env.c
@@ -57,7 +57,6 @@ main(int argc, char *argv[])
#define OPTARGS ":iu:"
#endif
- int c = -1;
#ifdef HAS_GETOPT_LONG
// Strictly for GNUisms compatibility so no long-only options
// clang-format off
@@ -72,9 +71,9 @@ main(int argc, char *argv[])
// clang-format on
// Need + as first character to get POSIX-style option parsing
- while((c = getopt_long(argc, argv, "+" OPTARGS, opts, NULL)) != -1)
+ for(int c = -1; (c = getopt_long(argc, argv, "+" OPTARGS, opts, NULL)) != -1;)
#else
- while((c = getopt(argc, argv, OPTARGS)) != -1)
+ for(int c = -1; (c = getopt(argc, argv, OPTARGS)) != -1;)
#endif
{
switch(c)
diff --git a/cmd/id.c b/cmd/id.c
@@ -188,8 +188,7 @@ main(int argc, char *argv[])
struct passwd pw = {.pw_uid = uid, .pw_gid = gid};
struct passwd epw = {.pw_uid = euid, .pw_gid = egid};
- int c = -1;
- while((c = getopt(argc, argv, ":Ggunr")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":Ggunr")) != -1;)
{
switch(c)
{
diff --git a/cmd/install.c b/cmd/install.c
@@ -206,7 +206,6 @@ main(int argc, char *argv[])
mkdir_parents_filemask = umask(0);
umask(mkdir_parents_filemask);
- int c = -1;
#ifdef HAS_GETOPT_LONG
// Strictly for GNUisms compatibility so no long-only options
// clang-format off
@@ -225,9 +224,9 @@ main(int argc, char *argv[])
// clang-format on
// Need + as first character to get POSIX-style option parsing
- while((c = getopt_long(argc, argv, "+:CcDdpTt:g:m:o:v", opts, NULL)) != -1)
+ for(int c = -1; (c = getopt_long(argc, argv, "+:CcDdpTt:g:m:o:v", opts, NULL)) != -1;)
#else
- while((c = getopt(argc, argv, ":CcDdpTt:g:m:o:v")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":CcDdpTt:g:m:o:v")) != -1;)
#endif
{
switch(c)
diff --git a/cmd/ln.c b/cmd/ln.c
@@ -136,8 +136,7 @@ main(int argc, char *argv[])
{
bool verbose = false;
- int c = -1;
- while((c = getopt(argc, argv, ":fnsLPv")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":fnsLPv")) != -1;)
{
switch(c)
{
diff --git a/cmd/mkdir.c b/cmd/mkdir.c
@@ -53,8 +53,7 @@ main(int argc, char *argv[])
const char *errstr = NULL;
// clang-format on
- int c = -1;
- while((c = getopt(argc, argv, ":pvm:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":pvm:")) != -1;)
{
switch(c)
{
diff --git a/cmd/mkfifo.c b/cmd/mkfifo.c
@@ -28,8 +28,7 @@ main(int argc, char *argv[])
const char *errstr = NULL;
- int c = -1;
- while((c = getopt(argc, argv, ":m:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":m:")) != -1;)
{
switch(c)
{
diff --git a/cmd/mknod.c b/cmd/mknod.c
@@ -52,8 +52,7 @@ main(int argc, char *argv[])
const char *errstr = NULL;
- int c = -1;
- while((c = getopt(argc, argv, ":m:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":m:")) != -1;)
{
switch(c)
{
diff --git a/cmd/mktemp.c b/cmd/mktemp.c
@@ -71,7 +71,6 @@ main(int argc, char *argv[])
char template[PATH_MAX] = "tmp.XXXXXXXXXX";
const char *tmpdir = NULL;
- int c = -1;
#ifdef HAS_GETOPT_LONG
// Strictly for GNUisms compatibility so no long-only options
// clang-format off
@@ -85,9 +84,9 @@ main(int argc, char *argv[])
// clang-format on
// Need + as first character to get POSIX-style option parsing
- while((c = getopt_long(argc, argv, "+:dqp:tu", opts, NULL)) != -1)
+ for(int c = -1; (c = getopt_long(argc, argv, "+:dqp:tu", opts, NULL)) != -1;)
#else
- while((c = getopt(argc, argv, ":dqp:tu")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":dqp:tu")) != -1;)
#endif
{
switch(c)
diff --git a/cmd/mv.c b/cmd/mv.c
@@ -392,8 +392,7 @@ main(int argc, char *argv[])
.sep = "",
};
- int c = -1;
- while((c = getopt(argc, argv, ":fint:v")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":fint:v")) != -1;)
{
switch(c)
{
diff --git a/cmd/nice.c b/cmd/nice.c
@@ -25,8 +25,7 @@ main(int argc, char *argv[])
{
long incr = 0;
- int c = -1;
- while((c = getopt(argc, argv, ":n:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":n:")) != -1;)
{
char *endptr = NULL;
diff --git a/cmd/nproc.c b/cmd/nproc.c
@@ -29,8 +29,7 @@ main(int argc, char *argv[])
int target = _SC_NPROCESSORS_ONLN;
const char *target_str = "_SC_NPROCESSORS_ONLN";
- int c = -1;
- while((c = getopt(argc, argv, ":a")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":a")) != -1;)
{
switch(c)
{
diff --git a/cmd/paste.c b/cmd/paste.c
@@ -214,8 +214,9 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
errno = 0;
- int c = -1, seq = 0;
- while((c = getopt(argc, argv, ":d:sz")) != -1)
+ int seq = 0;
+
+ for(int c = -1; (c = getopt(argc, argv, ":d:sz")) != -1;)
{
switch(c)
{
diff --git a/cmd/pathchk.c b/cmd/pathchk.c
@@ -42,8 +42,7 @@ main(int argc, char *argv[])
size_t path_max = PATH_MAX - 1;
size_t name_max = NAME_MAX;
- int c = -1;
- while((c = getopt(argc, argv, ":pP")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":pP")) != -1;)
{
switch(c)
{
diff --git a/cmd/printf.c b/cmd/printf.c
@@ -95,18 +95,20 @@ main(int argc, char *argv[])
size_t len;
int end, rval;
char *format, *fmt, *start;
- int ch;
(void)setlocale(LC_ALL, "");
- while((ch = getopt(argc, argv, "")) != -1)
- switch(ch)
+ for(int c = -1; (c = getopt(argc, argv, "")) != -1;)
+ {
+ switch(c)
{
case '?':
default:
usage();
return (1);
}
+ }
+
argc -= optind;
argv += optind;
diff --git a/cmd/pwd.c b/cmd/pwd.c
@@ -74,8 +74,7 @@ main(int argc, char *argv[])
{
enum pwd mode = PWD_L;
- int c = -1;
- while((c = getopt(argc, argv, ":LP")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":LP")) != -1;)
{
switch(c)
{
diff --git a/cmd/realpath.c b/cmd/realpath.c
@@ -97,8 +97,7 @@ main_realpath(int argc, char *argv[])
int offset_sep = 0;
- int c = -1;
- while((c = getopt(argc, argv, ":Eemnsz")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":Eemnsz")) != -1;)
{
switch(c)
{
@@ -162,8 +161,7 @@ main_readlink(int argc, char *argv[])
int offset_sep = 0;
- int c = -1;
- while((c = getopt(argc, argv, ":femnz")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":femnz")) != -1;)
{
switch(c)
{
diff --git a/cmd/renice.c b/cmd/renice.c
@@ -71,8 +71,7 @@ main(int argc, char *argv[])
long adj = 0;
int which = PRIO_PROCESS;
- int c = -1;
- while((c = getopt(argc, argv, ":gpun:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":gpun:")) != -1;)
{
char *endptr = NULL;
diff --git a/cmd/rm.c b/cmd/rm.c
@@ -169,8 +169,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int c = -1;
- while((c = getopt(argc, argv, ":dfirRv")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":dfirRv")) != -1;)
{
switch(c)
{
diff --git a/cmd/rmdir.c b/cmd/rmdir.c
@@ -28,7 +28,6 @@ main(int argc, char *argv[])
{
bool parents = false, verbose = false, ign_enotempty = false;
- int c = -1;
#ifdef HAS_GETOPT_LONG
// clang-format off
enum long_opt_vals {
@@ -43,9 +42,9 @@ main(int argc, char *argv[])
// clang-format on
// Need + as first character to get POSIX-style option parsing
- while((c = getopt_long(argc, argv, "+:pv", opts, NULL)) != -1)
+ for(int c = -1; (c = getopt_long(argc, argv, "+:pv", opts, NULL)) != -1;)
#else
- while((c = getopt(argc, argv, ":pv")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":pv")) != -1;)
#endif
{
switch(c)
diff --git a/cmd/seq.c b/cmd/seq.c
@@ -80,7 +80,7 @@ main(int argc, char *argv[])
{
int c;
- while((c = getopt(argc, argv, ":ws:t:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":ws:t:")) != -1;)
{
switch(c)
{
diff --git a/cmd/sha1sum.c b/cmd/sha1sum.c
@@ -167,8 +167,7 @@ main(int argc, char *argv[])
{
bool opt_c = false;
- int c = -1;
- while((c = getopt(argc, argv, ":c")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":c")) != -1;)
{
switch(c)
{
diff --git a/cmd/sha256sum.c b/cmd/sha256sum.c
@@ -167,8 +167,7 @@ main(int argc, char *argv[])
{
bool opt_c = false;
- int c = -1;
- while((c = getopt(argc, argv, ":c")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":c")) != -1;)
{
switch(c)
{
diff --git a/cmd/sha512sum.c b/cmd/sha512sum.c
@@ -167,8 +167,7 @@ main(int argc, char *argv[])
{
bool opt_c = false;
- int c = -1;
- while((c = getopt(argc, argv, ":c")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":c")) != -1;)
{
switch(c)
{
diff --git a/cmd/split.c b/cmd/split.c
@@ -233,8 +233,7 @@ static const char *error_opt_b_l = "%s: error: Options -b and -l are mutually ex
int
main(int argc, char *argv[])
{
- int c = -1;
- while((c = getopt(argc, argv, ":a:b:l:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":a:b:l:")) != -1;)
{
char *endptr = NULL;
diff --git a/cmd/strings.c b/cmd/strings.c
@@ -103,7 +103,7 @@ int
main(int argc, char *argv[])
{
int c;
- while((c = getopt(argc, argv, ":an:t:z")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":an:t:z")) != -1;)
{
char *endptr = NULL;
diff --git a/cmd/sync.c b/cmd/sync.c
@@ -20,8 +20,7 @@ main(int argc, char *argv[])
int err = 0;
int (*sync_func)(int) = fsync;
- int c = -1;
- while((c = getopt(argc, argv, ":df")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":df")) != -1;)
{
switch(c)
{
diff --git a/cmd/tee.c b/cmd/tee.c
@@ -27,7 +27,7 @@ main(int argc, char *argv[])
FILE **fds = {NULL}; // Shut up GCC
int c;
- while((c = getopt(argc, argv, ":ai")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":ai")) != -1;)
{
switch(c)
{
diff --git a/cmd/time.c b/cmd/time.c
@@ -37,8 +37,7 @@ main(int argc, char *argv[])
return 0;
}
- int c = -1;
- while((c = getopt(argc, argv, ":pv")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":pv")) != -1;)
{
switch(c)
{
diff --git a/cmd/timeout.c b/cmd/timeout.c
@@ -60,8 +60,7 @@ main(int argc, char *argv[])
char *arg = NULL;
- int c = -1;
- while((c = getopt(argc, argv, ":fk:ps:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":fk:ps:")) != -1;)
{
switch(c)
{
diff --git a/cmd/touch.c b/cmd/touch.c
@@ -124,8 +124,7 @@ main(int argc, char *argv[])
int open_flags = O_WRONLY | O_CREAT | O_NOCTTY;
int utimensat_flags = 0;
- int c = 0;
- while((c = getopt(argc, argv, ":acfhmr:t:d:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":acfhmr:t:d:")) != -1;)
{
const char *errstr = NULL;
diff --git a/cmd/tr.c b/cmd/tr.c
@@ -74,13 +74,12 @@ static void usage(void);
int
main(int argc, char *argv[])
{
- int ch, cnt, lastch, *p;
int cflag, dflag, sflag;
cflag = dflag = sflag = 0;
- while((ch = getopt(argc, argv, "Ccds")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, "Ccds")) != -1;)
{
- switch(ch)
+ switch(c)
{
case 'C':
case 'c':
@@ -101,7 +100,7 @@ main(int argc, char *argv[])
usage();
return 1;
default:
- fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, ch);
+ fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c);
usage();
abort();
}
@@ -111,6 +110,8 @@ main(int argc, char *argv[])
if(argc < 1 || argc > 2) usage();
+ int ch, cnt, lastch, *p;
+
/*
* tr -ds [-Cc] string1 string2
* Delete all characters (or complemented characters) in string1.
diff --git a/cmd/truncate.c b/cmd/truncate.c
@@ -36,8 +36,7 @@ main(int argc, char *argv[])
};
char *ref_file = NULL;
- int c = -1;
- while((c = getopt(argc, argv, ":cr:s:")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":cr:s:")) != -1;)
{
switch(c)
{
diff --git a/cmd/uname.c b/cmd/uname.c
@@ -49,8 +49,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int c = -1;
- while((c = getopt(argc, argv, ":amnprsv")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":amnprsv")) != -1;)
{
switch(c)
{
diff --git a/cmd/uniq.c b/cmd/uniq.c
@@ -30,8 +30,7 @@ main(int argc, char *argv[])
char *endptr = NULL;
- int c = -1;
- while((c = getopt(argc, argv, ":cdf:s:u")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":cdf:s:u")) != -1;)
{
switch(c)
{
diff --git a/cmd/wc.c b/cmd/wc.c
@@ -214,7 +214,6 @@ main(int argc, char *argv[])
errno = 0;
int (*wc_file)(int, char *) = &wc_file_bytes;
- int c = -1;
#ifdef HAS_GETOPT_LONG
// Strictly for GNUisms compatibility so no long-only options
// clang-format off
@@ -228,9 +227,9 @@ main(int argc, char *argv[])
// clang-format on
// Need + as first character to get POSIX-style option parsing
- while((c = getopt_long(argc, argv, "+:clmw", opts, NULL)) != -1)
+ for(int c = -1; (c = getopt_long(argc, argv, "+:clmw", opts, NULL)) != -1;)
#else
- while((c = getopt(argc, argv, ":clmw")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, ":clmw")) != -1;)
#endif
{
switch(c)
diff --git a/cmd/which.c b/cmd/which.c
@@ -24,8 +24,7 @@ main(int argc, char *argv[])
return 1;
}
- int c = -1;
- while((c = getopt(argc, argv, "as")) != -1)
+ for(int c = -1; (c = getopt(argc, argv, "as")) != -1;)
{
switch(c)
{
diff --git a/configure.d/getopt_long.c b/configure.d/getopt_long.c
@@ -22,8 +22,7 @@ main(int argc, char *argv[])
};
// clang-format on
- int c = -1;
- while((c = getopt_long(argc, argv, ":hf:", opts, &optidx)) != -1)
+ for(int c = -1;(c = getopt_long(argc, argv, ":hf:", opts, &optidx)) != -1;)
{
switch(c)
{