Subject: bin/21970: printf(1) breaks when format begins with "-"
To: None <gnats-bugs@gnats.netbsd.org>
From: Alan Barrett <apb@cequrux.com>
List: netbsd-bugs
Date: 06/23/2003 18:10:08
>Number:         21970
>Category:       bin
>Synopsis:       printf(1) breaks when format begins with "-"
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jun 23 16:11:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     Alan Barrett
>Release:        NetBSD 1.6T
>Organization:
not much
>Environment:
System: NetBSD 1.6T
Architecture: i386
Machine: i386
>Description:
	printf(1) is documented as not taking any options.  The
	first arg is supposed to be a format string.

	However, if the format string begins with "-" then printf(1)
	incorrectly treats it like an option.

>How-To-Repeat:
1. The following command should output "-foo<newline>".
   Instead, it produces an error.

    $ printf "-%s\n" "foo"
    sh: unknown option -- f
    usage: printf format [arg ...]

2. The following command should output "--foo<newline>".
   Instead, it produces an error.

    $ printf "--%s\n" "foo"
    sh: unknown option -- -
    usage: printf format [arg ...]

3. The following command should output "-" (without a newline),
   and it works correctly.

    $ printf "-"
    -$

4. The following command should output "--" (without a newline).
   Instead, it produces an error.

    $ printf "--"
    printf: usage: printf format [arguments]

5. The following command should treat the first arg as a format string;
   so it should output "--" (without a newline), and it should ignore
   the second and third args.  Instead, it ignores the first arg, treats
   the second arg as a format string, and interpolates the third arg as
   a parameter in the format string.

    $ printf -- "--%s\n" "foo"
    --foo

>Fix:
To make the behaviour match the documentation, remove the getopt() loop
at the top of src/bin/printf/printf.c

Alternatively, if the observed behaviour is actually the desired
behaviour, then fix the documentation to: (1) mention the special
treatment of "--"; (2) mention that the format string may not begin with
"-", unless it is preceded by a special "--" arg.

>Release-Note:
>Audit-Trail:
>Unformatted: