文章目錄
一、概述
st_mode簡單介紹
st_mode詳細介紹
二、相關函數
1. stat
- 作用:獲得文件信息,也可以獲取文件大小。
- 頭文件:
- 參數說明:
- path 文件名
- buf 傳出參數,定義結構體 struct stat sb; &sb
- 返回值
- 失敗:返回 -1,設置 errno
- 成功:返回 0
注意: stat 碰到鏈接,會追溯到源文件,穿透!!!lstat 並不會穿透。
stat結構體:
linux 命令 stat 執行結果:
注意三個時間的區別:
time_t st_atime;/* time of last access */ 文件被讀,比如cat,open讀等
time_t st_mtime;/* time of last modification */ 文件內容發生改變
time_t st_ctime;/* time of last status change */文件屬性發生變化,比如大小,權限,硬連接數等
需求:使用stat實現實現 ls -l 的功能?
在實現的過程中需要獲取用戶名及組名,因此先看兩個函數:
1)getpwuid
- 作用:通過用戶的uid獲取用戶名
- 頭文件
參數說明: - uid用戶的uid
返回值
- 失敗:返回NULL
- 成功:返回 struct passwd * 結構體指針
2)getgrgid
- 作用:通過用戶的gid獲取用戶組名
- 頭文件
參數說明:
- gid用戶組的gid
返回值
- 失敗:返回NULL
- 成功:返回 struct group * 結構體指針
3)localtime
- 作用:獲取本地時間
- 頭文件
參數說明:
- timep:一個時間相關的結構體
返回值
- 失敗:返回NULL
- 成功:返回 struct tm * 結構體指針
傳入參數 timep 對應stat函數得到的結構體的秒數(time_t類型)。
最終實現:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<time.h>
#include <grp.h>
#include <pwd.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
struct stat sb;
stat(argv[1], &sb);
char stmode[11] = {0};
memset(stmode, '-', sizeof(stmode)-1);
//解析文件屬性
if (S_ISREG(sb.st_mode)) stmode[0] = '-'; //普通文件
if (S_ISDIR(sb.st_mode)) stmode[0] = 'd';
if (S_ISCHR(sb.st_mode)) stmode[0] = 'c';
if (S_ISBLK(sb.st_mode)) stmode[0] = 'b';
if (S_ISFIFO(sb.st_mode)) stmode[0] = 'p';
if (S_ISLNK(sb.st_mode)) stmode[0] = 'l';
if (S_ISSOCK(sb.st_mode)) stmode[0] = 's';
//解析權限
//user
if (sb.st_mode & S_IRUSR) stmode[1] = 'r';
if (sb.st_mode & S_IWUSR) stmode[2] = 'w';
if (sb.st_mode & S_IXUSR) stmode[3] = 'x';
//group
if (sb.st_mode & S_IRGRP) stmode[4] = 'r';
if (sb.st_mode & S_IWGRP) stmode[5] = 'w';
if (sb.st_mode & S_IXGRP) stmode[6] = 'x';
//other
if (sb.st_mode & S_IROTH) stmode[7] = 'r';
if (sb.st_mode & S_IWOTH) stmode[9] = 'w';
if (sb.st_mode & S_IXOTH) stmode[10] = 'x';
//分析 用戶名,組名可以通過函數獲得 getpwuid, getgrgid
//時間獲取
struct tm *filetm = localtime(&sb.st_atim.tv_sec);
char timebuf[20] = {0};
sprintf(timebuf, "%d月 %d %02d:%02d", filetm->tm_mon+1, filetm->tm_mday, filetm->tm_hour, filetm->tm_min);
printf("%s %ld %s %s %ld %s %s\n", stmode, sb.st_nlink, getpwuid(sb.st_uid)->pw_name,
getgrgid(sb.st_gid)->gr_name, sb.st_size, timebuf, argv[1]);
return 0;
}
2. access
- 作用:測試指定文件是否有某種權限
- 頭文件
參數說明: - pathname文件名
- mode:
- R_OK
- W_OK
- X_OK
- F_OK
返回值
- 失敗:返回-1,設置errno
- 成功:如果有權限或者文件存在,對應返回0
#include<stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
if (access(argv[1], R_OK) == 0) printf("%s read ok!\n", argv[1]);
if (access(argv[1], W_OK) == 0) printf("%s write ok!\n", argv[1]);
if (access(argv[1], X_OK) == 0) printf("%s exe ok!\n", argv[1]);
if (access(argv[1], F_OK) == 0) printf("%s file exists!\n", argv[1]);
return 0;
}
3. chmod
4. truncate
- 函數作用:截斷文件
- 頭文件
參數說明:
- path文件名
- length長度,長度如果大於原文件,直接拓展,如果小於原文件,截斷爲length長度。
返回值
- 成功:返回0
- 失敗:返回-1,設置errno
5. link
- 函數作用:創建硬連接
- 頭文件
參數說明:
- oldpath原文件
- newpath硬連接文件
返回值
- 成功:返回0
- 失敗:返回-1,設置errno
6. symlink
- 函數作用:創建軟連接
- 頭文件
參數解釋:
- oldpath原文件
- newpath創建軟連接文件
返回值
- 成功:返回0
- 失敗:返回-1,設置errno
7. readlink
- 函數作用:讀取文件鏈接信息
- 頭文件
參數解釋:
- path鏈接名
- buf緩衝區
- bufsiz緩衝區大小
返回值
- 成功:返回buf填充的大小
- 失敗:返回-1,設置errno
8. unlink
- 函數作用:刪除軟硬鏈接
- 頭文件
函數參數:
- pathname 鏈接名,文件也可以
返回值
- 成功:返回0
- 失敗:返回-1,設置errno
#include<stdio.h>
#include <unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
#include<sys/types.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
int fd = open(argv[1], O_WRONLY|O_CREAT, 0666);
//注意只要有進程在使用該文件,則unlink在該文件退出時刪除該文件
unlink(argv[1]);
int ret = write(fd, "hello", 5);
if (ret > 0)
{
printf("write ok! %d\n", ret);
}
if (ret < 0)
{
perror("write err");
}
close(fd);
return 0;
}
9. chown
- 函數作用:修改文件屬主及屬組
- 頭文件
函數參數:
- path文件名
- owner用戶ID,/etc/passwd
- owner組ID,/etc/group
返回值
- 成功:返回0
- 失敗:返回-1,設置errno
10. rename
- 函數作用:文件或者目錄重命名
- 頭文件
參數說明:
- oldpath文件名
- newpath文件新名
返回值
- 成功:返回0
- 失敗:返回-1,設置errno