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

Re: zsh malloc bug



A real example follows.

1283 Z% touch ,x
1284 Z% echo '  
import blah.*;
' | ./prepend ,x      
,x.TMP-prepend-21041
zsh: done       echo ' import '$package'.*; ' | 
zsh: bus error  ./prepend ,x
1285 Z% 

======= prepend
#!/bin/zsh

commandName=${0##*/}

usage() {
	if [[ $1 != '' ]] ; then echo 1>&2 "\n$1" ; fi
	echo 1>&2 "
Usage: $commandName [ -t ] file ...

Prepends stdin to each of the given files.
Reads stdin only once.

The -t option preserves each file's modify time.
"
	exit 2
}

zparseopts -D -K - -help=argHelp v=argVerbose t=argPreserveMtime

if [[ $#argHelp != 0 ]] ; then
	usage
fi

case $1 in
-*) usage "Unknown option: $1"
	;;
esac

#-----------------------------------------

sourceTemp=/tmp/$commandName.$$
cat > $sourceTemp
# I would have preferred this, but it discards trailing newlines:
# source=`cat`

TRAPINT() {
	echo 1>&2 "$commandName aborting: interrupted at file $file"
	rm -f $tmp
	exit 2
}

TRAPEXIT() {
	rm -f $sourceTemp
}

for file in $*
do
	if [[ -d $file ]] ; then
		echo 1>&2 "$commandName: skipping directory $file"
	else
		fileDir=${0%/*}
		fileName=${0##*/}
		tmp=$file.TMP-$commandName-$$
		echo $tmp
		cat $sourceTemp >  $tmp \
		&& cat $file	>> $tmp
		if [[ $? != 0 ]] ; then
			echo 1>&2 "$commandName aborting: trouble at file $file"
			rm -f $tmp
			exit 2
		else
			if [[ $#argPreserveMtime != 0 ]] ; then
				cpt -N $file $tmp
			fi
			mv -f $tmp $file
			if [[ $? != 0 ]] ; then
				echo 1>&2 "$commandName aborting: trouble at file $file"
				rm -f $tmp
				exit 2
			fi
			if [[ $#argVerbose != 0 ]] ; then
				echo $file
			fi
		fi
	fi
done



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