Running head on /dev/zero is … wrong.  Arguably without -c it should loop
forever until it crashes due to malloc() failure, or it should copy an
infinite sequence of zero bytes because it never reaches a newline.

The problem is that zero is a special character (end of string) — its
unclear to me what the correct behavior here should be.  What happens if
you embed zero bytes in a file?

Note that in POSIX, head lacks -c, so the behavior is not part of any
standard.

I *think* the “right” answer here is that NUL should be treated like any
other character, which precludes the use of normal C string handling
functions in head.  Even fgets(3C) and getline(3C) cannot be used.

Seriously, head and copy are for use in text files, not with arbitrary
binary data files, and so their handling (or mishandling) of binary data
should not come as a surprise.

That said, on BSD (Darwin) head -c 1 copies a single zero byte.  head
without -c just returns an error.  (Not sure what the error is, because I
can’t dtruss it in new Darwin.  Thanks Apple.)

 - Garrett


On Tue, Mar 22, 2016 at 10:19 AM, Richard PALO <rich...@netbsd.org> wrote:

> What I'm seeing is an infinite loop of the following (using /usr/bin/head):
> > truss  -u libc head -c 1 /dev/zero
> > ...
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1:   read(3, "\0\0\0\0\0\0\0\0\0\0\0\0".., 8192)     = 8192
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > /1@1: -> libc:fgets(0x8047680, 0x400, 0x80622b0, 0x0)
> > /1@1: <- libc:fgets() = 0x8047680
> > /1@1: -> libc:strlen()
> > /1@1: <- libc:strlen() = 0
> > /1@1: -> libc:printf()
> > /1@1: <- libc:printf() = 0
> > /1@1: -> libc:fflush(0x8062290, 0x8047680, 0x80622b0, 0x0)
> > /1@1: <- libc:fflush() = 0
> > ...
> and again and again
> 
> --
> Richard PALO
> 



-------------------------------------------
illumos-discuss
Archives: https://www.listbox.com/member/archive/182180/=now
RSS Feed: https://www.listbox.com/member/archive/rss/182180/21175430-2e6923be
Modify Your Subscription: 
https://www.listbox.com/member/?member_id=21175430&id_secret=21175430-6a77cda4
Powered by Listbox: http://www.listbox.com

Reply via email to