zsh-users
 help / color / mirror / code / Atom feed
From: Izumi Natsuka <izumi.natsuka@hotmail.com>
To: zsh-users@zsh.org
Subject: cat as a builtin command
Date: Fri, 29 Aug 2014 09:40:29 +0800	[thread overview]
Message-ID: <BLU436-SMTP2045383EF41756D3376DB3CF4DB0@phx.gbl> (raw)

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]. And I have put the following in my zshrc:

cat() {
	if [[ $# -le 1 ]] && [[ ${1:0:1} != '-' || ${1} == '-' ]];then
		local file
		exec {file}<${1:-0}
		read -Erd '' -u ${file}
		exec {file}>&-
	else
		command cat $@
	fi
}

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. As I don't known how to avoid that I tried to use another method(via sysread):

cat() {
	if [[ $# -le 1 ]] && [[ ${1:0:1} != '-' || ${1} == '-' ]];then
		local file REPLY
		exec {file}<${1:-0}
		if [[ -t 1 ]] && [[ $# -eq 0 || ${1} == '-' ]];then
			read -Erd '' -u ${file}
		else
			sysread -i ${file} -o 1 -s $(zstat +size ${1})
		fi
		exec {file}>&-
	else
		command cat $@
	fi
}

It works with raw files. Unfortunately this method doesn't avoid the use of subshell[2], it doesn't fit my need[1] and also doesn't work when you want to cat a fifo.

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

Izumi Natsuka

0: https://www.gnu.org/software/coreutils/manual/html_node/cat-invocation.html
1: https://www.ohnekontur.de/2013/02/17/making-cat-a-shell-builtin/
2: http://stackoverflow.com/questions/21976606


             reply	other threads:[~2014-08-29  1:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-29  1:40 Izumi Natsuka [this message]
2014-08-29 11:58 ` Roman Neuhauser
2014-08-29 12:24 ` Dirk Heinrichs
2014-08-29 13:20   ` Roman Neuhauser
2014-08-29 13:33     ` Dirk Heinrichs
2014-08-29 13:03 ` Peter Stephenson
2014-08-30 10:11   ` Mikael Magnusson
2014-09-01  7:53 ` Han Pingtian
2014-09-01 18:39   ` Bart Schaefer
2014-09-02  1:08     ` Han Pingtian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BLU436-SMTP2045383EF41756D3376DB3CF4DB0@phx.gbl \
    --to=izumi.natsuka@hotmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).