linux c模擬ls命令詳解
來(lái)源:易賢網(wǎng) 閱讀:1113 次 日期:2014-09-05 15:03:15
溫馨提示:易賢網(wǎng)小編為您整理了“l(fā)inux c模擬ls命令詳解”,方便廣大網(wǎng)友查閱!

本篇文章是對(duì)linux中基于c模擬ls命令的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

代碼如下:

/*

模擬ls命令,實(shí)現(xiàn)參數(shù)-tariRl.有些代碼重復(fù)出現(xiàn),可改進(jìn)

可以在該程序的基礎(chǔ)上增加參數(shù),該程序思路清晰,容易擴(kuò)展

*/

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/types.h>

#include<unistd.h>

#include<sys/stat.h>

#include<dirent.h>

#include<sys/ioctl.h>

#include<pwd.h>

#include<grp.h>

#define LL (1<<0) /*帶l參數(shù)標(biāo)志*/

#define IL (1<<1) /*帶i參數(shù)標(biāo)志*/

#define RU (1<<2) /*帶R參數(shù)標(biāo)志*/

#define TL (1<<16) /*帶t參數(shù)標(biāo)志*/

#define AL (1<<17) /*帶a參數(shù)標(biāo)志*/

#define RL (1<<18) /*帶r參數(shù)標(biāo)志*/

#define LENGTH 500 /*字符串最大長(zhǎng)度*/

#define MAX_FILE_COUNT 500/*文件列表最大長(zhǎng)度*/

/*函數(shù)聲明*/

void error(char* error_message,int line);

void display(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode);

int file_list(char* path,struct stat* stalist,char (*namelist)[LENGTH],int mode);

void display_simply(char (*namelist)[LENGTH],int n);

void display_LL(struct stat * stalist,char (*namelist)[LENGTH],int n);

void display_IL(struct stat * stalist,char (*namelist)[LENGTH],int n);

void display_LL_IL(struct stat * stalist,char (*namelist)[LENGTH],int n);

void display_RU(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode);

void display_RU_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode);

void display_RU_IL(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode);

void display_RU_IL_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode);

/*主函數(shù)*/

int main(int argc,char **argv)

{

int i,j;

/*mode的前16個(gè)位用來(lái)標(biāo)志那些能影響顯示的參數(shù),16位之后的位用來(lái)標(biāo)志不影響輸出格式的參數(shù)。這個(gè)程序只用到了0-3位,16-18位,如果要在該程序的基礎(chǔ)上增加參數(shù),可以用其他沒(méi)有使用的位。

mode的第0位為1表示帶l參數(shù),mode的第1位為1表示帶i參數(shù),mode的第2位為1表示帶R參數(shù),mode的第16位為1表示帶t參數(shù),mode的第17位為1表示帶a參數(shù),mode的第18位為1表示帶r參數(shù)*/

int mode=0;

char error_message[LENGTH]={0};/*錯(cuò)誤信息*/

char path[LENGTH]={0};/*路徑*/

struct stat stalist[MAX_FILE_COUNT];/*文件詳細(xì)信息數(shù)組*/

char namelist[MAX_FILE_COUNT][LENGTH];/*文件名數(shù)組*/

int flag=0;/*判斷是否指定了目錄*/

int count;/*文件個(gè)數(shù)*/

/*解析參數(shù)*/

for(i=1;i<argc;i++){

if(argv[i][0]=='-'){/*如果是選項(xiàng)參數(shù)*/

for(j=1;j<strlen(argv[i]);j++){

if(argv[i][j]=='l')

mode=mode|LL;

else if(argv[i][j]=='i')

mode=mode|IL;

else if(argv[i][j]=='R')

mode=mode|RU;

else if(argv[i][j]=='r')

mode=mode|RL;

else if(argv[i][j]=='a')

mode=mode|AL;

else if(argv[i][j]=='t')

mode=mode|TL;

else{

snprintf(error_message,LENGTH,"no option of %c",argv[i][j]);

error(error_message,__LINE__);

}

}

}else{/*參數(shù)為目錄或文件*/

if(flag==1)

error("can not specify more then two dir or file",__LINE__);

else

flag=1;

if(argv[i][0]!='/'){/*相對(duì)路徑*/

strcat(path,get_current_dir_name());

strcat(path,"/");

strncat(path,argv[i],strlen(argv[i]));

}else/*絕對(duì)路徑*/

strcpy(path,argv[i]);

}

}

if(flag==0)/*未指定任何目錄或文件,則使用默認(rèn)當(dāng)前目錄*/

strcat(path,get_current_dir_name());

/*根據(jù)mode獲取指定目錄下的文件*/

count=file_list(path,stalist,namelist,mode);

/*根據(jù)mode顯示文件*/

display(stalist,namelist,count,mode);

return 0;

}

/*獲取文件列表*/

int file_list(char* path,struct stat* stalist,char (*namelist)[LENGTH],int mode)

{

int i=0,index,j,k;

DIR *dir;

struct dirent *ptr;

char str[LENGTH];

char nametemp[LENGTH];

struct stat statemp;

if(stat(path,&stalist[0])<0){

// puts(path);

error("the specify file is not exist",__LINE__);

}

if(S_ISDIR(stalist[0].st_mode)){/*如果該路徑指向的是一個(gè)目錄*/

if((dir=opendir(path))==NULL){/*打開(kāi)目錄*/

sprintf(str,"can not open %s",path);

error(str,__LINE__);

}

chdir(path);/*改變當(dāng)前工作目錄到path*/

i=0;

while((ptr=readdir(dir))!=NULL){/*依次讀取目錄下的所有文件*/

if(ptr->d_name[0]=='.' && !(mode & AL))/*忽略隱藏文件*/

continue;

/*按字母順序插入到stalist數(shù)組及namelist數(shù)組*/

for(j=i;j>0;j--){

if(strcmp(ptr->d_name,namelist[j-1])>0)

break;

}

for(k=i;k>j;k--){

strcpy(namelist[k],namelist[k-1]);

stalist[k]=stalist[k-1];

}

strcpy(namelist[j],ptr->d_name);

stat(ptr->d_name,&stalist[j]);

i++;

if(i==MAX_FILE_COUNT)

error("file count beyond MAX_FILE_COUNT",__LINE__);

}

closedir(dir);

}else{

j=strlen(path)-1;

while(j>=0 && path[j]!='/'){

j--;

}

if(j<0)

error("path error",__LINE__);

j++;

k=0;

while(path[j]){

namelist[0][k]=path[j];

j++;

k++;

}

namelist[0][k]=0;

return 1;

}

if(mode & TL){/*按文件修改時(shí)間排序*/

for(j=0;j<i;j++){

index=j;

for(k=j+1;k<i;k++)

if(stalist[index].st_mtime<stalist[k].st_mtime){

index=k;

}

statemp=stalist[j];

stalist[j]=stalist[index];

stalist[index]=statemp;

strcpy(nametemp,namelist[j]);

strcpy(namelist[j],namelist[index]);

strcpy(namelist[index],nametemp);

}

}

if(mode & RL){/*反序排列*/

for(j=0;j<i/2;j++){

strcpy(nametemp,namelist[j]);

strcpy(namelist[j],namelist[i-j-1]);

strcpy(namelist[i-j-1],nametemp);

statemp=stalist[j];

stalist[j]=stalist[i-j-1];

stalist[i-j-1]=statemp;

}

}

return i;

}

/*根據(jù)mode選擇不同的函數(shù)進(jìn)行顯示*/

void display(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode)

{

/*使mode的高16位為0*/

int m = mode & ((1<<16)-1);

switch(m){

case 0:

display_simply(namelist,n);

break;

case 1:

display_LL(stalist,namelist,n);

break;

case 2:

display_IL(stalist,namelist,n);

break;

case 3:

display_LL_IL(stalist,namelist,n);

break;

case 4:

display_RU(stalist,namelist,n,mode);

break;

case 5:

display_RU_LL(stalist,namelist,n,mode);

break;

case 6:

display_RU_IL(stalist,namelist,n,mode);

break;

case 7:

display_RU_IL_LL(stalist,namelist,n,mode);

break;

}

}

/*不帶顯示選項(xiàng),簡(jiǎn)單顯示文件名*/

void display_simply(char (*namelist)[LENGTH],int n)

{

int i,maxlength=0,cols;

struct winsize ts;

for(i=0;i<n;i++)

if(strlen(namelist[i])>maxlength)

maxlength=strlen(namelist[i]);

/*獲取終端的大小*/

ioctl(STDIN_FILENO,TIOCGWINSZ,&ts);

cols=ts.ws_col;

cols/=(maxlength+1);

//lines=ts.ws_row;

for(i=0;i<n;i++){

if(i!=0 && i%cols==0)

puts("");

printf("%*s",-(maxlength+1),namelist[i]);

}

putchar('n');

}

/*-l 參數(shù),顯示詳細(xì)文件信息*/

void display_LL(struct stat * stalist,char (*namelist)[LENGTH],int n)

{

int i,mode;

char* str;

for(i=0;i<n;i++){

mode=stalist[i].st_mode;

if(S_ISDIR(mode))

printf("d");

else

printf("-");

if(mode & (1<<8))

printf("r");

else

printf("-");

if(mode & (1<<7))

printf("w");

else

printf("-");

if(mode & (1<<6))

printf("x");

else

printf("-");

if(mode & (1<<5))

printf("r");

else

printf("-");

if(mode & (1<<4))

printf("w");

else

printf("-");

if(mode & (1<<3))

printf("x");

else

printf("-");

if(mode & (1<<2))

printf("r");

else

printf("-");

if(mode & (1<<1))

printf("w");

else

printf("-");

if(mode & (1<<0))

printf("x");

else

printf("-");

printf(" %-3d",stalist[i].st_nlink);

printf(" %-6s",getpwuid(stalist[i].st_uid)->pw_name);

printf(" %-6s",getgrgid(stalist[i].st_gid)->gr_name);

printf(" %-10d",stalist[i].st_size);

str=ctime(&stalist[i].st_mtime);

str[strlen(str)-2]=0;

printf(" %s",str);

printf(" %sn",namelist[i]);

}

}

/*-i 參數(shù),顯示文件名及節(jié)點(diǎn)號(hào)*/

void display_IL(struct stat* stalist,char (*namelist)[LENGTH],int n)

{

int i,maxlength=0,cols;

struct winsize ts;

for(i=0;i<n;i++)

if(strlen(namelist[i])>maxlength)

maxlength=strlen(namelist[i]);

/*獲取終端的大小*/

ioctl(STDIN_FILENO,TIOCGWINSZ,&ts);

cols=ts.ws_col;

cols/=(maxlength+9);

//lines=ts.ws_row;

for(i=0;i<n;i++){

if(i!=0 && i%cols==0)

puts("");

printf("%-8d ",stalist[i].st_ino);

printf("%*s",-(maxlength+1),namelist[i]);

}

putchar('n');

}

/*-li 參數(shù),顯示詳細(xì)文件信息和節(jié)點(diǎn)號(hào)*/

void display_LL_IL(struct stat * stalist,char (*namelist)[LENGTH],int n)

{

int i,mode;

char* str;

for(i=0;i<n;i++){

printf("%-8d ",stalist[i].st_ino);

mode=stalist[i].st_mode;

if(S_ISDIR(mode))

printf("d");

else

printf("-");

if(mode & (1<<8))

printf("r");

else

printf("-");

if(mode & (1<<7))

printf("w");

else

printf("-");

if(mode & (1<<6))

printf("x");

else

printf("-");

if(mode & (1<<5))

printf("r");

else

printf("-");

if(mode & (1<<4))

printf("w");

else

printf("-");

if(mode & (1<<3))

printf("x");

else

printf("-");

if(mode & (1<<2))

printf("r");

else

printf("-");

if(mode & (1<<1))

printf("w");

else

printf("-");

if(mode & (1<<0))

printf("x");

else

printf("-");

printf(" %-3d",stalist[i].st_nlink);

printf(" %-6s",getpwuid(stalist[i].st_uid)->pw_name);

printf(" %-6s",getgrgid(stalist[i].st_gid)->gr_name);

printf(" %-10d",stalist[i].st_size);

str=ctime(&stalist[i].st_mtime);

str[strlen(str)-2]=0;

printf(" %s",str);

printf(" %sn",namelist[i]);

}

}

/*-R 參數(shù),簡(jiǎn)單顯示所有文件,包括目錄下面的子目錄*/

void display_RU(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode)

{

int i,count;

char path[LENGTH]={0},temp[LENGTH]={0};

struct stat sta[MAX_FILE_COUNT];

char name[MAX_FILE_COUNT][LENGTH];

puts(get_current_dir_name());

display_simply(namelist,n);

putchar('n');

strcpy(path,get_current_dir_name());

for(i=0;i<n;i++){

if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)

continue;

if(S_ISDIR(stalist[i].st_mode)){

strcpy(temp,path);

strcat(path,"/");

strcat(path,namelist[i]);

count=file_list(path,sta,name,mode);

display_RU(sta,name,count,mode);

strcpy(path,temp);

}

}

}

/*-Rl 參數(shù),顯示所有文件,包括目錄下面的子目錄的詳細(xì)信息*/

void display_RU_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode)

{

int i,count;

char path[LENGTH]={0},temp[LENGTH]={0};

struct stat sta[MAX_FILE_COUNT];

char name[MAX_FILE_COUNT][LENGTH];

puts(get_current_dir_name());

display_LL(stalist,namelist,n);

putchar('n');

strcpy(path,get_current_dir_name());

for(i=0;i<n;i++){

if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)

continue;

if(S_ISDIR(stalist[i].st_mode)){

strcpy(temp,path);

strcat(path,"/");

strcat(path,namelist[i]);

count=file_list(path,sta,name,mode);

display_RU_LL(sta,name,count,mode);

strcpy(path,temp);

}

}

}

/*-Ri 參數(shù),簡(jiǎn)單顯示所有文件,包括目錄下的子目錄,及節(jié)點(diǎn)號(hào)*/

void display_RU_IL(struct stat* stalist,char (*namelist)[LENGTH],int n,int mode)

{

int i,count;

char path[LENGTH]={0},temp[LENGTH]={0};

struct stat sta[MAX_FILE_COUNT];

char name[MAX_FILE_COUNT][LENGTH];

puts(get_current_dir_name());

display_IL(stalist,namelist,n);

putchar('n');

strcpy(path,get_current_dir_name());

for(i=0;i<n;i++){

if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)

continue;

if(S_ISDIR(stalist[i].st_mode)){

strcpy(temp,path);

strcat(path,"/");

strcat(path,namelist[i]);

count=file_list(path,sta,name,mode);

display_RU_IL(sta,name,count,mode);

strcpy(path,temp);

}

}

}

/*-Ril 參數(shù),顯示所有文件,包括目錄下面的子目錄的詳細(xì)信息,及節(jié)點(diǎn)號(hào)*/

void display_RU_IL_LL(struct stat * stalist,char (*namelist)[LENGTH],int n,int mode)

{

int i,count;

char path[LENGTH]={0},temp[LENGTH]={0};

struct stat sta[MAX_FILE_COUNT];

char name[MAX_FILE_COUNT][LENGTH];

puts(get_current_dir_name());

display_LL_IL(stalist,namelist,n);

putchar('n');

strcpy(path,get_current_dir_name());

for(i=0;i<n;i++){

if(strcmp(namelist[i],".")==0 || strcmp(namelist[i],"..")==0)

continue;

if(S_ISDIR(stalist[i].st_mode)){

strcpy(temp,path);

strcat(path,"/");

strcat(path,namelist[i]);

count=file_list(path,sta,name,mode);

display_RU_IL_LL(sta,name,count,mode);

strcpy(path,temp);

}

}

}

//錯(cuò)誤處理函數(shù)

void error(char* error_message,int line)

{

char str[5];

sprintf(str,"%d:",line);

strcat(str,error_message);

perror(str);

exit(EXIT_FAILURE);

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:linux c模擬ls命令詳解
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)