logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 6cd739a74b65f2cca5e87130168cf6f5bdc46324
parent 8d6cb11c793530c7e89db32808e3cb657a55fa8d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 18 Mar 2025 01:50:05 +0100

cmd/*: gracefully handle errorneous long options

Diffstat:

Mcmd/base64.c5++++-
Mcmd/basename.c6+++++-
Mcmd/cat.c6+++++-
Mcmd/chmod.c8+++++++-
Mcmd/chown.c8+++++++-
Mcmd/cmp.c6+++++-
Mcmd/cut.c5++++-
Mcmd/date.c6+++++-
Mcmd/df.c5++++-
Mcmd/env.c7+++++++
Mcmd/getconf.c10+++++++++-
Mcmd/head.c6+++++-
Mcmd/id.c6+++++-
Mcmd/install.c8+++++++-
Mcmd/join.c6+++++-
Mcmd/ln.c7++++++-
Mcmd/mkdir.c6+++++-
Mcmd/mkfifo.c7++++++-
Mcmd/mknod.c7++++++-
Mcmd/mktemp.c7++++++-
Mcmd/mv.c6+++++-
Mcmd/nice.c6+++++-
Mcmd/nproc.c6+++++-
Mcmd/paste.c6+++++-
Mcmd/pathchk.c8+++++++-
Mcmd/printf.c6+++++-
Mcmd/pwd.c6+++++-
Mcmd/realpath.c12++++++++++--
Mcmd/renice.c8+++++++-
Mcmd/rm.c6+++++-
Mcmd/rmdir.c10+++++++++-
Mcmd/seq.c8+++++++-
Mcmd/sha1sum.c5++++-
Mcmd/sha256sum.c5++++-
Mcmd/sha512sum.c5++++-
Mcmd/shuf.c6+++++-
Mcmd/split.c5++++-
Mcmd/strings.c8+++++++-
Mcmd/sync.c7++++++-
Mcmd/tee.c7++++++-
Mcmd/time.c8+++++++-
Mcmd/timeout.c6+++++-
Mcmd/touch.c7++++++-
Mcmd/tr.c6+++++-
Mcmd/truncate.c6+++++-
Mcmd/uname.c8+++++++-
Mcmd/uniq.c5++++-
Mcmd/wc.c8+++++++-
Mcmd/which.c7++++++-
Mtest-cmd/cat.sh2+-
Mtest-cmd/tee.sh2+-
51 files changed, 282 insertions(+), 51 deletions(-)

diff --git a/cmd/base64.c b/cmd/base64.c @@ -259,7 +259,7 @@ main(int argc, char *argv[]) int ret = 0; - for(int c = -1; (c = getopt(argc, argv, ":dw:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":dw:-:")) != -1;) { switch(c) { @@ -280,6 +280,9 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt); return 1; diff --git a/cmd/basename.c b/cmd/basename.c @@ -49,7 +49,7 @@ main(int argc, char *argv[]) char *suffix = NULL; char delim = '\n'; - for(int c = -1; (c = getopt(argc, argv, ":as:z")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":as:z-:")) != -1;) { switch(c) { @@ -63,6 +63,10 @@ main(int argc, char *argv[]) case 'z': delim = '\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/cat.c b/cmd/cat.c @@ -24,13 +24,17 @@ usage(void) int main(int argc, char *argv[]) { - for(int c = -1; (c = getopt(argc, argv, ":u")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":u-:")) != -1;) { switch(c) { case 'u': // POSIX: Ignored, buffered streams aren't used break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/chmod.c b/cmd/chmod.c @@ -356,7 +356,7 @@ main(int argc, char *argv[]) // Need + as first character to get POSIX-style option parsing c = getopt_long(argc, argv, "+:cRv", opts, NULL); #else - c = getopt(argc, argv, ":cRv"); + c = getopt(argc, argv, ":cRv-:"); #endif if(c == -1) break; @@ -374,6 +374,12 @@ main(int argc, char *argv[]) case 'v': // GNU opt_v = true; break; +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; +#endif case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/chown.c b/cmd/chown.c @@ -220,7 +220,7 @@ main(int argc, char *argv[]) // Need + as first character to get POSIX-style option parsing for(int c = -1; (c = getopt_long(argc, argv, "+:hRHLPv", opts, NULL)) != -1;) #else - for(int c = -1; (c = getopt(argc, argv, ":hRHLPv")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":hRHLPv-:")) != -1;) #endif { switch(c) @@ -243,6 +243,12 @@ main(int argc, char *argv[]) case 'v': opt_v = true; break; +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; +#endif case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/cmp.c b/cmd/cmp.c @@ -97,7 +97,7 @@ main(int argc, char *argv[]) { char *endptr = NULL; - for(int c = -1; (c = getopt(argc, argv, ":ln:s")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":ln:s-:")) != -1;) { switch(c) { @@ -125,6 +125,10 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/cut.c b/cmd/cut.c @@ -376,7 +376,7 @@ main(int argc, char *argv[]) } errno = 0; - for(int c = -1; (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) { @@ -424,6 +424,9 @@ main(int argc, char *argv[]) case 's': opt_s = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt); return 1; diff --git a/cmd/date.c b/cmd/date.c @@ -151,7 +151,7 @@ main(int argc, char *argv[]) return 1; } - for(int c = -1; (c = getopt(argc, argv, ":d:f:I:jr:Ru")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":d:f:I:jr:Ru-:")) != -1;) { const char *errstr = NULL; switch(c) @@ -261,6 +261,10 @@ main(int argc, char *argv[]) case 'j': jflag = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/df.c b/cmd/df.c @@ -50,7 +50,7 @@ main(int argc, char *argv[]) size_t only_count = 0; char *only[4096]; - for(int c = -1; (c = getopt(argc, argv, ":ahilPkTt:x:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":ahilPkTt:x:-:")) != -1;) { switch(c) { @@ -84,6 +84,9 @@ main(int argc, char *argv[]) case 'x': excluded[excluded_count++] = optarg; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; diff --git a/cmd/env.c b/cmd/env.c @@ -23,6 +23,7 @@ extern char **environ; char *envclear[1]; +const char *argv0 = "env"; static int do_export(void) @@ -115,6 +116,12 @@ main(int argc, char *argv[]) } break; #endif +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; +#endif case ':': fprintf(stderr, "env: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/getconf.c b/cmd/getconf.c @@ -17,6 +17,8 @@ // need to be after system headers #include "./getconf_vars.h" +const char *argv0 = "getconf"; + static int print_system_var(const char *var) { @@ -133,7 +135,7 @@ main(int argc, char *argv[]) bool o_all = false; int c = -1; - while((c = getopt(argc, argv, ":av:")) != -1) + while((c = getopt(argc, argv, ":av:-:")) != -1) { switch(c) { @@ -143,6 +145,12 @@ main(int argc, char *argv[]) case 'v': fputs("getconf: -v option is unsupported\n", stderr); return 1; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; + case '?': + fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); + return 1; } } diff --git a/cmd/head.c b/cmd/head.c @@ -168,7 +168,7 @@ main(int argc, char *argv[]) continue; } - int c = getopt(argc, argv, ":qvc:n:z"); + int c = getopt(argc, argv, ":qvc:n:z-:"); if(c == -1) break; switch(c) @@ -218,6 +218,10 @@ main(int argc, char *argv[]) case 'z': delim = '\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/id.c b/cmd/id.c @@ -188,7 +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}; - for(int c = -1; (c = getopt(argc, argv, ":Ggunr")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":Ggunr-:")) != -1;) { switch(c) { @@ -207,6 +207,10 @@ main(int argc, char *argv[]) case 'r': real_flag = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case '?': fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/install.c b/cmd/install.c @@ -227,7 +227,7 @@ main(int argc, char *argv[]) // Need + as first character to get POSIX-style option parsing for(int c = -1; (c = getopt_long(argc, argv, "+:CcDdpTt:g:m:o:v", opts, NULL)) != -1;) #else - for(int c = -1; (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) @@ -271,6 +271,12 @@ main(int argc, char *argv[]) opt_v = true; mkdir_parents_verbose = true; break; +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; +#endif case ':': fprintf(stderr, "install: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/join.c b/cmd/join.c @@ -127,7 +127,7 @@ main(int argc, char *argv[]) F2 = &input2; aflag = vflag = 0; - while((ch = getopt(argc, argv, ":a:e:j:1:2:o:t:v:")) != -1) + while((ch = getopt(argc, argv, ":a:e:j:1:2:o:t:v:-:")) != -1) { switch(ch) { @@ -195,6 +195,10 @@ main(int argc, char *argv[]) } if(*end) utils_errx(1, "illegal file number -- %s", optarg); break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/ln.c b/cmd/ln.c @@ -24,6 +24,7 @@ static bool opt_s = false, force = false; static int link_flags = 0; static int open_dir_flags = O_RDONLY | O_DIRECTORY; +const char *argv0 = "ln"; static int do_link(char *src, char *dest) @@ -136,7 +137,7 @@ main(int argc, char *argv[]) { bool verbose = false; - for(int c = -1; (c = getopt(argc, argv, ":fnsLPv")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":fnsLPv-:")) != -1;) { switch(c) { @@ -158,6 +159,10 @@ main(int argc, char *argv[]) case 'v': verbose = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case '?': fprintf(stderr, "ln: error: Unknown option '-%c'\n", optopt); usage(); diff --git a/cmd/mkdir.c b/cmd/mkdir.c @@ -53,7 +53,7 @@ main(int argc, char *argv[]) const char *errstr = NULL; // clang-format on - for(int c = -1; (c = getopt(argc, argv, ":pvm:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":pvm:-:")) != -1;) { switch(c) { @@ -71,6 +71,10 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/mkfifo.c b/cmd/mkfifo.c @@ -14,6 +14,7 @@ #include <unistd.h> // getopt mode_t filemask; +const char *argv0 = "mkfifo"; static void usage(void) @@ -28,7 +29,7 @@ main(int argc, char *argv[]) const char *errstr = NULL; - for(int c = -1; (c = getopt(argc, argv, ":m:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":m:-:")) != -1;) { switch(c) { @@ -40,6 +41,10 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "mkfifo: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/mknod.c b/cmd/mknod.c @@ -16,6 +16,7 @@ #include <unistd.h> // getopt mode_t filemask; +const char *argv0 = "mknod"; static void usage(void) @@ -52,7 +53,7 @@ main(int argc, char *argv[]) const char *errstr = NULL; - for(int c = -1; (c = getopt(argc, argv, ":m:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":m:-:")) != -1;) { switch(c) { @@ -64,6 +65,10 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "mknod: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/mktemp.c b/cmd/mktemp.c @@ -96,7 +96,7 @@ main(int argc, char *argv[]) // Need + as first character to get POSIX-style option parsing for(int c = -1; (c = getopt_long(argc, argv, "+:dqp:tu", opts, NULL)) != -1;) #else - for(int c = -1; (c = getopt(argc, argv, ":dqp:tu")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":dqp:tu-:")) != -1;) #endif { switch(c) @@ -117,6 +117,11 @@ main(int argc, char *argv[]) case 'u': o_unsafe = true; break; +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; +#endif case '?': fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; diff --git a/cmd/mv.c b/cmd/mv.c @@ -392,7 +392,7 @@ main(int argc, char *argv[]) .sep = "", }; - for(int c = -1; (c = getopt(argc, argv, ":fint:v")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":fint:v-:")) != -1;) { switch(c) { @@ -428,6 +428,10 @@ main(int argc, char *argv[]) case 'v': verbose = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "mv: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/nice.c b/cmd/nice.c @@ -25,7 +25,7 @@ main(int argc, char *argv[]) { long incr = 0; - for(int c = -1; (c = getopt(argc, argv, ":n:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":n:-:")) != -1;) { char *endptr = NULL; @@ -46,6 +46,10 @@ main(int argc, char *argv[]) return 125; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/nproc.c b/cmd/nproc.c @@ -29,7 +29,7 @@ main(int argc, char *argv[]) int target = _SC_NPROCESSORS_ONLN; const char *target_str = "_SC_NPROCESSORS_ONLN"; - for(int c = -1; (c = getopt(argc, argv, ":a")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":a-:")) != -1;) { switch(c) { @@ -37,6 +37,10 @@ main(int argc, char *argv[]) target = _SC_NPROCESSORS_CONF; target_str = "_SC_NPROCESSORS_CONF"; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/paste.c b/cmd/paste.c @@ -223,7 +223,7 @@ main(int argc, char *argv[]) int seq = 0; - for(int c = -1; (c = getopt(argc, argv, ":d:sz")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":d:sz-:")) != -1;) { switch(c) { @@ -247,6 +247,10 @@ main(int argc, char *argv[]) case 'z': linedelim = L'\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/pathchk.c b/cmd/pathchk.c @@ -12,6 +12,8 @@ #include <sys/stat.h> // lstat #include <unistd.h> // getopt +const char *argv0 = "pathchk"; + // POSIX Portable Character Set // Returns 0 on success, or the first invalid character found static char @@ -42,7 +44,7 @@ main(int argc, char *argv[]) size_t path_max = PATH_MAX - 1; size_t name_max = NAME_MAX; - for(int c = -1; (c = getopt(argc, argv, ":pP")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":pP-:")) != -1;) { switch(c) { @@ -54,6 +56,10 @@ main(int argc, char *argv[]) path_max = _POSIX_PATH_MAX - 1; name_max = _POSIX_NAME_MAX; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "pathchk: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/printf.c b/cmd/printf.c @@ -105,10 +105,14 @@ main(int argc, char *argv[]) strerror(errno)); } - for(int c = -1; (c = getopt(argc, argv, "")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":-:")) != -1;) { switch(c) { + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case '?': default: usage(); diff --git a/cmd/pwd.c b/cmd/pwd.c @@ -74,7 +74,7 @@ main(int argc, char *argv[]) { enum pwd mode = PWD_L; - for(int c = -1; (c = getopt(argc, argv, ":LP")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":LP-:")) != -1;) { switch(c) { @@ -84,6 +84,10 @@ main(int argc, char *argv[]) case 'P': mode = PWD_P; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/realpath.c b/cmd/realpath.c @@ -97,7 +97,7 @@ main_realpath(int argc, char *argv[]) int offset_sep = 0; - for(int c = -1; (c = getopt(argc, argv, ":Eemnsz")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":Eemnsz-:")) != -1;) { switch(c) { @@ -116,6 +116,10 @@ main_realpath(int argc, char *argv[]) case 'z': sep = '\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage_realpath(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage_realpath(); @@ -161,7 +165,7 @@ main_readlink(int argc, char *argv[]) int offset_sep = 0; - for(int c = -1; (c = getopt(argc, argv, ":femnz")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":femnz-:")) != -1;) { switch(c) { @@ -178,6 +182,10 @@ main_readlink(int argc, char *argv[]) case 'z': sep = '\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage_readlink(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage_readlink(); diff --git a/cmd/renice.c b/cmd/renice.c @@ -12,6 +12,8 @@ #include <sys/resource.h> // getpriority, setpriority #include <unistd.h> // getopt, nice +const char *argv0 = "renice"; + static int renice(int which, id_t who, int adj) { @@ -71,7 +73,7 @@ main(int argc, char *argv[]) long adj = 0; int which = PRIO_PROCESS; - for(int c = -1; (c = getopt(argc, argv, ":gpun:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":gpun:-:")) != -1;) { char *endptr = NULL; @@ -115,6 +117,10 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "renice: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/rm.c b/cmd/rm.c @@ -169,7 +169,7 @@ usage(void) int main(int argc, char *argv[]) { - for(int c = -1; (c = getopt(argc, argv, ":dfirRv")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":dfirRv-:")) != -1;) { switch(c) { @@ -191,6 +191,10 @@ main(int argc, char *argv[]) case 'v': verbose = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "rm: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/rmdir.c b/cmd/rmdir.c @@ -15,6 +15,8 @@ #include <getopt.h> #endif +const char *argv0 = "rmdir"; + static void usage(void) { @@ -46,7 +48,7 @@ main(int argc, char *argv[]) // Need + as first character to get POSIX-style option parsing for(int c = -1; (c = getopt_long(argc, argv, "+:pv", opts, NULL)) != -1;) #else - for(int c = -1; (c = getopt(argc, argv, ":pv")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":pv-:")) != -1;) #endif { switch(c) @@ -62,6 +64,12 @@ main(int argc, char *argv[]) case 'v': verbose = true; break; +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; +#endif case ':': fprintf(stderr, "rmdir: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/seq.c b/cmd/seq.c @@ -12,6 +12,8 @@ #include <string.h> // strerror #include <unistd.h> // getopt, optarg, optind +const char *argv0 = "seq"; + const char *sep = "\n"; const char *term = "\n"; @@ -78,7 +80,7 @@ usage(void) int main(int argc, char *argv[]) { - for(int c = -1; (c = getopt(argc, argv, ":ws:t:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":ws:t:-:")) != -1;) { switch(c) { @@ -91,6 +93,10 @@ main(int argc, char *argv[]) case 't': term = optarg; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "seq: error: Option -%c requires an operand\n", optopt); usage(); diff --git a/cmd/sha1sum.c b/cmd/sha1sum.c @@ -167,13 +167,16 @@ main(int argc, char *argv[]) { bool opt_c = false; - for(int c = -1; (c = getopt(argc, argv, ":c")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":c-:")) != -1;) { switch(c) { case 'c': opt_c = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; diff --git a/cmd/sha256sum.c b/cmd/sha256sum.c @@ -167,13 +167,16 @@ main(int argc, char *argv[]) { bool opt_c = false; - for(int c = -1; (c = getopt(argc, argv, ":c")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":c-:")) != -1;) { switch(c) { case 'c': opt_c = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; diff --git a/cmd/sha512sum.c b/cmd/sha512sum.c @@ -167,13 +167,16 @@ main(int argc, char *argv[]) { bool opt_c = false; - for(int c = -1; (c = getopt(argc, argv, ":c")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":c-:")) != -1;) { switch(c) { case 'c': opt_c = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; diff --git a/cmd/shuf.c b/cmd/shuf.c @@ -92,7 +92,7 @@ main(int argc, char *argv[]) bool e_flag = false; srand((int)time(NULL)); - for(int c = -1; (c = getopt(argc, argv, ":en:z")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":en:z-:")) != -1;) { char *endptr = NULL; @@ -126,6 +126,10 @@ main(int argc, char *argv[]) case 'z': delim = '\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/split.c b/cmd/split.c @@ -233,7 +233,7 @@ static const char *error_opt_b_l = "%s: error: Options -b and -l are mutually ex int main(int argc, char *argv[]) { - for(int c = -1; (c = getopt(argc, argv, ":a:b:l:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":a:b:l:-:")) != -1;) { char *endptr = NULL; @@ -301,6 +301,9 @@ main(int argc, char *argv[]) return 1; } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt); return 1; diff --git a/cmd/strings.c b/cmd/strings.c @@ -12,6 +12,8 @@ #include <string.h> /* strerror(), strncmp() */ #include <unistd.h> /* read(), write(), close(), getopt(), optarg, optind */ +const char *argv0 = "strings"; + size_t opt_min_strlen = 4; const char *opt_offset_format = NULL; int delim = '\n'; @@ -102,7 +104,7 @@ usage(void) int main(int argc, char *argv[]) { - for(int c = -1; (c = getopt(argc, argv, ":an:t:z")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":an:t:z-:")) != -1;) { char *endptr = NULL; @@ -164,6 +166,10 @@ main(int argc, char *argv[]) case 'z': delim = '\0'; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "strings: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/sync.c b/cmd/sync.c @@ -16,13 +16,15 @@ #include <string.h> // strerror #include <unistd.h> // fsync, sync, getopt, syncfs +const char *argv0 = "sync"; + int main(int argc, char *argv[]) { int err = 0; int (*sync_func)(int) = fsync; - for(int c = -1; (c = getopt(argc, argv, ":df")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":df-:")) != -1;) { switch(c) { @@ -39,6 +41,9 @@ main(int argc, char *argv[]) err = 1; break; #endif + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case '?': fprintf(stderr, "sync: error: Unrecognized option '-%c', continuing with error status set\n", diff --git a/cmd/tee.c b/cmd/tee.c @@ -11,6 +11,8 @@ #include <string.h> /* strerror() */ #include <unistd.h> /* getopt(), opt… */ +const char *argv0 = "tee"; + static void cleanup(FILE **fds) { @@ -27,7 +29,7 @@ main(int argc, char *argv[]) FILE **fds = {NULL}; // Shut up GCC int c; - for(int c = -1; (c = getopt(argc, argv, ":ai")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":ai-:")) != -1;) { switch(c) { @@ -37,6 +39,9 @@ main(int argc, char *argv[]) case 'i': /* ignore SIGINT */; signal(SIGINT, SIG_IGN); break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "tee: error: Missing operand for option: '-%c'\n", optopt); return 1; diff --git a/cmd/time.c b/cmd/time.c @@ -12,6 +12,8 @@ #include <sys/wait.h> // waitpid #include <unistd.h> // sysconf, fork, execvp, getopt +const char *argv0 = "time"; + enum cmd_time_mode { CMD_TIME_POSIX = 0, @@ -37,7 +39,7 @@ main(int argc, char *argv[]) return 0; } - for(int c = -1; (c = getopt(argc, argv, ":pv")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":pv-:")) != -1;) { switch(c) { @@ -47,6 +49,10 @@ main(int argc, char *argv[]) case 'v': mode = CMD_TIME_VERBOSE; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "time: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/timeout.c b/cmd/timeout.c @@ -60,7 +60,7 @@ main(int argc, char *argv[]) char *arg = NULL; - for(int c = -1; (c = getopt(argc, argv, ":fk:ps:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":fk:ps:-:")) != -1;) { switch(c) { @@ -136,6 +136,10 @@ main(int argc, char *argv[]) } } break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "timeout: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/touch.c b/cmd/touch.c @@ -19,6 +19,8 @@ #include <time.h> /* mktime */ #include <unistd.h> /* getopt, opt*, close */ +const char *argv0 = "touch"; + // [[CC]YY]MMDDhhmm[.SS] static struct timespec opt_t_parse(char *arg, const char **errstr) @@ -123,7 +125,7 @@ main(int argc, char *argv[]) int open_flags = O_WRONLY | O_CREAT | O_NOCTTY; int utimensat_flags = 0; - for(int c = -1; (c = getopt(argc, argv, ":acfhmr:t:d:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":acfhmr:t:d:-:")) != -1;) { const char *errstr = NULL; @@ -202,6 +204,9 @@ main(int argc, char *argv[]) errno = 0; break; } + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "touch: error: Missing operand for option: '-%c'\n", optopt); return 1; diff --git a/cmd/tr.c b/cmd/tr.c @@ -77,7 +77,7 @@ main(int argc, char *argv[]) int cflag, dflag, sflag; cflag = dflag = sflag = 0; - for(int c = -1; (c = getopt(argc, argv, "Ccds")) != -1;) + for(int c = -1; (c = getopt(argc, argv, "Ccds-:")) != -1;) { switch(c) { @@ -91,6 +91,10 @@ main(int argc, char *argv[]) case 's': sflag = 1; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt); usage(); diff --git a/cmd/truncate.c b/cmd/truncate.c @@ -36,7 +36,7 @@ main(int argc, char *argv[]) }; char *ref_file = NULL; - for(int c = -1; (c = getopt(argc, argv, ":cr:s:")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":cr:s:-:")) != -1;) { switch(c) { @@ -67,6 +67,10 @@ main(int argc, char *argv[]) } size_set = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "truncate: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/uname.c b/cmd/uname.c @@ -12,6 +12,8 @@ #include <sys/utsname.h> // uname #include <unistd.h> // getopt +const char *argv0 = "uname"; + enum uname_names { CMD_UNAME_SYSNAME = 1 << 1, @@ -49,7 +51,7 @@ usage(void) int main(int argc, char *argv[]) { - for(int c = -1; (c = getopt(argc, argv, ":amnprsv")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":amnprsv-:")) != -1;) { switch(c) { @@ -72,6 +74,10 @@ main(int argc, char *argv[]) case 'v': names |= CMD_UNAME_VERSION; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; case ':': fprintf(stderr, "uname: error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/cmd/uniq.c b/cmd/uniq.c @@ -30,7 +30,7 @@ main(int argc, char *argv[]) char *endptr = NULL; - for(int c = -1; (c = getopt(argc, argv, ":cdf:s:u")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":cdf:s:u-:")) != -1;) { switch(c) { @@ -94,6 +94,9 @@ main(int argc, char *argv[]) } mode = NO_REPEAT; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case ':': fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt); return 1; diff --git a/cmd/wc.c b/cmd/wc.c @@ -237,7 +237,7 @@ main(int argc, char *argv[]) // Need + as first character to get POSIX-style option parsing for(int c = -1; (c = getopt_long(argc, argv, "+:clmw", opts, NULL)) != -1;) #else - for(int c = -1; (c = getopt(argc, argv, ":clmw")) != -1;) + for(int c = -1; (c = getopt(argc, argv, ":clmw-:")) != -1;) #endif { switch(c) @@ -256,6 +256,12 @@ main(int argc, char *argv[]) case 'w': wc_opts |= WC_OPT_W; break; +#ifdef HAS_GETOPT_LONG + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + usage(); + return 1; +#endif case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage(); diff --git a/cmd/which.c b/cmd/which.c @@ -11,6 +11,8 @@ #include <string.h> // strtok #include <unistd.h> // access, getopt +const char *argv0 = "which"; + int main(int argc, char *argv[]) { @@ -24,7 +26,7 @@ main(int argc, char *argv[]) return 1; } - for(int c = -1; (c = getopt(argc, argv, "as")) != -1;) + for(int c = -1; (c = getopt(argc, argv, "as-:")) != -1;) { switch(c) { @@ -34,6 +36,9 @@ main(int argc, char *argv[]) case 's': opt_s = true; break; + case '-': + fprintf(stderr, "%s: error: Unsupported long option: '--%s'\n", argv0, optarg); + return 1; case '?': fprintf(stderr, "which: error: Unrecognised option: '-%c'\n", optopt); return 1; diff --git a/test-cmd/cat.sh b/test-cmd/cat.sh @@ -47,6 +47,6 @@ t --exit=1 enoent /var/empty/e/no/ent "cat: error: Failed opening file '/var/emp t_file doubledash "$WD/inputs/all_bytes" -- "$WD/inputs/all_bytes" -t --exit=1 tripledash "--- $WD/inputs/all_bytes" "cat: error: Unrecognised option: '--' +t --exit=1 tripledash "--- $WD/inputs/all_bytes" "cat: error: Unsupported long option: '---' Usage: cat [-u] [files ...] " diff --git a/test-cmd/tee.sh b/test-cmd/tee.sh @@ -45,5 +45,5 @@ t --exit=1 --input='foo' enoent '/var/empty/e/no/ent' 'tee: error: Failed openin t_file --infile="${WD}/inputs/all_bytes" doubledash "${WD}/inputs/all_bytes" -- -t --exit=1 --input='foo' tripledash '---' "tee: error: Unrecognised option: '--' +t --exit=1 --input='foo' tripledash '---' "tee: error: Unsupported long option: '---' "