logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 1a622d996f83e14a43b84c0cc572eaf7650961e6
parent c5bcb7d2aec67109b975fb708cdf4ae4bd1c30bd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 26 Sep 2024 09:20:02 +0200

cmd/{join,paste}: handle getopt errors

Diffstat:

Mcmd/join.c13+++++++++++--
Mcmd/paste.c12++++++++++--
2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/cmd/join.c b/cmd/join.c @@ -104,6 +104,8 @@ static void slurp(INPUT *); static wchar_t *towcs(const char *); static void usage(void); +const char *argv0 = "join"; + int main(int argc, char *argv[]) { @@ -117,7 +119,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) { @@ -185,9 +187,16 @@ main(int argc, char *argv[]) } if(*end) errx(1, "error: illegal file number -- %s", optarg); break; + case ':': + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); + usage(); + return 1; case '?': - default: + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); + return 1; + default: + abort(); } } argc -= optind; diff --git a/cmd/paste.c b/cmd/paste.c @@ -54,6 +54,7 @@ static int tr(wchar_t *); static void usage(void); static wchar_t tab[] = L"\t"; +const char *argv0 = "paste"; int main(int argc, char *argv[]) @@ -66,7 +67,7 @@ main(int argc, char *argv[]) setlocale(LC_CTYPE, ""); seq = 0; - while((ch = getopt(argc, argv, "d:sz")) != -1) + while((ch = getopt(argc, argv, ":d:sz")) != -1) switch(ch) { case 'd': @@ -87,9 +88,16 @@ main(int argc, char *argv[]) case 'z': linedelim = L'\0'; break; + case ':': + fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); + usage(); + return 1; case '?': - default: + fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); + return 1; + default: + abort(); } argc -= optind; argv += optind;