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N@hF,%P%$%H%"%I%l%9(B
$B!&(Bargv[3] $B$O(B "2011" $B$H$$$&J8;zNs$,F~$C$?(B5$B%P%$%H$NNN0h$N@hF,%P%$%H%"%I%l%9!J>!
$B!J$=$l$>$l#1%P%$%HB?$$$h$&$J5$$,$9$k$N$O!"@hJ}$,J8;zNs$N:G8e$KJ8;zNs=*C<$rI=$9(B'\0'$B$r$/$C$D$1$F$/$l$F$$$k$+$i$G$9(B)
$B$I$&$^$:$$$+!"
$ gcc -Wall -o 02-01-OpenFile-NotSoGood 02-01-OpenFile-NotSoGood.c
$ emacs abc.txt ($B$J$K$+:n@.$7$F$_$^$7$g$&!#:#2s$OCf?H$O6u$G$b9=$$$^$;$s!#(B)
$ ls ($B>/$J$/$H$b(B 02-01-OpenFile-NotSoGood, 02-01-OpenFile-NotSoGood.c, abc.txt$B$O$"$k$O$:(B)
$ ./02-01-OpenFile-NotSoGood abc.txt ($B$^$H$b$KF0$/Nc(B)
$ ./02-01-OpenFile-NotSoGood ($BMn$A$^$9(B)
$ ./02-01-OpenFile-NotSoGood def.txt ($BMn$A$^$9(B)
$ chmod u-r abc.txt ($B%f!<%6<+?H$G$9$i(Babc.txt$B$rFI$a$J$$>uBV$K$9$k$H!E(B)
$ ./02-01-OpenFile-NotSoGood abc.txt (abc.txt$B$r;XDj$7$F$bMn$A$k$h$&$K$J$j$^$9(B)
$ chmod u+r abc.txt ($B%f!<%6<+?H$G(Babc.txt$B$rFI$a$k$h$&$KLa$9$H!E(B)
$ ./02-01-OpenFile-NotSoGood abc.txt ($B$^$H$b$KF0$/$h$&$KLa$C$F$-$^$7$?(B)
$B$3$l$i$N%;%0%a%s%F!<%7%g%s%U%)!<%k%H$O9T57$N$h$$%W%m%0%i%`$r=q$1$P!J@5$7$/H~$7$$(BC$B8@8l$r=q$1$P!K$9$Y$F2sHr$G$-$^$9!#(B
$B$^$?!"3'$5$s$O$3$&$7$?%/%j%F%#%+%k%(%i!<$r2sHr$9$Y$-$G$9!#(B
02.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