Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: cat as a builtin command



# izumi.natsuka@xxxxxxxxxxx / 2014-08-29 09:40:29 +0800:
> Hello, I'm going to write a shell function that provides a basic
> functionality (print the content of a file or stdin) of cat[0], in
> order to avoid forking too many process when I call it in a loop[1].

> 		exec {file}<${1:-0}
> 		read -Erd '' -u ${file}
 
> It works perfectly except when I want to cat a binary file:
> 
> $ zstat +size archlinux-2012.09.07-dual.iso
> 411041792
> $ cat archlinux-2012.09.07-dual.iso | wc -c
> 39
> 
> Seems that the file was cut by some special raw bytes.

i thought i could get it through with empty $IFS, alas, it stops on
the first ^M anyway.  which is what the manpage says:

  Read one line and break it into fields using the characters in $IFS
  as separators, except as noted below.

there *is* a way, it's just slow as hell:

  setopt localoptions nomultibyte
  declare -A st
  zstat -H st +size ${1:-0}
  read -Erd '' -u ${file} -k ${st[size]}

> As I don't known how to avoid that I tried to use another method (via
> sysread):

> 			sysread -i ${file} -o 1 -s $(zstat +size ${1})

see `zstat -H` above.

none of that solves the FIFO problem.

> Is there any way that can perform the basic functionality of cat
> without calling external command?

i'd go for a zsh module (see zshmodules(1)).  the sources for
zsh/example and zsh/files should give you a kickstart (i found
them useful for my needs).

-- 
roman



Messages sorted by: Reverse Date, Date, Thread, Author