Le 22/03/16 18:43, Garrett D'Amore a écrit :
> 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
> 

Well, 'man' does say that it copies 'n' bytes, not characters.

I can dd a byte from /dev/zero and head -c 1 now returns

but it seems still buggy...
Truss shows it reads twice:
> open64("/tmp/fooz", O_RDONLY)                 = 3
> fstat64(3, 0x08047570)                                = 0
> fstat64(3, 0x08047470)                                = 0
> ioctl(3, TCGETA, 0x0804752E)                  Err#25 ENOTTY
> read(3, "\0", 4096)                           = 1
> read(3, 0x0806346C, 4096)                     = 0
> ioctl(1, TCGETA, 0x080467BE)                  = 0
> fstat64(1, 0x08046700)                                = 0
> read(3, 0x0806346C, 4096)                     = 0
> llseek(3, 0, SEEK_CUR)                                = 1
> _exit(0)

and if I try to do a 'wc -c'
> $ head -c 1 /tmp/fooz |wc -c
>        0
> $ sh -c 'head -c 1 /tmp/fooz |wc -c'
>        1
> $ ghead -c 1 /tmp/fooz |wc -c
>        1
just to compare to gnu head and sh builtin 

 


-- 
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