xref: /openbsd/gnu/usr.bin/perl/t/io/nargv.t (revision f6aab3d8)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require "./test.pl";
6    set_up_inc('../lib');
7}
8
9print "1..7\n";
10
11my $j = 1;
12for $i ( 1,2,5,4,3 ) {
13    $file = mkfiles($i);
14    open(FH, "> $file") || die "can't create $file: $!";
15    print FH "not ok " . $j++ . "\n";
16    close(FH) || die "Can't close $file: $!";
17}
18
19
20{
21    local *ARGV;
22    local $^I = '.bak';
23    local $_;
24    @ARGV = mkfiles(1..3);
25    $n = 0;
26    while (<>) {
27	print STDOUT "# initial \@ARGV: [@ARGV]\n";
28	if ($n++ == 2) {
29	    other();
30	}
31	show();
32    }
33}
34
35$^I = undef;
36@ARGV = mkfiles(1..3);
37$n = 0;
38while (<>) {
39    print STDOUT "#final \@ARGV: [@ARGV]\n";
40    if ($n++ == 2) {
41	other();
42    }
43    show();
44}
45
46# test setuid is preserved (and hopefully setgid)
47#
48# With nested in-place editing PL_oldname and PL_filemode would
49# be overwritten by the values for the last file in the nested
50# loop.  This is now all stored as magic in *ARGVOUT{IO}
51$^I = "";
52@ARGV = mkfiles(1..3);
53my $sidfile = $ARGV[1];
54chmod(04600, $sidfile);
55my $mode = (stat $ARGV[1])[2];
56$n = 0;
57while (<>) {
58    print STDOUT "#final \@ARGV: [@ARGV]\n";
59    if ($n++ == 1) {
60	other();
61    }
62    print;
63}
64my $newmode = (stat $sidfile)[2];
65printf "# before %#o after %#o\n", $mode, $newmode;
66print +($mode == $newmode ? "" : "not "). "ok 6 # check setuid mode preserved\n";
67
68sub show {
69    #warn "$ARGV: $_";
70    s/^not //;
71    print;
72}
73
74sub other {
75    no warnings 'once';
76    print STDOUT "# Calling other\n";
77    local *ARGV;
78    local *ARGVOUT;
79    local $_;
80    @ARGV = mkfiles(5, 4);
81    while (<>) {
82	print STDOUT "# inner \@ARGV: [@ARGV]\n";
83	show();
84    }
85}
86
87{
88    # (perl #133314) directory handle leak
89    #
90    # We process a significant number of files here to make sure any
91    # leaks are significant
92    @ARGV = mkfiles(1 .. 10);
93    for my $file (@ARGV) {
94        open my $f, ">", $file;
95        print $f "\n";
96        close $f;
97    }
98    local $^I = ".bak";
99    local $_;
100    while (<>) {
101        s/^/foo/;
102    }
103}
104
105{
106    # (perl #133314) directory handle leak
107    # We open three handles here because the file processing opened:
108    #  - the original file
109    #  - the output file, and finally
110    #  - the directory
111    # so we need to open the first two to use up the slots used for the original
112    # and output files.
113    # This test assumes fd are allocated in the typical *nix way - lowest
114    # available, which I believe is the case for the Win32 CRTs too.
115    # If this turns out not to be the case this test will need to skip on
116    # such platforms or only run on a small set of known-good platforms.
117    my $tfile = mkfiles(1);
118    open my $f, "<", $tfile
119      or die "Cannot open temp: $!";
120    open my $f2, "<", $tfile
121      or die "Cannot open temp: $!";
122    open my $f3, "<", $tfile
123      or die "Cannot open temp: $!";
124    print +(fileno($f3) < 20 ? "ok" : "not ok"), " 7 check fd leak\n";
125    close $f;
126    close $f2;
127    close $f3;
128}
129
130
131my @files;
132sub mkfiles {
133    foreach (@_) {
134	$files[$_] ||= tempfile();
135    }
136    my @results = @files[@_];
137    return wantarray ? @results : @results[-1];
138}
139
140END { unlink_all map { ($_, "$_.bak") } @files }
141