1*9a7741deSElliott Hughesecho T.argv: misc tests of argc and argv 2*9a7741deSElliott Hughes 3*9a7741deSElliott Hughesawk=${awk-../a.out} 4*9a7741deSElliott Hughes 5*9a7741deSElliott Hughesecho >foo1 6*9a7741deSElliott Hughesecho >foo2 7*9a7741deSElliott Hughes$awk ' 8*9a7741deSElliott HughesBEGIN { 9*9a7741deSElliott Hughes for (i = 1; i < ARGC-1; i++) 10*9a7741deSElliott Hughes printf "%s ", ARGV[i] 11*9a7741deSElliott Hughes if (ARGC > 1) 12*9a7741deSElliott Hughes printf "%s", ARGV[i] 13*9a7741deSElliott Hughes printf "\n" 14*9a7741deSElliott Hughes exit 15*9a7741deSElliott Hughes}' * >foo1 16*9a7741deSElliott Hughesecho * >foo2 17*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (echo1 *)' 18*9a7741deSElliott Hughes 19*9a7741deSElliott Hughes$awk ' 20*9a7741deSElliott HughesBEGIN { 21*9a7741deSElliott Hughes for (i = 1; i < ARGC; i++) { 22*9a7741deSElliott Hughes printf "%s", ARGV[i] 23*9a7741deSElliott Hughes if (i < ARGC-1) 24*9a7741deSElliott Hughes printf " " 25*9a7741deSElliott Hughes } 26*9a7741deSElliott Hughes printf "\n" 27*9a7741deSElliott Hughes exit 28*9a7741deSElliott Hughes}' * >foo1 29*9a7741deSElliott Hughesecho * >foo2 30*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (echo2 *)' 31*9a7741deSElliott Hughes 32*9a7741deSElliott Hughes$awk ' 33*9a7741deSElliott HughesBEGIN { 34*9a7741deSElliott Hughes print ARGC 35*9a7741deSElliott Hughes ARGV[ARGC-1] = "" 36*9a7741deSElliott Hughes for (i=0; i < ARGC; i++) 37*9a7741deSElliott Hughes print ARGV[i] 38*9a7741deSElliott Hughes exit 39*9a7741deSElliott Hughes} 40*9a7741deSElliott Hughes' a bc def gh >foo1 41*9a7741deSElliott Hughesecho "5 42*9a7741deSElliott Hughes$awk 43*9a7741deSElliott Hughesa 44*9a7741deSElliott Hughesbc 45*9a7741deSElliott Hughesdef 46*9a7741deSElliott Hughes" >foo2 47*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (argc *)' 48*9a7741deSElliott Hughes 49*9a7741deSElliott Hughesecho '1 50*9a7741deSElliott Hughes2 51*9a7741deSElliott Hughes3' >foo0 52*9a7741deSElliott Hughesecho 'foo1 53*9a7741deSElliott Hughesfoo2 54*9a7741deSElliott Hughesfoo3' >foo1 55*9a7741deSElliott Hughes$awk '{print L $0}' L=foo <foo0 >foo2 56*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (L=foo <foo1)' 57*9a7741deSElliott Hughes 58*9a7741deSElliott Hughesecho '1 59*9a7741deSElliott Hughes2 60*9a7741deSElliott Hughes3' >foo0 61*9a7741deSElliott Hughesecho 'foo1 62*9a7741deSElliott Hughesfoo2 63*9a7741deSElliott Hughesfoo3' >foo1 64*9a7741deSElliott Hughes$awk '{print L $0}' L=foo foo0 >foo2 65*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (L=foo foo1)' 66*9a7741deSElliott Hughes 67*9a7741deSElliott Hughesecho '1 68*9a7741deSElliott Hughes2 69*9a7741deSElliott Hughes3' >foo0 70*9a7741deSElliott Hughesecho 'foo1 71*9a7741deSElliott Hughesfoo2 72*9a7741deSElliott Hughesfoo3' >foo1 73*9a7741deSElliott Hughescat foo0 | $awk '{print L $0}' L=foo - >foo2 74*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (L=foo -)' 75*9a7741deSElliott Hughes 76*9a7741deSElliott Hughesecho '1 77*9a7741deSElliott Hughes2 78*9a7741deSElliott Hughes3' >foo0 79*9a7741deSElliott Hughesecho 'foo1 80*9a7741deSElliott Hughesfoo2 81*9a7741deSElliott Hughesfoo3 82*9a7741deSElliott Hughesglop1 83*9a7741deSElliott Hughesglop2 84*9a7741deSElliott Hughesglop3' >foo1 85*9a7741deSElliott Hughes$awk '{print L $0}' L=foo foo0 L=glop foo0 >foo2 86*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (L=foo L=glop)' 87*9a7741deSElliott Hughes 88*9a7741deSElliott Hughesecho '1 89*9a7741deSElliott Hughes2 90*9a7741deSElliott Hughes3' >foo0 91*9a7741deSElliott Hughesecho '111 92*9a7741deSElliott Hughes112 93*9a7741deSElliott Hughes113 94*9a7741deSElliott Hughes221 95*9a7741deSElliott Hughes222 96*9a7741deSElliott Hughes223' >foo1 97*9a7741deSElliott Hughes$awk '{print L $0}' L=11 foo0 L=22 foo0 >foo2 98*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (L=11 L=22)' 99*9a7741deSElliott Hughes 100*9a7741deSElliott Hughesecho >foo0 101*9a7741deSElliott Hughesecho 'name=value 102*9a7741deSElliott Hughesname=value' >foo1 103*9a7741deSElliott Hughes$awk 'BEGIN { print ARGV[1] } { print ARGV[1] }' name=value foo0 >foo2 104*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv assignment operand modified' 105*9a7741deSElliott Hughes 106*9a7741deSElliott Hughesecho 3.345 >foo1 107*9a7741deSElliott Hughes$awk 'BEGIN { print ARGV[1] + ARGV[2]}' 1 2.345 >foo2 108*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (ARGV[1] + ARGV[2])' 109*9a7741deSElliott Hughes 110*9a7741deSElliott Hughesecho 3.345 >foo1 111*9a7741deSElliott Hughesx1=1 x2=2.345 $awk 'BEGIN { print ENVIRON["x1"] + ENVIRON["x2"]}' 1 2.345 >foo2 112*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv (ENVIRON[x1] + ENVIRON[x2])' 113*9a7741deSElliott Hughes 114*9a7741deSElliott Hughes 115*9a7741deSElliott Hughesecho 'foo1' >foo1 116*9a7741deSElliott Hughesecho 'foo2' >foo2 117*9a7741deSElliott Hughesecho 'foo3' >foo3 118*9a7741deSElliott Hughes$awk 'BEGIN { ARGV[2] = "" } 119*9a7741deSElliott Hughes { print }' foo1 foo2 foo3 >foo4 120*9a7741deSElliott Hughesecho 'foo1 121*9a7741deSElliott Hughesfoo3' >foo5 122*9a7741deSElliott Hughesdiff foo4 foo5 || echo 'BAD: T.argv zap ARGV[2]' 123*9a7741deSElliott Hughes 124*9a7741deSElliott Hughesecho hi > foo1 ; mv foo1 foo2 125*9a7741deSElliott Hughes$awk 'BEGIN { ARGV[1] = "foo2" ; print FILENAME } 126*9a7741deSElliott Hughes { print FILENAME }' foo1 >foo3 127*9a7741deSElliott Hughesecho ' 128*9a7741deSElliott Hughesfoo2' >foo4 129*9a7741deSElliott Hughesdiff foo3 foo4 || echo 'BAD: T.argv startup FILENAME' 130*9a7741deSElliott Hughes # assumes that startup FILENAME is "" 131*9a7741deSElliott Hughes 132*9a7741deSElliott Hughes 133*9a7741deSElliott Hughes# test data balanced on pinhead... 134*9a7741deSElliott Hughesecho 'ARGV[3] is /dev/null 135*9a7741deSElliott HughesARGV[0] is ../a.out 136*9a7741deSElliott HughesARGV[1] is /dev/null' >foo1 137*9a7741deSElliott Hughes 138*9a7741deSElliott Hughes$awk 'BEGIN { # this is a variant of arnolds original example 139*9a7741deSElliott Hughes ARGV[1] = "/dev/null" 140*9a7741deSElliott Hughes ARGV[2] = "glotch" # file open must skipped deleted argv 141*9a7741deSElliott Hughes ARGV[3] = "/dev/null" 142*9a7741deSElliott Hughes ARGC = 4 143*9a7741deSElliott Hughes delete ARGV[2] 144*9a7741deSElliott Hughes} 145*9a7741deSElliott Hughes# note that input is read here 146*9a7741deSElliott HughesEND { 147*9a7741deSElliott Hughes for (i in ARGV) 148*9a7741deSElliott Hughes printf("ARGV[%d] is %s\n", i, ARGV[i]) 149*9a7741deSElliott Hughes}' >foo2 150*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.argv delete ARGV[2]' 151*9a7741deSElliott Hughes 152*9a7741deSElliott Hughes# deleting ARGV used to trigger a use-after-free crash when awk 153*9a7741deSElliott Hughes# iterates over it to read files. 154*9a7741deSElliott Hughesecho >foo1 155*9a7741deSElliott Hughesecho >foo2 156*9a7741deSElliott Hughesecho >foo3 157*9a7741deSElliott Hughes 158*9a7741deSElliott Hughes$awk 'BEGIN { 159*9a7741deSElliott Hughes delete ARGV 160*9a7741deSElliott Hughes ARGV[0] = "awk" 161*9a7741deSElliott Hughes ARGV[1] = "/dev/null" 162*9a7741deSElliott Hughes ARGC = 2 163*9a7741deSElliott Hughes} { 164*9a7741deSElliott Hughes # this should not be executed 165*9a7741deSElliott Hughes print "FILENAME: " FILENAME 166*9a7741deSElliott Hughes fflush() 167*9a7741deSElliott Hughes}' foo1 foo2 foo3 >foo4 168*9a7741deSElliott Hughes 169*9a7741deSElliott Hughesawkstatus=$? 170*9a7741deSElliott Hughesdiff /dev/null foo4 171*9a7741deSElliott Hughesif [ $? -ne 0 ] || [ $awkstatus -ne 0 ]; then 172*9a7741deSElliott Hughes echo 'BAD: T.argv delete ARGV' 173*9a7741deSElliott Hughesfi 174