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