Hi all, a friend of mine is working on a script which we want to be portable
between Linux and FreeBSD (it utilizes several external programs to do its
work). The last thing we've tripped on seems like a show-stopper.

We need to echo a string *and* a NUL character (\0) into a stream so that a
program that delimits its input by \0 characters will do the right thing.

He had been doing this via printf(1) like so:

  % printf "some string\0" | some_process

however, it never worked under FreeBSD. Upon further inspection, our printf(1)
does not work like the GNU one with the above string. Consider this from a
linux box:

  linux [~]<374>% printf "foo\0bar\0" | od -c
  0000000   f   o   o  \0   b   a   r  \0

Now the same command from a FreeBSD (4.7-STABLE) box:

  freebsd [~]<76>% printf "foo\0bar\0" | od -c
  0000000    f   o   o                                                    

After checking the man pages, I also used a complete octal constant but that
doesn't work either:

  linux [~]<376>% printf "foo\0000" | od -c
  0000000   f   o   o  \0

  freebsd [~]<77>% printf "foo\0000" | od -c
  0000000    f   o   o                                                    

I checked our implementation and it seems wrong. The first step done in the
source code is to interpolate all escape sequences. However, when it does this
and the octal number happens to be "0", this fact is not "captured" later on
and that 0 becomes the NULL terminator for the string and nothing else is
printed after it.

  linux [~]<379>% printf "foo\0%d" 4 | od -c
  0000000   f   o   o  \0   4
  freebsd [~]<78>% printf "foo\0%d" 4 | od -c
  printf: missing format character
  0000000    f   o   o                                                    

This behavior has been checked on HP-UX and Solaris and those two systems are
identical to the Linux one. It seems to me that our printf(1)'s behavior is
incorrect. Comments?


|John Reynolds       Sr. Component Design Engineer - ICG/EID/Si Engineering |
|Intel Corporation   MS: CH6-302   Phone: 480-554-9092   pgr: 602-868-6512  |
|[EMAIL PROTECTED]  http://www-aec.ch.intel.com/~jreynold/       |

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-questions" in the body of the message

Reply via email to