Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdlib Don't segfault when complaining about an inv...



details:   https://anonhg.NetBSD.org/src/rev/cd7d27c31a00
branches:  trunk
changeset: 487608:cd7d27c31a00
user:      nathanw <nathanw%NetBSD.org@localhost>
date:      Fri Jun 09 21:35:53 2000 +0000

description:
Don't segfault when complaining about an invalid single-character option.

When complaining about a long option which requires an argument,
print the actual option, not some pointer-garbage.

(Hint: "%c" and "%s" are different.)

diffstat:

 lib/libc/stdlib/getopt_long.c |  20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)

diffs (78 lines):

diff -r 4b653e5c9d8a -r cd7d27c31a00 lib/libc/stdlib/getopt_long.c
--- a/lib/libc/stdlib/getopt_long.c     Fri Jun 09 19:11:43 2000 +0000
+++ b/lib/libc/stdlib/getopt_long.c     Fri Jun 09 21:35:53 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getopt_long.c,v 1.5 2000/04/02 22:04:06 christos Exp $ */
+/*     $NetBSD: getopt_long.c,v 1.6 2000/06/09 21:35:53 nathanw Exp $  */
 
 /*-
  * Copyright (c) 2000 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getopt_long.c,v 1.5 2000/04/02 22:04:06 christos Exp $");
+__RCSID("$NetBSD: getopt_long.c,v 1.6 2000/06/09 21:35:53 nathanw Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -92,10 +92,12 @@
 static int nonopt_end = -1;   /* first option after non options (for permute) */
 
 /* Error messages */
-static const char recarg[] = "option requires an argument -- %c";
+static const char recargchar[] = "option requires an argument -- %c";
+static const char recargstring[] = "option requires an argument -- %s";
 static const char ambig[] = "ambiguous option -- %.*s";
 static const char noarg[] = "option doesn't take an argument -- %.*s";
-static const char illopt[] = "illegal option -- %s";
+static const char illoptchar[] = "illegal option -- %c";
+static const char illoptstring[] = "illegal option -- %s";
 
 
 extern char *__progname;
@@ -246,7 +248,7 @@
                if (!*place)
                        ++optind;
                if (PRINT_ERROR)
-                       warnx(illopt, optchar);
+                       warnx(illoptchar, optchar);
                optopt = optchar;
                return BADCH;
        }
@@ -258,7 +260,7 @@
                if (++optind >= nargc) {        /* no arg */
                        place = EMSG;
                        if (PRINT_ERROR)
-                               warnx(recarg, optchar);
+                               warnx(recargchar, optchar);
                        optopt = optchar;
                        /* XXX: GNU returns '?' if options[0] != ':' */
                        return BADARG;
@@ -282,7 +284,7 @@
                        if (++optind >= nargc) {        /* no arg */
                                place = EMSG;
                                if (PRINT_ERROR)
-                                       warnx(recarg, optchar);
+                                       warnx(recargchar, optchar);
                                optopt = optchar;
                                /* XXX: GNU returns '?' if options[0] != ':' */
                                return BADARG;
@@ -439,7 +441,7 @@
                                 * indicates no error should be generated
                                 */
                                if (PRINT_ERROR)
-                                       warnx(recarg, current_argv);
+                                       warnx(recargstring, current_argv);
                                /*
                                 * XXX: GNU sets optopt to val regardless
                                 * of flag
@@ -454,7 +456,7 @@
                        }
                } else {                        /* unknown option */
                        if (PRINT_ERROR)
-                               warnx(illopt, current_argv);
+                               warnx(illoptstring, current_argv);
                        optopt = 0;
                        return BADCH;
                }



Home | Main Index | Thread Index | Old Index