"Linux C learn Stat" Get the properties of a file

Source: Internet
Author: User
Tags posix

NAME

Stat Get file properties

This function is located in the <sys/stat.h> header file.

Function Prototypes:

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

Number of references:

Path file paths + file name

BUF Pointer to buffer

return value:

-1 encountered an error

0 successful return

Function Effect:

Copy the information from the path file into the struct that the pointer buf refers to.


Descriptive narrative

Stat structure:

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 */               time_t    st_ctime;   /* Time of last status change * *           };

dev_t Include the ID of the device file

St_ino I junction

St_mode file types and license permissions

St_nline File links

St_uid ID of all users

St_gid ID of the owning group

St_rdev Device ID (assuming specified file)

The number of bytes that the st_size occupies

St_blksize File system I/O block size

St_block allocated for 512B size

St_atime Last interview time

St_mtime Last modification time

St_ctime state last changed time


The following POSIX macros define the file types used to check the St_mode domain

           S_isreg (M)  is a regular file?           S_isdir (m)  folder?           S_ISCHR (m)  character device?           S_ISBLK (m)  block device?           S_isfifo (m) FIFO?           S_islnk (m)  symbolic link?  (not in posix.1-1996.)           S_issock (m) sockets?  (not in posix.1-1996.)

The following flags are used for the St_mode domain:

           S_IFMT 0170000 bit mask for the file type bit fields S_ifsock 0140000 socket s_if           LNK 0120000 Symbolic Link s_ifreg 0100000 regular file s_ifblk 0060000 block device           S_ifdir 0040000 directory s_ifchr 0020000 character device S_ififo 0010000 FIFO S_isuid 0004000 set-user-id bit s_isgid 0002000 set-group-id bit (see below) S_ISV    TX 0001000 sticky bit (see below) S_irwxu 00700 mask for file owner permissions S_irusr     00400 owner has Read permission s_iwusr 00200 owner have write permission S_ixusr 00100     Owner have execute permission s_irwxg 00070 mask for group permissions S_irgrp 00040 Group has Read permission S_iwgrp 00020 Group have write permission S_ixgrp 00010 Group H   As Execute permission        S_irwxo 00007 Mask for permissions to others (not in group) S_iroth 00004 others has Rea D Permission S_iwoth 00002 Others has write permission S_ixoth 00001 others have execut E permission

Instance:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <        Stdlib.h>int Main (int argc, char *argv[]) {struct STAT info;        if (argc! = 2) {fprintf (stderr, "Usage:%s <pathname>\n", argv[0]);    Exit (Exit_failure);        } if (stat (argv[1], &info) = =-1) {perror ("stat");    Exit (Exit_failure);                  } printf ("File type;    ");       Switch (Info.st_mode & s_ifmt) {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) Info.st_ino);    printf ("Mode:%lo (octal) \ n", (unsigned long) info.st_mode);    printf ("Link count:%ld\n", (long) info.st_nlink);    printf ("Ownership:uid=%ld gid=%ld\n", (Long) Info.st_uid, (long) info.st_gid);    printf ("Preferred I/O block size:%ld bytes\n", (long) info.st_blksize);    printf ("File Size:%lld bytes\n", (Long Long) info.st_size);    printf ("Blocks Allocated:%lld\n", (Long Long) info.st_blocks);    printf ("Last Status change:%s", CTime (&info.st_ctime));    printf ("Last File access:%s", CTime (&info.st_atime));    printf ("Last File Modification:%s", CTime (&info.st_mtime)); Exit (exit_success);}

Execution Result:

[Email protected]:~/linux_program/list$./stat stat.cfile type;                  Regular Filei-node Number:              679622Mode:                       100644 (octal) Link count:                 1Ownership:                  uid=1000 gid= 1000Preferred I/O block size:   4096 bytesfile size:                  2102 bytesblocks Allocated:           8Last status change:         Wed Jul 23:26:20 2014Last file access:           Wed Jul 23:26:35 2014Last file modification:     Wed Jul 16 23:26:20 2014






The appendix is/usr/include/sys/stat.h source code:

/*-* Copyright (c) 1982, 1986, 1989 the Regents of the University. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, is permitted provided that the FOL lowing conditions * is met: * 1. Redistributions of source code must retain the above copyright * Notice, this list of conditions and the following disc Laimer. * 2. Redistributions in binary form must reproduce the above copyright * Notice, this list of conditions and the following D Isclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: *th Is product includes software developed by the University of *california, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * May is used to endorse or promote products Deriv Ed from this software * WithouT specific prior written permission. * * This software are provided by the REGENTS and CONTRIBUTORS "as is" and * any EXPRESS OR implied warranties, including  LIMITED to, the * implied warranties of merchantability and FITNESS for A particular PURPOSE * is disclaimed. In NO EVENT shall the REGENTS OR CONTRIBUTORS is liable * for any DIRECT, INDIRECT, incidental, special, exemplary, or CO Nsequential * Damages (including, but not LIMITED to, procurement of substitute GOODS * OR SERVICES; LOSS of Use, DATA, OR profits; or business interruption) * However caused and on any theory of liability, WHETHER in contract, STRICT * liability, OR TOR T (including negligence OR OTHERWISE) arising in any-the-out-of-the-use of this software, even IF advised of the Possibi Lity of * SUCH DAMAGE. * *@ (#) stat.h7.11 (Berkeley) 3/3/91 */struct stat{dev_tst_dev;/* Inode's device */ino_tst_ino;/* inode ' s number */mode_ts t_mode;/* inode Protection mode */nlink_tst_nlink;/* number of hard links */uid_tst_uid;/* User ID of the file ' s owner */gid_tst_gid;/* Group ID of the file ' s group */dev_tst_rdev;/* device type */off _tst_size;/* file size, in bytes */time_tst_atime;/* time of last access */longst_spare1;time_tst_mtime;/* time's last Da Ta modification */longst_spare2;time_tst_ctime;/* Time of last file status change */longst_spare3;longst_blksize;/* Optimal blocksize for I/O */longst_blocks;/* blocks allocated for file */u_longst_flags;/* user defined flags for file */u _longst_gen;/* file generation number */}; #defineS_ISUID0004000/* Set User ID on Execution */#defineS_ISGID0002000//Set G Roup ID on Execution */#ifndef _posix_source#defines_istxt0001000/* sticky bit */#endif #defines_irwxu0000700/* RWX Mask F  or owner */#defineS_IRUSR0000400/* R for Owner */#defineS_IWUSR0000200/* W for Owner */#defineS_IXUSR0000100/* X for Owner */#ifndef _posix_source#defines_ireads_irusr#defines_iwrites_iwusr#defines_iexecs_ixusr#endif#defines_ irwxg0000070/* RWX Mask for group */#defineS_IRGRP0000040/* R for Group * * #defineS_IWGRP0000020/* W for Group */#defineS_IXGRP0000010/* X for Group */#defineS_IRWXO0000007/  * RWX Mask for other */#defineS_IROTH0000004/* R for other */#defineS_IWOTH0000002/* W for other */#defineS_IXOTH0000001/* X for other */#ifndef _posix_source#defines_ifmt 0170000/* type of file */#defineS_IFIFO 0010000/* named pipe (FIFO) */#d EFINES_IFCHR 0020000/* Character Special * * #defineS_IFDIR 0040000/* directory */#defineS_IFBLK 0060000/* block Special */ #defineS_IFREG 0100000/* Regular */#defineS_IFLNK 0120000/* Symbolic link */#defineS_IFSOCK 0140000/* socket */#defineS_ ISVTX 0001000/* Save swapped text even after use */#define s_blksize512/* block size used in the stat struct *//* 0666 */# Definedeffilemode (s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth) #endif #defines_isdir (M) ((M & 0170000) = = 0040000)/* Directory */#defineS_ISCHR (M) ((M & 0170000) = = 0020000)/* Char Special */#defineS_ISBLK (M & 0170000) = = 0060000)//Block Special */#definEs_isreg (M) ((M & 0170000) = = 0100000)//Regular file */#defineS_ISFIFO (M) ((M & 0170000) = = 0010000)/* FIFO */#ifnd  EF _posix_source#defines_islnk (M) ((M & 0170000) = = 0120000)/* Symbolic link */#defineS_ISSOCK (M) ((M & 0170000) = = 0140000)/* socket */#endif #ifndef kernel#include <sys/cdefs.h>__begin_declsmode_tumask __p ((mode_t)); intchmod __p ((const char *, mode_t)), Intfstat __p ((int, struct stat *)), Intmkdir __p ((const char *, mode_t)), Intmkfifo __p ((const C Har *, mode_t)); Intstat __p ((const char *, struct stat *)), #ifndef _posix_sourceintfchmod __p ((int, mode_t)), Intlstat __p ( (const char *, struct stat *)); #endif/* Not POSIX */__END_DECLS#ENDIF



"Linux C learn Stat" Get the properties of a file

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.