编写Linux C程序,列出stu2主目录中的文件信息,并将其中saturday.txt文件的权限设置为文件所有者可读可写、同组用户只读、其他用户无权限。
时间: 2024-03-26 12:34:15 浏览: 28
可以使用Linux系统调用中的stat()函数和chmod()函数来实现该功能。
下面是一个示例程序,可以列出stu2主目录中的文件信息并修改saturday.txt的权限:
```c
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
char* dir_path = "/home/stu2"; //stu2主目录路径
char* file_name = "saturday.txt"; //要修改权限的文件名
//获取stu2主目录下的文件信息
struct stat st;
if (stat(dir_path, &st) == -1) {
perror("stat");
return 1;
}
//打印文件信息
printf("%s:\n", dir_path);
printf(" 文件类型: ");
if (S_ISREG(st.st_mode)) printf("普通文件\n");
else if (S_ISDIR(st.st_mode)) printf("目录\n");
else printf("其他文件类型\n");
printf(" 文件权限: ");
printf((st.st_mode & S_IRUSR) ? "r" : "-");
printf((st.st_mode & S_IWUSR) ? "w" : "-");
printf((st.st_mode & S_IXUSR) ? "x" : "-");
printf((st.st_mode & S_IRGRP) ? "r" : "-");
printf((st.st_mode & S_IWGRP) ? "w" : "-");
printf((st.st_mode & S_IXGRP) ? "x" : "-");
printf((st.st_mode & S_IROTH) ? "r" : "-");
printf((st.st_mode & S_IWOTH) ? "w" : "-");
printf((st.st_mode & S_IXOTH) ? "x" : "-");
printf("\n");
//修改saturday.txt的权限
char* file_path = malloc(strlen(dir_path) + strlen(file_name) + 2);
sprintf(file_path, "%s/%s", dir_path, file_name);
if (chmod(file_path, S_IRUSR | S_IWUSR | S_IRGRP) == -1) {
perror("chmod");
return 1;
}
//打印修改后的权限信息
if (stat(file_path, &st) == -1) {
perror("stat");
return 1;
}
printf("%s:\n", file_path);
printf(" 文件类型: ");
if (S_ISREG(st.st_mode)) printf("普通文件\n");
else if (S_ISDIR(st.st_mode)) printf("目录\n");
else printf("其他文件类型\n");
printf(" 文件权限: ");
printf((st.st_mode & S_IRUSR) ? "r" : "-");
printf((st.st_mode & S_IWUSR) ? "w" : "-");
printf((st.st_mode & S_IXUSR) ? "x" : "-");
printf((st.st_mode & S_IRGRP) ? "r" : "-");
printf((st.st_mode & S_IWGRP) ? "w" : "-");
printf((st.st_mode & S_IXGRP) ? "x" : "-");
printf((st.st_mode & S_IROTH) ? "r" : "-");
printf((st.st_mode & S_IWOTH) ? "w" : "-");
printf((st.st_mode & S_IXOTH) ? "x" : "-");
printf("\n");
free(file_path);
return 0;
}
```
该程序先使用stat()函数获取stu2主目录的文件信息,并使用chmod()函数将saturday.txt的权限设置为文件所有者可读可写、同组用户只读、其他用户无权限。最后再次使用stat()函数打印修改后的权限信息。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)