summaryrefslogtreecommitdiff
path: root/usr/src/man/man2/stat.2
blob: a3d2d8dd4efb06d56b0ce9fd3086fa020667577e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
'\" te
.\" Copyright (c) 2007, Sun Microsystems, Inc.  All Rights Reserved.
.\" Copyright 1989 AT&T.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH STAT 2 "June 13, 2021"
.SH NAME
stat, lstat, fstat, fstatat \- get file status
.SH SYNOPSIS
.nf
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

\fBint\fR \fBstat\fR(\fBconst char *restrict\fR \fIpath\fR, \fBstruct stat *restrict\fR \fIbuf\fR);
.fi

.LP
.nf
\fBint\fR \fBlstat\fR(\fBconst char *restrict\fR \fIpath\fR, \fBstruct stat *restrict\fR \fIbuf\fR);
.fi

.LP
.nf
\fBint\fR \fBfstat\fR(\fBint\fR \fIfildes\fR, \fBstruct stat *\fR\fIbuf\fR);
.fi

.LP
.nf
\fBint\fR \fBfstatat\fR(\fBint\fR \fIfildes\fR, \fBconst char *\fR\fIpath\fR, \fBstruct stat *\fR\fIbuf\fR,
     \fBint\fR \fIflag\fR);
.fi

.SH DESCRIPTION
The \fBstat()\fR function obtains information about the file pointed to by
\fIpath\fR. Read, write, or execute permission of the named file is not
required, but all directories listed in the path name leading to the file must
be searchable.
.sp
.LP
The \fBlstat()\fR function obtains file attributes similar to \fBstat()\fR,
except when the named file is a symbolic link; in that case \fBlstat()\fR
returns information about the link, while \fBstat()\fR returns information
about the file the link references.
.sp
.LP
The \fBfstat()\fR function obtains information about an open file known by the
file descriptor \fIfildes\fR, obtained from a successful \fBopen\fR(2),
\fBcreat\fR(2), \fBdup\fR(2), \fBfcntl\fR(2), or \fBpipe\fR(2) function. If
\fIfildes\fR references a shared memory object, the system updates in the
\fBstat\fR structure pointed to by the \fIbuf\fR argument only the
\fBst_uid\fR, \fBst_gid\fR, \fBst_size\fR, and \fBst_mode\fR fields, and only
the \fBS_IRUSR\fR, \fBS_IWUSR\fR, \fBS_IRGRP\fR, \fBS_IWGRP\fR, \fBS_IROTH\fR,
and \fBS_IWOTH\fR file permission bits need be valid. The system can update
other fields and flags. The \fBfstat()\fR function updates any pending
time-related fields before writing to the \fBstat\fR structure.
.sp
.LP
The \fBfstatat()\fR function obtains file attributes similar to the
\fBstat()\fR, \fBlstat()\fR, and \fBfstat()\fR functions.  If the \fIpath\fR
argument is a relative path, it is resolved relative to the \fIfildes\fR
argument rather than the current working directory.  If \fIpath\fR is absolute,
the \fIfildes\fR argument is unused.  If the \fIfildes\fR argument has the
special value \fBAT_FDCWD\fR, relative paths are resolved from the current
working directory. If \fBAT_SYMLINK_NOFOLLOW\fR is set in the \fIflag\fR
argument, the function behaves like \fBlstat()\fR and does not automatically
follow symbolic links. See \fBfsattr\fR(7). If \fB_ATTR_TRIGGER\fR is set in
the  \fIflag\fR argument and the vnode is a trigger mount point, the mount is
performed and the function returns the attributes of the root of the mounted
filesystem.
.sp
.LP
The \fIbuf\fR argument is a pointer to a \fBstat\fR structure into which
information is placed concerning the file. A \fBstat\fR structure includes the
following members:
.sp
.in +2
.nf
mode_t   st_mode;          /* File mode (see mknod(2)) */
ino_t    st_ino;           /* Inode number */
dev_t    st_dev;           /* ID of device containing */
                           /* a directory entry for this file */
dev_t    st_rdev;          /* ID of device */
                           /* This entry is defined only for */
                           /* char special or block special files */
nlink_t  st_nlink;         /* Number of links */
uid_t    st_uid;           /* User ID of the file's owner */
gid_t    st_gid;           /* Group ID of the file's group */
off_t    st_size;          /* File size in bytes */
time_t   st_atime;         /* Time of last access */
time_t   st_mtime;         /* Time of last data modification */
time_t   st_ctime;         /* Time of last file status change */
                           /* Times measured in seconds since */
                           /* 00:00:00 UTC, Jan. 1, 1970 */
long     st_blksize;       /* Preferred I/O block size */
blkcnt_t st_blocks;        /* Number of 512 byte blocks allocated*/
char     st_fstype[_ST_FSTYPSZ];
                           /* Null-terminated type of filesystem */
.fi
.in -2

.sp
.LP
Descriptions of structure members are as follows:
.sp
.ne 2
.na
\fB\fBst_mode\fR\fR
.ad
.RS 14n
The mode of the file as described for the \fBmknod()\fR function. In addition
to the modes described on the \fBmknod\fR(2) manual page, the mode of a file
can also be \fBS_IFSOCK\fR if the file is a socket, \fBS_IFDOOR\fR if the file
is a door, \fBS_IFPORT\fR if the file is an event port, or \fBS_IFLNK\fR if the
file is a symbolic link. \fBS_IFLNK\fR can be returned either by \fBlstat()\fR
or by \fBfstatat()\fR when the \fBAT_SYMLINK_NOFOLLOW\fR flag is set.
.RE

.sp
.ne 2
.na
\fB\fBst_ino\fR\fR
.ad
.RS 14n
This field uniquely identifies the file in a given file system. The pair
\fBst_ino\fR and  \fBst_dev\fR uniquely identifies regular files.
.RE

.sp
.ne 2
.na
\fB\fBst_dev\fR\fR
.ad
.RS 14n
This field uniquely identifies the file system that contains the file. Its
value may be used as input to the \fBustat()\fR function to determine more
information about this file system. No other meaning is associated with this
value.
.RE

.sp
.ne 2
.na
\fB\fBst_rdev\fR\fR
.ad
.RS 14n
This field should be used only by administrative commands. It is valid only for
block special or character special files and only has meaning on the system
where the file was configured.
.RE

.sp
.ne 2
.na
\fB\fBst_nlink\fR\fR
.ad
.RS 14n
This field should be used only by administrative commands.
.RE

.sp
.ne 2
.na
\fB\fBst_uid\fR\fR
.ad
.RS 14n
The user \fBID\fR of the file's owner.
.RE

.sp
.ne 2
.na
\fB\fBst_gid\fR\fR
.ad
.RS 14n
The group \fBID\fR of the file's group.
.RE

.sp
.ne 2
.na
\fB\fBst_size\fR\fR
.ad
.RS 14n
For regular files, this is the address of the end of the file. For block
special or character special, this is not defined. See also \fBpipe\fR(2).
.RE

.sp
.ne 2
.na
\fB\fBst_atime\fR\fR
.ad
.RS 14n
Time when file data was last accessed. Some of the functions that change this
member are: \fBcreat()\fR, \fBmknod()\fR, \fBpipe()\fR, \fButime\fR(2), and
\fBread\fR(2).
.RE

.sp
.ne 2
.na
\fB\fBst_mtime\fR\fR
.ad
.RS 14n
Time when data was last modified. Some of the functions that change this member
are: \fBcreat()\fR, \fBmknod()\fR, \fBpipe()\fR, \fButime()\fR, and
\fBwrite\fR(2).
.RE

.sp
.ne 2
.na
\fB\fBst_ctime\fR\fR
.ad
.RS 14n
Time when file status was last changed. Some of the functions that change this
member are: \fBchmod\fR(2), \fBchown\fR(2), \fBcreat\fR(2), \fBlink\fR(2),
\fBmknod\fR(2), \fBpipe\fR(2), \fBrename\fR(2), \fBunlink\fR(2),
\fButime\fR(2), and \fBwrite\fR(2).
.RE

.sp
.ne 2
.na
\fB\fBst_blksize\fR\fR
.ad
.RS 14n
A hint as to the "best" unit size for I/O operations. This field is not defined
for block special or character special files.
.RE

.sp
.ne 2
.na
\fB\fBst_blocks\fR\fR
.ad
.RS 14n
The total number of physical blocks of size 512 bytes actually allocated on
disk. This field is not defined for block special or character special files.
.RE

.sp
.ne 2
.na
\fB\fBst_fstype\fR\fR
.ad
.RS 14n
A null-terminated string that uniquely identifies the type of the filesystem
that contains the file.
.RE

.SH RETURN VALUES
Upon successful completion, \fB0\fR is returned. Otherwise, \fB\(mi1\fR is
returned and \fBerrno\fR is set to indicate the error.
.SH ERRORS
The \fBstat()\fR, \fBfstat()\fR, \fBlstat()\fR, and \fBfstatat()\fR functions
will fail if:
.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 13n
An error occurred while reading from the file system.
.RE

.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 13n
The file size in bytes or the number of blocks allocated to the file or the
file serial number cannot be represented correctly in the structure pointed to
by \fIbuf\fR.
.RE

.sp
.LP
The \fBstat()\fR, \fBlstat()\fR, and \fBfstatat()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
Search permission is denied for a component of the path prefix.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The  \fIbuf\fR or \fIpath\fR argument points to an illegal address.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
A signal was caught during the execution of the  \fBstat()\fR or \fBlstat()\fR
function.
.RE

.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
A loop exists in symbolic links encountered during the resolution of the
\fIpath\fR argument.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of the \fIpath\fR argument exceeds {\fBPATH_MAX\fR}, or the length
of a \fIpath\fR component exceeds {\fBNAME_MAX\fR} while \fB_POSIX_NO_TRUNC\fR
is in effect.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
A component of \fIpath\fR does not name an existing file or \fIpath\fR is an
empty string.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to a remote machine and the link to that machine
is no longer active.
.RE

.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix is not a directory, or the \fIfildes\fR argument
does not refer to a valid directory when given a non-null relative path.
.RE

.sp
.LP
The \fBfstat()\fR and \fBfstatat()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 11n
The \fIfildes\fR argument is not a valid open file descriptor. The \fIfildes\fR
argument to \fBfstatat()\fR can also have the valid value of \fBAT_FDCWD\fR.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 11n
The \fIbuf\fR argument points to an illegal address.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 11n
A signal was caught during the execution of the \fBfstat()\fR function.
.RE

.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 11n
The \fIfildes\fR argument points to a remote machine and the link to that
machine is no longer active.
.RE

.sp
.LP
The \fBstat()\fR, \fBfstat()\fR, and \fBlstat()\fR functions may fail if:
.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 13n
One of the members is too large to store in the \fBstat\fR structure pointed to
by \fIbuf\fR.
.RE

.sp
.LP
The \fBstat()\fR and \fBlstat()\fR functions may fail if:
.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
More than {\fBSYMLOOP_MAX\fR} symbolic links were encountered during the
resolution of the \fIpath\fR argument.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
As a result of encountering a symbolic link in resolution of the \fIpath\fR
argument, the length of the substituted pathname strings exceeds
{\fBPATH_MAX\fR}.
.RE

.sp
.LP
The \fBstat()\fR and \fBfstatat()\fR functions may fail if:
.sp
.ne 2
.na
\fB\fBENXIO\fR\fR
.ad
.RS 9n
The \fIpath\fR argument names a character or block device special file and the
corresponding I/O device has been retired by the fault management framework.
.RE

.SH EXAMPLES
\fBExample 1 \fRUse \fBstat()\fR to obtain file status information.
.sp
.LP
The following example shows how to obtain file status information for a file
named \fB/home/cnd/mod1\fR. The structure variable buffer is defined for the
\fBstat\fR structure.

.sp
.in +2
.nf
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct stat buffer;
int         status;
\&...
status = stat("/home/cnd/mod1", &buffer);
.fi
.in -2

.LP
\fBExample 2 \fRUse \fBstat()\fR to get directory information.
.sp
.LP
The following example fragment gets status information for each entry in a
directory. The call to the \fBstat()\fR function stores file information in the
\fBstat\fR structure pointed to by \fIstatbuf\fR. The lines that follow the
\fBstat()\fR call format the fields in the \fBstat\fR structure for
presentation to the user of the program.

.sp
.in +2
.nf
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
#include <stdint.h>
struct dirent *dp;
struct stat   statbuf;
struct passwd *pwd;
struct group  *grp;
struct tm     *tm;
char          datestring[256];
\&...
/* Loop through directory entries */
while ((dp = readdir(dir)) != NULL) {
    /* Get entry's information. */
    if (stat(dp->d_name, &statbuf) == -1)
    continue;

     /* Print out type, permissions, and number of links. */
     printf("%10.10s", sperm (statbuf.st_mode));
     printf("%4d", statbuf.st_nlink);

     /* Print out owners name if it is found using getpwuid(). */
     if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
        printf(" %-8.8s", pwd->pw_name);
     else
        printf(" %-8d", statbuf.st_uid);

     /* Print out group name if it's found using getgrgid(). */
     if ((grp = getgrgid(statbuf.st_gid)) != NULL)
        printf(" %-8.8s", grp->gr_name);
     else
        printf(" %-8d", statbuf.st_gid);

     /* Print size of file. */
     printf(" %9jd", (intmax_t)statbuf.st_size);
     tm = localtime(&statbuf.st_mtime);

     /* Get localized date string. */
     strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);

     printf(" %s %s\en", datestring, dp->d_name);
 }
.fi
.in -2

.LP
\fBExample 3 \fRUse \fBfstat()\fR to obtain file status information.
.sp
.LP
The following example shows how to obtain file status information for a file
named \fB/home/cnd/mod1\fR. The structure variable buffer is defined for the
\fBstat\fR structure. The \fB/home/cnd/mod1 file\fR is opened with read/write
privileges and is passed to the open file descriptor \fIfildes\fR.

.sp
.in +2
.nf
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct stat buffer;
int         status;
\&...
fildes = open("/home/cnd/mod1", O_RDWR);
status = fstat(fildes, &buffer);
.fi
.in -2

.LP
\fBExample 4 \fRUse \fBlstat()\fR to obtain symbolic link status information.
.sp
.LP
The following example shows how to obtain status information for a symbolic
link named \fB/modules/pass1\fR. The structure variable buffer is defined for
the \fBstat\fR structure. If the \fIpath\fR argument specified the filename for
the file pointed to by the symbolic link (\fB/home/cnd/mod1\fR), the results of
calling the function would be the same as those returned by a call to the
\fBstat()\fR function.

.sp
.in +2
.nf
#include <sys/stat.h>
struct stat buffer;
int         status;
\&...
status = lstat("/modules/pass1", &buffer);
.fi
.in -2

.SH USAGE
If \fBchmod()\fR or \fBfchmod()\fR is used to change the file group owner
permissions on a file with non-trivial ACL entries, only the ACL mask is set to
the new permissions and the group owner permission bits in the file's mode
field (defined in \fBmknod\fR(2)) are unchanged.  A non-trivial ACL entry is
one whose meaning cannot be represented in the file's mode field alone. The new
ACL mask permissions  might change the effective permissions for additional
users and groups that have ACL entries on the file.
.sp
.LP
The \fBstat()\fR, \fBfstat()\fR, and \fBlstat()\fR functions have transitional
interfaces for 64-bit file offsets.  See \fBlf64\fR(7).
.SH ATTRIBUTES
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	Async-Signal-Safe
_
Standard	See below.
.TE

.sp
.LP
For \fBstat()\fR, \fBfstat()\fR, and \fBlstat()\fR, see \fBstandards\fR(7).
.SH SEE ALSO
.BR access (2),
.BR chmod (2),
.BR chown (2),
.BR creat (2),
.BR link (2),
.BR mknod (2),
.BR pipe (2),
.BR read (2),
.BR time (2),
.BR unlink (2),
.BR utime (2),
.BR write (2),
.BR fattach (3C),
.BR stat.h (3HEAD),
.BR attributes (7),
.BR fsattr (7),
.BR lf64 (7),
.BR standards (7)