Linux下编写自己的ls命令


Linux系统中ls -l命令可以获得某个目录下文件的具体信息,比如:
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$ ls -l
total 20
-rw-r--r-- 1 gaolu gaolu 2578 2009-03-08 02:10 gao.lu.c
-rw-r--r-- 1 gaolu gaolu 2577 2009-03-08 02:07 gao.lu.c~
-rwxr-xr-x 1 gaolu gaolu 9675 2009-03-08 02:12 ls.o
-rwxr--r-- 1 gaolu gaolu    0 2009-03-08 01:50 test1
-rw-r--r-- 1 gaolu gaolu    0 2009-03-08 01:50 test2
-rw-r--r-- 1 gaolu gaolu    0 2009-03-08 01:50 test3
-rw-r--r-- 1 gaolu gaolu    0 2009-03-08 01:51 test4
系统版本不同,显示的内容可能略有差别,主要包括几项:文件所在的inode值,权限信息,用户主和所属的组,最后修改日期,文件名,链接数等等。
【模块划分】
文件的这些基本信息都可以通过stat函数获得,因此只要可以获得某个目录下面的所有文件信息,再依次调用stat函数得到每个文件的信息,就可以实现ls -l命令的基本功能了。
结构如下:
------获得目录下所有文件列表
|
--目录文件列表获得模块-
|                      |
模块划分------                       ------将文件列表存入链表
|                -----------遍历链表
--信息输出模块--|
|
-------对每个文件调用stat获得信息并打印
【主要函数】
(1)DIR* opendir(const char *name)
功能:打开name指定的目录,返回指针,失败返回NULL.
相关头文件:
sys/types.h
dirent.h
(2)struct dirent* readdir(DIR* dir)
功能:获得某个打开目录下的具体内容
相关头文件:
sys/types.h
dirent.h
【代码实现】
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
//用于保存文件或者目录的名称
typedef struct dir_link{
char d_name[256];
struct dir_link * next;
}dirlink;
//保存单个文件的具体信息,方便输出
typedef struct item_info{
unsigned long inode;
char permission[11];
int owner;
int group;
off_t size;
time_t mod_time;
char name[256];
}info;
dirlink* get_dir_detail(char* dirname)
{
DIR* dir;
struct dirent* drt;
dirlink* head_node =NULL;
dirlink* cur_node = NULL;
dirlink* next_node = NULL;
dir = opendir(dirname);  //打开指定目录
if(NULL == dir)
{
perror("Can't open the directroy.");
return NULL;
}
while((drt = readdir(dir)) != NULL)
{
if((strcmp(drt->d_name,".")==0)||(strcmp(drt->d_name,"..")==0))
{
continue;    //忽略.目录和..目录
}
next_node = (dirlink*)malloc(sizeof(dirlink));
if(NULL == head_node)
{
head_node = next_node;   //首节点
}
else
{
cur_node->next = next_node;
}
cur_node = next_node;
strcpy(cur_node->d_name,drt->d_name);
}
cur_node->next = NULL;    //最后一个节点
closedir(dir);
return head_node;   //返回链表头指针
}
void print_file_info(dirlink* head_node)
{
struct stat file_stat;
dirlink* cur_node = head_node;
info file_info;
static char* perm[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
while(NULL != cur_node)   //遍历链表
{
int i = 3;
int j = 0;
unsigned int mask = 0700;
if(stat(cur_node->d_name,&file_stat) == -1)
{
perror("Can't get the information of the file.\n");
cur_node = cur_node->next;
continue;
}
if(S_ISREG(file_stat.st_mode))    //是普通文件
{
file_info.permission[0]='-';
}
if(S_ISDIR(file_stat.st_mode))    //是目录
{
file_info.permission[0]='d';
}

  • 1
  • 2
  • 下一页

相关内容