编程实现Linux下的ls -l

时间:2022-11-27 17:11:40

头文件

#ifndef __FUNC_H__
#define __FUNC_H__ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h> #define ENT_CNT 128
#define FILE_LEN 256 int get_file_name(DIR* pdir,char* dirname ,char names[][FILE_LEN]);
void str_sort(char arr[][FILE_LEN], int cnt);
void show_ent(char name[][FILE_LEN], int cnt);
void mode_to_str(mode_t mode, char* dest);
char* time_format(char* src); #endif

主函数

/*************************************************************************
> File Name: func.c
> Author: KrisChou
> Mail: zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:31:34 PM CST
************************************************************************/
#include "func.h" int main(int argc, char* argv[])
{
DIR* pdir ;
struct dirent *pent ;
    
    char file_names[ENT_CNT][FILE_LEN] ;
int cnt ; //所遍历目录子项的个数
char pold[128]=""; //记录程序原来的工作路径
char pnew[128]="."; //记录所需遍历目录的绝对路径,使用stat获取文件信息需要绝对路径(除非程序遍历当前目录)
getcwd(pold, 128);
if(argc == 2)
{
pdir = opendir(argv[1]);
chdir(argv[1]);
getcwd(pnew, 128);
chdir(pold);
}else if(argc == 1)
{
pdir = opendir("."); }else
{
printf("USAGE: EXE DIR \n");
exit(1);
} if(pdir == NULL)
{
perror("opendir");
exit(1);
}
cnt = get_file_names(pdir,pnew ,file_names); //将每一项的 绝对路径(pnew) + filename 存入数组,以便stat获取文件信息
str_sort(file_names, cnt);
show_ent(file_names, cnt);
return 0 ;
}

1. get_file_name

/*************************************************************************
> File Name: ./src/get_file_name.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 03:03:23 PM CST
************************************************************************/ #include "func.h"
int get_file_names(DIR* pdir, char* dirname, char names[][FILE_LEN])
{
struct dirent* pent ;
int cnt = 0 ;
while( (pent = readdir(pdir)) != NULL )
{
if(strncmp(pent ->d_name, ".", 1) == 0 || strncmp(pent ->d_name, "..", 2) == 0)
{
continue ;
}
strcpy(names[cnt],dirname);
strcat(names[cnt], "/");
strcat(names[cnt], pent -> d_name);
cnt ++ ;
}
closedir(pdir);
return cnt ; }

2. str_sort

/*************************************************************************
> File Name: ./src/str_sort.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 03:19:05 PM CST
************************************************************************/
#include "func.h" static int my_cmp(const void* left, const void* right )
{
return strcmp((char* )left , (char*)right);
} void str_sort(char arr[][FILE_LEN], int cnt)
{
qsort(arr, cnt, FILE_LEN, my_cmp);
}

3. show_ent

/*************************************************************************
> File Name: show_ent.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:36:17 PM CST
************************************************************************/
#include "func.h" void show_ent(char name[][FILE_LEN], int cnt)
{
struct stat my_stat ;
char buf[11]="";
char *pstr ;
int index ;
for(index = 0; index < cnt; index ++)
{
memset(&my_stat, 0, sizeof(my_stat));
if( stat(name[index], &my_stat) == -1 )
{
perror("stat");
exit (1);
}
mode_to_str(my_stat.st_mode, buf);
pstr = ctime(&(my_stat.st_atime));//Mon Aug 18 14:37:40 2014
pstr = time_format(pstr);
printf("%10s.%2d%7s%7s%5d%13s %s\n",buf, my_stat.st_nlink, getpwuid(my_stat.st_uid)->pw_name, getgrgid(my_stat.st_gid) ->gr_name, my_stat.st_size, pstr,name[index]); }
}

3.1 mode_to_str

/*************************************************************************
> File Name: mode_to_str.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:32:33 PM CST
************************************************************************/
#include "func.h" void mode_to_str(mode_t mode, char* dest)
{
memset(dest,'-',10);
if(S_ISDIR(mode))
{
dest[0]='d';
}
if(S_ISREG(mode))
{
dest[0]='-';
} if(mode & S_IRUSR)
{
dest[1] = 'r' ;
}
if(mode & S_IWUSR)
{
dest[2] = 'w' ;
}
if(mode & S_IXUSR)
{
dest[3] = 'x' ;
}
if(mode & S_IRGRP)
{
dest[4] = 'r' ;
}
if(mode & S_IWGRP)
{
dest[5] = 'w' ;
}
if(mode & S_IXGRP)
{
dest[6] = 'x' ;
}
if(mode & S_IROTH)
{
dest[7] = 'r' ;
}
if(mode & S_IWOTH)
{
dest[8] = 'w' ;
}
if(mode & S_IXOTH)
{
dest[9] = 'x' ;
}
}

3.2 time_format

/*************************************************************************
> File Name: time_format.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 19 Aug 2014 02:35:12 PM CST
************************************************************************/
#include "func.h" char* time_format(char* src)
{
int index = strlen(src) - 1 ;
for(; src[index]!=':'; index -- )
{ }
src[index] = '\0' ;
return src + 4 ;
}

Makefile

SRC_DIR := ./src
INC_DIR := ./include
EXE_DIR := ./bin
OBJECTS := $(wildcard $(SRC_DIR)/*.c)
INCLUDES :=$(wildcard $(INC_DIR)/*.h)
CC := gcc
FLAGS := -o
$(EXE_DIR)/main : $(OBJECTS) $(INCLUDES)
$(CC) $(FLAGS) $@ $(OBJECTS) -I$(INC_DIR)