123f24377SWarner Loshecho T.argv: misc tests of argc and argv 223f24377SWarner Losh 323f24377SWarner Loshawk=${awk-../a.out} 423f24377SWarner Losh 523f24377SWarner Loshecho >foo1 623f24377SWarner Loshecho >foo2 723f24377SWarner Losh$awk ' 823f24377SWarner LoshBEGIN { 923f24377SWarner Losh for (i = 1; i < ARGC-1; i++) 1023f24377SWarner Losh printf "%s ", ARGV[i] 1123f24377SWarner Losh if (ARGC > 1) 1223f24377SWarner Losh printf "%s", ARGV[i] 1323f24377SWarner Losh printf "\n" 1423f24377SWarner Losh exit 1523f24377SWarner Losh}' * >foo1 1623f24377SWarner Loshecho * >foo2 1723f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (echo1 *)' 1823f24377SWarner Losh 1923f24377SWarner Losh$awk ' 2023f24377SWarner LoshBEGIN { 2123f24377SWarner Losh for (i = 1; i < ARGC; i++) { 2223f24377SWarner Losh printf "%s", ARGV[i] 2323f24377SWarner Losh if (i < ARGC-1) 2423f24377SWarner Losh printf " " 2523f24377SWarner Losh } 2623f24377SWarner Losh printf "\n" 2723f24377SWarner Losh exit 2823f24377SWarner Losh}' * >foo1 2923f24377SWarner Loshecho * >foo2 3023f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (echo2 *)' 3123f24377SWarner Losh 3223f24377SWarner Losh$awk ' 3323f24377SWarner LoshBEGIN { 3423f24377SWarner Losh print ARGC 3523f24377SWarner Losh ARGV[ARGC-1] = "" 3623f24377SWarner Losh for (i=0; i < ARGC; i++) 3723f24377SWarner Losh print ARGV[i] 3823f24377SWarner Losh exit 3923f24377SWarner Losh} 4023f24377SWarner Losh' a bc def gh >foo1 4123f24377SWarner Loshecho "5 4223f24377SWarner Losh$awk 4323f24377SWarner Losha 4423f24377SWarner Loshbc 4523f24377SWarner Loshdef 4623f24377SWarner Losh" >foo2 4723f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (argc *)' 4823f24377SWarner Losh 4923f24377SWarner Loshecho '1 5023f24377SWarner Losh2 5123f24377SWarner Losh3' >foo0 5223f24377SWarner Loshecho 'foo1 5323f24377SWarner Loshfoo2 5423f24377SWarner Loshfoo3' >foo1 5523f24377SWarner Losh$awk '{print L $0}' L=foo <foo0 >foo2 5623f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (L=foo <foo1)' 5723f24377SWarner Losh 5823f24377SWarner Loshecho '1 5923f24377SWarner Losh2 6023f24377SWarner Losh3' >foo0 6123f24377SWarner Loshecho 'foo1 6223f24377SWarner Loshfoo2 6323f24377SWarner Loshfoo3' >foo1 6423f24377SWarner Losh$awk '{print L $0}' L=foo foo0 >foo2 6523f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (L=foo foo1)' 6623f24377SWarner Losh 6723f24377SWarner Loshecho '1 6823f24377SWarner Losh2 6923f24377SWarner Losh3' >foo0 7023f24377SWarner Loshecho 'foo1 7123f24377SWarner Loshfoo2 7223f24377SWarner Loshfoo3' >foo1 7323f24377SWarner Loshcat foo0 | $awk '{print L $0}' L=foo - >foo2 7423f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (L=foo -)' 7523f24377SWarner Losh 7623f24377SWarner Loshecho '1 7723f24377SWarner Losh2 7823f24377SWarner Losh3' >foo0 7923f24377SWarner Loshecho 'foo1 8023f24377SWarner Loshfoo2 8123f24377SWarner Loshfoo3 8223f24377SWarner Loshglop1 8323f24377SWarner Loshglop2 8423f24377SWarner Loshglop3' >foo1 8523f24377SWarner Losh$awk '{print L $0}' L=foo foo0 L=glop foo0 >foo2 8623f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (L=foo L=glop)' 8723f24377SWarner Losh 8823f24377SWarner Loshecho '1 8923f24377SWarner Losh2 9023f24377SWarner Losh3' >foo0 9123f24377SWarner Loshecho '111 9223f24377SWarner Losh112 9323f24377SWarner Losh113 9423f24377SWarner Losh221 9523f24377SWarner Losh222 9623f24377SWarner Losh223' >foo1 9723f24377SWarner Losh$awk '{print L $0}' L=11 foo0 L=22 foo0 >foo2 9823f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (L=11 L=22)' 9923f24377SWarner Losh 100f32a6403SWarner Loshecho >foo0 101f32a6403SWarner Loshecho 'name=value 102f32a6403SWarner Loshname=value' >foo1 103f32a6403SWarner Losh$awk 'BEGIN { print ARGV[1] } { print ARGV[1] }' name=value foo0 >foo2 104f32a6403SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv assignment operand modified' 105f32a6403SWarner Losh 10623f24377SWarner Loshecho 3.345 >foo1 10723f24377SWarner Losh$awk 'BEGIN { print ARGV[1] + ARGV[2]}' 1 2.345 >foo2 10823f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (ARGV[1] + ARGV[2])' 10923f24377SWarner Losh 11023f24377SWarner Loshecho 3.345 >foo1 11123f24377SWarner Loshx1=1 x2=2.345 $awk 'BEGIN { print ENVIRON["x1"] + ENVIRON["x2"]}' 1 2.345 >foo2 11223f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv (ENVIRON[x1] + ENVIRON[x2])' 11323f24377SWarner Losh 11423f24377SWarner Losh 11523f24377SWarner Loshecho 'foo1' >foo1 11623f24377SWarner Loshecho 'foo2' >foo2 11723f24377SWarner Loshecho 'foo3' >foo3 11823f24377SWarner Losh$awk 'BEGIN { ARGV[2] = "" } 11923f24377SWarner Losh { print }' foo1 foo2 foo3 >foo4 12023f24377SWarner Loshecho 'foo1 12123f24377SWarner Loshfoo3' >foo5 12223f24377SWarner Loshdiff foo4 foo5 || echo 'BAD: T.argv zap ARGV[2]' 12323f24377SWarner Losh 12423f24377SWarner Loshecho hi > foo1 ; mv foo1 foo2 12523f24377SWarner Losh$awk 'BEGIN { ARGV[1] = "foo2" ; print FILENAME } 12623f24377SWarner Losh { print FILENAME }' foo1 >foo3 12723f24377SWarner Loshecho ' 12823f24377SWarner Loshfoo2' >foo4 12923f24377SWarner Loshdiff foo3 foo4 || echo 'BAD: T.argv startup FILENAME' 13023f24377SWarner Losh # assumes that startup FILENAME is "" 13123f24377SWarner Losh 13223f24377SWarner Losh 13323f24377SWarner Losh# test data balanced on pinhead... 13423f24377SWarner Loshecho 'ARGV[3] is /dev/null 13523f24377SWarner LoshARGV[0] is ../a.out 13623f24377SWarner LoshARGV[1] is /dev/null' >foo1 13723f24377SWarner Losh 13823f24377SWarner Losh$awk 'BEGIN { # this is a variant of arnolds original example 13923f24377SWarner Losh ARGV[1] = "/dev/null" 14023f24377SWarner Losh ARGV[2] = "glotch" # file open must skipped deleted argv 14123f24377SWarner Losh ARGV[3] = "/dev/null" 14223f24377SWarner Losh ARGC = 4 14323f24377SWarner Losh delete ARGV[2] 14423f24377SWarner Losh} 14523f24377SWarner Losh# note that input is read here 14623f24377SWarner LoshEND { 14723f24377SWarner Losh for (i in ARGV) 14823f24377SWarner Losh printf("ARGV[%d] is %s\n", i, ARGV[i]) 14923f24377SWarner Losh}' >foo2 15023f24377SWarner Loshdiff foo1 foo2 || echo 'BAD: T.argv delete ARGV[2]' 151*17853db4SWarner Losh 152*17853db4SWarner Losh# deleting ARGV used to trigger a use-after-free crash when awk 153*17853db4SWarner Losh# iterates over it to read files. 154*17853db4SWarner Loshecho >foo1 155*17853db4SWarner Loshecho >foo2 156*17853db4SWarner Loshecho >foo3 157*17853db4SWarner Losh 158*17853db4SWarner Losh$awk 'BEGIN { 159*17853db4SWarner Losh delete ARGV 160*17853db4SWarner Losh ARGV[0] = "awk" 161*17853db4SWarner Losh ARGV[1] = "/dev/null" 162*17853db4SWarner Losh ARGC = 2 163*17853db4SWarner Losh} { 164*17853db4SWarner Losh # this should not be executed 165*17853db4SWarner Losh print "FILENAME: " FILENAME 166*17853db4SWarner Losh fflush() 167*17853db4SWarner Losh}' foo1 foo2 foo3 >foo4 168*17853db4SWarner Losh 169*17853db4SWarner Loshawkstatus=$? 170*17853db4SWarner Loshdiff /dev/null foo4 171*17853db4SWarner Loshif [ $? -ne 0 ] || [ $awkstatus -ne 0 ]; then 172*17853db4SWarner Losh echo 'BAD: T.argv delete ARGV' 173*17853db4SWarner Loshfi 174