14 $Is_Amiga = $^O eq 'amigaos';
15 $Is_Cygwin = $^O eq 'cygwin';
16 $Is_Dos = $^O eq 'dos';
17 $Is_MPE = $^O eq 'mpeix';
18 $Is_MSWin32 = $^O eq 'MSWin32';
19 $Is_NetWare = $^O eq 'NetWare';
20 $Is_OS2 = $^O eq 'os2';
21 $Is_Solaris = $^O eq 'solaris';
22 $Is_VMS = $^O eq 'VMS';
24 $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin;
26 my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
27 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
29 my $Curdir = File::Spec->curdir;
32 my $tmpfile = 'Op_stat.tmp';
33 my $tmpfile_link = $tmpfile.'2';
37 open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
40 open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
42 my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
44 skip "No link count", 1 if $Is_VMS;
46 is($nlink, 1, 'nlink on regular file');
50 skip "mtime and ctime not reliable", 2
51 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos;
53 ok( $mtime, 'mtime' );
54 is( $mtime, $ctime, 'mtime == ctime' );
58 # Cygwin seems to have a 3 second granularity on its timestamps.
59 my $funky_FAT_timestamps = $Is_Cygwin;
60 sleep 3 if $funky_FAT_timestamps;
62 print FOO "Now is the time for all good men to come to.\n";
65 sleep 2 unless $funky_FAT_timestamps;
70 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
71 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
73 is( $@, '', 'link() implemented' );
74 ok( $lnk_result, 'linked tmp testfile' );
75 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
77 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
80 skip "No link count", 1 if $Config{dont_use_nlink};
81 is($nlink, 2, 'Link count on hard linked file' );
85 my $cwd = File::Spec->rel2abs($Curdir);
86 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
87 if $Is_Solaris and $cwd =~ m#^/tmp# and
88 $mtime && $mtime == $ctime;
89 skip "AFS has different mtime/ctime link semantics", 2
90 if $cwd =~ m#$Config{'afsroot'}/#;
91 skip "AmigaOS has different mtime/ctime link semantics", 2
94 if( !ok($mtime, 'hard link mtime') ||
95 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
97 # Check if you are on a tmpfs of some sort. Building in /tmp sometimes
98 # has this problem. Also building on the ClearCase VOBS filesystem may
106 # truncate and touch $tmpfile.
107 open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
110 ok(-z $tmpfile, '-z on empty file');
111 ok(! -s $tmpfile, ' and -s');
113 open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
117 ok(! -z $tmpfile, '-z on non-empty file');
118 ok(-s $tmpfile, ' and -s');
121 # Strip all access rights from the file.
122 ok( chmod(0000, $tmpfile), 'chmod 0000' );
125 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
128 # Going to try to switch away from root. Might not work.
131 skip "Can't test -r or -w meaningfully if you're superuser", 2
135 skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin;
136 ok(!-r $tmpfile, " -r");
139 ok(!-w $tmpfile, " -w");
141 # switch uid back (may not be implemented)
142 eval { $> = $olduid; };
145 ok(! -x $tmpfile, ' -x');
151 # in ms windows, $tmpfile inherits owner uid from directory
152 # not sure about os/2, but chown is harmless anyway
153 eval { chown $>,$tmpfile; 1 } or print "# $@" ;
155 ok(chmod(0700,$tmpfile), 'chmod 0700');
156 ok(-r $tmpfile, ' -r');
157 ok(-w $tmpfile, ' -w');
160 skip "-x simply determins if a file ends in an executable suffix", 1
163 ok(-x $tmpfile, ' -x');
166 ok( -f $tmpfile, ' -f');
167 ok(! -d $tmpfile, ' !-d');
170 ok( -d $Curdir, '-d cwd' );
171 ok(! -f $Curdir, '!-f cwd' );
175 unlink($tmpfile_link);
176 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
177 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
179 is( $@, '', 'symlink() implemented' );
180 ok( $symlink_rslt, 'symlink() ok' );
181 ok(-l $tmpfile_link, '-l');
184 ok(-o $tmpfile, '-o');
186 ok(-e $tmpfile, '-e');
188 unlink($tmpfile_link);
189 ok(! -e $tmpfile_link, ' -e on unlinked file');
192 skip "No character, socket or block special files", 3
193 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
194 skip "/dev isn't available to test against", 3
195 unless -d '/dev' && -r '/dev' && -x '/dev';
197 my $LS = $Config{d_readlink} ? "ls -lL" : "ls -l";
198 my $CMD = "$LS /dev";
201 skip "$CMD failed", 3 if $DEV eq '';
203 my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
205 skip "opendir failed: $!", 3 if @DEV == 0;
208 my @c1 = eval qq[\$DEV =~ /^$_[0]/mg];
209 my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
212 is($c1, $c2, "ls and $_[1] agree on /dev ($c1 $c2)");
220 ok(! -b $Curdir, '!-b cwd');
221 ok(! -c $Curdir, '!-c cwd');
222 ok(! -S $Curdir, '!-S cwd');
225 skip "No setuid", 3 if $Is_MPE or $Is_Amiga or $Is_Dosish or $Is_Cygwin;
230 # Find a set of directories that's very likely to have setuid files
231 # but not likely to be *all* setuid files.
232 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
233 skip "Can't find a setuid file to test with", 3 unless @bin;
236 opendir BIN, $bin or die "Can't opendir $bin: $!";
237 while (defined($_ = readdir BIN)) {
241 last if $uid && $uid < $cnt;
246 if( !isnt($cnt, 0, 'found some programs') ||
247 !isnt($uid, 0, 'found some setuid programs') ||
248 !ok($uid < $cnt, " they're not all setuid") )
251 # The above two tests assume that at least one of these directories
252 # are readable, executable and contain at least one setuid file
253 # (but aren't all setuid).
260 # To assist in automated testing when a controlling terminal (/dev/tty)
261 # may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
262 # can be set to skip the tests that need a tty.
264 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
266 my $TTY = $^O eq 'rhapsody' ? "/dev/ttyp0" : "/dev/tty";
269 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
270 skip "No TTY to test -t with", 2 unless -e $TTY;
273 warn "Can't open $TTY--run t/TEST outside of make.\n";
275 ok(-c TTY, 'tty is -c');
278 ok(! -t TTY, '!-t on closed TTY filehandle');
279 ok(-t, '-t on STDIN');
282 my $Null = File::Spec->devnull;
284 skip "No null device to test with", 1 unless -e $Null;
286 open(NULL, $Null) or BAIL_OUT("Can't open $Null: $!");
287 ok(! -t NULL, 'null device is not a TTY');
292 # These aren't strictly "stat" calls, but so what?
294 ok(-T 'op/stat.t', '-T');
295 ok(! -B 'op/stat.t', '!-B');
300 open(FOO,'op/stat.t');
303 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
305 is( $@, '', '-T on filehandle causes no errors' );
308 ok(! -B FOO, ' !-B');
311 ok(/perl/, 'after readline');
312 ok(-T FOO, ' still -T');
313 ok(! -B FOO, ' still -B');
316 open(FOO,'op/stat.t');
318 ok(/perl/, 'reopened and after readline');
319 ok(-T FOO, ' still -T');
320 ok(! -B FOO, ' still !-B');
322 ok(seek(FOO,0,0), 'after seek');
323 ok(-T FOO, ' still -T');
324 ok(! -B FOO, ' still !-B');
326 # It's documented this way in perlfunc *shrug*
328 ok(eof FOO, 'at EOF');
329 ok(-T FOO, ' still -T');
330 ok(-B FOO, ' now -B');
336 skip "No null device to test with", 2 unless -e $Null;
338 ok(-T $Null, 'null device is -T');
339 ok(-B $Null, ' and -B');
343 # and now, a few parsing tests:
345 ok(-f, 'bare -f uses $_');
348 unlink $tmpfile or print "# unlink failed: $!\n";
350 # bug id 20011101.069
352 is(scalar @r, 13, 'stat returns full 13 elements');