⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ls02.c

📁 在linux下实现ls -R功能
💻 C
字号:
/* lsR.c 
 * purpose list contents of directory recurtions
  */

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<malloc.h>
#include<string.h>
#define LEN sizeof(struct sub_dir)


struct sub_dir       
{  
  char sub_name[30];
  
  struct sub_dir *next;       
  };
void do_ls(char[]);
void do_lsR(char[]);
void do_R(struct sub_dir *);

struct sub_dir *head;
struct sub_dir *last;
struct sub_dir *now;
int main(int ac,char *av[])
{
   if(ac==1)
     do_ls(".");
   else
   { if(strcmp(av[1],'-R')==0)
     {  if(ac==2) 
          do_lsR(".");
         else 
          while(ac>1)
          {
          	--ac;
          	printf("%s:\n",*++av);
          	do_lsR(*av);
          	}
      }
     else while(--ac)
     	{
     		 printf("%s:\n",*++av);
     		 do_ls(*av);
     		}  
    }
}
    
    
void do_ls(char dirname[])
/*
 *list files in directory called dirname
 */
{     
  DIR *dir_ptr;  /*the directory*/
  struct dirent *direnttp;
  if((dir_ptr=opendir(dirname))==NULL)
       fprintf(stderr,"ls:cannot open %s\n",dirname);
   else 
    {
        while((direntp=readdir(dir_ptr))!=NULL)
             printf("%s\n",direntp->d_name);
         closedir(dir_ptr);
    }
 }
 
 
 
 void do_lsR(char dirname[])
{
     struct sub_dir dir_buffer;
     struct sub_dir *dir_head;
     
 
     dir_head=&dir_buffer;

     head=&dir_buffer;
     strcpy(dir_head->d_name,dirname);
     dir_head->next=NULL;
     last=head;
     do_R(&dir_buffer);
     

     while(1)
     {
          if(head->next==NULL) break;
          head=head->next;
          printf("%s \n",head->d_name);
          do_R(head);
     }
}


void do_R(struct sub_dir *dir_sh)
{
	DIR *dir_ptr;
	struct dirent *direntp;
	struct stat info;
	}
 
 

 
 
 
  
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -