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