This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #21442] only check link() error codes where link() works
[perl5.git] / t / op / stat.t
CommitLineData
8d063cd8
LW
1#!./perl
2
ea368a7c
CS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
2bc69dc4 6 require './test.pl'; # for which_perl() etc
ea368a7c
CS
7}
8
9use Config;
563ff921
NC
10
11my ($Null, $Curdir);
12if(eval {require File::Spec; 1}) {
13 $Null = File::Spec->devnull;
14 $Curdir = File::Spec->curdir;
15} else {
16 die $@ unless is_miniperl();
17 $Curdir = '.';
18 diag("miniperl failed to load File::Spec, error is:\n$@");
19 diag("\ncontinuing, assuming '.' for current directory. Some tests will be skipped.");
20}
21
ea368a7c 22
daa30a68 23plan tests => 113;
8d063cd8 24
2bc69dc4 25my $Perl = which_perl();
b5fe401b 26
d4046c48
NC
27$ENV{LC_ALL} = 'C'; # Forge English error messages.
28$ENV{LANGUAGE} = 'C'; # Ditto in GNU.
29
cc25fa79
MS
30$Is_Amiga = $^O eq 'amigaos';
31$Is_Cygwin = $^O eq 'cygwin';
be4e88b6 32$Is_Darwin = $^O eq 'darwin';
cc25fa79 33$Is_Dos = $^O eq 'dos';
68dc0745 34$Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 35$Is_NetWare = $^O eq 'NetWare';
cc25fa79
MS
36$Is_OS2 = $^O eq 'os2';
37$Is_Solaris = $^O eq 'solaris';
de5a37b2 38$Is_VMS = $^O eq 'VMS';
ca37ab50 39$Is_MPRAS = $^O =~ /svr4/ && -f '/etc/.relid';
7e0262e5 40$Is_Android = $^O =~ /android/;
cc25fa79 41
b595cd4b 42$Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare;
cc25fa79 43
822f7be5 44$Is_UFS = $Is_Darwin && (() = `df -t ufs . 2>/dev/null`) == 2;
be4e88b6 45
b595cd4b
RU
46if ($Is_Cygwin) {
47 require Win32;
48 Win32->import;
49}
50
cc25fa79
MS
51my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
52 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
d48672a2 53
1c25d394
NC
54my $tmpfile = tempfile();
55my $tmpfile_link = tempfile();
4435c477 56
295d5f02 57chmod 0666, $tmpfile;
c291be4e 58unlink_all $tmpfile;
c4fbe247 59open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
de5a37b2 60close FOO;
8d220878 61
c4fbe247 62open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
4435c477 63
cc25fa79 64my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
16ed4686 65
67fcfcf9
JL
66# The clock on a network filesystem might be different from the
67# system clock.
68my $Filesystem_Time_Offset = abs($mtime - time);
69
9accb295 70#nlink should if link support configured in Perl.
b5bfebd7 71SKIP: {
9accb295
CB
72 skip "No link count - Hard link support not built in.", 1
73 unless $Config{d_link};
b5bfebd7
MS
74
75 is($nlink, 1, 'nlink on regular file');
76}
8d220878 77
cc25fa79 78SKIP: {
d5b53b20 79 skip "mtime and ctime not reliable", 2
7b903762 80 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos or $Is_Darwin;
cc25fa79
MS
81
82 ok( $mtime, 'mtime' );
83 is( $mtime, $ctime, 'mtime == ctime' );
4435c477 84}
8d063cd8 85
cc25fa79
MS
86
87# Cygwin seems to have a 3 second granularity on its timestamps.
88my $funky_FAT_timestamps = $Is_Cygwin;
89sleep 3 if $funky_FAT_timestamps;
90
91print FOO "Now is the time for all good men to come to.\n";
92close(FOO);
93
151269f6 94sleep 2;
cc25fa79
MS
95
96
97SKIP: {
98 unlink $tmpfile_link;
de5a37b2
MS
99 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
100 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
cc25fa79 101
de5a37b2
MS
102 is( $@, '', 'link() implemented' );
103 ok( $lnk_result, 'linked tmp testfile' );
cc25fa79
MS
104 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
105
106 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
107
108 SKIP: {
109 skip "No link count", 1 if $Config{dont_use_nlink};
f672c027 110 skip "Cygwin9X fakes hard links by copying", 1
7d38e28d 111 if $Config{myuname} =~ /^cygwin_(?:9\d|me)\b/i;
f672c027 112
cc25fa79
MS
113 is($nlink, 2, 'Link count on hard linked file' );
114 }
115
116 SKIP: {
563ff921 117 skip_if_miniperl("File::Spec not built for minitest", 2);
de5a37b2 118 my $cwd = File::Spec->rel2abs($Curdir);
d5b53b20
NIS
119 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
120 if $Is_Solaris and $cwd =~ m#^/tmp# and
cc25fa79
MS
121 $mtime && $mtime == $ctime;
122 skip "AFS has different mtime/ctime link semantics", 2
123 if $cwd =~ m#$Config{'afsroot'}/#;
124 skip "AmigaOS has different mtime/ctime link semantics", 2
125 if $Is_Amiga;
d5b53b20
NIS
126 # Win32 could pass $mtime test but as FAT and NTFS have
127 # no ctime concept $ctime is ALWAYS == $mtime
128 # expect netware to be the same ...
129 skip "No ctime concept on this OS", 2
be4e88b6
MS
130 if $Is_MSWin32 ||
131 ($Is_Darwin && $Is_UFS);
132
cc25fa79
MS
133 if( !ok($mtime, 'hard link mtime') ||
134 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
be4e88b6 135 print STDERR <<DIAG;
d5b53b20 136# Check if you are on a tmpfs of some sort. Building in /tmp sometimes
61967be2 137# has this problem. Building on the ClearCase VOBS filesystem may also
cc25fa79 138# cause this failure.
61967be2
AL
139#
140# Darwin's UFS doesn't have a ctime concept, and thus is expected to fail
141# this test.
cc25fa79
MS
142DIAG
143 }
144 }
145
3fe9a6f1 146}
8d063cd8 147
cc25fa79 148# truncate and touch $tmpfile.
c4fbe247 149open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
bda6ed21
PM
150ok(-z \*F, '-z on empty filehandle');
151ok(! -s \*F, ' and -s');
cc25fa79
MS
152close F;
153
154ok(-z $tmpfile, '-z on empty file');
155ok(! -s $tmpfile, ' and -s');
156
c4fbe247 157open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
cc25fa79
MS
158print F "hi\n";
159close F;
160
bda6ed21
PM
161open(F, "<$tmpfile") || DIE("Can't open temp test file: $!");
162ok(!-z *F, '-z on empty filehandle');
163ok( -s *F, ' and -s');
164close F;
165
cc25fa79
MS
166ok(! -z $tmpfile, '-z on non-empty file');
167ok(-s $tmpfile, ' and -s');
168
169
170# Strip all access rights from the file.
171ok( chmod(0000, $tmpfile), 'chmod 0000' );
172
173SKIP: {
de5a37b2 174 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
cc25fa79
MS
175
176 SKIP: {
de5a37b2
MS
177 # Going to try to switch away from root. Might not work.
178 my $olduid = $>;
179 eval { $> = 1; };
d5b53b20 180 skip "Can't test -r or -w meaningfully if you're superuser", 2
b595cd4b 181 if ($Is_Cygwin ? Win32::IsAdminUser : $> == 0);
de5a37b2
MS
182
183 SKIP: {
b595cd4b 184 skip "Can't test -r meaningfully?", 1 if $Is_Dos;
de5a37b2
MS
185 ok(!-r $tmpfile, " -r");
186 }
cc25fa79 187
de5a37b2 188 ok(!-w $tmpfile, " -w");
cc25fa79 189
de5a37b2
MS
190 # switch uid back (may not be implemented)
191 eval { $> = $olduid; };
192 }
193
194 ok(! -x $tmpfile, ' -x');
a245ea2d 195}
cc25fa79 196
de5a37b2 197
cc25fa79 198
cc25fa79
MS
199ok(chmod(0700,$tmpfile), 'chmod 0700');
200ok(-r $tmpfile, ' -r');
201ok(-w $tmpfile, ' -w');
202
203SKIP: {
61967be2 204 skip "-x simply determines if a file ends in an executable suffix", 1
7b903762 205 if $Is_Dosish;
cc25fa79
MS
206
207 ok(-x $tmpfile, ' -x');
6eb13c3b 208}
cc25fa79
MS
209
210ok( -f $tmpfile, ' -f');
211ok(! -d $tmpfile, ' !-d');
212
213# Is this portable?
563ff921
NC
214ok( -d '.', '-d cwd' );
215ok(! -f '.', '!-f cwd' );
de5a37b2 216
cc25fa79
MS
217
218SKIP: {
de5a37b2
MS
219 unlink($tmpfile_link);
220 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
221 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
cc25fa79 222
de5a37b2
MS
223 is( $@, '', 'symlink() implemented' );
224 ok( $symlink_rslt, 'symlink() ok' );
225 ok(-l $tmpfile_link, '-l');
6eb13c3b 226}
cc25fa79
MS
227
228ok(-o $tmpfile, '-o');
229
230ok(-e $tmpfile, '-e');
de5a37b2
MS
231
232unlink($tmpfile_link);
cc25fa79
MS
233ok(! -e $tmpfile_link, ' -e on unlinked file');
234
235SKIP: {
feef4889 236 skip "No character, socket or block special files", 6
cc25fa79 237 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
feef4889 238 skip "/dev isn't available to test against", 6
3fcc9fea 239 unless -d '/dev' && -r '/dev' && -x '/dev';
61967be2 240 skip "Skipping: unexpected ls output in MP-RAS", 6
ca37ab50 241 if $Is_MPRAS;
de5a37b2 242
16ed4686
JM
243 # VMS problem: If GNV or other UNIX like tool is installed, then
244 # sometimes Perl will find /bin/ls, and will try to run it.
245 # But since Perl on VMS does not know to run it under Bash, it will
246 # try to run the DCL verb LS. And if the VMS product Language
247 # Sensitive Editor is installed, or some other LS verb, that will
248 # be run instead. So do not do this until we can teach Perl
249 # when to use BASH on VMS.
250 skip "ls command not available to Perl in OpenVMS right now.", 6
251 if $Is_VMS;
252
3aa1294d 253 delete $ENV{CLICOLOR_FORCE};
7e0262e5 254 my $LS = $Config{d_readlink} && !$Is_Android ? "ls -lL" : "ls -l";
eb1102fc 255 my $CMD = "$LS /dev 2>/dev/null";
a7ec4029
JH
256 my $DEV = qx($CMD);
257
feef4889 258 skip "$CMD failed", 6 if $DEV eq '';
a7ec4029 259
3fcc9fea
JH
260 my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
261
feef4889 262 skip "opendir failed: $!", 6 if @DEV == 0;
3fcc9fea 263
9aa9a1a6 264 # /dev/stdout might be either character special or a named pipe,
db8ab41e
JH
265 # or a symlink, or a socket, depending on which OS and how are
266 # you running the test, so let's censor that one away.
6320a86a 267 # Similar remarks hold for stderr.
db8ab41e 268 $DEV =~ s{^[cpls].+?\sstdout$}{}m;
267513dc 269 @DEV = grep { $_ ne 'stdout' } @DEV;
6320a86a
AD
270 $DEV =~ s{^[cpls].+?\sstderr$}{}m;
271 @DEV = grep { $_ ne 'stderr' } @DEV;
267513dc 272
c7974a0a
JH
273 # /dev/printer is also naughty: in IRIX it shows up as
274 # Srwx-----, not srwx------.
275 $DEV =~ s{^.+?\sprinter$}{}m;
276 @DEV = grep { $_ ne 'printer' } @DEV;
277
267513dc
JH
278 # If running as root, we will see .files in the ls result,
279 # and readdir() will see them always. Potential for conflict,
280 # so let's weed them out.
281 $DEV =~ s{^.+?\s\..+?$}{}m;
282 @DEV = grep { ! m{^\..+$} } @DEV;
9aa9a1a6 283
085a16fc
JH
284 # Irix ls -l marks sockets with 'S' while 's' is a 'XENIX semaphore'.
285 if ($^O eq 'irix') {
286 $DEV =~ s{^S(.+?)}{s$1}mg;
287 }
288
3fcc9fea 289 my $try = sub {
9f966f7a 290 my @c1 = eval qq[\$DEV =~ /^$_[0].*/mg];
3fcc9fea
JH
291 my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
292 my $c1 = scalar @c1;
293 my $c2 = scalar @c2;
9aa9a1a6 294 is($c1, $c2, "ls and $_[1] agreeing on /dev ($c1 $c2)");
3fcc9fea
JH
295 };
296
8d87852b 297{
3fcc9fea
JH
298 $try->('b', '-b');
299 $try->('c', '-c');
300 $try->('s', '-S');
378cc40b
LW
301}
302
3fcc9fea 303ok(! -b $Curdir, '!-b cwd');
de5a37b2
MS
304ok(! -c $Curdir, '!-c cwd');
305ok(! -S $Curdir, '!-S cwd');
cc25fa79 306
95036ac7
JH
307}
308
cc25fa79 309SKIP: {
cc25fa79
MS
310 my($cnt, $uid);
311 $cnt = $uid = 0;
312
313 # Find a set of directories that's very likely to have setuid files
314 # but not likely to be *all* setuid files.
315 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
de5a37b2 316 skip "Can't find a setuid file to test with", 3 unless @bin;
cc25fa79
MS
317
318 for my $bin (@bin) {
319 opendir BIN, $bin or die "Can't opendir $bin: $!";
320 while (defined($_ = readdir BIN)) {
321 $_ = "$bin/$_";
322 $cnt++;
323 $uid++ if -u;
324 last if $uid && $uid < $cnt;
325 }
326 }
327 closedir BIN;
328
2304a331
JH
329 skip "No setuid programs", 3 if $uid == 0;
330
331 isnt($cnt, 0, 'found some programs');
332 isnt($uid, 0, ' found some setuid programs');
333 ok($uid < $cnt, " they're not all setuid");
378cc40b 334}
b8440792 335
378cc40b 336
3e3baf6d
TB
337# To assist in automated testing when a controlling terminal (/dev/tty)
338# may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
339# can be set to skip the tests that need a tty.
cc25fa79
MS
340SKIP: {
341 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
342
7eb35c03 343 my $TTY = "/dev/tty";
cc25fa79
MS
344
345 SKIP: {
346 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
347 skip "No TTY to test -t with", 2 unless -e $TTY;
348
d5b53b20 349 open(TTY, $TTY) ||
cc25fa79
MS
350 warn "Can't open $TTY--run t/TEST outside of make.\n";
351 ok(-t TTY, '-t');
352 ok(-c TTY, 'tty is -c');
353 close(TTY);
3e3baf6d 354 }
cc25fa79 355 ok(! -t TTY, '!-t on closed TTY filehandle');
28e8237b
MS
356
357 {
358 local $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
359 ok(-t, '-t on STDIN');
360 }
68dc0745 361}
cc25fa79 362
cc25fa79 363SKIP: {
de5a37b2 364 skip "No null device to test with", 1 unless -e $Null;
d5b53b20 365 skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
cc25fa79 366
c4fbe247 367 open(NULL, $Null) or DIE("Can't open $Null: $!");
de5a37b2 368 ok(! -t NULL, 'null device is not a TTY');
cc25fa79 369 close(NULL);
378cc40b 370}
cc25fa79 371
378cc40b
LW
372
373# These aren't strictly "stat" calls, but so what?
563ff921 374my $statfile = './op/stat.t';
dc459aad
JH
375ok( -T $statfile, '-T');
376ok(! -B $statfile, '!-B');
b5fe401b
MS
377ok(-B $Perl, '-B');
378ok(! -T $Perl, '!-T');
378cc40b 379
dc459aad 380open(FOO,$statfile);
cc25fa79
MS
381SKIP: {
382 eval { -T FOO; };
de5a37b2 383 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
cc25fa79
MS
384
385 is( $@, '', '-T on filehandle causes no errors' );
386
387 ok(-T FOO, ' -T');
388 ok(! -B FOO, ' !-B');
389
f0fcb552 390 $_ = <FOO>;
1d662fb6 391 like($_, qr/perl/, 'after readline');
cc25fa79
MS
392 ok(-T FOO, ' still -T');
393 ok(! -B FOO, ' still -B');
f0fcb552
LW
394 close(FOO);
395
dc459aad 396 open(FOO,$statfile);
f0fcb552 397 $_ = <FOO>;
1d662fb6 398 like($_, qr/perl/, 'reopened and after readline');
cc25fa79
MS
399 ok(-T FOO, ' still -T');
400 ok(! -B FOO, ' still !-B');
401
402 ok(seek(FOO,0,0), 'after seek');
de5a37b2
MS
403 ok(-T FOO, ' still -T');
404 ok(! -B FOO, ' still !-B');
405
406 # It's documented this way in perlfunc *shrug*
407 () = <FOO>;
408 ok(eof FOO, 'at EOF');
409 ok(-T FOO, ' still -T');
410 ok(-B FOO, ' now -B');
f0fcb552
LW
411}
412close(FOO);
378cc40b 413
cc25fa79 414
de5a37b2
MS
415SKIP: {
416 skip "No null device to test with", 2 unless -e $Null;
417
418 ok(-T $Null, 'null device is -T');
419 ok(-B $Null, ' and -B');
420}
cc25fa79 421
108ed793
SM
422
423# and now, a few parsing tests:
cc25fa79
MS
424$_ = $tmpfile;
425ok(-f, 'bare -f uses $_');
426ok(-f(), ' -f() "');
108ed793 427
cc25fa79 428unlink $tmpfile or print "# unlink failed: $!\n";
58d95175
RG
429
430# bug id 20011101.069
dc459aad 431my @r = \stat($Curdir);
cc25fa79 432is(scalar @r, 13, 'stat returns full 13 elements');
5c9aa243 433
049f818b
YST
434stat $0;
435eval { lstat _ };
436like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
437 'lstat _ croaks after stat' );
109c43ed
FC
438eval { lstat *_ };
439like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
440 'lstat *_ croaks after stat' );
441eval { lstat \*_ };
442like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
443 'lstat \*_ croaks after stat' );
049f818b
YST
444eval { -l _ };
445like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
446 '-l _ croaks after stat' );
447
448lstat $0;
449eval { lstat _ };
450is( "$@", "", "lstat _ ok after lstat" );
451eval { -l _ };
452is( "$@", "", "-l _ ok after lstat" );
020cc77c 453
84f93674 454eval { lstat "test.pl" };
020cc77c
FC
455{
456 open my $fh, "test.pl";
457 stat *$fh{IO};
458 eval { lstat _ }
459}
460like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
461'stat $ioref resets stat type';
0d5064f1
FC
462
463{
464 my @statbuf = stat STDOUT;
465 stat "test.pl";
466 my @lstatbuf = lstat *STDOUT{IO};
467 is "@lstatbuf", "@statbuf", 'lstat $ioref reverts to regular fstat';
468}
049f818b 469
5c9aa243 470SKIP: {
049f818b 471 skip "No lstat", 2 unless $Config{d_lstat};
5c9aa243 472
5c9aa243
RGS
473 # bug id 20020124.004
474 # If we have d_lstat, we should have symlink()
bee528ec 475 my $linkname = 'stat-' . rand =~ y/.//dr;
102411b6
CB
476 my $target = $Perl;
477 $target =~ s/;\d+\z// if $Is_VMS; # symlinks don't like version numbers
478 symlink $target, $linkname or die "# Can't symlink $0: $!";
5c9aa243
RGS
479 lstat $linkname;
480 -T _;
481 eval { lstat _ };
3db621ff 482 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
5c9aa243
RGS
483 'lstat croaks after -T _' );
484 eval { -l _ };
3db621ff 485 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
5c9aa243
RGS
486 '-l _ croaks after -T _' );
487 unlink $linkname or print "# unlink $linkname failed: $!\n";
488}
98a392ec 489
67fcfcf9
JL
490SKIP: {
491 skip "Too much clock skew between system and filesystem", 5
492 if ($Filesystem_Time_Offset > 5);
493 print "# Zzz...\n";
494 sleep($Filesystem_Time_Offset+1);
495 my $f = 'tstamp.tmp';
496 unlink $f;
497 ok (open(S, "> $f"), 'can create tmp file');
498 close S or die;
499 my @a = stat $f;
500 print "# time=$^T, stat=(@a)\n";
501 my @b = (-M _, -A _, -C _);
502 print "# -MAC=(@b)\n";
503 ok( (-M _) < 0, 'negative -M works');
504 ok( (-A _) < 0, 'negative -A works');
505 ok( (-C _) < 0, 'negative -C works');
506 ok(unlink($f), 'unlink tmp file');
507}
a80d47c2 508
7b64ff23 509# [perl #4253]
25988e07
NE
510{
511 ok(open(F, ">", $tmpfile), 'can create temp file');
512 close F;
513 chmod 0077, $tmpfile;
514 my @a = stat($tmpfile);
515 my $s1 = -s _;
516 -T _;
517 my $s2 = -s _;
518 is($s1, $s2, q(-T _ doesn't break the statbuffer));
84f93674
FC
519 SKIP: {
520 skip "No lstat", 1 unless $Config{d_lstat};
3bf50cd4 521 skip "uid=0", 1 unless $<&&$>;
b555e4be 522 skip "Readable by group/other means readable by me", 1 if $^O eq 'VMS';
84f93674
FC
523 lstat($tmpfile);
524 -T _;
525 ok(eval { lstat _ },
526 q(-T _ doesn't break lstat for unreadable file));
527 }
8ca3c3a5 528 unlink $tmpfile;
25988e07
NE
529}
530
5228a96c 531SKIP: {
3497a01f 532 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
586251be
SP
533 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
534 ok(stat(DIR), "stat() on dirhandle works");
5228a96c 535 ok(-d -r _ , "chained -x's on dirhandle");
6d6dd8fe
NC
536 ok(-d DIR, "-d on a dirhandle works");
537
93f09d7b 538 # And now for the ambiguous bareword case
3963d30b
NC
539 {
540 no warnings 'deprecated';
541 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
542 || diag "Can't open 'TEST': $!";
543 }
6d6dd8fe
NC
544 my $size = (stat(DIR))[7];
545 ok(defined $size, "stat() on bareword works");
546 is($size, -s "TEST", "size returned by stat of bareword is for the file");
547 ok(-f _, "ambiguous bareword uses file handle, not dir handle");
548 ok(-f DIR);
549 closedir DIR or die $!;
550 close DIR or die $!;
586251be
SP
551}
552
553{
554 # RT #8244: *FILE{IO} does not behave like *FILE for stat() and -X() operators
555 ok(open(F, ">", $tmpfile), 'can create temp file');
556 my @thwap = stat *F{IO};
557 ok(@thwap, "stat(*F{IO}) works");
558 ok( -f *F{IO} , "single file tests work with *F{IO}");
c84ba4c0 559 close F;
586251be
SP
560 unlink $tmpfile;
561
562 #PVIO's hold dirhandle information, so let's test them too.
563
564 SKIP: {
3497a01f 565 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
586251be
SP
566 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
567 ok(stat(*DIR{IO}), "stat() on *DIR{IO} works");
20c932f8 568 ok(-d _ , "The special file handle _ is set correctly");
586251be 569 ok(-d -r *DIR{IO} , "chained -x's on *DIR{IO}");
20c932f8 570
93f09d7b 571 # And now for the ambiguous bareword case
3963d30b
NC
572 {
573 no warnings 'deprecated';
574 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
575 || diag "Can't open 'TEST': $!";
576 }
20c932f8
NC
577 my $size = (stat(*DIR{IO}))[7];
578 ok(defined $size, "stat() on *THINGY{IO} works");
579 is($size, -s "TEST",
580 "size returned by stat of *THINGY{IO} is for the file");
581 ok(-f _, "ambiguous *THINGY{IO} uses file handle, not dir handle");
582 ok(-f *DIR{IO});
583 closedir DIR or die $!;
584 close DIR or die $!;
586251be 585 }
5228a96c
SP
586}
587
daa30a68
FC
588# [perl #71002]
589{
590 local $^W = 1;
591 my $w;
592 local $SIG{__WARN__} = sub { warn shift; ++$w };
593 stat 'prepeinamehyparcheiarcheiometoonomaavto';
594 stat _;
595 is $w, undef, 'no unopened warning from stat _';
596}
597
98a392ec 598END {
295d5f02 599 chmod 0666, $tmpfile;
c291be4e 600 unlink_all $tmpfile;
98a392ec 601}