Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/rc/get_file.rc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc 


. /bin/maths.rc
. /bin/httplib.rc

fn length {
	HEAD $args $1 |[2] awk ' BEGIN { ranged=0} /Accept-Ranges: bytes/ { ranged = 1} /^Content-Length:/ { size =  $2 } END { if(ranged) { print size } else { print 0 } }'

}

fn got {
	ls -l $out  | awk '/[0-9]/ { print $6 ; exit} { print "0" } '
}


fn fetch {
	size = `{length $1}
	if(test $size -gt 0) {
		tries = 0
		red = `{got}
		while(test $red -lt $size ) {
			GET $args -r $red `{add $red $block} $1 >> $out >[2]/dev/null
			red = `{got}
		}
	}
}

fn usage {
	echo Usage : get_file.rc [-e referer]  [-b blocksize] -o outfile uri >[1=2]
	exit usage
}

out = ''
args = ()
block = 1024

while(~ $1 -*) {
	switch($1) {
	case -e :
		args = `{echo $args -e $2}
		shift 2
	case -b :
		block = $2
		shift 2
	case -o :
		out = $2
		shift 2
	}
}

test -z $out && usage
if(test -e $out) {
	if(test -w $out)
		fetch $1
}
if not {
	touch $out
	fetch $1
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.