type3 | type2 | type1 | type0 | suid | sgid | sticky | r | w | x | r | w | x | r | w | x |
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 用户所有者的ID*/
gid_t st_gid; /* group ID of owner 所属组的ID*/
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 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 文件属性最后改变时间*/
};
struct stat statbuf;
mode_t modes
stat( "filaname", &statbuf );
modes = statbuf.st_mode;
if(!S_ISDIR(modes) && (modes & S_IRWXU) == S_IXUSR)
...
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int i;
struct stat buf;
char *ptr;
for (i = 0; i < argc; i++) {
printf ("%s: ", argv[i]);
if (lstat (argv[i], &buf) < 0) {
perror ("lstat");
continue;
}
if (S_ISREG(buf.st_mode))
ptr = "regular file";
else if (S_ISDIR(buf.st_mode))
ptr = "directory fle";
else if (S_ISCHR(buf.st_mode))
ptr = "character special file";
else if (S_ISBLK(buf.st_mode))
ptr = "block special file";
else if (S_ISFIFO(buf.st_mode))
ptr = "fifo file";
#ifdef S_ISLNK
else if (S_ISLNK(buf.st_mode))
ptr = "symbolic link";
#endif
#ifdef S_ISSOCK
else if (S_ISSOCK(buf.st_mode))
ptr = "socket";
#endif
else
ptr = "** unknown mode **";
printf ("%s\\n", ptr);
}
return (0);
}
./lstat dir_test
./lstat: regular file
dir_test: directory fle
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |