Header file
#ifndef __func_h__#define__func_h__#include<stdio.h>#include<stdio.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>#defineENT_CNT 128#defineFile_len 256intGet_file_name (dir* Pdir,Char* DirName,CharNames[][file_len]);voidStr_sort (CharArr[][file_len],intCNT);voidShow_ent (CharName[][file_len],intCNT);voidMODE_TO_STR (mode_t mode,Char*dest);Char* Time_format (Char*src);#endif
Main function
/************************************************************************* > File name:func.c > Author: Krischou > Mail: [email protected] > Created time:tue 02:31:34 PM CST ****************************** ******************************************/#include"func.h"intMainintargcChar*argv[]) {DIR*Pdir; CharFile_names[ent_cnt][file_len]; intCNT;//number of traversed directory subkeys structDirent *Pent; Charpold[ -]="";//record the original working path of the program Charpnew[ -]="";//record the absolute path required to traverse the directory, using stat to get the file information requires absolute path (unless the program traverses the current directory)GETCWD (Pold, -); if(ARGC = =2) {Pdir= Opendir (argv[1]); ChDir (argv[1]); GETCWD (Pnew, -); 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);//The Absolute Path (pnew) + filename of each item is stored in the array so that stat gets the file informationStr_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:[email protected] > Created time:tue-03:03:23 PM CST ****************** ******************************************************/#include"func.h"intGet_file_names (dir* Pdir,Char* DirName,CharNames[][file_len]) { structdirent*Pent; intCNT =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); returnCNT;}2. Str_sort
/************************************************************************* > File Name:./src/str_sort.c > A Uthor:krischou > Mail:[email protected] > Created time:tue-03:19:05 PM CST ******************** ****************************************************/#include"func.h"Static intMY_CMP (Const void* Left,Const void*Right ) { returnstrcmp ((Char*) Left, (Char*Right );}voidStr_sort (CharArr[][file_len],intCNT) {qsort (arr, CNT, File_len, my_cmp);}3. Show_ent
/************************************************************************* > File name:show_ent.c > Author: Krischou > Mail:[email protected] > Created time:tue-02:36:17 PM CST ******************************* *****************************************/#include"func.h"voidShow_ent (CharName[][file_len],intCNT) { structstat My_stat; Charbuf[ One]=""; Char*pstr; intindex; 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 14:37:40PSTR =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:[email protected] > Created time:tue-02:32:33 PM CST *********************** *************************************************/#include"func.h"voidMODE_TO_STR (mode_t mode,Char*dest) {memset (dest,'-',Ten); 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 > Auth Or:krischou > Mail:[email protected] > Created time:tue-02:35:12 PM CST *********************** *************************************************/#include"func.h"Char* Time_format (Char*src) { intindex = strlen (SRC)-1 ; for(; src[index]!=':'; Index--) {} Src[index]=' /' ; returnSRC +4 ;}Makefile
Src_dir: =./srcinc_dir:=./includeexe_dir:=./binobjects:= $ (wildcard $ (src_dir)/*
. C) Includes: =$ (wildcard $ (inc_dir)/*.h) CC: = gccflags: =-o$ (Exe_dir)/main: $ (OBJECTS) $ (includes) $ ( cc) $ ( FLAGS) [email protected] $ (OBJECTS)-i$ (inc_dir)