c言語

5207 ワード

stat()関数
    :fstat, lstat, chmod, chown, readlink, utime

   :#include  #include

    :int stat(const char * file_name, struct stat *buf);

    :stat()       file_name        ,       buf       。

構造体内部パラメータの説明
struct stat {
    dev_t st_dev; //device        
    ino_t st_ino; //inode    i-node
    mode_t st_mode; //protection            
    nlink_t st_nlink; //number of hard links            ,         1.
    uid_t st_uid; //user ID of owner             
    gid_t st_gid; //group ID of owner            
    dev_t st_rdev; //device type            ,         
    off_t st_size; //total size, in bytes     ,       
    unsigned long st_blksize; //blocksize for filesystem I/O      I/O      . 
    u nsigned long st_blocks; //number of blocks allocated          ,        512    . 
    time_t st_atime; //time of lastaccess                 ,       mknod、 utime、read、write   tructate    .
    time_t st_mtime; //time of last modification             ,       mknod、 utime   write      
    time_t st_ctime; //time of last change i-node           ,           、 、          
};

st_mode
1、S_IFMT 0170000          
2、S_IFSOCK 0140000 scoket
3、S_IFLNK 0120000      
4、S_IFREG 0100000      
5、S_IFBLK 0060000      
6、S_IFDIR 0040000    
7、S_IFCHR 0020000      
8、S_IFIFO 0010000      
9、S_ISUID 04000     (set user-id on execution)  
10、S_ISGID 02000     (set group-id on execution)  
11、S_ISVTX 01000    sticky   

12、S_IRUSR (S_IREAD) 00400             
13、S_IWUSR (S_IWRITE)00200             
14、S_IXUSR (S_IEXEC) 00100             
15、S_IRGRP 00040           
16、S_IWGRP 00020           
17、S_IXGRP 00010           
18、S_IROTH 00004            
19、S_IWOTH 00002            
20、S_IXOTH 00001                    POSIX                

21、S_ISLNK (st_mode)           
22、S_ISREG (st_mode)         
23、S_ISDIR (st_mode)       
24、S_ISCHR (st_mode)           
25、S_ISBLK (s3e)         
26、S_ISSOCK (st_mode)    socket       sticky   (S_ISVTX),                      、       root       .
//    :       0,    -1,      errno。

エラーコード
1、ENOENT   file_name          
2、ENOTDIR                 
3、ELOOP                ,    16     
4、EFAULT   buf      ,            
5、EACCESS         
6、ENOMEM       
7、ENAMETOOLONG   file_name        

リファレンスリンク
C言語stat()関数:ファイル状態の取得
LinuxでUIDとGIDでユーザ名とグループ名を取得
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int exchange (char* path) {
    struct stat st;
    if (stat(path, &st) == -1) {
        perror("stat error");
        return -1;
    }

    struct passwd *passwd;
    passwd = getpwuid (st.st_uid);
    printf ("User:\t%s
", passwd->pw_name); struct group *group; group = getgrgid (passwd->pw_gid); printf ("Group:\t%s
", group->gr_name); }

strtchr()関数
#include
char *strrchr(const char *str, char c);

ある文字cが別の文字列strに最後に現れる位置(すなわちstrの右側から文字cが最初に現れる位置)を探し、その文字のアドレスを返します.指定された文字が見つからない場合、関数はNULLを返します.
strftime()
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

リファレンスリンク
C言語strftimeフォーマット表示日時タイムスタンプ
ケーススタディ
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//    path           
int test(char* path){
    //       
    char* filename = NULL;
    // filename                 '/'      
    if ((filename = strrchr(path, '/')) != NULL) {
    //       filename                
        filename++;
    }

    //       ,            -1
    struct stat st;
    if (stat(path, &st) == -1) {
        perror("stat error");
        return -1;
    }

    //     ,  /   /   
    //                 
    if (st.st_mode & S_IRUSR) {
        printf("r");
    }
    else{
        printf("-");
    }
    if (st.st_mode & S_IWUSR) {
        printf("w");
    }
    else{
        printf("-");
    }
    if (st.st_mode & S_IXUSR) {
        printf("x");
    }
    else{
        printf("-");
    }

    //    UID       
    struct passwd *passwd;
    passwd = getpwuid (st.st_uid);
    printf (" %s ", passwd->pw_name);
    //    GID      
    struct group *group;
    group = getgrgid (passwd->pw_gid);
    printf ("%s", group->gr_name);

    //                  
    time_t mtime = st.st_mtime;
    struct tm * timeinfo;
    timeinfo = localtime (&mtime);
    char timebuffer [128];
    strftime(timebuffer,sizeof(timebuffer), "%Y/%m/%d", timeinfo);
    printf(" %s ", timebuffer);
}