stat 的结构体
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
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; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
文件类型
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
char get_fileType(mode_t m)
{
char c = 0;
if (S_ISREG(m))
{
c = '-';
}
else if (S_ISDIR(m))
{
c = 'd';
}
else if (S_ISCHR(m))
{
c = 'c';
}
else if (S_ISBLK(m))
{
c = 'b';
}
else if (S_ISFIFO(m))
{
c = 'p';
}
else if (S_ISLNK(m))
{
c = 'l';
}
else if (S_ISSOCK(m))
{
c = 's';
}
return c;
}
文件权限
char *get_FilePermission(mode_t m, char ptr[10])
{
char str[4] = "rwx";
int i = 0;
for (i == 0; i < 9; i++)
{
if ((m & (0400 >> i)) == 0)
{
ptr[i] = '-';
continue;
}
ptr[i] = str[i % 3];
}
return ptr;
}
链接数
printf("%ld ", buf.st_nlink);
所属用户名
char *get_pwdname(uid_t uid, char *ptr)
{
struct passwd *pwd = getpwuid(uid);
if (NULL == pwd)
{
perror("getpwuid");
return NULL;
}
strcpy(ptr, pwd->pw_name);
return ptr;
}
所属组用户名
char *get_grname(gid_t gid, char *ptr)
{
struct group *grp = getgrgid(gid);
if (NULL == grp)
{
perror("getgrgid");
return NULL;
}
strcpy(ptr, grp->gr_name);
return ptr;
}
文件大小
printf("%ld ", buf.st_size);
最后一次修改时间
void get_localTime(time_t t)
{
struct tm *info;
info=localtime(&t);
printf("%02d %2d %2d:%2d ",info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min);
}
文件名
总体代码
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grp.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
// void get_FilePermission(mode_t m)
// {
// int i = 0;
// char a[3] = {'r', 'w', 'x'};
// for (i = 0; i < 9; i++)
// {
// if (m & 0400)
// {
// putchar(a[i % 3]);
// }
// else
// {
// putchar('-');
// }
// m << = 1;
// }
// }
char *get_FilePermission(mode_t m, char ptr[10])
{
char str[4] = "rwx";
int i = 0;
for (i == 0; i < 9; i++)
{
if ((m & (0400 >> i)) == 0)
{
ptr[i] = '-';
continue;
}
ptr[i] = str[i % 3];
}
return ptr;
}
char get_fileType(mode_t m)
{
char c = 0;
if (S_ISREG(m))
{
c = '-';
}
else if (S_ISDIR(m))
{
c = 'd';
}
else if (S_ISCHR(m))
{
c = 'c';
}
else if (S_ISBLK(m))
{
c = 'b';
}
else if (S_ISFIFO(m))
{
c = 'p';
}
else if (S_ISLNK(m))
{
c = 'l';
}
else if (S_ISSOCK(m))
{
c = 's';
}
return c;
}
char *get_pwdname(uid_t uid, char *ptr)
{
struct passwd *pwd = getpwuid(uid);
if (NULL == pwd)
{
perror("getpwuid");
return NULL;
}
strcpy(ptr, pwd->pw_name);
return ptr;
}
char *get_grname(gid_t gid, char *ptr)
{
struct group *grp = getgrgid(gid);
if (NULL == grp)
{
perror("getgrgid");
return NULL;
}
strcpy(ptr, grp->gr_name);
return ptr;
}
void get_localTime(time_t t)
{
struct tm *info;
info=localtime(&t);
printf("%02d %2d %2d:%2d ",info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min);
}
int main(int argc, const char *argv[])
{
char mode[20] = "";
char user[20] = "";
char group[20] = "";
char type = 0;
if (argc < 2)
{
fprintf(stderr, "请输入文件名");
return -1;
}
struct stat buf;
if (stat(argv[1], &buf) < 0)
{
perror("stat");
return -1;
}
type = get_fileType(buf.st_mode);
printf("%c", type);
//printf("st_mode: %o\n", buf.st_mode);
get_FilePermission(buf.st_mode, mode);
printf("%s ", mode);
// printf("st_nlink: %ld\n", buf.st_nlink);
printf("%ld ", buf.st_nlink);
// printf("st_uid: %d\n", buf.st_uid);
get_pwdname(buf.st_uid, user);
printf("%s ", user);
// printf("st_gid: %d\n", buf.st_gid);
get_grname(buf.st_gid, group);
printf("%s ", group);
// printf("st_size: %ld\n", buf.st_size);
printf("%ld ", buf.st_size);
//printf("st_atime: %ld\n", buf.st_atime);
get_localTime(buf.st_atime);
printf("%s\n",argv[1]);
return 0;
}