d53f0ecc1d1c6c8d135e426327739a33d98502b2
[perl.git] / do / stat
1 int
2 do_stat(TARG,arg,gimme,arglast)
3 STR *TARG;
4 register ARG *arg;
5 int gimme;
6 int *arglast;
7 {
8     register ARRAY *ary = stack;
9     register int sp = arglast[0] + 1;
10     int max = 13;
11
12     if ((arg[1].arg_type & A_MASK) == A_WORD) {
13         tmpstab = arg[1].arg_ptr.arg_stab;
14         if (tmpstab != defstab) {
15             laststype = O_STAT;
16             statstab = tmpstab;
17             str_set(statname,"");
18             if (!stab_io(tmpstab) || !stab_io(tmpstab)->ifp ||
19               fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
20                 max = 0;
21                 laststatval = -1;
22             }
23         }
24         else if (laststatval < 0)
25             max = 0;
26     }
27     else {
28         str_set(statname,str_get(ary->ary_array[sp]));
29         statstab = Nullstab;
30 #ifdef HAS_LSTAT
31         laststype = arg->arg_type;
32         if (arg->arg_type == O_LSTAT)
33             laststatval = lstat(str_get(statname),&statcache);
34         else
35 #endif
36             laststatval = stat(str_get(statname),&statcache);
37         if (laststatval < 0) {
38             if (dowarn && index(str_get(statname), '\n'))
39                 warn(warn_nl, "stat");
40             max = 0;
41         }
42     }
43
44     if (gimme != G_ARRAY) {
45         if (max)
46             str_sset(TARG,&str_yes);
47         else
48             str_sset(TARG,&str_undef);
49         STABSET(TARG);
50         ary->ary_array[sp] = TARG;
51         return sp;
52     }
53     sp--;
54     if (max) {
55 #ifndef lint
56         (void)astore(ary,++sp,
57           str_2mortal(str_nmake((double)statcache.st_dev)));
58         (void)astore(ary,++sp,
59           str_2mortal(str_nmake((double)statcache.st_ino)));
60         (void)astore(ary,++sp,
61           str_2mortal(str_nmake((double)statcache.st_mode)));
62         (void)astore(ary,++sp,
63           str_2mortal(str_nmake((double)statcache.st_nlink)));
64         (void)astore(ary,++sp,
65           str_2mortal(str_nmake((double)statcache.st_uid)));
66         (void)astore(ary,++sp,
67           str_2mortal(str_nmake((double)statcache.st_gid)));
68         (void)astore(ary,++sp,
69           str_2mortal(str_nmake((double)statcache.st_rdev)));
70         (void)astore(ary,++sp,
71           str_2mortal(str_nmake((double)statcache.st_size)));
72         (void)astore(ary,++sp,
73           str_2mortal(str_nmake((double)statcache.st_atime)));
74         (void)astore(ary,++sp,
75           str_2mortal(str_nmake((double)statcache.st_mtime)));
76         (void)astore(ary,++sp,
77           str_2mortal(str_nmake((double)statcache.st_ctime)));
78 #ifdef STATBLOCKS
79         (void)astore(ary,++sp,
80           str_2mortal(str_nmake((double)statcache.st_blksize)));
81         (void)astore(ary,++sp,
82           str_2mortal(str_nmake((double)statcache.st_blocks)));
83 #else
84         (void)astore(ary,++sp,
85           str_2mortal(str_make("",0)));
86         (void)astore(ary,++sp,
87           str_2mortal(str_make("",0)));
88 #endif
89 #else /* lint */
90         (void)astore(ary,++sp,str_nmake(0.0));
91 #endif /* lint */
92     }
93     return sp;
94 }
95