CVS commit: src/usr.bin/printf

2024-08-07 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed Aug  7 15:40:03 UTC 2024

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Correctly handle extracting wide chars from empty strings.

Fix a (probably would have rarely been seen) bug I installed yesterday.

It turns out that mbtowc() needs to include the terminating \0 in the
length arg passed to it, or it errors (EILSEQ) on a zero length (instead
of doing the sane thing and treating that the same as "\0" (treated as
being length 1).   So, increase the length passed to mbtowc() by 1.
That makes no difference in the typical case, it is an upper limit on
the number of bytes to examine, and mbtowc() stops after it has
converted 1 character, so in the non "" input cases, nothing that
matters changes.

The rest of this you can skip if you like, not directly related to
this change...

Note: it is not clear to me what is correct here, POSIX looks to be
ambiguous, or strange anyway; in the RETURN VALUE section it says:

   If s is not a null pointer, mbtowc() shall either return 0 (if s points
   to the null byte), or return the number of bytes [...]

Further for the error possibilities it says:

[EILSEQ]  An invalid character sequence is detected. In the POSIX locale
  an [EILSEQ] error cannot occur since all byte values are valid
  characters.

On the other hand our mbtowc(3) says:

 There are special cases:

 n == 0 In this case, the first n bytes of the array pointed to by
s never form a complete character.  Thus, the mbtowc()
always fails.

Since EILSEQ is the only defined error for mbtowc() in POSIX, and
cannot happen (according to it) in the POSIX locale, that "always fails"
in our manual page looks dubious.

What actually happens in our mbtowc() in the POSIX locale, is that if
passed n==0 (and *s == '\0') mbtowc() returns 0 (that's good) but
also sets errno to EILSEQ (not so good - though this is not one of
the functions guaranteed to not alter errno if it doesn't fail).

In other locales it returns -1 (with errno == EILSEQ) when n == 0.
(Well, in some other locales anyway, I didn't go and test all of them).

Where POSIX gets weird, is that earlier it says:

At most n bytes of the array pointed to by s shall be examined.

If n == 0, then no bytes can be examined.  In that case mbtowc()
cannot test whether s points to the null byte, even in the POSIX locale.

So it is unclear (to me) what should be returned in that case.


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.57 src/usr.bin/printf/printf.c:1.58
--- src/usr.bin/printf/printf.c:1.57	Tue Aug  6 14:55:48 2024
+++ src/usr.bin/printf/printf.c	Wed Aug  7 15:40:03 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.57 2024/08/06 14:55:48 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.58 2024/08/07 15:40:03 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.57 2024/08/06 14:55:48 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.58 2024/08/07 15:40:03 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -778,7 +778,7 @@ wide_char(const char *p, int all)
 	int n;
 
 	(void)mbtowc(NULL, NULL, 0);
-	n = mbtowc(, p + all, len = strlen(p + all));
+	n = mbtowc(, p + all, (len = strlen(p + all)) + 1);
 	if (n < 0) {
 		warn("%s", p);
 		rval = -1;



CVS commit: src/usr.bin/printf

2024-08-07 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed Aug  7 15:40:03 UTC 2024

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Correctly handle extracting wide chars from empty strings.

Fix a (probably would have rarely been seen) bug I installed yesterday.

It turns out that mbtowc() needs to include the terminating \0 in the
length arg passed to it, or it errors (EILSEQ) on a zero length (instead
of doing the sane thing and treating that the same as "\0" (treated as
being length 1).   So, increase the length passed to mbtowc() by 1.
That makes no difference in the typical case, it is an upper limit on
the number of bytes to examine, and mbtowc() stops after it has
converted 1 character, so in the non "" input cases, nothing that
matters changes.

The rest of this you can skip if you like, not directly related to
this change...

Note: it is not clear to me what is correct here, POSIX looks to be
ambiguous, or strange anyway; in the RETURN VALUE section it says:

   If s is not a null pointer, mbtowc() shall either return 0 (if s points
   to the null byte), or return the number of bytes [...]

Further for the error possibilities it says:

[EILSEQ]  An invalid character sequence is detected. In the POSIX locale
  an [EILSEQ] error cannot occur since all byte values are valid
  characters.

On the other hand our mbtowc(3) says:

 There are special cases:

 n == 0 In this case, the first n bytes of the array pointed to by
s never form a complete character.  Thus, the mbtowc()
always fails.

Since EILSEQ is the only defined error for mbtowc() in POSIX, and
cannot happen (according to it) in the POSIX locale, that "always fails"
in our manual page looks dubious.

What actually happens in our mbtowc() in the POSIX locale, is that if
passed n==0 (and *s == '\0') mbtowc() returns 0 (that's good) but
also sets errno to EILSEQ (not so good - though this is not one of
the functions guaranteed to not alter errno if it doesn't fail).

In other locales it returns -1 (with errno == EILSEQ) when n == 0.
(Well, in some other locales anyway, I didn't go and test all of them).

Where POSIX gets weird, is that earlier it says:

At most n bytes of the array pointed to by s shall be examined.

If n == 0, then no bytes can be examined.  In that case mbtowc()
cannot test whether s points to the null byte, even in the POSIX locale.

So it is unclear (to me) what should be returned in that case.


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2024-08-06 Thread Valery Ushakov
Module Name:src
Committed By:   uwe
Date:   Tue Aug  6 17:23:01 UTC 2024

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
printf(1): misc tweaks

Move the advise about getopt's "--" to where the format is disussed.
Use .Vt for types.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.39 src/usr.bin/printf/printf.1:1.40
--- src/usr.bin/printf/printf.1:1.39	Tue Aug  6 17:18:00 2024
+++ src/usr.bin/printf/printf.1	Tue Aug  6 17:23:01 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.39 2024/08/06 17:18:00 uwe Exp $
+.\"	$NetBSD: printf.1,v 1.40 2024/08/06 17:23:01 uwe Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -56,13 +56,24 @@ are converted and copied to the standard
 each of which causes printing of the next successive
 .Ar argument  .
 .Pp
+If the first character of
+.Ar format
+is a dash,
+.Ar format
+must be preceded by a word consisting of two dashes
+.Pq Sq Fl Fl
+to prevent it
+from being interpreted as an option string.
+See
+.Xr getopt 3 .
+.Pp
 The
 .Fl L
 option causes all floating point values resulting from format
 conversions to be printed using
-.Em long double
+.Vt long double
 formats, rather than the default
-.Em double .
+.Vt double .
 .Pp
 The
 .Ar arguments
@@ -424,17 +435,6 @@ precision is omitted, all characters in 
 In no case does a non-existent or small field width cause truncation of
 a field; padding takes place only if the specified field width exceeds
 the actual width.
-.Pp
-If the first character of
-.Ar format
-is a dash,
-.Ar format
-must be preceded by a word consisting of two dashes
-.Pq Sq Fl Fl
-to prevent it
-from being interpreted as an option string.
-See
-.Xr getopt 3 .
 .Sh EXIT STATUS
 .Ex -std
 .Sh SEE ALSO



CVS commit: src/usr.bin/printf

2024-08-06 Thread Valery Ushakov
Module Name:src
Committed By:   uwe
Date:   Tue Aug  6 17:23:01 UTC 2024

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
printf(1): misc tweaks

Move the advise about getopt's "--" to where the format is disussed.
Use .Vt for types.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2024-08-06 Thread Valery Ushakov
Module Name:src
Committed By:   uwe
Date:   Tue Aug  6 17:18:01 UTC 2024

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
printf(1): move \e description to its lexicographic place


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.38 src/usr.bin/printf/printf.1:1.39
--- src/usr.bin/printf/printf.1:1.38	Tue Aug  6 14:55:48 2024
+++ src/usr.bin/printf/printf.1	Tue Aug  6 17:18:00 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.38 2024/08/06 14:55:48 kre Exp $
+.\"	$NetBSD: printf.1,v 1.39 2024/08/06 17:18:00 uwe Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -92,10 +92,6 @@ Character escape sequences are in backsl
 .St -ansiC .
 The characters and their meanings are as follows:
 .Bl -tag -offset indent -width Cm
-.It Cm \ee
-Write an
-.Aq escape
-character.
 .It Cm \ea
 Write a
 .Aq bell
@@ -104,6 +100,10 @@ character.
 Write a
 .Aq backspace
 character.
+.It Cm \ee
+Write an
+.Aq escape
+character.
 .It Cm \ef
 Write a
 .Aq form-feed



CVS commit: src/usr.bin/printf

2024-08-06 Thread Valery Ushakov
Module Name:src
Committed By:   uwe
Date:   Tue Aug  6 17:18:01 UTC 2024

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
printf(1): move \e description to its lexicographic place


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2024-08-06 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Aug  6 14:55:48 UTC 2024

Modified Files:
src/usr.bin/printf: printf.1 printf.c

Log Message:
Add %C format conversion and -L option to printf(1)

%C does what everyone always thought %c should do, but doesn't,
and operates rather like the %c conversion in printf(3) (to be
more precise, like %lc).   It takes a code point integer value
in the current locale's LC_CTYPE and prints the character designated.

-L (this printf's first, and only, option) makes the floating conversions
use long double instead of double.

In the manual (printf.1) document both of those, and also be more
precise as to when things are affecting bytes, and when they're
manipulating characters (which makes no difference if LC_ALL=C).


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/printf/printf.1
cvs rdiff -u -r1.56 -r1.57 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.37 src/usr.bin/printf/printf.1:1.38
--- src/usr.bin/printf/printf.1:1.37	Mon Feb 13 23:02:27 2023
+++ src/usr.bin/printf/printf.1	Tue Aug  6 14:55:48 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.37 2023/02/13 23:02:27 andvar Exp $
+.\"	$NetBSD: printf.1,v 1.38 2024/08/06 14:55:48 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	from: @(#)printf.1	8.1 (Berkeley) 6/6/93
 .\"
-.Dd May 19, 2021
+.Dd August 6, 2024
 .Dt PRINTF 1
 .Os
 .Sh NAME
@@ -40,6 +40,7 @@
 .Nd formatted output
 .Sh SYNOPSIS
 .Nm
+.Op Fl L
 .Ar format
 .Op Ar arguments  ...
 .Sh DESCRIPTION
@@ -56,6 +57,14 @@ each of which causes printing of the nex
 .Ar argument  .
 .Pp
 The
+.Fl L
+option causes all floating point values resulting from format
+conversions to be printed using
+.Em long double
+formats, rather than the default
+.Em double .
+.Pp
+The
 .Ar arguments
 after the first are treated as strings if the corresponding format is
 either
@@ -69,8 +78,9 @@ otherwise it is evaluated as a C\~consta
 .It
 A leading plus or minus sign is allowed.
 .It
-If the leading character is a single or double quote, the value is the ASCII
+If the leading character is a single or double quote, the value is the
 code of the next character.
+No further characters are permitted.
 .El
 .Pp
 The format string is reused as often as necessary to satisfy the
@@ -154,6 +164,7 @@ character specifying that the value shou
 For
 .Cm b ,
 .Cm c ,
+.Cm C ,
 .Cm d ,
 and
 .Cm s
@@ -219,7 +230,7 @@ if both are used;
 .It Field Width :
 An optional digit string specifying a
 .Em field width ;
-if the output string has fewer characters than the field width it will
+if the output string has fewer bytes than the field width it will
 be space-padded on the left (or right, if the left-adjustment indicator
 has been given) to make up the field width (note that a leading zero
 is a flag, but an embedded zero is part of a field width);
@@ -233,7 +244,7 @@ for
 .Cm e
 and
 .Cm f
-formats, or the maximum number of characters to be printed
+formats, or the maximum number of bytes to be printed
 from a string
 .Sm off
 .Pf ( Cm b ,
@@ -245,7 +256,7 @@ formats); if the digit string is missing
 as zero;
 .It Format :
 A character which indicates the type of format to use (one of
-.Cm diouxXfFeEgGaAbBcs ) .
+.Cm diouxXfFeEgGaAbBcCs ) .
 .El
 .Pp
 A field width or precision may be
@@ -396,10 +407,16 @@ formats described above.
 The first character of
 .Ar argument
 is printed.
+.It Cm C
+The
+.Ar argument ,
+which must represent an integer constant,
+with an optional leading plus or minus sign,
+is treated as a wide character code point, and printed.
 .It Cm s
 Characters from the string
 .Ar argument
-are printed until the end is reached or until the number of characters
+are printed until the end is reached or until the number of bytes
 indicated by the precision specification is reached; if the
 precision is omitted, all characters in the string are printed.
 .El
@@ -416,6 +433,8 @@ must be preceded by a word consisting of
 .Pq Sq Fl Fl
 to prevent it
 from being interpreted as an option string.
+See
+.Xr getopt 3 .
 .Sh EXIT STATUS
 .Ex -std
 .Sh SEE ALSO
@@ -436,7 +455,9 @@ are optional in POSIX.
 .Pp
 The behaviour of the
 .Cm \&%B
-format and the
+and
+.Cm \&%C
+formats and the
 .Cm \e\(aq ,
 .Cm \e\*q ,
 .Cm \ee ,
@@ -466,7 +487,9 @@ One might expect the
 format to do likewise, but in fact it does not.
 .Pp
 To convert a string representation of a decimal, octal, or hexadecimal
-number into the corresponding character, two nested
+number into the corresponding character,
+using a portable invocation,
+two nested
 .Nm
 invocations may be used, in which the inner invocation
 converts the input to an octal string, and the outer
@@ -475,3 +498,9 @@ For example, the 

CVS commit: src/usr.bin/printf

2024-08-06 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Aug  6 14:55:48 UTC 2024

Modified Files:
src/usr.bin/printf: printf.1 printf.c

Log Message:
Add %C format conversion and -L option to printf(1)

%C does what everyone always thought %c should do, but doesn't,
and operates rather like the %c conversion in printf(3) (to be
more precise, like %lc).   It takes a code point integer value
in the current locale's LC_CTYPE and prints the character designated.

-L (this printf's first, and only, option) makes the floating conversions
use long double instead of double.

In the manual (printf.1) document both of those, and also be more
precise as to when things are affecting bytes, and when they're
manipulating characters (which makes no difference if LC_ALL=C).


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/printf/printf.1
cvs rdiff -u -r1.56 -r1.57 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2024-08-06 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Aug  6 07:48:16 UTC 2024

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
PR bin/58534 -- printf(1) source code comment fix

Update the comment near the start of main() in printf.c so it
explains what is really happening and why, rather than being a
whole bunch of incorrect BS about what posix does or doesn't require.

This changes comments only, NFC (should be no binary change at all).


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.55 src/usr.bin/printf/printf.c:1.56
--- src/usr.bin/printf/printf.c:1.55	Thu Jul 18 12:08:11 2024
+++ src/usr.bin/printf/printf.c	Tue Aug  6 07:48:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.55 2024/07/18 12:08:11 wiz Exp $	*/
+/*	$NetBSD: printf.c,v 1.56 2024/08/06 07:48:16 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.55 2024/07/18 12:08:11 wiz Exp $");
+__RCSID("$NetBSD: printf.c,v 1.56 2024/08/06 07:48:16 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -144,23 +144,33 @@ main(int argc, char *argv[])
 	rval = 0;	/* clear for builtin versions (avoid holdover) */
 	clearerr(stdout);	/* for the builtin version */
 
-	/*
-	 * printf does not comply with Posix XBD 12.2 - there are no opts,
-	 * not even the -- end of options marker.   Do not run getopt().
-	 */
 	if (argc > 2 && strchr(argv[1], '%') == NULL) {
 		int o;
 
 		/*
-		 * except that if there are multiple args and
-		 * the first (the nominal format) contains no '%'
-		 * conversions (which we will approximate as no '%'
-		 * characters at all, conversions or not) then the
-		 * results are unspecified, and we can do what we
-		 * like.   So in that case, for some backward compat
-		 * to scripts which (stupidly) do:
-		 *	printf -- format args
-		 * process this case the old way.
+		 * We only do this for argc > 2, as:
+		 *
+		 * for argc <= 1
+		 *	at best we have a bare "printf" so there cannot be
+		 *	any options, thus getopts() would be a waste of time.
+		 *	The usage() below is assured.
+		 *
+		 * for argc == 2
+		 *	There is only one arg (argv[1])	which logically must
+		 *	be intended to be the (required) format string for
+		 *	printf, without which we can do nothing	so rather
+		 *	than usage() if it happens to start with a '-' we
+		 *	just avoid getopts() and treat it as a format string.
+		 *
+		 * Then, for argc > 2, we also skip this if there is a '%'
+		 * anywhere in argv[1] as it is likely that would be intended
+		 * to be the format string, rather than options, even if it
+		 * starts with a '-' so we skip getopts() in that case as well.
+		 *
+		 * Note that this would fail should there ever be an option
+		 * which takes an arbitrary string value, which could be given
+		 * as -Oabc%def so should that ever become possible, remove
+		 * the strchr() test above.
 		 */
 
 		while ((o = getopt(argc, argv, "")) != -1) {
@@ -178,13 +188,13 @@ main(int argc, char *argv[])
 		argv += 1;
 	}
 
-	if (argc < 1) {
+	if (argc < 1) {		/* Nothing left at all? */
 		usage();
 		return 1;
 	}
 
-	format = *argv;
-	gargv = ++argv;
+	format = *argv;		/* First remaining arg is the format string */
+	gargv = ++argv;		/* remaining args are for that to consume */
 
 #define SKIP1	"#-+ 0'"
 #define SKIP2	"0123456789"



CVS commit: src/usr.bin/printf

2024-08-06 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Aug  6 07:48:16 UTC 2024

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
PR bin/58534 -- printf(1) source code comment fix

Update the comment near the start of main() in printf.c so it
explains what is really happening and why, rather than being a
whole bunch of incorrect BS about what posix does or doesn't require.

This changes comments only, NFC (should be no binary change at all).


To generate a diff of this commit:
cvs rdiff -u -r1.55 -r1.56 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2024-07-18 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Thu Jul 18 12:08:11 UTC 2024

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Fix typo in comment.

Reported by Emanuele Torre in PR 58439.


To generate a diff of this commit:
cvs rdiff -u -r1.54 -r1.55 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.54 src/usr.bin/printf/printf.c:1.55
--- src/usr.bin/printf/printf.c:1.54	Thu May 20 02:01:07 2021
+++ src/usr.bin/printf/printf.c	Thu Jul 18 12:08:11 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.54 2021/05/20 02:01:07 christos Exp $	*/
+/*	$NetBSD: printf.c,v 1.55 2024/07/18 12:08:11 wiz Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.54 2021/05/20 02:01:07 christos Exp $");
+__RCSID("$NetBSD: printf.c,v 1.55 2024/07/18 12:08:11 wiz Exp $");
 #endif
 #endif /* not lint */
 
@@ -146,7 +146,7 @@ main(int argc, char *argv[])
 
 	/*
 	 * printf does not comply with Posix XBD 12.2 - there are no opts,
-	 * not even the -- end of options marker.   Do not run getoot().
+	 * not even the -- end of options marker.   Do not run getopt().
 	 */
 	if (argc > 2 && strchr(argv[1], '%') == NULL) {
 		int o;



CVS commit: src/usr.bin/printf

2024-07-18 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Thu Jul 18 12:08:11 UTC 2024

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Fix typo in comment.

Reported by Emanuele Torre in PR 58439.


To generate a diff of this commit:
cvs rdiff -u -r1.54 -r1.55 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2021-05-19 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu May 20 02:01:07 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
fix typo


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.53 src/usr.bin/printf/printf.c:1.54
--- src/usr.bin/printf/printf.c:1.53	Wed May 19 18:41:19 2021
+++ src/usr.bin/printf/printf.c	Wed May 19 22:01:07 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.53 2021/05/19 22:41:19 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.54 2021/05/20 02:01:07 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.53 2021/05/19 22:41:19 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.54 2021/05/20 02:01:07 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -744,7 +744,7 @@ wide_char(const char *p)
 	intmax_t ch = (intmax_t)(unsigned char)p[1];
 
 	if (ch != 0 && p[2] != '\0') {
-		warnx("%s: not completelty converted", p);
+		warnx("%s: not completely converted", p);
 		rval = 1;
 	}
 



CVS commit: src/usr.bin/printf

2021-05-19 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu May 20 02:01:07 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
fix typo


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2021-05-19 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed May 19 22:41:19 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Changes for POSIX conformance.

1.  exit(1) with an error message on stderr if an I/O error occurs.
1a. To work properly when built into /bin/sh sprinkle clearerr() at
appropriate places.

2.  Verify that when a 'X data value is used with one of the numeric
conversions, that nothing follows the 'X'.   It used to be unclear
in the standard whether this was required or not, it is clear that
with numeric conversions the entire data value must be used, or an
error must result.   But with string conversions, that isn't the case
and unused parts are simply ignored.   This one is a numeric conversion
with a string value, so which applies?   The standard used to contain
an example of '+3 being converted, producing the same as '+ ignoring
the '3' with no mention of any error, so that's the approach we adopted,
The forthcoming version now explicitly states that an error would also
be generated from that case, as the '3' was not used by the numeric
conversion.

2a. We support those conversions with floating as well as integer conversions,
as the standard used to suggest that was required (but it makes no sense,
the values are always integers, printing them in a floating format is
dumb).  The standard has been revised to make it clear that only the
integer numeric conversions %d %u %x (etc) are supposed to handle the 'X
form of data value.   We still allow it with the floating formats as an
extension, for backward compat, just in case someone (other than the ATF
tests) is using it.   It might go away.

2b. These formats are sypposed to convert 'X where 'X' is a character
(perhaps multibyte encoded) in the current LC_CTYPE locale category.
We don't handle that, only 1 byte characters are handled currently.
However the framework is now there to allow code to (one hopes, easily)
be added to handle multi-byte locales.   (Note that for the purposes of
#2 above, 'X' must be a single character, not a single byte.)


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2021-05-19 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed May 19 22:41:19 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Changes for POSIX conformance.

1.  exit(1) with an error message on stderr if an I/O error occurs.
1a. To work properly when built into /bin/sh sprinkle clearerr() at
appropriate places.

2.  Verify that when a 'X data value is used with one of the numeric
conversions, that nothing follows the 'X'.   It used to be unclear
in the standard whether this was required or not, it is clear that
with numeric conversions the entire data value must be used, or an
error must result.   But with string conversions, that isn't the case
and unused parts are simply ignored.   This one is a numeric conversion
with a string value, so which applies?   The standard used to contain
an example of '+3 being converted, producing the same as '+ ignoring
the '3' with no mention of any error, so that's the approach we adopted,
The forthcoming version now explicitly states that an error would also
be generated from that case, as the '3' was not used by the numeric
conversion.

2a. We support those conversions with floating as well as integer conversions,
as the standard used to suggest that was required (but it makes no sense,
the values are always integers, printing them in a floating format is
dumb).  The standard has been revised to make it clear that only the
integer numeric conversions %d %u %x (etc) are supposed to handle the 'X
form of data value.   We still allow it with the floating formats as an
extension, for backward compat, just in case someone (other than the ATF
tests) is using it.   It might go away.

2b. These formats are sypposed to convert 'X where 'X' is a character
(perhaps multibyte encoded) in the current LC_CTYPE locale category.
We don't handle that, only 1 byte characters are handled currently.
However the framework is now there to allow code to (one hopes, easily)
be added to handle multi-byte locales.   (Note that for the purposes of
#2 above, 'X' must be a single character, not a single byte.)


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.52 src/usr.bin/printf/printf.c:1.53
--- src/usr.bin/printf/printf.c:1.52	Fri Apr 16 18:31:28 2021
+++ src/usr.bin/printf/printf.c	Wed May 19 22:41:19 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.52 2021/04/16 18:31:28 christos Exp $	*/
+/*	$NetBSD: printf.c,v 1.53 2021/05/19 22:41:19 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.52 2021/04/16 18:31:28 christos Exp $");
+__RCSID("$NetBSD: printf.c,v 1.53 2021/05/19 22:41:19 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -74,6 +74,7 @@ static int	 getwidth(void);
 static intmax_t	 getintmax(void);
 static char	*getstr(void);
 static char	*mklong(const char *, char);
+static intmax_t	 wide_char(const char *);
 static void  check_conversion(const char *, const char *);
 static void	 usage(void);
 
@@ -141,6 +142,7 @@ main(int argc, char *argv[])
 #endif
 
 	rval = 0;	/* clear for builtin versions (avoid holdover) */
+	clearerr(stdout);	/* for the builtin version */
 
 	/*
 	 * printf does not comply with Posix XBD 12.2 - there are no opts,
@@ -372,10 +374,16 @@ main(int argc, char *argv[])
 			*fmt = nextch;
 			/* escape if a \c was encountered */
 			if (rval & 0x100)
-return rval & ~0x100;
+goto done;
 		}
 	} while (gargv != argv && *gargv);
 
+  done:
+	(void)fflush(stdout);
+	if (ferror(stdout)) {
+		clearerr(stdout);
+		err(1, "write error");
+	}
 	return rval & ~0x100;
   out:
 	warn("print failed");
@@ -628,8 +636,9 @@ mklong(const char *str, char ch)
 
 	len = strlen(str) + 2;
 	if (len > sizeof copy) {
-		warnx("format %s too complex", str);
+		warnx("format \"%s\" too complex", str);
 		len = 4;
+		rval = 1;
 	}
 	(void)memmove(copy, str, len - 3);
 	copy[len - 3] = 'j';
@@ -691,7 +700,7 @@ getintmax(void)
 	gargv++;
 
 	if (*cp == '\"' || *cp == '\'')
-		return *(cp + 1);
+		return wide_char(cp);
 
 	errno = 0;
 	val = strtoimax(cp, , 0);
@@ -708,8 +717,9 @@ getdouble(void)
 	if (!*gargv)
 		return 0.0;
 
-	if (**gargv == '\"' || **gargv == '\'')
-		return (double) *((*gargv++)+1);
+	/* This is a NetBSD extension, not required by POSIX (it is useless) */
+	if (*(ep = *gargv) == '\"' || *ep == '\'')
+		return (double)wide_char(ep);
 
 	errno = 0;
 	val = strtod(*gargv, );
@@ -717,6 +727,30 @@ getdouble(void)
 	return val;
 }
 
+/*
+ * XXX This is just a placeholder for a later version which
+ * will do mbtowc() on p+1 (and after 

CVS commit: src/usr.bin/printf

2021-05-19 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed May 19 22:29:18 UTC 2021

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Don't describe "%%: in the format as a format conversion, it isn't,
it is an escaped literal '%' character.   None of what applies to
format conversions applies to this.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.35 src/usr.bin/printf/printf.1:1.36
--- src/usr.bin/printf/printf.1:1.35	Mon Jun 29 22:50:11 2020
+++ src/usr.bin/printf/printf.1	Wed May 19 22:29:18 2021
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.35 2020/06/29 22:50:11 uwe Exp $
+.\"	$NetBSD: printf.1,v 1.36 2021/05/19 22:29:18 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	from: @(#)printf.1	8.1 (Berkeley) 6/6/93
 .\"
-.Dd August 31, 2018
+.Dd May 19, 2021
 .Dt PRINTF 1
 .Os
 .Sh NAME
@@ -136,6 +136,11 @@ value is the 1- or 2-digit hexadecimal n
 .Pp
 Each format specification is introduced by the percent character
 .Pq Ql \&% .
+To produce a literal percent
+.Pq Ql \&% 
+in the output, write the percent chracter twice:
+.Pq Ql \&%% .
+This is not a format conversion.
 The remainder of the format specification includes,
 in the following order:
 .Bl -tag -width 5n
@@ -397,10 +402,6 @@ Characters from the string
 are printed until the end is reached or until the number of characters
 indicated by the precision specification is reached; if the
 precision is omitted, all characters in the string are printed.
-.It Cm \&%
-Print a literal percent character
-.Pq Ql % ;
-no argument is used.
 .El
 .Pp
 In no case does a non-existent or small field width cause truncation of



CVS commit: src/usr.bin/printf

2021-05-19 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed May 19 22:29:18 UTC 2021

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Don't describe "%%: in the format as a format conversion, it isn't,
it is an escaped literal '%' character.   None of what applies to
format conversions applies to this.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2021-04-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Apr 16 18:31:28 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
make value an int to avoid all the casts and conversion warnings.


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.51 src/usr.bin/printf/printf.c:1.52
--- src/usr.bin/printf/printf.c:1.51	Fri Apr 16 11:10:18 2021
+++ src/usr.bin/printf/printf.c	Fri Apr 16 14:31:28 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.51 2021/04/16 15:10:18 christos Exp $	*/
+/*	$NetBSD: printf.c,v 1.52 2021/04/16 18:31:28 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.51 2021/04/16 15:10:18 christos Exp $");
+__RCSID("$NetBSD: printf.c,v 1.52 2021/04/16 18:31:28 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -119,9 +119,9 @@ static char  **gargv;
 }
 
 #define isodigit(c)	((c) >= '0' && (c) <= '7')
-#define octtobin(c)	(char)((c) - '0')
-#define check(c, a)	(c) >= (a) && (c) <= (a) + 5 ? (char)((c) - (a) + 10)
-#define hextobin(c)	(check(c, 'a') : check(c, 'A') : (char)((c) - '0'))
+#define octtobin(c)	((c) - '0')
+#define check(c, a)	(c) >= (a) && (c) <= (a) + 5 ? (c) - (a) + 10
+#define hextobin(c)	(check(c, 'a') : check(c, 'A') : (c) - '0')
 #ifdef main
 int main(int, char *[]);
 #endif
@@ -486,7 +486,7 @@ conv_escape_str(char *str, void (*do_put
 static char *
 conv_escape(char *str, char *conv_ch, int quiet)
 {
-	char value = 0;
+	int value = 0;
 	char ch, *begin;
 	int c;
 
@@ -520,8 +520,7 @@ conv_escape(char *str, char *conv_ch, in
 		begin = str;
 		for (c = 2; c-- && isxdigit((unsigned char)*str); str++) {
 			value <<= 4;
-			const char d = hextobin(*str);
-			value += d;
+			value += hextobin(*str);
 		}
 		if (str == begin) {
 			if (!quiet)
@@ -552,7 +551,7 @@ conv_escape(char *str, char *conv_ch, in
 		break;
 	}
 
-	*conv_ch = value;
+	*conv_ch = (char)value;
 	return str;
 }
 



CVS commit: src/usr.bin/printf

2021-04-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Apr 16 18:31:28 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
make value an int to avoid all the casts and conversion warnings.


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2021-04-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Apr 16 15:10:18 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Change octal and hex parsing to not use strtoul so that they don't handle
'-'. From Martijn van Duren.
Also add a warning if the conversion fails (like the gnu printf does)


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2021-04-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Apr 16 15:10:18 UTC 2021

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Change octal and hex parsing to not use strtoul so that they don't handle
'-'. From Martijn van Duren.
Also add a warning if the conversion fails (like the gnu printf does)


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.50 src/usr.bin/printf/printf.c:1.51
--- src/usr.bin/printf/printf.c:1.50	Mon Jul 22 13:34:31 2019
+++ src/usr.bin/printf/printf.c	Fri Apr 16 11:10:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.50 2019/07/22 17:34:31 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.51 2021/04/16 15:10:18 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.50 2019/07/22 17:34:31 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.51 2021/04/16 15:10:18 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -118,6 +118,10 @@ static char  **gargv;
 		error = asprintf(cpp, f, func); \
 }
 
+#define isodigit(c)	((c) >= '0' && (c) <= '7')
+#define octtobin(c)	(char)((c) - '0')
+#define check(c, a)	(c) >= (a) && (c) <= (a) + 5 ? (char)((c) - (a) + 10)
+#define hextobin(c)	(check(c, 'a') : check(c, 'A') : (char)((c) - '0'))
 #ifdef main
 int main(int, char *[]);
 #endif
@@ -482,9 +486,9 @@ conv_escape_str(char *str, void (*do_put
 static char *
 conv_escape(char *str, char *conv_ch, int quiet)
 {
-	char value;
-	char ch;
-	char num_buf[4], *num_end;
+	char value = 0;
+	char ch, *begin;
+	int c;
 
 	ch = *str++;
 
@@ -499,13 +503,11 @@ conv_escape(char *str, char *conv_ch, in
 
 	case '0': case '1': case '2': case '3':
 	case '4': case '5': case '6': case '7':
-		num_buf[0] = ch;
-		ch = str[0];
-		num_buf[1] = ch;
-		num_buf[2] = (char)(ch != '\0' ? str[1] : '\0');
-		num_buf[3] = '\0';
-		value = (char)strtoul(num_buf, _end, 8);
-		str += num_end  - (num_buf + 1);
+		str--;
+		for (c = 3; c-- && isodigit(*str); str++) {
+			value <<= 3;
+			value += octtobin(*str);
+		}
 		break;
 
 	case 'x':
@@ -515,12 +517,18 @@ conv_escape(char *str, char *conv_ch, in
 		 * way to detect the end of the constant.
 		 * Supporting 2 byte constants is a compromise.
 		 */
-		ch = str[0];
-		num_buf[0] = ch;
-		num_buf[1] = (char)(ch != '\0' ? str[1] : '\0');
-		num_buf[2] = '\0';
-		value = (char)strtoul(num_buf, _end, 16);
-		str += num_end - num_buf;
+		begin = str;
+		for (c = 2; c-- && isxdigit((unsigned char)*str); str++) {
+			value <<= 4;
+			const char d = hextobin(*str);
+			value += d;
+		}
+		if (str == begin) {
+			if (!quiet)
+warnx("\\x%s: missing hexadecimal number "
+"in escape", begin);
+			rval = 1;
+		}
 		break;
 
 	case '\\':	value = '\\';	break;	/* backslash */



CVS commit: src/usr.bin/printf

2020-06-29 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Mon Jun 29 22:50:11 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Add quotes around command substitution in the example
so that it works regardless of IFS and buts.
Requested by kre@


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.34 src/usr.bin/printf/printf.1:1.35
--- src/usr.bin/printf/printf.1:1.34	Fri Jun 26 22:05:05 2020
+++ src/usr.bin/printf/printf.1	Mon Jun 29 22:50:11 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.34 2020/06/26 22:05:05 uwe Exp $
+.\"	$NetBSD: printf.1,v 1.35 2020/06/29 22:50:11 uwe Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -473,4 +473,4 @@ invocation uses the octal string as part
 For example, the following command outputs the character whose code
 is 0x0a, which is a newline in ASCII:
 .Pp
-.Dl printf $(printf \(aq\e\e%o\(aq 0x0a)
+.Dl printf \*q$(printf \(aq\e\e%o\(aq 0x0a)\*q



CVS commit: src/usr.bin/printf

2020-06-29 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Mon Jun 29 22:50:11 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Add quotes around command substitution in the example
so that it works regardless of IFS and buts.
Requested by kre@


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



Re: CVS commit: src/usr.bin/printf

2020-06-29 Thread Robert Elz
Date:Sun, 28 Jun 2020 22:58:52 +0300
From:Valery Ushakov 
Message-ID:  <20200628195852.gc20...@pony.stderr.spb.ru>

  | but I'd expect people that actually use IFS with octal digits
  | or a backslash to also understand and know how to add necessary
  | quoting in that case.

You'd think so, right, but ...   and that assumes that the person
writing the printf code (copying the example from the man page) knows
what IFS is, or that it might have been altered.

  | Though perhaps it makes sense to them back for
  | pedagogical purposes.

That, and because it simply is the right way to code in sh 99% of the
time - omitting needed quotes is a major source of bizarre bugs.   When
they're like this, and only trigger in unusual circumstances, they're ever
harder to find.

kre



Re: CVS commit: src/usr.bin/printf

2020-06-28 Thread Valery Ushakov
On Sun, Jun 28, 2020 at 21:52:23 +0700, Robert Elz wrote:

> Date:Fri, 26 Jun 2020 22:05:05 +
> From:"Valeriy E. Ushakov" 
> Message-ID:  <20200626220505.e9030f...@cvs.netbsd.org>
> 
>   | Modified Files:
>   |   src/usr.bin/printf: printf.1
>   |
>   | Log Message:
>   | Drop redundant quoting in the nested printf example.
> 
> I don't think that is correct, the quotes around 0x0A were obviously
> redundant (and changing it to 0x0a doesn't alter that), but those
> around the $(printf ...) are not - without those the result from the
> command substitution would be subject to field splitting, and if IFS
> happens to contain a \ or an octal digit, bad things will happen.
> 
> In general it is never a good idea to omit quotes around sh word
> expansions except in the cases where you actually want field splitting
> (or pathname expansion, etc) to happen (and sometimes except in the
> contexts where those don't occur anyway, like x=$whatever)
> 
> For the example in question, try running it with IFS=1 or IFS=2
> to see what I mean.

Right, but I'd expect people that actually use IFS with octal digits
or a backslash to also understand and know how to add necessary
quoting in that case.  Though perhaps it makes sense to them back for
pedagogical purposes.

-uwe


Re: CVS commit: src/usr.bin/printf

2020-06-28 Thread Robert Elz
Date:Fri, 26 Jun 2020 22:05:05 +
From:"Valeriy E. Ushakov" 
Message-ID:  <20200626220505.e9030f...@cvs.netbsd.org>

  | Modified Files:
  | src/usr.bin/printf: printf.1
  |
  | Log Message:
  | Drop redundant quoting in the nested printf example.

I don't think that is correct, the quotes around 0x0A were obviously
redundant (and changing it to 0x0a doesn't alter that), but those
around the $(printf ...) are not - without those the result from the
command substitution would be subject to field splitting, and if IFS
happens to contain a \ or an octal digit, bad things will happen.

In general it is never a good idea to omit quotes around sh word
expansions except in the cases where you actually want field splitting
(or pathname expansion, etc) to happen (and sometimes except in the
contexts where those don't occur anyway, like x=$whatever)

For the example in question, try running it with IFS=1 or IFS=2
to see what I mean.

kre



CVS commit: src/usr.bin/printf

2020-06-26 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Fri Jun 26 22:05:05 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Drop redundant quoting in the nested printf example.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2020-06-26 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Fri Jun 26 22:05:05 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Drop redundant quoting in the nested printf example.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.33 src/usr.bin/printf/printf.1:1.34
--- src/usr.bin/printf/printf.1:1.33	Fri Jun 26 21:03:55 2020
+++ src/usr.bin/printf/printf.1	Fri Jun 26 22:05:05 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.33 2020/06/26 21:03:55 uwe Exp $
+.\"	$NetBSD: printf.1,v 1.34 2020/06/26 22:05:05 uwe Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -471,6 +471,6 @@ invocations may be used, in which the in
 converts the input to an octal string, and the outer
 invocation uses the octal string as part of a format.
 For example, the following command outputs the character whose code
-is 0x0A, which is a newline in ASCII:
+is 0x0a, which is a newline in ASCII:
 .Pp
-.Dl printf \&"$(printf \&"\e\e%o" \&"0x0A")"
+.Dl printf $(printf \(aq\e\e%o\(aq 0x0a)



CVS commit: src/usr.bin/printf

2020-06-26 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Fri Jun 26 21:03:56 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Use ASCII single quote.


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2020-06-26 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Fri Jun 26 21:03:56 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Use ASCII single quote.


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.32 src/usr.bin/printf/printf.1:1.33
--- src/usr.bin/printf/printf.1:1.32	Fri Jun 26 20:16:55 2020
+++ src/usr.bin/printf/printf.1	Fri Jun 26 21:03:55 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.32 2020/06/26 20:16:55 uwe Exp $
+.\"	$NetBSD: printf.1,v 1.33 2020/06/26 21:03:55 uwe Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -114,7 +114,7 @@ character.
 Write a
 .Aq vertical tab
 character.
-.It Cm \e\'
+.It Cm \e\(aq
 Write a
 .Aq single quote
 character.
@@ -436,7 +436,7 @@ are optional in POSIX.
 The behaviour of the
 .Cm \&%B
 format and the
-.Cm \e' ,
+.Cm \e\(aq ,
 .Cm \e\*q ,
 .Cm \ee ,
 .Cm \e Ns Ar num ,



CVS commit: src/usr.bin/printf

2020-06-26 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Fri Jun 26 20:16:56 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Try to improve markup to get a nicer PostScript output.

While here edit slightly for consistency: get the octal character
ranges in order, use \num to refer to octal everywhere, etc.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.31 src/usr.bin/printf/printf.1:1.32
--- src/usr.bin/printf/printf.1:1.31	Fri Aug 31 17:27:35 2018
+++ src/usr.bin/printf/printf.1	Fri Jun 26 20:16:55 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.31 2018/08/31 17:27:35 kre Exp $
+.\"	$NetBSD: printf.1,v 1.32 2020/06/26 20:16:55 uwe Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -64,9 +64,8 @@ either
 .Cm c ,
 or
 .Cm s ;
-otherwise it is evaluated as a C constant, with the following extensions:
-.Pp
-.Bl -bullet -offset indent -compact
+otherwise it is evaluated as a C\~constant, with the following extensions:
+.Bl -bullet -offset indent
 .It
 A leading plus or minus sign is allowed.
 .It
@@ -82,7 +81,7 @@ string.
 Character escape sequences are in backslash notation as defined in
 .St -ansiC .
 The characters and their meanings are as follows:
-.Bl -tag -width Ds -offset indent
+.Bl -tag -offset indent -width Cm
 .It Cm \ee
 Write an
 .Aq escape
@@ -119,32 +118,32 @@ character.
 Write a
 .Aq single quote
 character.
-.It Cm \e"
+.It Cm \e\*q
 Write a
 .Aq double quote
 character.
 .It Cm \e\e
 Write a backslash character.
 .It Cm \e Ns Ar num
-Write an 8\-bit character whose ASCII
-value is the 1\-, 2\-, or 3\-digit octal number
+Write an 8-bit character whose ASCII
+value is the 1-, 2-, or 3-digit octal number
 .Ar num .
 .It Cm \ex Ns Ar xx
-Write an 8\-bit character whose ASCII
-value is the 1\- or 2\-digit hexadecimal number
+Write an 8-bit character whose ASCII
+value is the 1- or 2-digit hexadecimal number
 .Ar xx .
 .El
 .Pp
 Each format specification is introduced by the percent character
-.Pq Dq \&% .
+.Pq Ql \&% .
 The remainder of the format specification includes,
 in the following order:
-.Bl -tag -width Ds
+.Bl -tag -width 5n
 .It Zero or more of the following flags :
-.Bl -tag -width Ds
+.Bl -tag -width Cm
 .It Cm #
 A
-.Sq #
+.Ql 

CVS commit: src/usr.bin/printf

2020-06-26 Thread Valeriy E. Ushakov
Module Name:src
Committed By:   uwe
Date:   Fri Jun 26 20:16:56 UTC 2020

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Try to improve markup to get a nicer PostScript output.

While here edit slightly for consistency: get the octal character
ranges in order, use \num to refer to octal everywhere, etc.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



Re: CVS commit: src/usr.bin/printf

2019-07-22 Thread Tobias Nygren
On Tue, 23 Jul 2019 00:31:32 +0700
Robert Elz  wrote:

> Date:Mon, 22 Jul 2019 12:23:35 +0200
> From:Tobias Nygren 
> Message-ID:  <20190722122335.55c0d421a18a082c8d67b...@netbsd.org>
> 
>   | Please exercise caution when changing established userland behaviour.
>   | This change broke the pkgsrc/lang/openjdk8(1) build.
> 
> I stand behind my previous reply, if that change seemed to break
> anything, then all it was really doing was revealing previous brokenness.
> 
> However, as well as saying that printf(1) has no options, and (by omission)
> also saying that it should not do getopt processing, POSIX also says
> that if the format arg (ie: the first arg) contains no % cpnversions,
> and yet there are more args (which more or less by defnition can never
> be used) then the results are unspecified.
> 
> If what is, I am guessing, the "established userland behaviour" was
>   printf -- format args
> which is truly pointless  (and incoorrect), but used to be accepted,
> then we can continue to allow that to work, relying upon that
> unspecified results clause from POSIX, "--" contains no % conversion,
> yet there are more args that nothing would normally ever consume
> (technically that format string is supposed to be used over and over
> again until the args are all used up ... but obviously that would never
> happen in this case - which is why it leads to unspecified results).
> 
> I intend to commit a change to printf in a few minutes to make it work
> like this (with the caveat that the "no % conversions" test will be "no
> % characters" which guarantees no % conversions of course, but is
> simpler).
> 
> If there is only one arg, it will always be treated as a format, so
>   printf ---\\n
> will continue working as it should, or if the arg contains a % char,
> then it will also be treated as a format, so
>   printf -%d\\n 3
> will print -3 as it should (neither of those wworked until recently).
> 
> This doesn't mean that whatever script in the openjdk8 build was using
> printf incorrectly shouldn't be fixed ... it should still be.
> 
> kre

Hi,

This sounds like a reasonable compromise in the interest of preserving
compatibility with other implementations. Thanks!

-Tobias


Re: CVS commit: src/usr.bin/printf

2019-07-22 Thread Robert Elz
Date:Mon, 22 Jul 2019 12:23:35 +0200
From:Tobias Nygren 
Message-ID:  <20190722122335.55c0d421a18a082c8d67b...@netbsd.org>

  | Please exercise caution when changing established userland behaviour.
  | This change broke the pkgsrc/lang/openjdk8(1) build.

I stand behind my previous reply, if that change seemed to break
anything, then all it was really doing was revealing previous brokenness.

However, as well as saying that printf(1) has no options, and (by omission)
also saying that it should not do getopt processing, POSIX also says
that if the format arg (ie: the first arg) contains no % cpnversions,
and yet there are more args (which more or less by defnition can never
be used) then the results are unspecified.

If what is, I am guessing, the "established userland behaviour" was
printf -- format args
which is truly pointless  (and incoorrect), but used to be accepted,
then we can continue to allow that to work, relying upon that
unspecified results clause from POSIX, "--" contains no % conversion,
yet there are more args that nothing would normally ever consume
(technically that format string is supposed to be used over and over
again until the args are all used up ... but obviously that would never
happen in this case - which is why it leads to unspecified results).

I intend to commit a change to printf in a few minutes to make it work
like this (with the caveat that the "no % conversions" test will be "no
% characters" which guarantees no % conversions of course, but is
simpler).

If there is only one arg, it will always be treated as a format, so
printf ---\\n
will continue working as it should, or if the arg contains a % char,
then it will also be treated as a format, so
printf -%d\\n 3
will print -3 as it should (neither of those wworked until recently).

This doesn't mean that whatever script in the openjdk8 build was using
printf incorrectly shouldn't be fixed ... it should still be.

kre



CVS commit: src/usr.bin/printf

2019-07-22 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Mon Jul 22 17:34:31 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Amend the previous change: we can have (almost) the best of both
worlds, as when the first arg (which should be the format) contains
no % conversions, and there are more args, the results are unspecified
(according to POSIX).

We can use this so the previous usage
printf -- format arg...
(which is stupid, and pointless, but used to work) continues to
simply ignore the -- (unspecified results mean we can do whatever
feels good...)

This brings back the #if 0'd block from the previous modification
(so there is no longer anything that needs cleaning up later) but runs
the getopt() loop it contained only when there are at least 2 args
(so any 1 arg printf always uses that arg as the format string,
whatever it contains, including just "--") and also only when the
first (format) arg contains no '%' characters (which guarantees no %
conversions without needing to actually parse the arg).  This is the
(or a) "unspecified results" case from POSIX, so we are free to do
anything we like - including assuming that we might have options
(we don't) and pretending to process them.


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.49 src/usr.bin/printf/printf.c:1.50
--- src/usr.bin/printf/printf.c:1.49	Sun Jul 21 15:25:39 2019
+++ src/usr.bin/printf/printf.c	Mon Jul 22 17:34:31 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.49 2019/07/21 15:25:39 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.50 2019/07/22 17:34:31 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.49 2019/07/21 15:25:39 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.50 2019/07/22 17:34:31 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -138,27 +138,39 @@ main(int argc, char *argv[])
 
 	rval = 0;	/* clear for builtin versions (avoid holdover) */
 
-#if 0
-	int o;
-
 	/*
 	 * printf does not comply with Posix XBD 12.2 - there are no opts,
 	 * not even the -- end of options marker.   Do not run getoot().
 	 */
-	while ((o = getopt(argc, argv, "")) != -1) {
-		switch (o) {
-		case '?':
-		default:
-			usage();
-			return 1;
+	if (argc > 2 && strchr(argv[1], '%') == NULL) {
+		int o;
+
+		/*
+		 * except that if there are multiple args and
+		 * the first (the nominal format) contains no '%'
+		 * conversions (which we will approximate as no '%'
+		 * characters at all, conversions or not) then the
+		 * results are unspecified, and we can do what we
+		 * like.   So in that case, for some backward compat
+		 * to scripts which (stupidly) do:
+		 *	printf -- format args
+		 * process this case the old way.
+		 */
+
+		while ((o = getopt(argc, argv, "")) != -1) {
+			switch (o) {
+			case '?':
+			default:
+usage();
+return 1;
+			}
 		}
+		argc -= optind;
+		argv += optind;
+	} else {
+		argc -= 1;	/* drop argv[0] (the program name) */
+		argv += 1;
 	}
-	argc -= optind;
-	argv += optind;
-#else
-	argc -= 1;
-	argv += 1;
-#endif
 
 	if (argc < 1) {
 		usage();



CVS commit: src/usr.bin/printf

2019-07-22 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Mon Jul 22 17:34:31 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Amend the previous change: we can have (almost) the best of both
worlds, as when the first arg (which should be the format) contains
no % conversions, and there are more args, the results are unspecified
(according to POSIX).

We can use this so the previous usage
printf -- format arg...
(which is stupid, and pointless, but used to work) continues to
simply ignore the -- (unspecified results mean we can do whatever
feels good...)

This brings back the #if 0'd block from the previous modification
(so there is no longer anything that needs cleaning up later) but runs
the getopt() loop it contained only when there are at least 2 args
(so any 1 arg printf always uses that arg as the format string,
whatever it contains, including just "--") and also only when the
first (format) arg contains no '%' characters (which guarantees no %
conversions without needing to actually parse the arg).  This is the
(or a) "unspecified results" case from POSIX, so we are free to do
anything we like - including assuming that we might have options
(we don't) and pretending to process them.


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



Re: CVS commit: src/usr.bin/printf

2019-07-22 Thread Robert Elz
Date:Mon, 22 Jul 2019 12:23:35 +0200
From:Tobias Nygren 
Message-ID:  <20190722122335.55c0d421a18a082c8d67b...@netbsd.org>

  | This change broke the pkgsrc/lang/openjdk8(1) build.

That it is broken and ought to be patched upstream, it is quite
clear that printf is required to take no options at all - there was
a bug report about it not correctly handling "printf ---" (actually with
a newline appended, but that is irrelevant) which it was correct to fix.

Sometimes stuff that has relied upon bugs simply breaks when the bugs
get fixed.

kre




Re: CVS commit: src/usr.bin/printf

2019-07-22 Thread Tobias Nygren
On Sun, 21 Jul 2019 15:25:39 +
Robert Elz  wrote:

> Module Name:  src
> Committed By: kre
> Date: Sun Jul 21 15:25:39 UTC 2019
> 
> Modified Files:
>   src/usr.bin/printf: printf.c
> 
> Log Message:
> Stop assuming that printf handles options in any way at all
> (it doesn't - that is, shouldn't) which includes processing -- as an
> "end of options".  The first arg is (always) the format string.
> 
> Remove call to getopt() (but still do associated changes to argc/argv)
> 
> Note: for now this is #if 0's out instead of being deleted, the old
> code should be fully removed sometime soon.
> 
> Problem pointed out on tech-userlevel by Thierry Laronde.

Hi!

Please exercise caution when changing established userland behaviour.
This change broke the pkgsrc/lang/openjdk8(1) build. We can patch it of
course but there may be other software out there that expects the old
behaviour.

(1) 
https://hg.openjdk.java.net/jdk8u/jdk8u/file/bb03d4e1088a/make/common/MakeBase.gmk#l59

Kind regards,
-Tobias


CVS commit: src/usr.bin/printf

2019-07-21 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sun Jul 21 15:25:39 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Stop assuming that printf handles options in any way at all
(it doesn't - that is, shouldn't) which includes processing -- as an
"end of options".  The first arg is (always) the format string.

Remove call to getopt() (but still do associated changes to argc/argv)

Note: for now this is #if 0's out instead of being deleted, the old
code should be fully removed sometime soon.

Problem pointed out on tech-userlevel by Thierry Laronde.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2019-07-21 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sun Jul 21 15:25:39 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Stop assuming that printf handles options in any way at all
(it doesn't - that is, shouldn't) which includes processing -- as an
"end of options".  The first arg is (always) the format string.

Remove call to getopt() (but still do associated changes to argc/argv)

Note: for now this is #if 0's out instead of being deleted, the old
code should be fully removed sometime soon.

Problem pointed out on tech-userlevel by Thierry Laronde.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.48 src/usr.bin/printf/printf.c:1.49
--- src/usr.bin/printf/printf.c:1.48	Sun Jan 27 12:03:09 2019
+++ src/usr.bin/printf/printf.c	Sun Jul 21 15:25:39 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.48 2019/01/27 12:03:09 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.49 2019/07/21 15:25:39 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.48 2019/01/27 12:03:09 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.49 2019/07/21 15:25:39 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -130,7 +130,7 @@ main(int argc, char *argv[])
 	char nextch;
 	char *format;
 	char ch;
-	int error, o;
+	int error;
 
 #if !defined(SHELL) && !defined(BUILTIN)
 	(void)setlocale (LC_ALL, "");
@@ -138,6 +138,13 @@ main(int argc, char *argv[])
 
 	rval = 0;	/* clear for builtin versions (avoid holdover) */
 
+#if 0
+	int o;
+
+	/*
+	 * printf does not comply with Posix XBD 12.2 - there are no opts,
+	 * not even the -- end of options marker.   Do not run getoot().
+	 */
 	while ((o = getopt(argc, argv, "")) != -1) {
 		switch (o) {
 		case '?':
@@ -148,6 +155,10 @@ main(int argc, char *argv[])
 	}
 	argc -= optind;
 	argv += optind;
+#else
+	argc -= 1;
+	argv += 1;
+#endif
 
 	if (argc < 1) {
 		usage();



Re: CVS commit: src/usr.bin/printf

2019-01-27 Thread Robert Elz
Actually, I suspect now that what I remember reading was the rationale in the
page in XCU section 4 for the printf command.

kre



Re: CVS commit: src/usr.bin/printf

2019-01-27 Thread Robert Elz
Date:Sun, 27 Jan 2019 18:42:22 +0300
From:Valery Ushakov 
Message-ID:  <20190127154222.gg18...@pony.stderr.spb.ru>

  | This was changed in Issue 7.  The link to the Austin group
  | interpretations repository seems to be broken, though.  Where can I
  | find the text for the rationale behind this?

Sorry, I cannot find it again now - though I read it just a few hours ago (the
bug report with the text in it that was adopted)...  their search tool for this
stuff is worse than pathetic (amongst other things it leads to multiple
submissions of the same issue, and even resolutions of issues that contradict
the resolution of the same issue when raised earlier - because no-one can
ever find anything they're actually looking for.)

I will look again tomorrow, but it is more likely to appear when I am looking
for something entirely unrelated.

kre



Re: CVS commit: src/usr.bin/printf

2019-01-27 Thread Valery Ushakov
On Sun, Jan 27, 2019 at 12:03:09 +, Robert Elz wrote:

> Modified Files:
>   src/usr.bin/printf: printf.c
> 
> Log Message:
> Revert previous, it was based upon a misreading of the POSIX
> spec.   POSIX requires "as if by calling strtod()" which we
> did already ... by calling strtod().   Go back to doing that.

This was changed in Issue 7.  The link to the Austin group
interpretations repository seems to be broken, though.  Where can I
find the text for the rationale behind this?

-uwe


CVS commit: src/usr.bin/printf

2019-01-27 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sun Jan 27 12:03:09 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Revert previous, it was based upon a misreading of the POSIX
spec.   POSIX requires "as if by calling strtod()" which we
did already ... by calling strtod().   Go back to doing that.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2019-01-27 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sun Jan 27 12:03:09 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Revert previous, it was based upon a misreading of the POSIX
spec.   POSIX requires "as if by calling strtod()" which we
did already ... by calling strtod().   Go back to doing that.


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.47 src/usr.bin/printf/printf.c:1.48
--- src/usr.bin/printf/printf.c:1.47	Sat Jan 26 15:22:54 2019
+++ src/usr.bin/printf/printf.c	Sun Jan 27 12:03:09 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.47 2019/01/26 15:22:54 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.48 2019/01/27 12:03:09 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.47 2019/01/26 15:22:54 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.48 2019/01/27 12:03:09 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -682,7 +682,7 @@ getdouble(void)
 		return (double) *((*gargv++)+1);
 
 	errno = 0;
-	val = strtod_l(*gargv, , LC_C_LOCALE);
+	val = strtod(*gargv, );
 	check_conversion(*gargv++, ep);
 	return val;
 }



CVS commit: src/usr.bin/printf

2019-01-26 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sat Jan 26 15:22:54 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Always convert input numbers (from the command line) in the C
locale, not as set in the environment.   Conforms with POSIX spec.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.46 src/usr.bin/printf/printf.c:1.47
--- src/usr.bin/printf/printf.c:1.46	Mon Sep 10 14:42:29 2018
+++ src/usr.bin/printf/printf.c	Sat Jan 26 15:22:54 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.46 2018/09/10 14:42:29 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.47 2019/01/26 15:22:54 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.46 2018/09/10 14:42:29 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.47 2019/01/26 15:22:54 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -682,7 +682,7 @@ getdouble(void)
 		return (double) *((*gargv++)+1);
 
 	errno = 0;
-	val = strtod(*gargv, );
+	val = strtod_l(*gargv, , LC_C_LOCALE);
 	check_conversion(*gargv++, ep);
 	return val;
 }



CVS commit: src/usr.bin/printf

2019-01-26 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Sat Jan 26 15:22:54 UTC 2019

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Always convert input numbers (from the command line) in the C
locale, not as set in the environment.   Conforms with POSIX spec.


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-09-10 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Mon Sep 10 14:42:29 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
A truly ancient bug found by Edgar Fuss

When printf is running builtin in a sh, global vars aren't reset to
0 between invocations.   This affects "rval" which remembers state
from a previous %b \c and thereafter always exits after the first
format conversion, until we get a conversion that generates an
error (which resets the flag almost by accident)

printf %b abc\\c
abc (no \n)
printf %s%s hello world
hello   (no \n, of course, no world ...)
printf %s%s hello world
hello
printf %s%s hello world
hello
printf %d hello
printf: hello: expected numeric value
0   (no \n)
printf %s%s hello world
helloworld  (no \n, and we are back!)

This affects both /bin/sh and /bin/csh (and has for a very long time).

XXX pullup -8


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.45 src/usr.bin/printf/printf.c:1.46
--- src/usr.bin/printf/printf.c:1.45	Tue Sep  4 01:13:50 2018
+++ src/usr.bin/printf/printf.c	Mon Sep 10 14:42:29 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.45 2018/09/04 01:13:50 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.46 2018/09/10 14:42:29 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.45 2018/09/04 01:13:50 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.46 2018/09/10 14:42:29 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -136,6 +136,8 @@ main(int argc, char *argv[])
 	(void)setlocale (LC_ALL, "");
 #endif
 
+	rval = 0;	/* clear for builtin versions (avoid holdover) */
+
 	while ((o = getopt(argc, argv, "")) != -1) {
 		switch (o) {
 		case '?':



CVS commit: src/usr.bin/printf

2018-09-10 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Mon Sep 10 14:42:29 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
A truly ancient bug found by Edgar Fuss

When printf is running builtin in a sh, global vars aren't reset to
0 between invocations.   This affects "rval" which remembers state
from a previous %b \c and thereafter always exits after the first
format conversion, until we get a conversion that generates an
error (which resets the flag almost by accident)

printf %b abc\\c
abc (no \n)
printf %s%s hello world
hello   (no \n, of course, no world ...)
printf %s%s hello world
hello
printf %s%s hello world
hello
printf %d hello
printf: hello: expected numeric value
0   (no \n)
printf %s%s hello world
helloworld  (no \n, and we are back!)

This affects both /bin/sh and /bin/csh (and has for a very long time).

XXX pullup -8


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-09-03 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Sep  4 01:13:50 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Printf's that support \e for escape all seem to also support \E.
Except us.   Now we do as well.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-09-03 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Sep  4 01:13:50 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Printf's that support \e for escape all seem to also support \E.
Except us.   Now we do as well.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.44 src/usr.bin/printf/printf.c:1.45
--- src/usr.bin/printf/printf.c:1.44	Mon Sep  3 04:10:20 2018
+++ src/usr.bin/printf/printf.c	Tue Sep  4 01:13:50 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.44 2018/09/03 04:10:20 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.45 2018/09/04 01:13:50 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.44 2018/09/03 04:10:20 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.45 2018/09/04 01:13:50 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -504,6 +504,7 @@ conv_escape(char *str, char *conv_ch, in
 	case 'a':	value = '\a';	break;	/* alert */
 	case 'b':	value = '\b';	break;	/* backspace */
 	case 'e':	value = ESCAPE;	break;	/* escape */
+	case 'E':	value = ESCAPE;	break;	/* escape */
 	case 'f':	value = '\f';	break;	/* form-feed */
 	case 'n':	value = '\n';	break;	/* newline */
 	case 'r':	value = '\r';	break;	/* carriage-return */



CVS commit: src/usr.bin/printf

2018-09-02 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Mon Sep  3 04:10:20 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Tighten syntax a little (no more %*4.*2d nonsense).
Include the format collected so far in "missing format char" err message.
Minor KNF and whitespace.


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.43 src/usr.bin/printf/printf.c:1.44
--- src/usr.bin/printf/printf.c:1.43	Fri Aug 31 17:27:35 2018
+++ src/usr.bin/printf/printf.c	Mon Sep  3 04:10:20 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.43 2018/08/31 17:27:35 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.44 2018/09/03 04:10:20 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.43 2018/08/31 17:27:35 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.44 2018/09/03 04:10:20 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -121,7 +121,9 @@ static char  **gargv;
 #ifdef main
 int main(int, char *[]);
 #endif
-int main(int argc, char *argv[])
+
+int
+main(int argc, char *argv[])
 {
 	char *fmt, *start;
 	int fieldwidth, precision;
@@ -189,34 +191,39 @@ int main(int argc, char *argv[])
 			if (*fmt == '*') {
 fmt++;
 fieldwidth = getwidth();
-			} else
+			} else {
 fieldwidth = -1;
 
-			/* skip to possible '.', get following precision */
-			fmt += strspn(fmt, SKIP2);
+/* skip to possible '.' for precision */
+fmt += strspn(fmt, SKIP2);
+			}
+
 			if (*fmt == '.') {
+ /* get following precision */
 fmt++;
 if (*fmt == '*') {
 	fmt++;
 	precision = getwidth();
-} else
+} else {
 	precision = -1;
+	fmt += strspn(fmt, SKIP2);
+}
 			} else
 precision = -1;
 
-			fmt += strspn(fmt, SKIP2);
-
 			ch = *fmt;
 			if (!ch) {
-warnx("missing format character");
+warnx("%s: missing format character", start);
 return 1;
 			}
+
 			/*
 			 * null terminate format string to we can use it
 			 * as an argument to printf.
 			 */
 			nextch = fmt[1];
 			fmt[1] = 0;
+
 			switch (ch) {
 
 			case 'B': {
@@ -323,6 +330,11 @@ int main(int argc, char *argv[])
 	goto out;
 break;
 			}
+			case '%':
+/* Don't ask, but this is useful ... */
+if (fieldwidth == 'N' && precision == 'B')
+	return 0;
+/* FALLTHROUGH */
 			default:
 warnx("%s: invalid directive", start);
 return 1;
@@ -617,7 +629,7 @@ getwidth(void)
 	char *s, *ep;
 
 	s = *gargv;
-	if (!*gargv)
+	if (s == NULL)
 		return 0;
 	gargv++;
 



CVS commit: src/usr.bin/printf

2018-09-02 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Mon Sep  3 04:10:20 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Tighten syntax a little (no more %*4.*2d nonsense).
Include the format collected so far in "missing format char" err message.
Minor KNF and whitespace.


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-08-31 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Fri Aug 31 17:27:35 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1 printf.c

Log Message:
PR standards/53563

POSIX requires that signed numbers (strings preceded by '+' or '-')
be allowed as inputs to all of the integer format conversions, including
those which treat the data as unsigned.

Hence we do not need a variant function whose only difference from its
companion is to reject strings starting with '-' - instead we use
the primary function (getintmax()) for everything and remove getuintmax().

Minor update to the man page to indicate that the arg to all of the
integer conversions (diouxX) must be an integer constant (with an
optional sign) and to make it blatantly clear that %o is octal and
%u is unsigned decimal (for some reason those weren't explicitly stated
unlike d i x and X).  Delete "respectively", it is not needed (and does
not really apply).

XXX pullup -8


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/usr.bin/printf/printf.1
cvs rdiff -u -r1.42 -r1.43 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.30 src/usr.bin/printf/printf.1:1.31
--- src/usr.bin/printf/printf.1:1.30	Tue Jul 24 20:58:39 2018
+++ src/usr.bin/printf/printf.1	Fri Aug 31 17:27:35 2018
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.30 2018/07/24 20:58:39 kre Exp $
+.\"	$NetBSD: printf.1,v 1.31 2018/08/31 17:27:35 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	from: @(#)printf.1	8.1 (Berkeley) 6/6/93
 .\"
-.Dd July 25, 2018
+.Dd August 31, 2018
 .Dt PRINTF 1
 .Os
 .Sh NAME
@@ -255,9 +255,12 @@ The format characters and their meanings
 .Bl -tag -width Fl
 .It Cm diouXx
 The
-.Ar argument
-is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
-or unsigned hexadecimal (X or x), respectively.
+.Ar argument ,
+which must represent an integer constant,
+with an optional leading plus or minus sign,
+is printed as a signed decimal (d or i),
+unsigned octal (o), unsigned decimal (u),
+or unsigned hexadecimal (X or x).
 .It Cm fF
 The
 .Ar argument

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.42 src/usr.bin/printf/printf.c:1.43
--- src/usr.bin/printf/printf.c:1.42	Wed Jul 25 15:35:27 2018
+++ src/usr.bin/printf/printf.c	Fri Aug 31 17:27:35 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.42 2018/07/25 15:35:27 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.43 2018/08/31 17:27:35 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.42 2018/07/25 15:35:27 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.43 2018/08/31 17:27:35 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -72,7 +72,6 @@ static char	 getchr(void);
 static double	 getdouble(void);
 static int	 getwidth(void);
 static intmax_t	 getintmax(void);
-static uintmax_t getuintmax(void);
 static char	*getstr(void);
 static char	*mklong(const char *, char);
 static void  check_conversion(const char *, const char *);
@@ -301,7 +300,7 @@ int main(int argc, char *argv[])
 			case 'u':
 			case 'x':
 			case 'X': {
-uintmax_t p = getuintmax();
+uintmax_t p = (uintmax_t)getintmax();
 char *f = mklong(start, ch);
 
 PF(f, p);
@@ -655,35 +654,6 @@ getintmax(void)
 	return val;
 }
 
-static uintmax_t
-getuintmax(void)
-{
-	uintmax_t val;
-	char *cp, *ep;
-
-	cp = *gargv;
-	if (cp == NULL)
-		return 0;
-	gargv++;
-
-	if (*cp == '\"' || *cp == '\'')
-		return (uintmax_t)*(cp + 1);
-
-	/* strtoumax won't error -ve values */
-	while (isspace(*(unsigned char *)cp))
-		cp++;
-	if (*cp == '-') {
-		warnx("%s: expected positive numeric value", cp);
-		rval = 1;
-		return 0;
-	}
-
-	errno = 0;
-	val = strtoumax(cp, , 0);
-	check_conversion(cp, ep);
-	return val;
-}
-
 static double
 getdouble(void)
 {



CVS commit: src/usr.bin/printf

2018-08-31 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Fri Aug 31 17:27:35 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1 printf.c

Log Message:
PR standards/53563

POSIX requires that signed numbers (strings preceded by '+' or '-')
be allowed as inputs to all of the integer format conversions, including
those which treat the data as unsigned.

Hence we do not need a variant function whose only difference from its
companion is to reject strings starting with '-' - instead we use
the primary function (getintmax()) for everything and remove getuintmax().

Minor update to the man page to indicate that the arg to all of the
integer conversions (diouxX) must be an integer constant (with an
optional sign) and to make it blatantly clear that %o is octal and
%u is unsigned decimal (for some reason those weren't explicitly stated
unlike d i x and X).  Delete "respectively", it is not needed (and does
not really apply).

XXX pullup -8


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/usr.bin/printf/printf.1
cvs rdiff -u -r1.42 -r1.43 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-25 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed Jul 25 15:35:27 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
NFC: More KNF (remove () around returned constants).


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.41 src/usr.bin/printf/printf.c:1.42
--- src/usr.bin/printf/printf.c:1.41	Wed Jul 25 14:41:52 2018
+++ src/usr.bin/printf/printf.c	Wed Jul 25 15:35:27 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.41 2018/07/25 14:41:52 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.42 2018/07/25 15:35:27 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.41 2018/07/25 14:41:52 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.42 2018/07/25 15:35:27 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -210,7 +210,7 @@ int main(int argc, char *argv[])
 			ch = *fmt;
 			if (!ch) {
 warnx("missing format character");
-return (1);
+return 1;
 			}
 			/*
 			 * null terminate format string to we can use it
@@ -619,7 +619,7 @@ getwidth(void)
 
 	s = *gargv;
 	if (!*gargv)
-		return (0);
+		return 0;
 	gargv++;
 
 	errno = 0;
@@ -691,7 +691,7 @@ getdouble(void)
 	char *ep;
 
 	if (!*gargv)
-		return (0.0);
+		return 0.0;
 
 	if (**gargv == '\"' || **gargv == '\'')
 		return (double) *((*gargv++)+1);



CVS commit: src/usr.bin/printf

2018-07-25 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed Jul 25 15:35:27 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
NFC: More KNF (remove () around returned constants).


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-25 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed Jul 25 14:41:52 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
NFC: whitespace & KNF.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.40 src/usr.bin/printf/printf.c:1.41
--- src/usr.bin/printf/printf.c:1.40	Tue Jul 24 20:49:19 2018
+++ src/usr.bin/printf/printf.c	Wed Jul 25 14:41:52 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.40 2018/07/24 20:49:19 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.41 2018/07/25 14:41:52 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.40 2018/07/24 20:49:19 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.41 2018/07/25 14:41:52 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -76,7 +76,7 @@ static uintmax_t getuintmax(void);
 static char	*getstr(void);
 static char	*mklong(const char *, char);
 static void  check_conversion(const char *, const char *);
-static void	 usage(void); 
+static void	 usage(void);
 
 static void	b_count(int);
 static void	b_output(int);
@@ -160,9 +160,9 @@ int main(int argc, char *argv[])
 		/*
 		 * Basic algorithm is to scan the format string for conversion
 		 * specifications -- once one is found, find out if the field
-		 * width or precision is a '*'; if it is, gather up value. 
+		 * width or precision is a '*'; if it is, gather up value.
 		 * Note, format strings are reused as necessary to use up the
-		 * provided arguments, arguments of zero/null string are 
+		 * provided arguments, arguments of zero/null string are
 		 * provided to use up the format string.
 		 */
 
@@ -179,8 +179,10 @@ int main(int argc, char *argv[])
 continue;
 			}
 
-			/* Ok - we've found a format specification,
-			   Save its address for a later printf(). */
+			/*
+			 * Ok - we've found a format specification,
+			 * Save its address for a later printf().
+			 */
 			start = fmt - 1;
 
 			/* skip to field width */
@@ -210,14 +212,17 @@ int main(int argc, char *argv[])
 warnx("missing format character");
 return (1);
 			}
-			/* null terminate format string to we can use it
-			   as an argument to printf. */
+			/*
+			 * null terminate format string to we can use it
+			 * as an argument to printf.
+			 */
 			nextch = fmt[1];
 			fmt[1] = 0;
 			switch (ch) {
 
 			case 'B': {
 const char *p = conv_expand(getstr());
+
 if (p == NULL)
 	goto out;
 *fmt = 's';
@@ -227,11 +232,14 @@ int main(int argc, char *argv[])
 break;
 			}
 			case 'b': {
-/* There has to be a better way to do this,
+/*
+ * There has to be a better way to do this,
  * but the string we generate might have
- * embedded nulls. */
+ * embedded nulls
+ */
 static char *a, *t;
 char *cp = getstr();
+
 /* Free on entry in case shell longjumped out */
 if (a != NULL)
 	free(a);
@@ -239,6 +247,7 @@ int main(int argc, char *argv[])
 if (t != NULL)
 	free(t);
 t = NULL;
+
 /* Count number of bytes we want to output */
 b_length = 0;
 conv_escape_str(cp, b_count, 0);
@@ -247,20 +256,24 @@ int main(int argc, char *argv[])
 	goto out;
 (void)memset(t, 'x', b_length);
 t[b_length] = 0;
+
 /* Get printf to calculate the lengths */
 *fmt = 's';
 APF(, start, t);
 if (error == -1)
 	goto out;
 b_fmt = a;
+
 /* Output leading spaces and data bytes */
 conv_escape_str(cp, b_output, 1);
+
 /* Add any trailing spaces */
 printf("%s", b_fmt);
 break;
 			}
 			case 'c': {
 char p = getchr();
+
 PF(start, p);
 if (error < 0)
 	goto out;
@@ -268,6 +281,7 @@ int main(int argc, char *argv[])
 			}
 			case 's': {
 char *p = getstr();
+
 PF(start, p);
 if (error < 0)
 	goto out;
@@ -277,6 +291,7 @@ int main(int argc, char *argv[])
 			case 'i': {
 intmax_t p = getintmax();
 char *f = mklong(start, ch);
+
 PF(f, p);
 if (error < 0)
 	goto out;
@@ -288,6 +303,7 @@ int main(int argc, char *argv[])
 			case 'X': {
 uintmax_t p = getuintmax();
 char *f = mklong(start, ch);
+
 PF(f, p);
 if (error < 0)
 	goto out;
@@ -302,6 +318,7 @@ int main(int argc, char *argv[])
 			case 'g':
 			case 'G': {
 double p = getdouble();
+
 PF(start, p);
 if (error < 0)
 	goto out;
@@ -320,7 +337,7 @@ int main(int argc, char *argv[])
 	} while (gargv != argv && *gargv);
 
 	return rval & ~0x100;
-out:
+  out:
 	warn("print failed");
 	return 1;
 }
@@ -356,7 +373,7 @@ b_output(int ch)
 
 
 /*
- * Print SysV echo(1) style escape string 
+ * Print SysV echo(1) style escape string
  *	Halts processing 

CVS commit: src/usr.bin/printf

2018-07-25 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Wed Jul 25 14:41:52 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
NFC: whitespace & KNF.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-24 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul 24 20:58:40 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Add the new formats to the list of format cracters (oops...)


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.29 src/usr.bin/printf/printf.1:1.30
--- src/usr.bin/printf/printf.1:1.29	Tue Jul 24 20:49:19 2018
+++ src/usr.bin/printf/printf.1	Tue Jul 24 20:58:39 2018
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.29 2018/07/24 20:49:19 kre Exp $
+.\"	$NetBSD: printf.1,v 1.30 2018/07/24 20:58:39 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -241,7 +241,7 @@ formats); if the digit string is missing
 as zero;
 .It Format :
 A character which indicates the type of format to use (one of
-.Cm diouxXfeEgGbBcs ) .
+.Cm diouxXfFeEgGaAbBcs ) .
 .El
 .Pp
 A field width or precision may be



CVS commit: src/usr.bin/printf

2018-07-24 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul 24 20:58:40 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Add the new formats to the list of format cracters (oops...)


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-24 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul 24 20:49:20 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1 printf.c

Log Message:
Add support for F a and A formats (which go with the eEfgG formats
already supported.)


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/printf/printf.1
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.28 src/usr.bin/printf/printf.1:1.29
--- src/usr.bin/printf/printf.1:1.28	Tue Jul 24 19:49:33 2018
+++ src/usr.bin/printf/printf.1	Tue Jul 24 20:49:19 2018
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.28 2018/07/24 19:49:33 kre Exp $
+.\"	$NetBSD: printf.1,v 1.29 2018/07/24 20:49:19 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	from: @(#)printf.1	8.1 (Berkeley) 6/6/93
 .\"
-.Dd March 11, 2018
+.Dd July 25, 2018
 .Dt PRINTF 1
 .Os
 .Sh NAME
@@ -169,6 +169,7 @@ For
 .Cm e ,
 .Cm E ,
 .Cm f ,
+.Cm F ,
 .Cm g ,
 and
 .Cm G
@@ -257,7 +258,7 @@ The
 .Ar argument
 is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
 or unsigned hexadecimal (X or x), respectively.
-.It Cm f
+.It Cm fF
 The
 .Ar argument
 is printed in the style
@@ -269,6 +270,17 @@ after the decimal point is equal to the 
 the argument.
 If the precision is missing, 6 digits are given; if the precision
 is explicitly 0, no digits and no decimal point are printed.
+If the number is Infinity, or Not a Number (NaN), then
+.Dq inf
+.Pq \ Dq nan
+is printed for
+.Cm f
+format, and
+.Dq INF
+.Pq \ Dq NAN
+for
+.Cm F
+format.
 .It Cm eE
 The
 .Ar argument
@@ -282,16 +294,28 @@ the precision specification for the argu
 missing, 6 digits are produced.
 An upper-case E is used for an
 .Sq E
+format, and upper-case for Infinity and NaN as for
+.Sq F
 format.
 .It Cm gG
 The
 .Ar argument
 is printed in style
 .Cm f
+.Pq Cm F
 or in style
 .Cm e
 .Pq Cm E
 whichever gives full precision in minimum space.
+.It Cm aA
+The
+.Ar argument
+is treated as a floating point number,
+for which the underlying hexadecimal representation is
+printed.
+See
+.Xr printf 3
+for the details.
 .It Cm b
 Characters from the string
 .Ar argument

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.39 src/usr.bin/printf/printf.c:1.40
--- src/usr.bin/printf/printf.c:1.39	Tue Jul  3 01:56:39 2018
+++ src/usr.bin/printf/printf.c	Tue Jul 24 20:49:19 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.39 2018/07/03 01:56:39 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.40 2018/07/24 20:49:19 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.39 2018/07/03 01:56:39 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.40 2018/07/24 20:49:19 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -293,9 +293,12 @@ int main(int argc, char *argv[])
 	goto out;
 break;
 			}
+			case 'a':
+			case 'A':
 			case 'e':
 			case 'E':
 			case 'f':
+			case 'F':
 			case 'g':
 			case 'G': {
 double p = getdouble();



CVS commit: src/usr.bin/printf

2018-07-24 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul 24 20:49:20 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1 printf.c

Log Message:
Add support for F a and A formats (which go with the eEfgG formats
already supported.)


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/printf/printf.1
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-24 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul 24 19:49:34 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Correct a typo (off by one  (key)) ...
There is no 'w' format, but there is an 'e'


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.27 src/usr.bin/printf/printf.1:1.28
--- src/usr.bin/printf/printf.1:1.27	Mon Mar 12 09:29:43 2018
+++ src/usr.bin/printf/printf.1	Tue Jul 24 19:49:33 2018
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.27 2018/03/12 09:29:43 wiz Exp $
+.\"	$NetBSD: printf.1,v 1.28 2018/07/24 19:49:33 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -240,7 +240,7 @@ formats); if the digit string is missing
 as zero;
 .It Format :
 A character which indicates the type of format to use (one of
-.Cm diouxXfwEgGbBcs ) .
+.Cm diouxXfeEgGbBcs ) .
 .El
 .Pp
 A field width or precision may be



CVS commit: src/usr.bin/printf

2018-07-24 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul 24 19:49:34 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Correct a typo (off by one  (key)) ...
There is no 'w' format, but there is an 'e'


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-02 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul  3 01:56:39 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Avoid printing error messages twice when an invalid
escape sequence (\ sequence) is present in an arg to a %b
conversion.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-07-02 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul  3 01:56:39 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Avoid printing error messages twice when an invalid
escape sequence (\ sequence) is present in an arg to a %b
conversion.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.38 src/usr.bin/printf/printf.c:1.39
--- src/usr.bin/printf/printf.c:1.38	Tue Jul  3 01:54:42 2018
+++ src/usr.bin/printf/printf.c	Tue Jul  3 01:56:39 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.38 2018/07/03 01:54:42 kre Exp $	*/
+/*	$NetBSD: printf.c,v 1.39 2018/07/03 01:56:39 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.38 2018/07/03 01:54:42 kre Exp $");
+__RCSID("$NetBSD: printf.c,v 1.39 2018/07/03 01:56:39 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -65,8 +65,8 @@ __RCSID("$NetBSD: printf.c,v 1.38 2018/0
 #define ESCAPE 033
 #endif
 
-static void	 conv_escape_str(char *, void (*)(int));
-static char	*conv_escape(char *, char *);
+static void	 conv_escape_str(char *, void (*)(int), int);
+static char	*conv_escape(char *, char *, int);
 static char	*conv_expand(const char *);
 static char	 getchr(void);
 static double	 getdouble(void);
@@ -170,7 +170,7 @@ int main(int argc, char *argv[])
 		for (fmt = format; (ch = *fmt++) != '\0';) {
 			if (ch == '\\') {
 char c_ch;
-fmt = conv_escape(fmt, _ch);
+fmt = conv_escape(fmt, _ch, 0);
 putchar(c_ch);
 continue;
 			}
@@ -241,7 +241,7 @@ int main(int argc, char *argv[])
 t = NULL;
 /* Count number of bytes we want to output */
 b_length = 0;
-conv_escape_str(cp, b_count);
+conv_escape_str(cp, b_count, 0);
 t = malloc(b_length + 1);
 if (t == NULL)
 	goto out;
@@ -254,7 +254,7 @@ int main(int argc, char *argv[])
 	goto out;
 b_fmt = a;
 /* Output leading spaces and data bytes */
-conv_escape_str(cp, b_output);
+conv_escape_str(cp, b_output, 1);
 /* Add any trailing spaces */
 printf("%s", b_fmt);
 break;
@@ -357,7 +357,7 @@ b_output(int ch)
  *	Halts processing string if a \c escape is encountered.
  */
 static void
-conv_escape_str(char *str, void (*do_putchar)(int))
+conv_escape_str(char *str, void (*do_putchar)(int), int quiet)
 {
 	int value;
 	int ch;
@@ -415,7 +415,7 @@ conv_escape_str(char *str, void (*do_put
 		}
 
 		/* Finally test for sequences valid in the format string */
-		str = conv_escape(str - 1, );
+		str = conv_escape(str - 1, , quiet);
 		do_putchar(c);
 	}
 }
@@ -424,7 +424,7 @@ conv_escape_str(char *str, void (*do_put
  * Print "standard" escape characters 
  */
 static char *
-conv_escape(char *str, char *conv_ch)
+conv_escape(char *str, char *conv_ch, int quiet)
 {
 	char value;
 	char ch;
@@ -434,7 +434,8 @@ conv_escape(char *str, char *conv_ch)
 
 	switch (ch) {
 	case '\0':
-		warnx("incomplete escape sequence");
+		if (!quiet)
+			warnx("incomplete escape sequence");
 		rval = 1;
 		value = '\\';
 		--str;
@@ -477,7 +478,8 @@ conv_escape(char *str, char *conv_ch)
 	case 'v':	value = '\v';	break;	/* vertical-tab */
 
 	default:
-		warnx("unknown escape sequence `\\%c'", ch);
+		if (!quiet)
+			warnx("unknown escape sequence `\\%c'", ch);
 		rval = 1;
 		value = ch;
 		break;



CVS commit: src/usr.bin/printf

2018-07-02 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul  3 01:54:42 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
>From leot@ on tech-userlevel:

Avoid running off into oblivion when a format string,
or arg to a %b conversion ends in an unescaped backslash.

Patch from Leo slightly modified by me.


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.37 src/usr.bin/printf/printf.c:1.38
--- src/usr.bin/printf/printf.c:1.37	Tue Jun 16 22:54:10 2015
+++ src/usr.bin/printf/printf.c	Tue Jul  3 01:54:42 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.37 2015/06/16 22:54:10 christos Exp $	*/
+/*	$NetBSD: printf.c,v 1.38 2018/07/03 01:54:42 kre Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)printf.c	8.2 (Berkeley) 3/22/95";
 #else
-__RCSID("$NetBSD: printf.c,v 1.37 2015/06/16 22:54:10 christos Exp $");
+__RCSID("$NetBSD: printf.c,v 1.38 2018/07/03 01:54:42 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -433,6 +433,13 @@ conv_escape(char *str, char *conv_ch)
 	ch = *str++;
 
 	switch (ch) {
+	case '\0':
+		warnx("incomplete escape sequence");
+		rval = 1;
+		value = '\\';
+		--str;
+		break;
+
 	case '0': case '1': case '2': case '3':
 	case '4': case '5': case '6': case '7':
 		num_buf[0] = ch;



CVS commit: src/usr.bin/printf

2018-07-02 Thread Robert Elz
Module Name:src
Committed By:   kre
Date:   Tue Jul  3 01:54:42 UTC 2018

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
>From leot@ on tech-userlevel:

Avoid running off into oblivion when a format string,
or arg to a %b conversion ends in an unescaped backslash.

Patch from Leo slightly modified by me.


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-03-12 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Mar 12 09:29:43 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Remove Tn.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.26 src/usr.bin/printf/printf.1:1.27
--- src/usr.bin/printf/printf.1:1.26	Mon Mar 12 00:42:05 2018
+++ src/usr.bin/printf/printf.1	Mon Mar 12 09:29:43 2018
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.26 2018/03/12 00:42:05 dholland Exp $
+.\"	$NetBSD: printf.1,v 1.27 2018/03/12 09:29:43 wiz Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -70,8 +70,7 @@ otherwise it is evaluated as a C constan
 .It
 A leading plus or minus sign is allowed.
 .It
-If the leading character is a single or double quote, the value is the
-.Tn ASCII
+If the leading character is a single or double quote, the value is the ASCII
 code of the next character.
 .El
 .Pp
@@ -127,13 +126,11 @@ character.
 .It Cm \e\e
 Write a backslash character.
 .It Cm \e Ns Ar num
-Write an 8\-bit character whose
-.Tn ASCII
+Write an 8\-bit character whose ASCII
 value is the 1\-, 2\-, or 3\-digit octal number
 .Ar num .
 .It Cm \ex Ns Ar xx
-Write an 8\-bit character whose
-.Tn ASCII
+Write an 8\-bit character whose ASCII
 value is the 1\- or 2\-digit hexadecimal number
 .Ar xx .
 .El
@@ -309,10 +306,8 @@ to ignore any remaining characters in th
 any remaining string operands, and any additional characters in
 the format operand.
 .It Cm \e0 Ns Ar num
-Write an 8\-bit character whose
-.Tn ASCII
-value is the 1\-, 2\-, or 3\-digit
-octal number
+Write an 8\-bit character whose ASCII value is the 1\-, 2\-, or
+3\-digit octal number
 .Ar num .
 .It Cm \e^ Ns Ar c
 Write the control character
@@ -384,10 +379,8 @@ are optional in POSIX.
 The behaviour of the %B format and the \e', \e", \exxx, \ee and
 \e[M][\-|^]c escape sequences are undefined in POSIX.
 .Sh BUGS
-Since the floating point numbers are translated from
-.Tn ASCII
-to floating-point and
-then back again, floating-point precision may be lost.
+Since the floating point numbers are translated from ASCII to
+floating-point and then back again, floating-point precision may be lost.
 .Pp
 Hexadecimal character constants are restricted to, and should be specified
 as, two character constants.



CVS commit: src/usr.bin/printf

2018-03-12 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Mar 12 09:29:43 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Remove Tn.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2018-03-11 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Mon Mar 12 00:42:06 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Explicitly mention use of -- for format strings beginning with -,
per PR 21970.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.25 src/usr.bin/printf/printf.1:1.26
--- src/usr.bin/printf/printf.1:1.25	Sun Apr 13 01:45:34 2014
+++ src/usr.bin/printf/printf.1	Mon Mar 12 00:42:05 2018
@@ -1,4 +1,4 @@
-.\"	$NetBSD: printf.1,v 1.25 2014/04/13 01:45:34 snj Exp $
+.\"	$NetBSD: printf.1,v 1.26 2018/03/12 00:42:05 dholland Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	from: @(#)printf.1	8.1 (Berkeley) 6/6/93
 .\"
-.Dd May 6, 2008
+.Dd March 11, 2018
 .Dt PRINTF 1
 .Os
 .Sh NAME
@@ -358,6 +358,13 @@ Print a `%'; no argument is used.
 In no case does a non-existent or small field width cause truncation of
 a field; padding takes place only if the specified field width exceeds
 the actual width.
+.Pp
+If the first character of
+.Ar format
+is a dash,
+.Ar format
+must be preceded by a word consisting of two dashes (--) to prevent it
+from being interpreted as an option string.
 .Sh EXIT STATUS
 .Ex -std
 .Sh SEE ALSO



CVS commit: src/usr.bin/printf

2018-03-11 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Mon Mar 12 00:42:06 UTC 2018

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
Explicitly mention use of -- for format strings beginning with -,
per PR 21970.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2013-07-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Jul 16 17:48:22 UTC 2013

Modified Files:
src/usr.bin/printf: Makefile printf.c

Log Message:
WARNS=6


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/printf/Makefile
cvs rdiff -u -r1.35 -r1.36 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/Makefile
diff -u src/usr.bin/printf/Makefile:1.11 src/usr.bin/printf/Makefile:1.12
--- src/usr.bin/printf/Makefile:1.11	Tue Aug 16 06:37:21 2011
+++ src/usr.bin/printf/Makefile	Tue Jul 16 13:48:22 2013
@@ -1,6 +1,6 @@
-#	$NetBSD: Makefile,v 1.11 2011/08/16 10:37:21 christos Exp $
+#	$NetBSD: Makefile,v 1.12 2013/07/16 17:48:22 christos Exp $
 #	from: @(#)Makefile	8.1 (Berkeley) 6/6/93
-
+WARNS=6
 PROG=	printf
 
 COPTS.printf.c = -Wno-format-nonliteral

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.35 src/usr.bin/printf/printf.c:1.36
--- src/usr.bin/printf/printf.c:1.35	Tue Mar 15 19:11:49 2011
+++ src/usr.bin/printf/printf.c	Tue Jul 16 13:48:22 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.35 2011/03/15 23:11:49 christos Exp $	*/
+/*	$NetBSD: printf.c,v 1.36 2013/07/16 17:48:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@ __COPYRIGHT(@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = @(#)printf.c	8.2 (Berkeley) 3/22/95;
 #else
-__RCSID($NetBSD: printf.c,v 1.35 2011/03/15 23:11:49 christos Exp $);
+__RCSID($NetBSD: printf.c,v 1.36 2013/07/16 17:48:22 christos Exp $);
 #endif
 #endif /* not lint */
 
@@ -68,13 +68,13 @@ __RCSID($NetBSD: printf.c,v 1.35 2011/0
 static void	 conv_escape_str(char *, void (*)(int));
 static char	*conv_escape(char *, char *);
 static char	*conv_expand(const char *);
-static int	 getchr(void);
+static char	 getchr(void);
 static double	 getdouble(void);
 static int	 getwidth(void);
 static intmax_t	 getintmax(void);
 static uintmax_t getuintmax(void);
 static char	*getstr(void);
-static char	*mklong(const char *, int);
+static char	*mklong(const char *, char);
 static void  check_conversion(const char *, const char *);
 static void	 usage(void); 
 
@@ -128,15 +128,15 @@ int main(int argc, char *argv[])
 	int fieldwidth, precision;
 	char nextch;
 	char *format;
-	int ch;
-	int error;
+	char ch;
+	int error, o;
 
 #if !defined(SHELL)  !defined(BUILTIN)
 	(void)setlocale (LC_ALL, );
 #endif
 
-	while ((ch = getopt(argc, argv, )) != -1) {
-		switch (ch) {
+	while ((o = getopt(argc, argv, )) != -1) {
+		switch (o) {
 		case '?':
 		default:
 			usage();
@@ -426,8 +426,8 @@ conv_escape_str(char *str, void (*do_put
 static char *
 conv_escape(char *str, char *conv_ch)
 {
-	int value;
-	int ch;
+	char value;
+	char ch;
 	char num_buf[4], *num_end;
 
 	ch = *str++;
@@ -438,9 +438,9 @@ conv_escape(char *str, char *conv_ch)
 		num_buf[0] = ch;
 		ch = str[0];
 		num_buf[1] = ch;
-		num_buf[2] = ch ? str[1] : 0;
-		num_buf[3] = 0;
-		value = strtoul(num_buf, num_end, 8);
+		num_buf[2] = (char)(ch != '\0' ? str[1] : '\0');
+		num_buf[3] = '\0';
+		value = (char)strtoul(num_buf, num_end, 8);
 		str += num_end  - (num_buf + 1);
 		break;
 
@@ -451,9 +451,9 @@ conv_escape(char *str, char *conv_ch)
 		   Supporting 2 byte constants is a compromise. */
 		ch = str[0];
 		num_buf[0] = ch;
-		num_buf[1] = ch ? str[1] : 0;
-		num_buf[2] = 0;
-		value = strtoul(num_buf, num_end, 16);
+		num_buf[1] = (char)(ch != '\0' ? str[1] : '\0');
+		num_buf[2] = '\0';
+		value = (char)strtoul(num_buf, num_end, 16);
 		str += num_end - num_buf;
 		break;
 
@@ -487,7 +487,7 @@ conv_expand(const char *str)
 {
 	static char *conv_str;
 	char *cp;
-	int ch;
+	char ch;
 
 	if (conv_str)
 		free(conv_str);
@@ -497,7 +497,7 @@ conv_expand(const char *str)
 		return NULL;
 	cp = conv_str;
 
-	while ((ch = *(const unsigned char *)str++) != '\0') {
+	while ((ch = *(const char *)str++) != '\0') {
 		switch (ch) {
 		/* Use C escapes for expected control characters */
 		case '\\':	ch = '\\';	break;	/* backslash */
@@ -513,7 +513,7 @@ conv_expand(const char *str)
 		case '\v':	ch = 'v';	break;	/* vertical-tab */
 		default:
 			/* Copy anything printable */
-			if (isprint(ch)) {
+			if (isprint((unsigned char)ch)) {
 *cp++ = ch;
 continue;
 			}
@@ -521,7 +521,7 @@ conv_expand(const char *str)
 			*cp++ = '\\';
 			if (ch  0200) {
 *cp++ = 'M';
-ch = ~0200;
+ch = (char)~0200;
 			}
 			if (ch == 0177) {
 *cp++ = '^';
@@ -546,7 +546,7 @@ conv_expand(const char *str)
 }
 
 static char *
-mklong(const char *str, int ch)
+mklong(const char *str, char ch)
 {
 	static char copy[64];
 	size_t len;	
@@ -563,12 +563,12 @@ mklong(const char *str, int ch)
 	return copy;	
 }
 
-static int
+static char
 getchr(void)
 {
 	if (!*gargv)
 		return 0;
-	return (int)**gargv++;
+	return **gargv++;
 }
 
 static char *
@@ -583,7 +583,7 @@ getstr(void)
 static int
 getwidth(void)
 {
-	long 

CVS commit: src/usr.bin/printf

2013-07-16 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Jul 16 17:48:22 UTC 2013

Modified Files:
src/usr.bin/printf: Makefile printf.c

Log Message:
WARNS=6


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/printf/Makefile
cvs rdiff -u -r1.35 -r1.36 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2011-03-15 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Mar 15 23:11:49 UTC 2011

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
support grouping format.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.34 src/usr.bin/printf/printf.c:1.35
--- src/usr.bin/printf/printf.c:1.34	Tue Oct 13 15:28:31 2009
+++ src/usr.bin/printf/printf.c	Tue Mar 15 19:11:49 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.34 2009/10/13 19:28:31 christos Exp $	*/
+/*	$NetBSD: printf.c,v 1.35 2011/03/15 23:11:49 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = @(#)printf.c	8.2 (Berkeley) 3/22/95;
 #else
-__RCSID($NetBSD: printf.c,v 1.34 2009/10/13 19:28:31 christos Exp $);
+__RCSID($NetBSD: printf.c,v 1.35 2011/03/15 23:11:49 christos Exp $);
 #endif
 #endif /* not lint */
 
@@ -154,7 +154,7 @@
 	format = *argv;
 	gargv = ++argv;
 
-#define SKIP1	#-+ 0
+#define SKIP1	#-+ 0'
 #define SKIP2	0123456789
 	do {
 		/*



CVS commit: src/usr.bin/printf

2011-03-15 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Mar 15 23:11:49 UTC 2011

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
support grouping format.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/usr.bin/printf

2010-04-05 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Mon Apr  5 21:24:14 UTC 2010

Modified Files:
src/usr.bin/printf: printf.1

Log Message:
\\*(Pm - \*(Pm


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/usr.bin/printf/printf.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.1
diff -u src/usr.bin/printf/printf.1:1.23 src/usr.bin/printf/printf.1:1.24
--- src/usr.bin/printf/printf.1:1.23	Wed Mar 11 13:57:39 2009
+++ src/usr.bin/printf/printf.1	Mon Apr  5 21:24:14 2010
@@ -1,4 +1,4 @@
-.\	$NetBSD: printf.1,v 1.23 2009/03/11 13:57:39 joerg Exp $
+.\	$NetBSD: printf.1,v 1.24 2010/04/05 21:24:14 joerg Exp $
 .\
 .\ Copyright (c) 1989, 1990, 1993
 .\	The Regents of the University of California.  All rights reserved.
@@ -277,7 +277,7 @@
 .Ar argument
 is printed in the style
 .Sm off
-.Pf [\-]d Cm \. No ddd Cm e No \\*(Pmdd
+.Pf [\-]d Cm \. No ddd Cm e No \*(Pmdd
 .Sm on
 where there
 is one digit before the decimal point and the number after is equal to



CVS commit: src/usr.bin/printf

2009-10-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Tue Oct 13 19:28:31 UTC 2009

Modified Files:
src/usr.bin/printf: printf.c

Log Message:
Avoid segv on printf '%*s' 666, from Maksymilian Arciemowicz


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/printf/printf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/printf/printf.c
diff -u src/usr.bin/printf/printf.c:1.33 src/usr.bin/printf/printf.c:1.34
--- src/usr.bin/printf/printf.c:1.33	Mon Jul 21 10:19:24 2008
+++ src/usr.bin/printf/printf.c	Tue Oct 13 15:28:31 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: printf.c,v 1.33 2008/07/21 14:19:24 lukem Exp $	*/
+/*	$NetBSD: printf.c,v 1.34 2009/10/13 19:28:31 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = @(#)printf.c	8.2 (Berkeley) 3/22/95;
 #else
-__RCSID($NetBSD: printf.c,v 1.33 2008/07/21 14:19:24 lukem Exp $);
+__RCSID($NetBSD: printf.c,v 1.34 2009/10/13 19:28:31 christos Exp $);
 #endif
 #endif /* not lint */
 
@@ -155,7 +155,7 @@
 	gargv = ++argv;
 
 #define SKIP1	#-+ 0
-#define SKIP2	*0123456789
+#define SKIP2	0123456789
 	do {
 		/*
 		 * Basic algorithm is to scan the format string for conversion
@@ -185,13 +185,23 @@
 
 			/* skip to field width */
 			fmt += strspn(fmt, SKIP1);
-			fieldwidth = *fmt == '*' ? getwidth() : -1;
+			if (*fmt == '*') {
+fmt++;
+fieldwidth = getwidth();
+			} else
+fieldwidth = -1;
 
 			/* skip to possible '.', get following precision */
 			fmt += strspn(fmt, SKIP2);
-			if (*fmt == '.')
-++fmt;
-			precision = *fmt == '*' ? getwidth() : -1;
+			if (*fmt == '.') {
+fmt++;
+if (*fmt == '*') {
+	fmt++;
+	precision = getwidth();
+} else
+	precision = -1;
+			} else
+precision = -1;
 
 			fmt += strspn(fmt, SKIP2);