From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14923 invoked from network); 9 Aug 2005 13:54:48 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 9 Aug 2005 13:54:48 -0000 Received: (qmail 31129 invoked from network); 9 Aug 2005 13:54:42 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 9 Aug 2005 13:54:42 -0000 Received: (qmail 7884 invoked by alias); 9 Aug 2005 13:54:39 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 21588 Received: (qmail 7875 invoked from network); 9 Aug 2005 13:54:38 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 9 Aug 2005 13:54:38 -0000 Received: (qmail 30853 invoked from network); 9 Aug 2005 13:54:38 -0000 Received: from imap.gmx.net (HELO mail.gmx.net) (213.165.64.20) by a.mx.sunsite.dk with SMTP; 9 Aug 2005 13:54:34 -0000 Received: (qmail invoked by alias); 09 Aug 2005 13:54:33 -0000 Received: from Bc894.b.pppool.de (EHLO pcdahl4201) [213.7.200.148] by mail.gmx.net (mp034) with SMTP; 09 Aug 2005 15:54:33 +0200 X-Authenticated: #21620914 Message-ID: <007901c59ceb$2bbbc220$9aa4fea9@pcdahl4201> From: "Thorsten Dahlheimer" To: "Wayne Davison" Cc: References: <024501c59c46$2ae19dc0$cc74fea9@pcdahl4201> <20050808230902.GC2986@blorf.net> Subject: Re: PATCH: Slightly improve printf %s Date: Tue, 9 Aug 2005 16:03:32 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 X-Y-GMX-Trusted: 0 X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00, RCVD_IN_SORBS_WEB autolearn=ham version=3.0.4 Wayne Davison wrote: > On Mon, Aug 08, 2005 at 08:21:28PM +0200, Thorsten Dahlheimer wrote: > > After I made this change, the test suite uncovered a bug (which I > > introduced) in the handling of %b (and now %s) when no argument is > > left, so the patch includes a fix for that, too. > > I didn't see that fix in the patch. I assume you're talking about > this line (which is executed when the arg is missing): > > count += fprintf(fout, "%*c", width, ' '); > > ... since that would output a single space, even when "width" is 0. I > changed the "else" that is in front of that line to "else if (width)", > and it fixes the problem (I could have also changed the line to use > fprintf(fout, "%*s", width, ""), but the former seems a litle more > optimal). Sorry, I attached the wrong diff (the one that I made before I discovered that bug). Anyway, the fix I made is exactly the one you've described. > As for the change to how %s is handled, can you show me an example where > this is needed? In my simple tests, if a variable has a literal null in > it, using either %b or %s to print it out with printf truncates the > variable at the null (and, of course, a backslash-zero can be passed to > %b to get a real null, but %s doesn't evaluate backslashes). I don't know if it's really needed, but it would certainly be more consistent. Currently you get: % s=$'a\0b' % print -n $s |od -tx1 0000000 61 00 62 0000003 % printf $s |od -tx1 0000000 61 00 62 0000003 % printf '%b' $s |od -tx1 0000000 61 00 62 0000003 % printf '%s' $s |od -tx1 0000000 61 0000001 (Note that the behaviour of %b in this case changed as a consequence of zsh-workers/21552, item 4.) Given that the first three variants treat the nul just like any other character, and the same is true for the rest of zsh, I find the behaviour of %s a bit surprising. I think it's conceivable that someone processes strings containing nuls (say, captured output from 'find ... -print0' or the like) and finally writes them out with print; this all works well with zsh. Then they change the output command from 'print -nr -- "$s"' to 'printf "...%s..." ... "$s" ...' and get bitten. Regards, Thorsten Dahlheimer