您的位置:首页 > 运维架构 > Linux

C 语言编写 Linux 中的 ls 命令

2014-03-22 20:07 981 查看
[转载地址:http://keren.blog.51cto.com/720558/144896]
Linux 中的 ls 命令主要负责显示文件的如下信息:

文件所在的inode值,
权限信息,
用户主和所属的组,
最后修改日期,
文件名,
链接数等等。

模块划分
目录文件列表获得模块
获得文件目录下的所有文件列表
将文件列表存入链表

信息输出模块
遍历链表
对每个文件调用 stat 获得信息再打印

函数库信息

#include <stdio.h>
#include <dirent.h> //POSIX.1标准定义的unix类目录操作的头文件
#include <stdlib.h> //包含了C、C++语言的最常用的系统函数
#include <unistd.h> // C C++ 提供对 POSIX 操作系统 API 的访问功能的头文件
#include <sys/stat.h> //stat函数可以返回一个结构,里面包括文件的全部属性。
#include <sys/types.h> //基本系统数据类型
#include <string.h>


两个 struct
// 保存文件或者目录的名称
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; // inux中的off_t类型默认是32位的long
time_t mod_time;
char name[256];
}info;


主要函数
DIR* opendir(const char *name)

功能:打开name指定的目录,返回指针,失败返回NULL.
相关头文件:sys/types.h   dirent.h

struct dirent* readdir(DIR* dir) 
功能:获得某个打开目录下的具体内容
相关头文件:sys/types.h   dirent.h

目录文件列表获得模块
获得文件目录下的所有文件列表
将文件列表存入链表

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(dir == NULL)
{
perror("Can't open the directory."); //将上一个函数发生错误的原因输出到标准设备
}

while ((drt = readdir(dir)) != NULL)
{
if((strcmp(drt->d_name,".") == 0) || (strcmp(drt->d_name,"..") == 0))
{
continue;  // 忽略.和..目录
}
next_node = (dirlink*) malloc(sizeof(dirlink));
if (head_node == NULL)
{
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;
}


信息输出模块
遍历链表
对每个文件调用 stat 获得信息再打印

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 (cur_node != NULL )
{
int i = 3;
int j = 0;
unsigned int mask = 0700;
if (stat(cur_node->d_name,&file_stat) == -1)
{
perror ("Cannot get the info\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';
}
while (i>0)
{
file_info.permission[1+j*3] = perm[(file_stat.st_mode & mask)>>(i-1)*3][0];
file_info.permission[2+j*3] = perm[(file_stat.st_mode & mask)>>(i-1)*3][1];
file_info.permission[3+j*3] = perm[(file_stat.st_mode & mask)>>(i-1)*3][2];
i--;
j++;
mask>>=3;
}

file_info.permission[10] = '\0';
file_info.owner = file_stat.st_uid;
file_info.group = file_stat.st_gid;
file_info.mod_time = file_stat.st_atime;
file_info.size = file_stat.st_size;
file_info.inode = file_stat.st_ino;
strcpy(file_info.name,cur_node->d_name);
//打印信息
printf("%u ",file_info.inode);
printf("%s ",file_info.permission);
printf("%d ",file_info.owner);
printf("%d ",file_info.group);
printf("%u ",file_info.size);
printf("%s ",ctime(&file_info.mod_time));
printf("%s \n",file_info.name);

cur_node = cur_node->next;

}
}


main 函数
int main(int argc,char* argv[])
{
dirlink* head = get_dir_detail(".");   //输入参数为当前目录
print_file_info(head);
return 0;
}


ls -d 命令

void ls_d( char* dirname)
{
DIR* dir;
struct dirent* drt;
dir = opendir (dirname);

if(dir == NULL)
{
perror("Can't open the directory.");
}

while ((drt = readdir(dir)) != NULL)
{
if( strcmp(drt->d_name,".") == 0)
{
printf("%s \n",drt->d_name);
}
}
closedir(dir);
}


[转载地址:http://my.oschina.net/sharelinux/blog/115389]

ls -R 命令

void ls_R( char* dirname)
{
DIR* dir;
struct dirent* drt;
dir = opendir (dirname);
struct stat file_stat;

if(dir == NULL)
{
perror("Can't open the directory.");
}
chdir(dirname);

while ((drt = readdir(dir)) != NULL)
{
stat(drt->d_name,&file_stat);
if(S_ISDIR(file_stat.st_mode))    //是目录
{
if((strcmp(drt->d_name,".") == 0) || (strcmp(drt->d_name,"..") == 0))
{
continue;  // 忽略.和..目录
}
printf("%s \n",drt->d_name);
ls_R(drt->d_name);
}
else
{
printf("%s \n",drt->d_name);
}

}
chdir("..");
closedir(dir);
}


ls -a 命令

void ls_a( char* dirname)
{
DIR* dir;
struct dirent* drt;
dir = opendir (dirname);
struct stat file_stat;

if(dir == NULL)
{
perror("Can't open the directory.");
}

while ((drt = readdir(dir)) != NULL)
{
stat(drt->d_name,&file_stat);
printf("%s \n",drt->d_name);
}

closedir(dir);
}


ls -i 命令

void ls_i( char* dirname)
{
DIR* dir;
struct dirent* drt;
dir = opendir (dirname);
struct stat file_stat;

if(dir == NULL)
{
perror("Can't open the directory.");
}

while ((drt = readdir(dir)) != NULL)
{
if((strcmp(drt->d_name,".") == 0) || (strcmp(drt->d_name,"..") == 0))
{
continue;  // 忽略.和..目录
}

stat(drt->d_name,&file_stat);
printf("%u\t%s\n",file_stat.st_ino, drt->d_name);
}

closedir(dir);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux c