xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/stat.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate    require './test.pl';	# for which_perl() etc
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gateuse Config;
10*0Sstevel@tonic-gateuse File::Spec;
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gateplan tests => 78;
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gatemy $Perl = which_perl();
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gate$Is_Amiga   = $^O eq 'amigaos';
17*0Sstevel@tonic-gate$Is_Cygwin  = $^O eq 'cygwin';
18*0Sstevel@tonic-gate$Is_Darwin  = $^O eq 'darwin';
19*0Sstevel@tonic-gate$Is_Dos     = $^O eq 'dos';
20*0Sstevel@tonic-gate$Is_MacOS   = $^O eq 'MacOS';
21*0Sstevel@tonic-gate$Is_MPE     = $^O eq 'mpeix';
22*0Sstevel@tonic-gate$Is_MSWin32 = $^O eq 'MSWin32';
23*0Sstevel@tonic-gate$Is_NetWare = $^O eq 'NetWare';
24*0Sstevel@tonic-gate$Is_OS2     = $^O eq 'os2';
25*0Sstevel@tonic-gate$Is_Solaris = $^O eq 'solaris';
26*0Sstevel@tonic-gate$Is_VMS     = $^O eq 'VMS';
27*0Sstevel@tonic-gate$Is_DGUX    = $^O eq 'dgux';
28*0Sstevel@tonic-gate$Is_MPRAS   = $^O =~ /svr4/ && -f '/etc/.relid';
29*0Sstevel@tonic-gate$Is_Rhapsody= $^O eq 'rhapsody';
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate$Is_Dosish  = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin;
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate$Is_UFS     = $Is_Darwin && (() = `df -t ufs .`) == 2;
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gatemy($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
36*0Sstevel@tonic-gate   $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gatemy $Curdir = File::Spec->curdir;
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gatemy $tmpfile = 'Op_stat.tmp';
42*0Sstevel@tonic-gatemy $tmpfile_link = $tmpfile.'2';
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate1 while unlink $tmpfile;
46*0Sstevel@tonic-gateopen(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
47*0Sstevel@tonic-gateclose FOO;
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gateopen(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gatemy($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
52*0Sstevel@tonic-gateSKIP: {
53*0Sstevel@tonic-gate    skip "No link count", 1 if $Is_VMS;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate    is($nlink, 1, 'nlink on regular file');
56*0Sstevel@tonic-gate}
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gateSKIP: {
59*0Sstevel@tonic-gate  skip "mtime and ctime not reliable", 2
60*0Sstevel@tonic-gate    if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos or $Is_MacOS;
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate  ok( $mtime,           'mtime' );
63*0Sstevel@tonic-gate  is( $mtime, $ctime,   'mtime == ctime' );
64*0Sstevel@tonic-gate}
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate# Cygwin seems to have a 3 second granularity on its timestamps.
68*0Sstevel@tonic-gatemy $funky_FAT_timestamps = $Is_Cygwin;
69*0Sstevel@tonic-gatesleep 3 if $funky_FAT_timestamps;
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gateprint FOO "Now is the time for all good men to come to.\n";
72*0Sstevel@tonic-gateclose(FOO);
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gatesleep 2 unless $funky_FAT_timestamps;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gateSKIP: {
78*0Sstevel@tonic-gate    unlink $tmpfile_link;
79*0Sstevel@tonic-gate    my $lnk_result = eval { link $tmpfile, $tmpfile_link };
80*0Sstevel@tonic-gate    skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate    is( $@, '',         'link() implemented' );
83*0Sstevel@tonic-gate    ok( $lnk_result,    'linked tmp testfile' );
84*0Sstevel@tonic-gate    ok( chmod(0644, $tmpfile),             'chmoded tmp testfile' );
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate    my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate    SKIP: {
89*0Sstevel@tonic-gate        skip "No link count", 1 if $Config{dont_use_nlink};
90*0Sstevel@tonic-gate        skip "Cygwin9X fakes hard links by copying", 1
91*0Sstevel@tonic-gate          if $Config{myuname} =~ /^cygwin_(?:9\d|me)\b/i;
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate        is($nlink, 2,     'Link count on hard linked file' );
94*0Sstevel@tonic-gate    }
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate    SKIP: {
97*0Sstevel@tonic-gate        my $cwd = File::Spec->rel2abs($Curdir);
98*0Sstevel@tonic-gate        skip "Solaris tmpfs has different mtime/ctime link semantics", 2
99*0Sstevel@tonic-gate                                     if $Is_Solaris and $cwd =~ m#^/tmp# and
100*0Sstevel@tonic-gate                                        $mtime && $mtime == $ctime;
101*0Sstevel@tonic-gate        skip "AFS has different mtime/ctime link semantics", 2
102*0Sstevel@tonic-gate                                     if $cwd =~ m#$Config{'afsroot'}/#;
103*0Sstevel@tonic-gate        skip "AmigaOS has different mtime/ctime link semantics", 2
104*0Sstevel@tonic-gate                                     if $Is_Amiga;
105*0Sstevel@tonic-gate        # Win32 could pass $mtime test but as FAT and NTFS have
106*0Sstevel@tonic-gate        # no ctime concept $ctime is ALWAYS == $mtime
107*0Sstevel@tonic-gate        # expect netware to be the same ...
108*0Sstevel@tonic-gate        skip "No ctime concept on this OS", 2
109*0Sstevel@tonic-gate                                     if $Is_MSWin32 ||
110*0Sstevel@tonic-gate                                        ($Is_Darwin && $Is_UFS);
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate        if( !ok($mtime, 'hard link mtime') ||
113*0Sstevel@tonic-gate            !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
114*0Sstevel@tonic-gate            print STDERR <<DIAG;
115*0Sstevel@tonic-gate# Check if you are on a tmpfs of some sort.  Building in /tmp sometimes
116*0Sstevel@tonic-gate# has this problem.  Building on the ClearCase VOBS filesystem may also
117*0Sstevel@tonic-gate# cause this failure.
118*0Sstevel@tonic-gate#
119*0Sstevel@tonic-gate# Darwin's UFS doesn't have a ctime concept, and thus is expected to fail
120*0Sstevel@tonic-gate# this test.
121*0Sstevel@tonic-gateDIAG
122*0Sstevel@tonic-gate        }
123*0Sstevel@tonic-gate    }
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate}
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate# truncate and touch $tmpfile.
128*0Sstevel@tonic-gateopen(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
129*0Sstevel@tonic-gateclose F;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gateok(-z $tmpfile,     '-z on empty file');
132*0Sstevel@tonic-gateok(! -s $tmpfile,   '   and -s');
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gateopen(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
135*0Sstevel@tonic-gateprint F "hi\n";
136*0Sstevel@tonic-gateclose F;
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gateok(! -z $tmpfile,   '-z on non-empty file');
139*0Sstevel@tonic-gateok(-s $tmpfile,     '   and -s');
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate# Strip all access rights from the file.
143*0Sstevel@tonic-gateok( chmod(0000, $tmpfile),     'chmod 0000' );
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gateSKIP: {
146*0Sstevel@tonic-gate    skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate    SKIP: {
149*0Sstevel@tonic-gate        # Going to try to switch away from root.  Might not work.
150*0Sstevel@tonic-gate        my $olduid = $>;
151*0Sstevel@tonic-gate        eval { $> = 1; };
152*0Sstevel@tonic-gate        skip "Can't test -r or -w meaningfully if you're superuser", 2
153*0Sstevel@tonic-gate          if $> == 0;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate        SKIP: {
156*0Sstevel@tonic-gate            skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin;
157*0Sstevel@tonic-gate            ok(!-r $tmpfile,    "   -r");
158*0Sstevel@tonic-gate        }
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate        ok(!-w $tmpfile,    "   -w");
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate        # switch uid back (may not be implemented)
163*0Sstevel@tonic-gate        eval { $> = $olduid; };
164*0Sstevel@tonic-gate    }
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate    ok(! -x $tmpfile,   '   -x');
167*0Sstevel@tonic-gate}
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate# in ms windows, $tmpfile inherits owner uid from directory
173*0Sstevel@tonic-gate# not sure about os/2, but chown is harmless anyway
174*0Sstevel@tonic-gateeval { chown $>,$tmpfile; 1 } or print "# $@" ;
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gateok(chmod(0700,$tmpfile),    'chmod 0700');
177*0Sstevel@tonic-gateok(-r $tmpfile,     '   -r');
178*0Sstevel@tonic-gateok(-w $tmpfile,     '   -w');
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gateSKIP: {
181*0Sstevel@tonic-gate    skip "-x simply determines if a file ends in an executable suffix", 1
182*0Sstevel@tonic-gate      if $Is_Dosish || $Is_MacOS;
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate    ok(-x $tmpfile,     '   -x');
185*0Sstevel@tonic-gate}
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gateok(  -f $tmpfile,   '   -f');
188*0Sstevel@tonic-gateok(! -d $tmpfile,   '   !-d');
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate# Is this portable?
191*0Sstevel@tonic-gateok(  -d $Curdir,          '-d cwd' );
192*0Sstevel@tonic-gateok(! -f $Curdir,          '!-f cwd' );
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gateSKIP: {
196*0Sstevel@tonic-gate    unlink($tmpfile_link);
197*0Sstevel@tonic-gate    my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
198*0Sstevel@tonic-gate    skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate    is( $@, '',     'symlink() implemented' );
201*0Sstevel@tonic-gate    ok( $symlink_rslt,      'symlink() ok' );
202*0Sstevel@tonic-gate    ok(-l $tmpfile_link,    '-l');
203*0Sstevel@tonic-gate}
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gateok(-o $tmpfile,     '-o');
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gateok(-e $tmpfile,     '-e');
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gateunlink($tmpfile_link);
210*0Sstevel@tonic-gateok(! -e $tmpfile_link,  '   -e on unlinked file');
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gateSKIP: {
213*0Sstevel@tonic-gate    skip "No character, socket or block special files", 6
214*0Sstevel@tonic-gate      if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
215*0Sstevel@tonic-gate    skip "/dev isn't available to test against", 6
216*0Sstevel@tonic-gate      unless -d '/dev' && -r '/dev' && -x '/dev';
217*0Sstevel@tonic-gate    skip "Skipping: unexpected ls output in MP-RAS", 6
218*0Sstevel@tonic-gate      if $Is_MPRAS;
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate    my $LS  = $Config{d_readlink} ? "ls -lL" : "ls -l";
221*0Sstevel@tonic-gate    my $CMD = "$LS /dev 2>/dev/null";
222*0Sstevel@tonic-gate    my $DEV = qx($CMD);
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate    skip "$CMD failed", 6 if $DEV eq '';
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate    my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate    skip "opendir failed: $!", 6 if @DEV == 0;
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate    # /dev/stdout might be either character special or a named pipe,
231*0Sstevel@tonic-gate    # or a symlink, or a socket, depending on which OS and how are
232*0Sstevel@tonic-gate    # you running the test, so let's censor that one away.
233*0Sstevel@tonic-gate    # Similar remarks hold for stderr.
234*0Sstevel@tonic-gate    $DEV =~ s{^[cpls].+?\sstdout$}{}m;
235*0Sstevel@tonic-gate    @DEV =  grep { $_ ne 'stdout' } @DEV;
236*0Sstevel@tonic-gate    $DEV =~ s{^[cpls].+?\sstderr$}{}m;
237*0Sstevel@tonic-gate    @DEV =  grep { $_ ne 'stderr' } @DEV;
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate    # /dev/printer is also naughty: in IRIX it shows up as
240*0Sstevel@tonic-gate    # Srwx-----, not srwx------.
241*0Sstevel@tonic-gate    $DEV =~ s{^.+?\sprinter$}{}m;
242*0Sstevel@tonic-gate    @DEV =  grep { $_ ne 'printer' } @DEV;
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate    # If running as root, we will see .files in the ls result,
245*0Sstevel@tonic-gate    # and readdir() will see them always.  Potential for conflict,
246*0Sstevel@tonic-gate    # so let's weed them out.
247*0Sstevel@tonic-gate    $DEV =~ s{^.+?\s\..+?$}{}m;
248*0Sstevel@tonic-gate    @DEV =  grep { ! m{^\..+$} } @DEV;
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate    # Irix ls -l marks sockets with 'S' while 's' is a 'XENIX semaphore'.
251*0Sstevel@tonic-gate    if ($^O eq 'irix') {
252*0Sstevel@tonic-gate        $DEV =~ s{^S(.+?)}{s$1}mg;
253*0Sstevel@tonic-gate    }
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate    my $try = sub {
256*0Sstevel@tonic-gate	my @c1 = eval qq[\$DEV =~ /^$_[0].*/mg];
257*0Sstevel@tonic-gate	my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
258*0Sstevel@tonic-gate	my $c1 = scalar @c1;
259*0Sstevel@tonic-gate	my $c2 = scalar @c2;
260*0Sstevel@tonic-gate	is($c1, $c2, "ls and $_[1] agreeing on /dev ($c1 $c2)");
261*0Sstevel@tonic-gate    };
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gateSKIP: {
264*0Sstevel@tonic-gate    skip("DG/UX ls -L broken", 3) if $Is_DGUX;
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate    $try->('b', '-b');
267*0Sstevel@tonic-gate    $try->('c', '-c');
268*0Sstevel@tonic-gate    $try->('s', '-S');
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate}
271*0Sstevel@tonic-gate
272*0Sstevel@tonic-gateok(! -b $Curdir,    '!-b cwd');
273*0Sstevel@tonic-gateok(! -c $Curdir,    '!-c cwd');
274*0Sstevel@tonic-gateok(! -S $Curdir,    '!-S cwd');
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate}
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gateSKIP: {
279*0Sstevel@tonic-gate    my($cnt, $uid);
280*0Sstevel@tonic-gate    $cnt = $uid = 0;
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate    # Find a set of directories that's very likely to have setuid files
283*0Sstevel@tonic-gate    # but not likely to be *all* setuid files.
284*0Sstevel@tonic-gate    my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
285*0Sstevel@tonic-gate    skip "Can't find a setuid file to test with", 3 unless @bin;
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate    for my $bin (@bin) {
288*0Sstevel@tonic-gate        opendir BIN, $bin or die "Can't opendir $bin: $!";
289*0Sstevel@tonic-gate        while (defined($_ = readdir BIN)) {
290*0Sstevel@tonic-gate            $_ = "$bin/$_";
291*0Sstevel@tonic-gate            $cnt++;
292*0Sstevel@tonic-gate            $uid++ if -u;
293*0Sstevel@tonic-gate            last if $uid && $uid < $cnt;
294*0Sstevel@tonic-gate        }
295*0Sstevel@tonic-gate    }
296*0Sstevel@tonic-gate    closedir BIN;
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate    skip "No setuid programs", 3 if $uid == 0;
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate    isnt($cnt, 0,    'found some programs');
301*0Sstevel@tonic-gate    isnt($uid, 0,    '  found some setuid programs');
302*0Sstevel@tonic-gate    ok($uid < $cnt,  "    they're not all setuid");
303*0Sstevel@tonic-gate}
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate# To assist in automated testing when a controlling terminal (/dev/tty)
307*0Sstevel@tonic-gate# may not be available (at, cron  rsh etc), the PERL_SKIP_TTY_TEST env var
308*0Sstevel@tonic-gate# can be set to skip the tests that need a tty.
309*0Sstevel@tonic-gateSKIP: {
310*0Sstevel@tonic-gate    skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate    my $TTY = $Is_Rhapsody ? "/dev/ttyp0" : "/dev/tty";
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate    SKIP: {
315*0Sstevel@tonic-gate        skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
316*0Sstevel@tonic-gate        skip "No TTY to test -t with", 2 unless -e $TTY;
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate        open(TTY, $TTY) ||
319*0Sstevel@tonic-gate          warn "Can't open $TTY--run t/TEST outside of make.\n";
320*0Sstevel@tonic-gate        ok(-t TTY,  '-t');
321*0Sstevel@tonic-gate        ok(-c TTY,  'tty is -c');
322*0Sstevel@tonic-gate        close(TTY);
323*0Sstevel@tonic-gate    }
324*0Sstevel@tonic-gate    ok(! -t TTY,    '!-t on closed TTY filehandle');
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate    {
327*0Sstevel@tonic-gate        local $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
328*0Sstevel@tonic-gate        ok(-t,          '-t on STDIN');
329*0Sstevel@tonic-gate    }
330*0Sstevel@tonic-gate}
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gatemy $Null = File::Spec->devnull;
333*0Sstevel@tonic-gateSKIP: {
334*0Sstevel@tonic-gate    skip "No null device to test with", 1 unless -e $Null;
335*0Sstevel@tonic-gate    skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate    open(NULL, $Null) or DIE("Can't open $Null: $!");
338*0Sstevel@tonic-gate    ok(! -t NULL,   'null device is not a TTY');
339*0Sstevel@tonic-gate    close(NULL);
340*0Sstevel@tonic-gate}
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gate# These aren't strictly "stat" calls, but so what?
344*0Sstevel@tonic-gatemy $statfile = File::Spec->catfile($Curdir, 'op', 'stat.t');
345*0Sstevel@tonic-gateok(  -T $statfile,    '-T');
346*0Sstevel@tonic-gateok(! -B $statfile,    '!-B');
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gateSKIP: {
349*0Sstevel@tonic-gate     skip("DG/UX", 1) if $Is_DGUX;
350*0Sstevel@tonic-gateok(-B $Perl,      '-B');
351*0Sstevel@tonic-gate}
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gateok(! -T $Perl,    '!-T');
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gateopen(FOO,$statfile);
356*0Sstevel@tonic-gateSKIP: {
357*0Sstevel@tonic-gate    eval { -T FOO; };
358*0Sstevel@tonic-gate    skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate    is( $@, '',     '-T on filehandle causes no errors' );
361*0Sstevel@tonic-gate
362*0Sstevel@tonic-gate    ok(-T FOO,      '   -T');
363*0Sstevel@tonic-gate    ok(! -B FOO,    '   !-B');
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate    $_ = <FOO>;
366*0Sstevel@tonic-gate    like($_, qr/perl/, 'after readline');
367*0Sstevel@tonic-gate    ok(-T FOO,      '   still -T');
368*0Sstevel@tonic-gate    ok(! -B FOO,    '   still -B');
369*0Sstevel@tonic-gate    close(FOO);
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate    open(FOO,$statfile);
372*0Sstevel@tonic-gate    $_ = <FOO>;
373*0Sstevel@tonic-gate    like($_, qr/perl/,      'reopened and after readline');
374*0Sstevel@tonic-gate    ok(-T FOO,      '   still -T');
375*0Sstevel@tonic-gate    ok(! -B FOO,    '   still !-B');
376*0Sstevel@tonic-gate
377*0Sstevel@tonic-gate    ok(seek(FOO,0,0),   'after seek');
378*0Sstevel@tonic-gate    ok(-T FOO,          '   still -T');
379*0Sstevel@tonic-gate    ok(! -B FOO,        '   still !-B');
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate    # It's documented this way in perlfunc *shrug*
382*0Sstevel@tonic-gate    () = <FOO>;
383*0Sstevel@tonic-gate    ok(eof FOO,         'at EOF');
384*0Sstevel@tonic-gate    ok(-T FOO,          '   still -T');
385*0Sstevel@tonic-gate    ok(-B FOO,          '   now -B');
386*0Sstevel@tonic-gate}
387*0Sstevel@tonic-gateclose(FOO);
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate
390*0Sstevel@tonic-gateSKIP: {
391*0Sstevel@tonic-gate    skip "No null device to test with", 2 unless -e $Null;
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate    ok(-T $Null,  'null device is -T');
394*0Sstevel@tonic-gate    ok(-B $Null,  '    and -B');
395*0Sstevel@tonic-gate}
396*0Sstevel@tonic-gate
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gate# and now, a few parsing tests:
399*0Sstevel@tonic-gate$_ = $tmpfile;
400*0Sstevel@tonic-gateok(-f,      'bare -f   uses $_');
401*0Sstevel@tonic-gateok(-f(),    '     -f() "');
402*0Sstevel@tonic-gate
403*0Sstevel@tonic-gateunlink $tmpfile or print "# unlink failed: $!\n";
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate# bug id 20011101.069
406*0Sstevel@tonic-gatemy @r = \stat($Curdir);
407*0Sstevel@tonic-gateis(scalar @r, 13,   'stat returns full 13 elements');
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gateSKIP: {
410*0Sstevel@tonic-gate    skip "No lstat", 4 unless $Config{d_lstat};
411*0Sstevel@tonic-gate
412*0Sstevel@tonic-gate    stat $0;
413*0Sstevel@tonic-gate    eval { lstat _ };
414*0Sstevel@tonic-gate    like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
415*0Sstevel@tonic-gate	'lstat _ croaks after stat' );
416*0Sstevel@tonic-gate    eval { -l _ };
417*0Sstevel@tonic-gate    like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
418*0Sstevel@tonic-gate	'-l _ croaks after stat' );
419*0Sstevel@tonic-gate
420*0Sstevel@tonic-gate    # bug id 20020124.004
421*0Sstevel@tonic-gate    # If we have d_lstat, we should have symlink()
422*0Sstevel@tonic-gate    my $linkname = 'dolzero';
423*0Sstevel@tonic-gate    symlink $0, $linkname or die "# Can't symlink $0: $!";
424*0Sstevel@tonic-gate    lstat $linkname;
425*0Sstevel@tonic-gate    -T _;
426*0Sstevel@tonic-gate    eval { lstat _ };
427*0Sstevel@tonic-gate    like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
428*0Sstevel@tonic-gate	'lstat croaks after -T _' );
429*0Sstevel@tonic-gate    eval { -l _ };
430*0Sstevel@tonic-gate    like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
431*0Sstevel@tonic-gate	'-l _ croaks after -T _' );
432*0Sstevel@tonic-gate    unlink $linkname or print "# unlink $linkname failed: $!\n";
433*0Sstevel@tonic-gate}
434*0Sstevel@tonic-gate
435*0Sstevel@tonic-gateprint "# Zzz...\n";
436*0Sstevel@tonic-gatesleep(3);
437*0Sstevel@tonic-gatemy $f = 'tstamp.tmp';
438*0Sstevel@tonic-gateunlink $f;
439*0Sstevel@tonic-gateok (open(S, "> $f"), 'can create tmp file');
440*0Sstevel@tonic-gateclose S or die;
441*0Sstevel@tonic-gatemy @a = stat $f;
442*0Sstevel@tonic-gateprint "# time=$^T, stat=(@a)\n";
443*0Sstevel@tonic-gatemy @b = (-M _, -A _, -C _);
444*0Sstevel@tonic-gateprint "# -MAC=(@b)\n";
445*0Sstevel@tonic-gateok( (-M _) < 0, 'negative -M works');
446*0Sstevel@tonic-gateok( (-A _) < 0, 'negative -A works');
447*0Sstevel@tonic-gateok( (-C _) < 0, 'negative -C works');
448*0Sstevel@tonic-gateok(unlink($f), 'unlink tmp file');
449*0Sstevel@tonic-gate
450*0Sstevel@tonic-gateEND {
451*0Sstevel@tonic-gate    1 while unlink $tmpfile;
452*0Sstevel@tonic-gate}
453