The stat and Access functions under Linux

Source: Internet
Author: User
Tags lstat file permissions

1.stat has both a command and a function of the same name, used to obtain the main information in the Inode (i.e. file type, file permissions, creation/modification/access time, etc.), stat tracking Symbolic Links, lstat not tracking symbolic links. You can view related information through Man 2 Stat.

 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>int stat (const char *path, struct stat      *BUF) int fstat (int fd, struct stat *buf), int lstat (const char *path, struct stat *buf); struct Stat {dev_t     St_dev;     /* ID of device containing file */ino_t St_ino;    /* inode number */mode_t st_mode;   /* Protection */nlink_t St_nlink;     /* Number of hard links */uid_t st_uid;     /* User ID of Owner */gid_t St_gid;    /* Group ID of Owner */dev_t St_rdev;    /* Device ID (if special file) */off_t st_size; /* Total size, in bytes */blksize_t st_blksize;  /* BlockSize for filesystem I/O */blkcnt_t st_blocks;   /* Number of 512B blocks allocated */time_t st_atime;   /* Time of last access */time_t st_mtime; /* Time of last modification *//refers to the modificationDocument Content time_t St_ctime; /* Time of last status change *//refers to modify Inode property};

#include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <unistd.h> #include <errno.h> #include <stdio.h>int main (int argc, char *argv[]) {struct stat filestat;if (ARGC < 2) {printf (" Usage:%s <pathname>\n ", argv[0]); return 0;} if (stat (argv[1], &filestat) = =-1) {perror ("Stat error:"); return 0;} printf ("File type:"); switch (Filestat.st_mode & s_ifmt)//0170000 bit mask for the File type bit fie Lds{case s_ifblk:printf ("Block device\n"); Break;case s_ifchr:printf ("character device\n"); Break;case s_ifdir:printf ("directory\n"); Break;case s_ififo:printf ("fifo/pipe\n"); Break;case s_iflnk:printf ("symlink\n"); Break;case S_ ifreg:printf ("regular file\n"); Break;case s_ifsock:printf ("socket\n"); break;default:printf ("unknown?\n"); break;} printf ("I-node Number:%ld\n", (long) Filestat.st_ino);p rintf ("Mode:%lo (octal) \ n", (unsign Ed long) Filestat.st_mode);p rintf ("Link count:%ld\n", (lONG) filestat.st_nlink);p rintf ("Ownership:uid=%ld gid=%ld\n", (Long) Filestat.st_uid, (long) filestat.st_ GID);p rintf ("Preferred I/O block size:%ld bytes\n", (long) filestat.st_blksize);p rintf ("File Size:%lld by Tes\n ", (Long Long) filestat.st_size);p rintf (" Blocks Allocated:%lld\n ", (Long Long) filestat.st_blocks);p rintf (" L AST status change:%s ', CTime (&filestat.st_ctime));p rintf ("Last File access:%s", CTime (&filestat.st _atime));p rintf ("Last File Modification:%s", CTime (&filestat.st_mtime)); return 0;}

2.access function

Function: Determines the access rights for a file or folder. That is, check how a file is accessed, such as read-only, write-only, and so on. If the specified access method is valid, the function returns 0, otherwise the function returns-1.

#include <unistd.h>int access (const char *pathname, int mode);
Test by actual user ID and actual group ID, track symbolic links
Parameter mode
R_ok whether read access is available
W_ok whether there is write permission
X_ok whether there is execute permission
F_OK testing whether a file exists

#include <stdio.h> #include <unistd.h>int main (int argc, char *argv[]) {if (ARGC < 2) {printf ("Usage:%s < Pathname>\n ", argv[0]); return 0;} if (Access (argv[1],r_ok| x_ok| W_OK) {==0) {printf ("read Wirte execute \ n");} return 0;}




Copyright NOTICE: Welcome to reprint, if there are deficiencies, please treatise.

The stat and Access functions under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.