#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h> #define ERR_EXIT( m ) \
do\
{ \
perror( m ); \
exit( EXIT_FAILURE ); \
}while( 0 ) #define MAJOR( a ) (int)((unsigned short)a >> 8 ) //获取高8位
#define MINOR( a ) (int)((unsigned short)a & 0xFF ) //获取低8位 //检测文件类型
int filetype( struct stat* st, char* filetype ); //检测文件权限
void fileperm( struct stat* st, char* perm ); int main( int argc, char* argv[] ) { int ret;
if( argc != 2 ) {
fprintf( stderr, "usage:%s filename\n", argv[0] );
exit( EXIT_FAILURE );
} struct stat st; ret = stat( argv[1], &st );
if( -1 == ret ) {
ERR_EXIT( "stat file error" );
} printf( "文件大小:st_size=%d\n", st.st_size );
printf( "用户id:uid=%d\n", st.st_uid );
printf( "组id:gid=%d\n", st.st_gid );
//printf( "主设备号:major_dev=%d\n", MAJOR( st.st_dev ) );
//printf( "次设备号:minor_dev=%d\n", MINOR( st.st_dev ) );
printf( "i节点:st_ino=%d\n", st.st_ino ); char filetypestr[64] = { 0 };
int ftype = filetype( &st, filetypestr );
if( ftype == 3 || ftype == 5 ) {
printf( "文件类型:%s\n", filetypestr );
printf( "主设备号:%d, 次设备号:%d\n", MAJOR( st.st_dev ), MINOR( st.st_dev ) );
}else {
printf( "文件类型:%s\n", filetypestr );
} char perm[11];
fileperm( &st, perm );
printf( "文件权限:%o, %s\n", st.st_mode & 07777, perm ); return 0;
} //检测文件类型,设置文件返回1 其他文件返回0
/*
S_IFSOCK 0140000 socket
S_IFLNK 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
*/
int filetype( struct stat* st, char* filetype ) { mode_t mode = st->st_mode;
int flag = 0; char* filetypes[] = {
"socket",
"symbolic link",
"regular file",
"block device",
"directory",
"character device",
"fifo"
}; switch( mode & S_IFMT ){
case S_IFSOCK:
strncpy( filetype, filetypes[0], strlen( filetypes[0] ) );
flag = 0;
break;
case S_IFLNK:
strncpy( filetype, filetypes[1], strlen( filetypes[1] ) );
flag = 1;
break;
case S_IFREG:
strncpy( filetype, filetypes[2], strlen( filetypes[2] ) );
flag = 2;
break;
case S_IFBLK:
strncpy( filetype, filetypes[3], strlen( filetypes[3] ) );
flag = 3;
break;
case S_IFDIR:
strncpy( filetype, filetypes[4], strlen( filetypes[4] ) );
flag = 4;
break;
case S_IFCHR:
strncpy( filetype, filetypes[5], strlen( filetypes[5] ) );
flag = 5;
break;
case S_IFIFO:
strncpy( filetype, filetypes[6], strlen( filetypes[6] ) );
flag = 6;
break;
} return flag;
} //检测文件权限
/*
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
*/
void fileperm( struct stat* st, char* perm ) { strncpy( perm, "----------", 10 );
perm[0] = '?';
mode_t mode = st->st_mode; char typestr[64] = { 0 };
char* abbreviation[] = {
"s", //socket
"l", //symbolic link
"-", //regular file
"b", //block file
"d", //directory
"c", //charactor device
"p" //fifo
}; int ret = filetype( st, typestr );
strncpy( perm, abbreviation[ret], 1 ); //检测文件所属的用户权限
if( mode & S_IRUSR ){
perm[1] = 'r';
}
if( mode & S_IWUSR ){
perm[2] = 'w';
}
if( mode & S_IXUSR ){
perm[3] = 'x';
} //检测用户组权限
if( mode & S_IRGRP ){
perm[4] = 'r';
}
if( mode & S_IWGRP ){
perm[5] = 'w';
}
if( mode & S_IXGRP ){
perm[6] = 'x';
} //检测其他组权限
if( mode & S_IROTH ){
perm[7] = 'r';
}
if( mode & S_IWOTH ){
perm[8] = 'w';
}
if( mode & S_IXOTH ){
perm[9] = 'x';
}
perm[10] = '\0';
}