利用UNIX文件函数和C语言实现
linux系统ls -l功能
1.获得本文件目录下的所有文件信息
2.获得指定文件目录下的所有文件信息
3.指定获得文件信息
/*===============================================
* 文件名称:newhomework.c
* 创 建 者:memories
* 创建日期:2023年05月12日
* 描 述:
================================================*/
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
/************************************************************************
* 文件说明
************************************************************************/
int dirProprety(char *dirPath);
int fileProprety(char *fileName);
int main(int argc, char *argv[])
{
if(argc == 1){
dirProprety(".");
}
if( argc == 2 ){
struct stat st;
if( lstat(argv[1], &st) ){
perror("lstat");
return -1;
}
if( S_ISDIR(st.st_mode ) ){ // 判断参数文件的类型为 目录还是 文件
chdir(argv[1]); // 切换或者改变 程序运行时的路径
dirProprety(argv[1]); // 打开目录,得到目录流,然后遍历目录流
} else {
fileProprety(argv[1]); // 获取文件属性
}
}
return 0;
}
int dirProprety(char *dirPath)
{
// 打开目录得到目录流
DIR *dir = opendir(dirPath);
if(dir == NULL){
perror("opendir");
return -1;
}
//循环的读取目录流中的每个子文件的信息
struct dirent *fp;
while( (fp = readdir(dir)) ){
//取出 当前文件的名字,并获取 文件名对应的属性
// 访问 stat结构体成员,输出文件属性
if( strcmp(fp->d_name, ".")== 0 || strcmp(fp->d_name, "..") == 0){
continue;
}
if( fileProprety(fp->d_name) < 0){
continue;
}
}
}
int fileProprety(char *fileName)
{
struct stat st;
if( lstat( fileName, &st) ){
perror("lstat");
return -1;
}
//判断文件类型
switch( st.st_mode & (0xf << 12))
{
case S_IFREG: printf("-"); break;
case S_IFDIR: printf("d"); break;
case S_IFBLK: printf("b"); break;
case S_IFCHR: printf("c"); break;
case S_IFLNK: printf("l"); break;
case S_IFIFO: printf("p"); break;
case S_IFSOCK: printf("s"); break;
}
//判断文件权限
for(int i = 8; i >= 0; i--){
if( st.st_mode & (1 << i)){
switch( i % 3 ){
case 2: printf("r"); break;
case 1: printf("w"); break;
case 0: printf("x"); break;
}
} else
printf("-");
}
printf(" ");
//硬链接个数
printf("%ld ", st.st_nlink);
// 获得 uid 对应的 用户信息结构体首地址
struct passwd *pw = getpwuid(st.st_uid);
printf("%s ", pw->pw_name);
// 获得用户 gid 对应用户组结构体信息首地址
struct group *gp = getgrgid(st.st_gid);
printf("%s ", gp->gr_name);
printf("%ld ", st.st_size);
// 获得 文件的最后一次修改日期和文件名
struct tm *tm = localtime( &st.st_mtime );
printf("%d月 %d %2d:%2d %s", tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, fileName);
printf("\n");
}
hqyj@ubuntu:~/5.11$ gcc newhomework.c
hqyj@ubuntu:~/5.11$ ./a.out
-rw-rw-r-- 1 hqyj hqyj 1219 5月 11 19:13 lstat_demo.c
-rw-rw-r-- 1 hqyj hqyj 806 5月 11 21:28 dir_demo.c
-rw-rw-r-- 1 hqyj hqyj 847 5月 11 19:12 write_demo.c
-rw-rw-r-- 1 hqyj hqyj 33 5月 11 11:45 file.txt
-rw-rw-r-- 1 hqyj hqyj 816 5月 11 19:33 read_demo.c
-rw-rw-r-- 1 hqyj hqyj 2760 5月 12 20: 2 homework.c
-rw-rw-r-- 1 hqyj hqyj 2819 5月 12 20:12 newhomework.c
-rw-rw-r-- 1 hqyj hqyj 884 5月 12 11:43 homework_main.c
-rwxrwxr-x 1 hqyj hqyj 17336 5月 12 20:12 a.out
drwxrwxr-x 2 hqyj hqyj 4096 5月 11 16:55 staticC
-rw-rw-r-- 1 hqyj hqyj 314 5月 12 11:32 homework.h
hqyj@ubuntu:~/5.11$ ./a.out ../5.10
-rw-rw-r-- 1 hqyj hqyj 896 5月 10 19:48 time_demo.c
-rwxrwxr-x 1 hqyj hqyj 0 5月 10 16:26 file1.txt
-rw-rw-r-- 1 hqyj hqyj 44 5月 11 9:15 file.txt
-rw-rw-r-- 1 hqyj hqyj 1444 5月 11 10:33 getsc_demo.c
-rw-rw-r-- 1 hqyj hqyj 227 5月 12 19:21 new_test3.txt
-rw-rw-r-- 1 hqyj hqyj 33 5月 10 19:50 new_test.txt
-rw-rw-r-- 1 hqyj hqyj 472 5月 10 19:33 time_test.demo.h
-rw-rw-r-- 1 hqyj hqyj 746 5月 10 14:23 fread_demo.c
-rw-rw-r-- 1 hqyj hqyj 901 5月 10 19:51 fputc_filecopy.c
-rw-rw-r-- 1 hqyj hqyj 817 5月 10 15:44 fseek_demo.c
-rw-rw-r-- 1 hqyj hqyj 612 5月 10 16:31 open.c
-rwxrwxr-x 1 hqyj hqyj 17224 5月 12 19:21 a.out
-rw-rw-r-- 1 hqyj hqyj 770 5月 10 19:48 time_test.demo.c
-rw-rw-r-- 1 hqyj hqyj 18 5月 10 10:56 2.txt
-rw-rw-r-- 1 hqyj hqyj 748 5月 10 19:42 new_test2.txt
-rw-rw-r-- 1 hqyj hqyj 675 5月 10 19: 9 fwrite_demo.c
-rw-rw-r-- 1 hqyj hqyj 961 5月 10 19:51 fputs_filecopy.c
-rw-rw-r-- 1 hqyj hqyj 0 5月 10 18: 7 ls
hqyj@ubuntu:~/5.11$ ./a.out homework.c
-rw-rw-r-- 1 hqyj hqyj 2760 5月 12 20: 2 homework.c