0% found this document useful (0 votes)
1 views

03-1-stat

Test

Uploaded by

JohnKevinStanley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

03-1-stat

Test

Uploaded by

JohnKevinStanley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Advanced Programming in

the UNIX Environment


Week 03, Segment 1: All about stat(2)

Department of Computer Science


Stevens Institute of Technology

Jan Schaumann
[email protected]
https://stevens.netmeister.org/631/
CS631 - Advanced Programming in the UNIX Environment

stat(2)

#include <sys/stat.h>

int stat(const char *path, struct stat *sb);


int lstat(const char *path, struct stat *sb);
int fstat(int fd, struct stat *sb);

#include <sys/stat.h>
#include <fcntl.h>

int fstatat(int fd, const char *path, struct stat *sb, int flag);
Returns: 0 if OK, -1 on error

All of these obtain information about the file pointed to by path (or, in the case of
fstat(2), fd). If path is a symlink, lstat(2) returns information about the link itself.
2
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

struct stat
struct stat {
dev_t st_dev; /* device number (filesystem) */
ino_t st_ino; /* i-node number (serial number) */
mod_t st_mode; /* file type & mode (permissions) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last file status change */
long st_blocks; /* number of 512-byte blocks allocated */
long st_blksize; /* optimal I/O block size */
}

Additional fields may be defined; check your system's manual page.


3
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

stat(2)

4
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

stat(2)

$ ls -l file
-rw-r--r-- 1 jschauma wheel 1048576 Sep 10 20:04 file

from st_mode from st_uid


st_size
not from stat at all

st_nlink from st_gid from st_mtime

5
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

stat(2)

The stat(1) utility displays information about the file pointed to by file.

$ stat -r file
16 3 0100644 1 1000 0 -1 1048576 1599768274 1599768274 1599768274 0 4096 2056 0 file

The default format displays the st_dev, st_ino, st_mode, st_nlink, st_uid,
st_gid, st_rdev, st_size, st_atime, st_mtime, st_ctime, st_birthtime,
st_blksize, st_blocks, and st_flags fields, in that order.

6
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

struct stat: st_mode

The st_mode field of the struct stat encodes the type of file:
• regular – most common, interpretation of data is up to application
• directory – contains names of other files and pointer to information on those files
• character special – used for certain types of devices, e.g., terminal
• block special – used for disk devices (typically)
• FIFO – used for interprocess communication (sometimes called a "named pipe")
• socket – used for network communication and non-network communication (same
host)
• symbolic link – points to another file

7
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

struct stat: st_mode

8
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

struct stat: st_mode

9
Jan Schaumann 2020-09-10
CS631 - Advanced Programming in the UNIX Environment

struct stat

We've met our new best friend, the struct stat.

We've seen how ls(1) can display most of the information from the struct stat.
stat(1) gets us the rest in a more flexible way.

We've revisited the st_blocks and st_blksize members from our discussion on I/O
efficiency.

We improved our simple-ls clone to display the type of file via the st_mode for files and
symlinks.

An yet, there's so much more to come...

10
Jan Schaumann 2020-09-10

You might also like