printf bad ?
Lennart Sorensen
lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Fri Mar 18 20:31:34 UTC 2005
On Fri, Mar 18, 2005 at 10:14:33PM +0200, Peter wrote:
>
> Hi,
>
> can someone please try this out:
>
> printf "%05d" 08
>
> it says 'invalid number' for any number of 0's before the 8. Why does it
> think it's octal ?! Both the shell version and the executable have the
> same problem, so I guess it's a shell problem. This also explains
> problems I had some time ago composing a shell script on this machine
> (I started a thread on this list about that - some numbers used to
> disappear for no reason - the numbers were sequential filename in aa
> script).
>
> Again:
>
> /usr/bin/printf "%05d" 08
> /usr/bin/printf: 08: value not completely converted
>
> printf "%05d" 08
> -bash: printf: 08: invalid number
>
> printf "%05d" 10#08
> -bash: printf: 10#08: invalid number
>
> (according to the bash manual that notation should force decimal - it
> does not - not in quotes and not in ticks and not with a
> backslash-escaped '#').
>
> I wrote a small C program to test atoi, strtod and strtoul and the
> latter is broken (is it ?). E.g.:
>
> plp at plp:~/tc$ ./1 07
> atoi: 7
> strtod: 7.000000
> strtoul: 7
> plp at plp:~/tc$ ./1 08
> atoi: 8
> strtod: 8.000000
> strtoul: 0
>
> I take this to mean that strtoul() assumes the lowest conversion base it
> can use if the third argument is 0. This is a bug. It should look at the
> whole string and match the highest base it can, starting from the last
> character that is not convertible with any base, imho. My program linked
> (automatically) against libc.so.6 (0x40025000). This system is Debian
> based. The program is available (I will post it here if needed).
The way you do numbers on unix is this:
If it starts with 1-9 it is decimal. If it starts with 0x it is hex,
otherwise if it starts with 0 it is octal. Since 08 isn't valid octal
it should complain about it.
Lennart Sorensen
--
The Toronto Linux Users Group. Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
More information about the Legacy
mailing list