收藏 分享(赏)

c语言实现ls -l的功能.doc

上传人:wspkg9802 文档编号:6975279 上传时间:2019-04-29 格式:DOC 页数:7 大小:58.50KB
下载 相关 举报
c语言实现ls -l的功能.doc_第1页
第1页 / 共7页
c语言实现ls -l的功能.doc_第2页
第2页 / 共7页
c语言实现ls -l的功能.doc_第3页
第3页 / 共7页
c语言实现ls -l的功能.doc_第4页
第4页 / 共7页
c语言实现ls -l的功能.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、Linux 下 C 语言实现 ls -l 功能 需求:用 ls -l 显示文件夹下所有的文件及属性 分析:1 用 ls 显示文件夹下的所有文件,首先用 opendir 打开文件夹,再用readdir 读取文件夹里面的文件,最后 closedir 关闭文件夹。2 用 ls -l 比较复杂,打印出来的内容包括:(文件属性+文件名) 由以下几部分构成文件的类型(d目录文件 -文本文件.)文件的可执行权限 所属组的权限,其它组的权限 硬链接数 文件拥有者 文件拥有者组 文件大小 时间 文件名 解决方案: 1. 要想实现 ls -l 就得知道通过什么系统调用获取文件信息,首先自然是要通过man 来查找相

2、关的系统调用。man -k file|grep statusman -k file|grep informationman -k file|grep info通过上面的搜索就可以得到 stat 这个系统调用获取文件属性。man 2 stat 获取系统调用的详细使用方法:#include #include #include 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_nli

3、nk; /* 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; /* blocksize for file system I/O */blkcnt_t st_blocks; /* number of 512B blocks alloca

4、ted */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 */;只要通过相应字段进行格式处理就行啦!2.权限处理:st_mode 就是文件的权限部分。St_mode 本身就是一个 16 位的二进制,前四位是文件的类型,紧接着三位是特殊权限,左后九位就是 ls -l 列出来的九个权限。 linux 本身提供了很多测试宏来测试文件的类型的#define _S_IFMT 01

5、70000 /* These bits determine file type. */* File types. */#define _S_IFDIR 0040000 /* Directory. */#define _S_IFCHR 0020000 /* Character device. */#define _S_IFBLK 0060000 /* Block device. */#define _S_IFREG 0100000 /* Regular file. */#define _S_IFIFO 0010000 /* FIFO. */#define _S_IFLNK 0120000 /*

6、Symbolic link. */#define _S_IFSOCK 0140000 /* Socket. */利用上面的测试宏就可以判断文件的类型,至于文件的权限部分可以使用掩码的方式来处理。具体代码如下:vim file_mode.c#include “list.h“/man 2 stat 可看到文件基本属性void file_mode(struct stat* buf)int i;char buff10 = “-“;switch(buf-st_mode break;case S_IFDIR:buff0 = d;break;case S_IFSOCK:buff0 = s;break;cas

7、e S_IFBLK:buff0 = b;break;case S_IFLNK:buff0 = l;break;if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode for(i=0;i pw_name,str - gr_name);/打印文件所有者和文件所有者组5.完整代码展示:list.h 头文件:#ifndef LIST_H#define LIST_H#include #incl

8、ude #include #include #include #include #include #include #include #include #include void file_mode(struct stat* buf);void file_gid_uid(int uid,int gid);void file_operation(char* argv);#endiflist.c 主框架:#include “list.h“int main(int argc,char* argv)if(argc = 1)argv1 = “./“;file_operation(argv);return

9、 0;file_mode.c 文件属性: #include “list.h“/man 2 stat 可看到文件基本属性void file_mode(struct stat* buf)int i;char buff10 = “-“;switch(buf-st_mode break;case S_IFDIR:buff0 = d;break;case S_IFSOCK:buff0 = s;break;case S_IFBLK:buff0 = b;break;case S_IFLNK:buff0 = l;break;if(buf-st_mode if(buf-st_mode if(buf-st_mod

10、e if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode if(buf-st_mode for(i=0;i pw_name,str - gr_name);/打印文件所有者和文件所有者组file_operation.c:#include “list.h“void file_operation(char* argv)DIR* fd; /定义文件夹类型struct dirent* fp;/fp 文件夹返回值struct stat buf;int ret;/获取文件属性char temp100; /中间变量

11、存放文件路径if(fd = opendir(argv1) = NULL)/打开文件夹perror(“open file fail!“);exit(0);while(fp = readdir(fd) != NULL)/循环读取文件夹中的文件信息strcpy(temp,argv1);/将路径付给中间变量 tempstrcat(temp,fp - d_name);/把文件名字添加到路径后面形成完整的路径if(ret = stat(temp,exit(0);file_mode(/文件属性(目录,链接,可读,可写,可执行)printf(“ %d“,buf.st_nlink);/打印链接数file_gid

12、_uid(buf.st_uid,buf.st_gid);/调用函数打印出文件拥有者和文件所有者组printf(“t%ld“,buf.st_size);/打印文件大小/Time();/获取时间printf(“t%.12s “,4 + ctime(printf(“ %sn“,fp - d_name);/打印文件名closedir(fd);makefile:# 生成可执行文件和动态链接库的 makefile#source file#源文件,自动找所有.c 和 .cpp 文件,并将目标定义为同名 .o 文件SOURCE := $(wildcard *.c) $(wildcard *.cpp)OBJS

13、:= $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)#target you can change test to what you want#目标文件名,输入任意你想要的执行文件名TARGET := appTARGET_2 := libtest.so#compile and lib parameter#编译参数CC := gccLIBS :=LDFLAGS :=DEFINES :=INCLUDE := -I.CFLAGS := -g $(DEFINES) $(INCLUDE)#-Wall -O3 -DDEBUGCXXFLAGS:= $(CFL

14、AGS) -DHAVE_CONFIG_HSHARE := -fPIC -shared -o #i think you should do anything here#下面的基本上不需要做任何改动了.PHONY : everything objs clean veryclean rebuildeverything : $(TARGET) $(TARGET_2)all : $(TARGET) $(TARGET_2)objs : $(OBJS)rebuild: veryclean everythingclean :#rm -fr *.sorm -fr *.orm -fr index_head.dat

15、 index_head.txt index_content.txt index_content.dat draw_word.txtveryclean : cleanrm -fr $(TARGET) rm -fr $(TARGET_2) rm -fr index_head.dat index_head.txt index_content.txt index_content.dat draw_word.txtrm -fr dict.dat$(TARGET_2) : $(OBJS)$(CC) $(CXXFLAGS) $(SHARE) $ $(OBJS) $(LDFLAGS) $(LIBS)$(TARGET) : $(OBJS)$(CC) $(CXXFLAGS) -o $ $(OBJS) $(LDFLAGS) $(LIBS)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报