xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/File/stat.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gateuse Test::More;
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gateBEGIN {
11*0Sstevel@tonic-gate    our $hasst;
12*0Sstevel@tonic-gate    eval { my @n = stat "TEST" };
13*0Sstevel@tonic-gate    $hasst = 1 unless $@ && $@ =~ /unimplemented/;
14*0Sstevel@tonic-gate    unless ($hasst) { plan skip_all => "no stat"; exit 0 }
15*0Sstevel@tonic-gate    use Config;
16*0Sstevel@tonic-gate    $hasst = 0 unless $Config{'i_sysstat'} eq 'define';
17*0Sstevel@tonic-gate    unless ($hasst) { plan skip_all => "no sys/stat.h"; exit 0 }
18*0Sstevel@tonic-gate    our @stat = stat "TEST"; # This is the function stat.
19*0Sstevel@tonic-gate    unless (@stat) { plan skip_all => "1..0 # Skip: no file TEST"; exit 0 }
20*0Sstevel@tonic-gate}
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gateplan tests => 19;
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gateuse_ok( 'File::stat' );
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gatemy $stat = File::stat::stat( "TEST" ); # This is the OO stat.
27*0Sstevel@tonic-gateok( ref($stat), 'should build a stat object' );
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gateis( $stat->dev, $stat[0], "device number in position 0" );
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate# On OS/2 (fake) ino is not constant, it is incremented each time
32*0Sstevel@tonic-gateSKIP: {
33*0Sstevel@tonic-gate	skip('inode number is not constant on OS/2', 1) if $^O eq 'os2';
34*0Sstevel@tonic-gate	is( $stat->ino, $stat[1], "inode number in position 1" );
35*0Sstevel@tonic-gate}
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateis( $stat->mode, $stat[2], "file mode in position 2" );
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gateis( $stat->nlink, $stat[3], "number of links in position 3" );
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gateis( $stat->uid, $stat[4], "owner uid in position 4" );
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gateis( $stat->gid, $stat[5], "group id in position 5" );
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gateis( $stat->rdev, $stat[6], "device identifier in position 6" );
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gateis( $stat->size, $stat[7], "file size in position 7" );
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gateis( $stat->atime, $stat[8], "last access time in position 8" );
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gateis( $stat->mtime, $stat[9], "last modify time in position 9" );
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gateis( $stat->ctime, $stat[10], "change time in position 10" );
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gateis( $stat->blksize, $stat[11], "IO block size in position 11" );
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gateis( $stat->blocks, $stat[12], "number of blocks in position 12" );
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gateSKIP: {
60*0Sstevel@tonic-gate	local *STAT;
61*0Sstevel@tonic-gate	skip("Could not open file: $!", 2) unless open(STAT, 'TEST');
62*0Sstevel@tonic-gate	ok( File::stat::stat('STAT'), '... should be able to find filehandle' );
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate	package foo;
65*0Sstevel@tonic-gate	local *STAT = *main::STAT;
66*0Sstevel@tonic-gate	main::ok( my $stat2 = File::stat::stat('STAT'),
67*0Sstevel@tonic-gate		'... and filehandle in another package' );
68*0Sstevel@tonic-gate	close STAT;
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate#	VOS open() updates atime; ignore this error (posix-975).
71*0Sstevel@tonic-gate	my $stat3 = $stat2;
72*0Sstevel@tonic-gate	if ($^O eq 'vos') {
73*0Sstevel@tonic-gate		$$stat3[8] = $$stat[8];
74*0Sstevel@tonic-gate	}
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate	main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
77*0Sstevel@tonic-gate	main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate	main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate	main::is( "@$stat", "@$stat3", '... and must match normal stat' );
82*0Sstevel@tonic-gate}
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gatelocal $!;
85*0Sstevel@tonic-gate$stat = stat '/notafile';
86*0Sstevel@tonic-gateisn't( $!, '', 'should populate $!, given invalid file' );
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate# Testing pretty much anything else is unportable.
89