This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove the Rhapsody port.
[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';
95036ac7 39$Is_DGUX = $^O eq 'dgux';
ca37ab50 40$Is_MPRAS = $^O =~ /svr4/ && -f '/etc/.relid';
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};
d5b53b20 254 my $LS = $Config{d_readlink} ? "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
95036ac7
JH
297SKIP: {
298 skip("DG/UX ls -L broken", 3) if $Is_DGUX;
299
3fcc9fea
JH
300 $try->('b', '-b');
301 $try->('c', '-c');
302 $try->('s', '-S');
95036ac7 303
378cc40b
LW
304}
305
3fcc9fea 306ok(! -b $Curdir, '!-b cwd');
de5a37b2
MS
307ok(! -c $Curdir, '!-c cwd');
308ok(! -S $Curdir, '!-S cwd');
cc25fa79 309
95036ac7
JH
310}
311
cc25fa79 312SKIP: {
cc25fa79
MS
313 my($cnt, $uid);
314 $cnt = $uid = 0;
315
316 # Find a set of directories that's very likely to have setuid files
317 # but not likely to be *all* setuid files.
318 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
de5a37b2 319 skip "Can't find a setuid file to test with", 3 unless @bin;
cc25fa79
MS
320
321 for my $bin (@bin) {
322 opendir BIN, $bin or die "Can't opendir $bin: $!";
323 while (defined($_ = readdir BIN)) {
324 $_ = "$bin/$_";
325 $cnt++;
326 $uid++ if -u;
327 last if $uid && $uid < $cnt;
328 }
329 }
330 closedir BIN;
331
2304a331
JH
332 skip "No setuid programs", 3 if $uid == 0;
333
334 isnt($cnt, 0, 'found some programs');
335 isnt($uid, 0, ' found some setuid programs');
336 ok($uid < $cnt, " they're not all setuid");
378cc40b 337}
b8440792 338
378cc40b 339
3e3baf6d
TB
340# To assist in automated testing when a controlling terminal (/dev/tty)
341# may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
342# can be set to skip the tests that need a tty.
cc25fa79
MS
343SKIP: {
344 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
345
7eb35c03 346 my $TTY = "/dev/tty";
cc25fa79
MS
347
348 SKIP: {
349 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
350 skip "No TTY to test -t with", 2 unless -e $TTY;
351
d5b53b20 352 open(TTY, $TTY) ||
cc25fa79
MS
353 warn "Can't open $TTY--run t/TEST outside of make.\n";
354 ok(-t TTY, '-t');
355 ok(-c TTY, 'tty is -c');
356 close(TTY);
3e3baf6d 357 }
cc25fa79 358 ok(! -t TTY, '!-t on closed TTY filehandle');
28e8237b
MS
359
360 {
361 local $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
362 ok(-t, '-t on STDIN');
363 }
68dc0745 364}
cc25fa79 365
cc25fa79 366SKIP: {
de5a37b2 367 skip "No null device to test with", 1 unless -e $Null;
d5b53b20 368 skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
cc25fa79 369
c4fbe247 370 open(NULL, $Null) or DIE("Can't open $Null: $!");
de5a37b2 371 ok(! -t NULL, 'null device is not a TTY');
cc25fa79 372 close(NULL);
378cc40b 373}
cc25fa79 374
378cc40b
LW
375
376# These aren't strictly "stat" calls, but so what?
563ff921 377my $statfile = './op/stat.t';
dc459aad
JH
378ok( -T $statfile, '-T');
379ok(! -B $statfile, '!-B');
378cc40b 380
95036ac7
JH
381SKIP: {
382 skip("DG/UX", 1) if $Is_DGUX;
b5fe401b 383ok(-B $Perl, '-B');
95036ac7
JH
384}
385
b5fe401b 386ok(! -T $Perl, '!-T');
378cc40b 387
dc459aad 388open(FOO,$statfile);
cc25fa79
MS
389SKIP: {
390 eval { -T FOO; };
de5a37b2 391 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
cc25fa79
MS
392
393 is( $@, '', '-T on filehandle causes no errors' );
394
395 ok(-T FOO, ' -T');
396 ok(! -B FOO, ' !-B');
397
f0fcb552 398 $_ = <FOO>;
1d662fb6 399 like($_, qr/perl/, 'after readline');
cc25fa79
MS
400 ok(-T FOO, ' still -T');
401 ok(! -B FOO, ' still -B');
f0fcb552
LW
402 close(FOO);
403
dc459aad 404 open(FOO,$statfile);
f0fcb552 405 $_ = <FOO>;
1d662fb6 406 like($_, qr/perl/, 'reopened and after readline');
cc25fa79
MS
407 ok(-T FOO, ' still -T');
408 ok(! -B FOO, ' still !-B');
409
410 ok(seek(FOO,0,0), 'after seek');
de5a37b2
MS
411 ok(-T FOO, ' still -T');
412 ok(! -B FOO, ' still !-B');
413
414 # It's documented this way in perlfunc *shrug*
415 () = <FOO>;
416 ok(eof FOO, 'at EOF');
417 ok(-T FOO, ' still -T');
418 ok(-B FOO, ' now -B');
f0fcb552
LW
419}
420close(FOO);
378cc40b 421
cc25fa79 422
de5a37b2
MS
423SKIP: {
424 skip "No null device to test with", 2 unless -e $Null;
425
426 ok(-T $Null, 'null device is -T');
427 ok(-B $Null, ' and -B');
428}
cc25fa79 429
108ed793
SM
430
431# and now, a few parsing tests:
cc25fa79
MS
432$_ = $tmpfile;
433ok(-f, 'bare -f uses $_');
434ok(-f(), ' -f() "');
108ed793 435
cc25fa79 436unlink $tmpfile or print "# unlink failed: $!\n";
58d95175
RG
437
438# bug id 20011101.069
dc459aad 439my @r = \stat($Curdir);
cc25fa79 440is(scalar @r, 13, 'stat returns full 13 elements');
5c9aa243 441
049f818b
YST
442stat $0;
443eval { lstat _ };
444like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
445 'lstat _ croaks after stat' );
109c43ed
FC
446eval { lstat *_ };
447like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
448 'lstat *_ croaks after stat' );
449eval { lstat \*_ };
450like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
451 'lstat \*_ croaks after stat' );
049f818b
YST
452eval { -l _ };
453like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
454 '-l _ croaks after stat' );
455
456lstat $0;
457eval { lstat _ };
458is( "$@", "", "lstat _ ok after lstat" );
459eval { -l _ };
460is( "$@", "", "-l _ ok after lstat" );
020cc77c 461
84f93674 462eval { lstat "test.pl" };
020cc77c
FC
463{
464 open my $fh, "test.pl";
465 stat *$fh{IO};
466 eval { lstat _ }
467}
468like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
469'stat $ioref resets stat type';
0d5064f1
FC
470
471{
472 my @statbuf = stat STDOUT;
473 stat "test.pl";
474 my @lstatbuf = lstat *STDOUT{IO};
475 is "@lstatbuf", "@statbuf", 'lstat $ioref reverts to regular fstat';
476}
049f818b 477
5c9aa243 478SKIP: {
049f818b 479 skip "No lstat", 2 unless $Config{d_lstat};
5c9aa243 480
5c9aa243
RGS
481 # bug id 20020124.004
482 # If we have d_lstat, we should have symlink()
bee528ec 483 my $linkname = 'stat-' . rand =~ y/.//dr;
102411b6
CB
484 my $target = $Perl;
485 $target =~ s/;\d+\z// if $Is_VMS; # symlinks don't like version numbers
486 symlink $target, $linkname or die "# Can't symlink $0: $!";
5c9aa243
RGS
487 lstat $linkname;
488 -T _;
489 eval { lstat _ };
3db621ff 490 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
5c9aa243
RGS
491 'lstat croaks after -T _' );
492 eval { -l _ };
3db621ff 493 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
5c9aa243
RGS
494 '-l _ croaks after -T _' );
495 unlink $linkname or print "# unlink $linkname failed: $!\n";
496}
98a392ec 497
67fcfcf9
JL
498SKIP: {
499 skip "Too much clock skew between system and filesystem", 5
500 if ($Filesystem_Time_Offset > 5);
501 print "# Zzz...\n";
502 sleep($Filesystem_Time_Offset+1);
503 my $f = 'tstamp.tmp';
504 unlink $f;
505 ok (open(S, "> $f"), 'can create tmp file');
506 close S or die;
507 my @a = stat $f;
508 print "# time=$^T, stat=(@a)\n";
509 my @b = (-M _, -A _, -C _);
510 print "# -MAC=(@b)\n";
511 ok( (-M _) < 0, 'negative -M works');
512 ok( (-A _) < 0, 'negative -A works');
513 ok( (-C _) < 0, 'negative -C works');
514 ok(unlink($f), 'unlink tmp file');
515}
a80d47c2 516
7b64ff23 517# [perl #4253]
25988e07
NE
518{
519 ok(open(F, ">", $tmpfile), 'can create temp file');
520 close F;
521 chmod 0077, $tmpfile;
522 my @a = stat($tmpfile);
523 my $s1 = -s _;
524 -T _;
525 my $s2 = -s _;
526 is($s1, $s2, q(-T _ doesn't break the statbuffer));
84f93674
FC
527 SKIP: {
528 skip "No lstat", 1 unless $Config{d_lstat};
3bf50cd4 529 skip "uid=0", 1 unless $<&&$>;
b555e4be 530 skip "Readable by group/other means readable by me", 1 if $^O eq 'VMS';
84f93674
FC
531 lstat($tmpfile);
532 -T _;
533 ok(eval { lstat _ },
534 q(-T _ doesn't break lstat for unreadable file));
535 }
8ca3c3a5 536 unlink $tmpfile;
25988e07
NE
537}
538
5228a96c 539SKIP: {
3497a01f 540 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
586251be
SP
541 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
542 ok(stat(DIR), "stat() on dirhandle works");
5228a96c 543 ok(-d -r _ , "chained -x's on dirhandle");
6d6dd8fe
NC
544 ok(-d DIR, "-d on a dirhandle works");
545
93f09d7b 546 # And now for the ambiguous bareword case
3963d30b
NC
547 {
548 no warnings 'deprecated';
549 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
550 || diag "Can't open 'TEST': $!";
551 }
6d6dd8fe
NC
552 my $size = (stat(DIR))[7];
553 ok(defined $size, "stat() on bareword works");
554 is($size, -s "TEST", "size returned by stat of bareword is for the file");
555 ok(-f _, "ambiguous bareword uses file handle, not dir handle");
556 ok(-f DIR);
557 closedir DIR or die $!;
558 close DIR or die $!;
586251be
SP
559}
560
561{
562 # RT #8244: *FILE{IO} does not behave like *FILE for stat() and -X() operators
563 ok(open(F, ">", $tmpfile), 'can create temp file');
564 my @thwap = stat *F{IO};
565 ok(@thwap, "stat(*F{IO}) works");
566 ok( -f *F{IO} , "single file tests work with *F{IO}");
c84ba4c0 567 close F;
586251be
SP
568 unlink $tmpfile;
569
570 #PVIO's hold dirhandle information, so let's test them too.
571
572 SKIP: {
3497a01f 573 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
586251be
SP
574 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
575 ok(stat(*DIR{IO}), "stat() on *DIR{IO} works");
20c932f8 576 ok(-d _ , "The special file handle _ is set correctly");
586251be 577 ok(-d -r *DIR{IO} , "chained -x's on *DIR{IO}");
20c932f8 578
93f09d7b 579 # And now for the ambiguous bareword case
3963d30b
NC
580 {
581 no warnings 'deprecated';
582 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
583 || diag "Can't open 'TEST': $!";
584 }
20c932f8
NC
585 my $size = (stat(*DIR{IO}))[7];
586 ok(defined $size, "stat() on *THINGY{IO} works");
587 is($size, -s "TEST",
588 "size returned by stat of *THINGY{IO} is for the file");
589 ok(-f _, "ambiguous *THINGY{IO} uses file handle, not dir handle");
590 ok(-f *DIR{IO});
591 closedir DIR or die $!;
592 close DIR or die $!;
586251be 593 }
5228a96c
SP
594}
595
daa30a68
FC
596# [perl #71002]
597{
598 local $^W = 1;
599 my $w;
600 local $SIG{__WARN__} = sub { warn shift; ++$w };
601 stat 'prepeinamehyparcheiarcheiometoonomaavto';
602 stat _;
603 is $w, undef, 'no unopened warning from stat _';
604}
605
98a392ec 606END {
295d5f02 607 chmod 0666, $tmpfile;
c291be4e 608 unlink_all $tmpfile;
98a392ec 609}