www.pudn.com > Ftpwork > File.h


#include 
#include 
#include 
#include 			/* declare the 'stat' structure	*/
#include 
#include 			/* Directory information.	*/
#include 
#include 
#include 

#include 

#define MAX_NAMEAT 100
#define FILE_EXIST 0
#define NOT_EXIST -1
#define DIR_EXIST 1


struct MyFile
{
   char errer;
   int Number;
};


int DirExist(char *filename)
{
        struct stat stat_p;
        if ( -1 ==  stat (filename, &stat_p))
        {
          return NOT_EXIST;
        }
        else
        {
          if(S_ISDIR(stat_p.st_mode)==TRUE){
             return DIR_EXIST;
           }
          else {
            return FILE_EXIST;
          }
        }
}


MyFile GetList(const char *Entry,char*Buffer )
{
    MyFile file;
    DIR           *dir_p;
    struct dirent *dir_entry_p;
    struct stat stat_p;

				/* Open the current directory		*/
    dir_p = opendir(Entry);

				/* read each entry until NULL.		*/
		char FileAtribute[ MAX_NAMEAT];


    while( NULL != (dir_entry_p = readdir(dir_p)))
    {
				char filename[MAX_NAMEAT];
				
        sprintf( filename, "%s", "" );
 				strcat( filename ,Entry );
				strcat( filename,dir_entry_p->d_name );
				
				if(StringCompare(dir_entry_p->d_name,".")==FALSE
				       &&StringCompare(dir_entry_p->d_name,"..")==FALSE)
				{
				 if ( -1 ==  stat (filename, &stat_p))
         {
          printf(" Error occoured attempting to stat %s\n", filename);
         }
         else
         {
           if(S_ISDIR(stat_p.st_mode)==TRUE){
            sprintf(FileAtribute,"<\033[01;34m%s\033[01;0m>", dir_entry_p->d_name);
            strcat( Buffer, FileAtribute );
           }
           else {
            char tem[MAX_NAMEAT];
   		  		sprintf(FileAtribute,"<\033[01;32m%s  ", dir_entry_p->d_name);
            sprintf(tem,"%d\033[01;0m>", stat_p.st_size);
            strcat( Buffer, FileAtribute );
            strcat( Buffer, tem );

          }
         }
        }
        else{
  		  		sprintf(FileAtribute,"[<%s>]", dir_entry_p->d_name);
            strcat( Buffer, FileAtribute );
        }
				
    }
				
				/* Tidy up.				*/
    closedir(dir_p);

    return file;
}

long GetLen(const char *Entry)
{
    MyFile file;
    DIR           *dir_p;
    long len=0;
    struct dirent *dir_entry_p;
    char FileAtribute[ MAX_NAMEAT];
    struct stat stat_p;

				/* Open the current directory		*/
    dir_p = opendir(Entry);

				/* read each entry until NULL.		*/

    while( NULL != (dir_entry_p = readdir(dir_p)))
    {
				char filename[MAX_NAMEAT];
				
        sprintf( filename, "%s", "" );
 				strcat( filename ,Entry );
				strcat( filename,dir_entry_p->d_name );
				
				if(StringCompare(dir_entry_p->d_name,".")==FALSE
				       &&StringCompare(dir_entry_p->d_name,"..")==FALSE)
				{
				 if ( -1 ==  stat (filename, &stat_p))
         {
          printf(" Error occoured attempting to stat %s\n", filename);
         }
         else
         {
           if(S_ISDIR(stat_p.st_mode)==TRUE){
   		  		sprintf(FileAtribute,"<\033[01;34m%s\033[01;0m>", dir_entry_p->d_name);
            len+=strlen(FileAtribute);
           }
           else {
            char tem[MAX_NAMEAT];
   		  		//sprintf(FileAtribute,"[%s(", dir_entry_p->d_name);
            //sprintf(tem,"%d)]", stat_p.st_size);
   		  		sprintf(FileAtribute,"<\033[01;32m%s  ", dir_entry_p->d_name);
            sprintf(tem,"%d\033[01;0m>", stat_p.st_size);
            len+=strlen(FileAtribute);
            len+=strlen(tem);

          }
         }
        }
        else{
  		  		sprintf(FileAtribute,"[<%s>]", dir_entry_p->d_name);
            len+=strlen(FileAtribute);
        }
				
    }
    len+=1;

				/* Tidy up.				*/
    closedir(dir_p);

    return len;
}