C LINUX SFIS 上传下载后台命令行程式

#ifndef _DELETE_FILE
#define _DELETE_FILE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>


bool QueryProgName();
bool WriteCfig(char *FileName,char *Str);
bool ReadChkWriteInfo();
bool IsExistJobNumberInfo(char *JobNumber);
bool WriteLineStation();
char *ReadStr();
char *ReadPath(char *FileName);
bool ScanSN();
bool DownProgram(char *FolderName,bool IsCreate,char *SourcePath,char *CpyPath);


bool is_dir(const char *path);
bool is_file(const char *path);
bool is_special_dir(const char *path);
void get_file_path(const char *Path,const char *file_name,char *file_path);
void delete_file(const char *path);
void Copy(char *spathname,char *tpathname);//文件拷贝
void d_copy(char *spathname,char *tpathname);//文件夹拷贝
char *CharProcess(char *str);
bool RunTest(char *CFGFileName);
bool DeleteFolder(char *FolderName);
bool IsExistFile(char *FileName);
bool IsUpdateDataFile();
bool ShowExistResult(char *Path,char *FileName);
char *PathStrProc(char *PathFileName);
bool WriteUpdataLog(char *UpdataFileName);
char *StrProcess(char *Path,char *FileName);
void ShowWrite(char *Result,bool IsTrue);
char *FcReadStr(char *FileName,char *Str);
void help();


char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];
char DataPath[255];
bool Satisfying=false;




int main(int argc,char *argv[])
{
if(argc==2)
{
 if(strcmp(argv[1],"/QP")==0)
   {
     if(QueryProgName()==true)
   return 0;
   }
   else if(strcmp(argv[1],"/RCW")==0)
   {
   if( ReadChkWriteInfo()==true)
     return 0;
   else
   {
   if(WriteLineStation()==true)
     return 0;
   }
   }
   else if(strcmp(argv[1],"/SN")==0)
   {
   if(ScanSN()==true)
     return 0;
   }
   else if(strcmp(argv[1],"/DN")==0)
   {
     char SourcePath[255];
     char ObjectPath[255];
     strcpy(SourcePath,ReadPath("CpyPath.cfg"));  
     strcpy(ObjectPath,CharProcess(SourcePath));
     if(DownProgram("/LINUX",true,SourcePath,ObjectPath)==true)
       return 0;
   }
   else if(strcmp(argv[1],"/RN")==0)
   {
          if(RunTest("RunPath.cfg")==true)
           return 0;
   }
   else if(strcmp(argv[1],"/DL")==0)
   {
     if(DeleteFolder("/LINUX")==true)
       return 0;
   }
   else if(strcmp(argv[1],"/JB")==0)
   {
     if(IsExistFile("/LINUX/cpy.ok")==true)
      return 0;
   }
   else if(strcmp(argv[1],"/UPDATA")==0)
   {
     if(IsUpdateDataFile()==true)
     {
         FILE *fp;
         char UpdataFileStr[255];
         fp=fopen("DOWNPART.DAT","r");
         if(fp==NULL)
         {
           printf("\033[31m Not Open DOWNPART.DAT file\033[0m\n");
           return 1;
         }
         fgets(UpdataFileStr,sizeof(UpdataFileStr),fp);
         UpdataFileStr[strlen(UpdataFileStr)]='\0';
         if(WriteUpdataLog(UpdataFileStr)==true)
         {
           printf("\033[32m Test data are successfully uploaded to the server\033[0m\n");
           return 0;
         }
     }
   }
   else
    help();

}
else
   help();
return 1;
}


char *StrProcess(char *Path,char *FileName)
{
static char TextPath[255];
FILE *fp;
strcpy(TextPath,Path);
strcat(TextPath,FileName);
fp=fopen(TextPath,"r");
if(fp==0)
{
 printf("\033[31m Not Open %s file\033[0m\n",TextPath);
 Satisfying=false;
 return NULL;
}
fgets(TextPath,sizeof(TextPath),fp);
TextPath[strlen(TextPath)]='\0';
Satisfying=true;
fclose(fp);
return TextPath;
}


void ShowWrite(char *Result,bool IsTrue)
{
 if(IsTrue==true)
  printf("\033[32m %s Test Data File Write [PASS]\033[0m\n",Result);
 else if(IsTrue=false)
  printf("\033[31m %s Test data file write [FIAL]\033[0m\n",Result);
}


char *FcReadStr(char *FileName,char *Str)
{
static char Buffer[255];
char Temp[255];
FILE *fp;
fp=fopen(FileName,"r");
if(fp==NULL)
{
 printf("\033[31m Not Open %s file\033[0m\n",FileName);
 exit(1);
}
while((fgets(Temp,sizeof(Temp),fp))!=NULL)
  {
  Temp[strlen(Temp)-2]='\0';
  int n=16,e=0;
  char Sort[255];
  for(;n<strlen(Temp);n++,e++)
     Sort[e]=Temp[n];
  Sort[strlen(Sort)]='\0';
  if(strcmp(Sort,Str)==0)
  {
  Satisfying=true;
  strcpy(Buffer,Temp);
  fclose(fp);
  return Buffer;
  }
  }
  Satisfying=false;
  fclose(fp);
return "NULL";
}


bool WriteUpdataLog(char *UpdataFileName)
{
FILE *fp;
char DataLog[255],SortDataPath[255];

/*写SN测试记录*/
strcpy(DataLog,StrProcess(DataPath,"/SN.DAT"));
if(Satisfying==true)
{
   strcat(DataLog,",");
   ShowWrite("SN.DAT",true);
}
else
{
 ShowWrite("SN.DAT",false);
 return false;
}

/*写MAC测试记录*/
  strcat(DataLog,StrProcess(DataPath,"/MAC.DAT"));
  if(Satisfying==true)
{
   strcat(DataLog,",");
   ShowWrite("MAC.DAT",true);
}
else
{
 ShowWrite("MAC.DAT",false);
 return false;
}

/*写OK测试记录*/
strcat(DataLog,"OK,");

/*读取工号、线体、站位信息写入*/
strcat(DataLog,FcReadStr("/mnt/LINUX/EmployeeInfor.txt",ReadPath("JobNumber.DAT")));
if(Satisfying==true)
{
   strcat(DataLog,",");
   ShowWrite("JobNumber.DAT",true);
}
else
{
 ShowWrite("JobNumber.DAT",false);
 return false;
}
/*读取机型名称信息写入*/
strcat(DataLog,StrProcess(DataPath,"/BIOSNAME.DAT"));
  if(Satisfying==true)
{
   strcat(DataLog,",");
   ShowWrite("BIOSNAME.DAT",true);
}
else
{
 ShowWrite("BIOSNAME.DAT",false);
 return false;
}

/*读取测试时间信息写入*/
strcat(DataLog,StrProcess(DataPath,"/NOWDATE.DAT"));
  if(Satisfying==true)
{
  strcat(DataLog,"\r\n");
  ShowWrite("NOWDATE.DAT",true);
}
else
{
 ShowWrite("NOWDATE.DAT",false);
 return false;
}

/*生成路径写入测试数据*/
strcpy(SortDataPath,"/mnt/LOG/XPLOG/");
strcat(SortDataPath,ReadPath("DOWNPART.DAT"));
strcat(SortDataPath,".TXT");
fp=fopen(SortDataPath,"a");
if(fp==NULL)
{
 printf("\033[31m Not Open %s file\033[0m\n",SortDataPath);
 return false;
}
fputs(DataLog,fp);
fclose(fp);
return true;
}


char *PathStrProc(char *PathFileName)
{
 static char TextStr[255];
 char Buffer[255];
 FILE *fp;
 fp=fopen(PathFileName,"r");
 if(fp==NULL)
 {
     printf("\033[31m Not Open %s file\033[0m\n",PathFileName);
   exit(1);
 
 }
 int s=1,c=0;
 fgets(Buffer,sizeof(Buffer),fp);
 Buffer[strlen(Buffer)]='\0';
 while(Buffer[s]!='/')s++;
 for(;s<strlen(Buffer);s++,c++)
     TextStr[c]=Buffer[s];
 TextStr[strlen(TextStr)]='\0';
 fclose(fp);
 return TextStr;
}


bool ShowExistResult(char *Path,char *FileName)
{
char Temp[255];
strcpy(Temp,Path);
strcat(Temp,FileName);
if(IsExistFile(Temp)!=true)
{
 printf("\033[31m Not Exist %s Data file\033[0m\n",Temp);
 return false;
}
return true;
}


bool IsUpdateDataFile()
{
char Path[255];
char DownPart[255];
char JobNumber[255];
strcpy(Path,PathStrProc("CpyPath.cfg"));
strcat(Path,"/TESTINFO");
strcpy(DataPath,Path);

if((ShowExistResult(Path,"/SN.DAT")==false)||
(ShowExistResult(Path,"/MAC.DAT")==false)||
(ShowExistResult(Path,"/JobNumber.DAT")==false)||
(ShowExistResult(Path,"/BIOSNAME.DAT")==false)||
(ShowExistResult(Path,"/NOWDATE.DAT")==false)) 
return false;

/*copy DOWNPART.DAT文件到/LINUX/Mode/TESTINFO */
strcpy(DownPart,"cp DOWNPART.DAT ");
strcat(DownPart,Path);
strcat(DownPart,"/DOWNPART.DAT");
system(DownPart);

/*copy JobNumber.DAT文件到/LINUX/Mode/TESTINFO*/
strcpy(JobNumber,"cp JobNumber.DAT ");
strcat(JobNumber,Path);
strcat(JobNumber,"/JobNumber.DAT");
system(JobNumber);
return true;
}


bool IsExistFile(char *FileName)
{
 if((access("/LINUX/cpy.ok",F_OK))!=-1)
   return true;
else
 return false;
}


void help()
{
system("clear");
 printf("=========================================================================================================\n");
 printf("LINUX Down Program Tool -Ferex Incorporation ,By ShenBo,2017-07-20\n");
 printf("Usage   :DwonTest[Parameter]\n");
 printf("[Command]:\n");
 printf("            \033[37;44m DownTest /QP  ------->The program name entry and check it;                 \033[0m\n");
 printf("            \033[37;44m DownTest /RCW ------->Check the work number input and employee information;\033[0m\n");
 printf("            \033[37;44m DownTest /SN  ------->SN barcode scanning and input, generate;             \033[0m\n");
 printf("            \033[37;44m DownTest /DN  ------->Down Test Program;                                   \033[0m\n");
 printf("            \033[37;44m DownTest /RN  ------->Run Test Program;                                    \033[0m\n");
 printf("            \033[37;44m DownTest /DL  ------->Delete Program Folder;                               \033[0m\n");
 printf("            \033[37;44m DownTest /JB  ------->To determine whether a file exists                   \033[0m\n");
 printf("            \033[37;44m DownTest /UPDATA ---->Upload the test records to the server                \033[0m\n");
 /*printf("\n");*/
 printf("[Example]:\n");
 printf("           DownTest /QP\n");
 printf("=========================================================================================================\n");
 printf("\n");
}


bool DeleteFolder(char *FolderName)
{
    int Del;
 delete_file(FolderName);
 Del=remove(FolderName);
 if(Del!=0)
 {
   printf("\033[31m Not Delete %s Folder\033[0m\n",FolderName);
   return false;
 }
 return true;
}


bool RunTest(char *CFGFileName)
{
FILE *fp;
fp=fopen(CFGFileName,"r");
char RunStr[255];
if(fp==NULL)
{
printf("\033[31m Not Open %s CFG File\033[0m \n",CFGFileName);
return false;
}
fgets(RunStr,sizeof(RunStr),fp);
RunStr[strlen(RunStr)]='\0';
fclose(fp);
system(RunStr);
return true;
}


char *CharProcess(char*str)
{
int s=1,e=0;
static char ReadStr[255];
while(str[s]!='/') s++;
for(;s<strlen(str);s++,e++)
   ReadStr[e]=str[s];
ReadStr[e]='\0';
return ReadStr;
}


bool QueryProgName()
{
bool IsFind=false;
char InputStr[255];
static char ReadStr[255];
char Temp[255];
char TempStr[255];
char Buffer[255];
while(IsFind==false)
{
system("clear");
 FILE *fp;
 printf("                                                       Build DATE:2017-007-17\n");
 printf("FEREX                                             EnvirSet   Copyright by SHENBO SW1.00\n");
 printf("================================================================================================================================================\n");
 printf("\n");
 printf("\033[37;44m Please Input Part Number\033[0m");
 fgets(InputStr,255,stdin);
 InputStr[strlen(InputStr)-1]='\0';
 WriteCfig("DOWNPART.DAT",InputStr);
 int i=0;
 for(;i<strlen(InputStr);i++)
     InputStr[i]=toupper(InputStr[i]);
 fp=fopen("/mnt/LINUX/FEREX.TXT","r");
 if(fp==NULL)
 {
 printf("\033[31m Not Open LINUX/FEREX.TXT File\033[0m ");
 return 1;
 }
 while(fgets(Temp,255,fp)!=NULL)
 {
 int n=0;
 for(n=0;n<strlen(InputStr);n++)
     Buffer[n]=toupper(Temp[n]);
 Buffer[n]='\0';
 if((strcmp(Buffer,InputStr))==0)
 {
   int s=0,e=0,i=0;
   char Option;
   char ModeStr[255];
   while(Temp[s]!='|') s++;
   for(e=s+1;Temp[e]!='|';e++,i++)
     ModeStr[i]=Temp[e];    
   ModeStr[i]='\0';
   printf("%s [Y,N] ?",ModeStr);
   scanf("%c",&Option);
   Option=toupper(Option);
   if(Option=='Y')
   {
     int st=(s+1)+strlen(ModeStr)+1;
     int sn=0;
     char ReadPath[255];
     char CpyPath[255];
     for(;Temp[st]!='#';st++,sn++)
         ReadPath[sn]=Temp[st];
     ReadPath[sn-1]='\0';
     /*strcpy(Store,ReadPath);*/
     st=st+1;
     for(sn=0;st<strlen(Temp);st++,sn++)
         CpyPath[sn]=Temp[st];
     CpyPath[sn]='\0';
     /*printf("%s",PathStr);*/
     fclose(fp);
     if((WriteCfig("RunPath.cfg",ReadPath)==true)&&(WriteCfig("CpyPath.cfg",CpyPath)==true))
     {
       return true;
     }
     else
         return false;
   }
   else
   {
   fclose(fp);
   return false;
   }
     
 }
 }
   fclose(fp);
}

return false;
}


char *ReadPath(char *FileName)
{
static char Temp[255];
FILE *fp;
fp=fopen(FileName,"r");
if(fp==NULL)
{
 printf("\033[31m Not Open %s File\033[0m\n",FileName);
 return NULL;
}
fgets(Temp,sizeof(Temp),fp);
Temp[strlen(Temp)]='\0';
fclose(fp);
return Temp;
}


bool WriteCfig(char *FileName,char *Str)
{
FILE *fp;
fp=fopen(FileName,"w+");
if(fp==NULL)
{
printf("\033[31m Not Open %s File\033[0m\n",FileName);
return false;
}
fputs(Str,fp);
fclose(fp);
return true;
}


bool ReadChkWriteInfo()
{
char JobNumber[255];
while(strlen(JobNumber)!=6)
{
 system("clear");
 printf("                                                       Build DATE:2017-007-18\n");
   printf("FEREX                                             EnvirSet   Copyright by SHENBO SW1.00\n");
   printf("================================================================================================================================================\n");
   printf("\n");
 printf("Please Keyin or Scan your Operator ID:");
   fgets(JobNumber,sizeof(JobNumber),stdin);
   JobNumber[strlen(JobNumber)-1]='\0';  
}
if(WriteCfig("JobNumber.DAT",JobNumber)==true)
{
 if(IsExistJobNumberInfo(JobNumber)==true)
     return true;
}
return false;
}


bool WriteLineStation()
{
char Line[255];
char Buffer[255];
FILE *fp;
while(strlen(Line)!=11)
{
 system("clear");
 printf("                                                       Build DATE:2017-007-19\n");
   printf("FEREX                                             EnvirSet   Copyright by SHENBO SW1.00\n");
   printf("================================================================================================================================================\n");
   printf("\n");
   printf("Please use Barcode reader to scan Station:");
   fgets(Line,sizeof(Line),stdin);
   Line[strlen(Line)-1]='\0';
}
int n=0;
for(n=0;n<strlen(Line);n++)
  Line[n]=toupper(Line[n]);
fp=fopen("/mnt/LINUX/EmployeeInfor.txt","a");
if(fp==NULL)
{
printf("\033[31m Not Open \"/mnt/LINUX/EmployeeInfor.txt\" File\033[0m");
return false;
}
strcpy(Buffer,Line);
strcat(Buffer,",021,");
strcat(Buffer,ReadStr());
strcat(Buffer,"\r\n");
fputs(Buffer,fp);
fclose(fp);
return true;
}


char *ReadStr()
{
FILE *fp;
static char Temp[255];
fp=fopen("JobNumber.DAT","r");
if(fp==NULL)
{
printf("\033[31m Not Open JobNumber.Dat File\033[0m");
exit (1);
}
fgets(Temp,sizeof(Temp),fp);
Temp[strlen(Temp)]='\0';
fclose(fp);
return Temp;
}


bool IsExistJobNumberInfo(char *JobNumber)
{
FILE *fp;
char Temp[255];
memset(Temp,0,sizeof(Temp)/sizeof(char));
fp=fopen("/mnt/LINUX/EmployeeInfor.txt","r");
if(fp==NULL)
{
printf("\033[31m Not Open \"/mnt/LINUX/EmployeeInfor.txt\" File\033[0m");
return false;
}
while(fgets(Temp,255,fp)!=NULL)
{
char JobNumberStr[255];
char Linebody[225];
Temp[strlen(Temp)-1]='\0';
int n=strlen(Temp)-7,i=0;
for(;n<strlen(Temp);n++,i++)
   JobNumberStr[i]=Temp[n];
JobNumberStr[i-1]='\0';
if(strcmp(JobNumberStr,JobNumber)==0)
{
   fclose(fp);
   return true;
}
}
fclose(fp);
return false;
}


bool ScanSN()
{
FILE *fp;
fp=fopen("SN.DAT","w+");
if(fp==NULL)
{
printf("\033[31m Not Open SN.DAT File\033[0m");
return false;
}
char Buffer[255];
memset(Buffer,0,sizeof(Buffer)/sizeof(char));
while(strlen(Buffer)<12)
{
system("clear");
printf("                                                       Build DATE:2017-007-19\n");
 printf("FEREX                                             EnvirSet   Copyright by SHENBO SW1.00\n");
 printf("================================================================================================================================================\n");
 printf("\n");
 printf("Please scan SN barcode:");
 fgets(Buffer,sizeof(Buffer),stdin);
 Buffer[strlen(Buffer)-1]='\0';
}

fputs(Buffer,fp);
fclose(fp);
return true;
}


/*判断是否为目录*/
bool is_dir(const char *path)
{
struct stat statbuf;
if(lstat(path,&statbuf)==0)/*lstat返回文件的信息,文件信息存放在stat结构中*/
{
return S_ISDIR(statbuf.st_mode)!=0;/*S_ISDIRP宏,判断文件类型是否为目录*/
}
return false;
}


/*判断是否为常规文件*/
bool is_file(const char *path)
{
struct stat statbuf;
if(lstat(path,&statbuf)==0)
return S_ISREG(statbuf.st_mode)!=0;/*判断文件是否为常规文件*/
return false;
}


/*判断是否是特殊目录*/
bool is_special_dir(const char *path)
{
return strcmp(path,".")==0||strcmp(path,"..")==0;
}


/*生成完整的文件路径*/
void get_file_path(const char *path,const char *file_name,char *file_path)
{
strcpy(file_path,path);
if(file_path[strlen(path)-1]!='/')
strcat(file_path,"/");
strcat(file_path,file_name);
}


void delete_file(const char *path)
{
DIR *dir;
struct dirent *dir_info;
char file_path[PATH_MAX];
if(is_file(path))
{
remove(path);
return;
}
if(is_dir(path))
{
if((dir=opendir(path))==NULL)
return;
while((dir_info=readdir(dir))!=NULL)
{
get_file_path(path,dir_info->d_name,file_path);
if(is_special_dir(dir_info->d_name))
continue;
delete_file(file_path);
rmdir(file_path);
}
}
}


bool DownProgram(char *FolderName,bool IsCreate,char *SourcePath,char *CpyPath)
{
int Del,status;
if(opendir("/LINUX")!=NULL)
{
 delete_file(FolderName);
   Del=remove(FolderName);
   if(Del!=0)
   {
     printf("\033[31m Not Delete %s Folder\033[0m\n",FolderName);
     return false;
   }
}

if(IsCreate==true)
{
status=mkdir("/LINUX",S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
if(Del!=0)
 {
printf("\033[31m Not Create %s Folder\033[0m\n",FolderName);
return false;
 }
}
struct dirent *sp,*tp;
  char spath[1000],tpath[1000],temp_spath[1000],temp_tpath[1000];
  struct stat sbuf,tbuf,temp_sbuf,temp_tbuf;
  char tdirect[1000],judge;
  DIR *dir_s,*dir_t;
  dir_s=opendir(SourcePath);
  if(dir_s==NULL)
  {
      printf("\033[31m This directory don't exist !\033[0m\n");
      return 0;
  }
  if(stat(SourcePath,&sbuf)!=0)
  {
      printf("\033[31m Get status error !\033[0m\n");
      return 0;
  }
  dir_t=opendir(CpyPath);
  if(dir_t==NULL)
  {
     mkdir(CpyPath,sbuf.st_mode);
     chown(CpyPath,sbuf.st_uid,sbuf.st_gid);
     dir_t=opendir(CpyPath);
  }
  else
  {
     chmod(CpyPath,sbuf.st_mode);
     chown(CpyPath,sbuf.st_uid,sbuf.st_gid);
  }   
  strcpy(spath,SourcePath);
  strcpy(tpath,CpyPath);
  strcpy(temp_spath,SourcePath);
  strcpy(temp_tpath,CpyPath);
  printf("\033[32m Begin copy ........\033[0m\n");
  while((sp=readdir(dir_s))!=NULL)
  {
     if(strcmp(sp->d_name,".")!=0&&strcmp(sp->d_name,"..")!=0)
     {
         strcat(temp_spath,"/");
         strcat(temp_spath,sp->d_name);
         strcat(temp_tpath,"/");
         strcat(temp_tpath,sp->d_name);
         lstat(temp_spath,&sbuf);
         temp_sbuf.st_mode=sbuf.st_mode;
         if(S_ISLNK(temp_sbuf.st_mode))
         {
             printf("\033[32m %s is a symbolic link file\033[0m\n",temp_spath);
         }
         else if((S_IFMT&temp_sbuf.st_mode)==S_IFREG)
         {
             printf("\033[32m Copy file %s ......\033[0m\n",temp_spath);
             Copy(temp_spath,temp_tpath);
             strcpy(temp_tpath,tpath);
             strcpy(temp_spath,spath);
         }
         else if((S_IFMT&temp_sbuf.st_mode)==S_IFDIR)
         {
             printf("\033[32m Copy directory %s ......\033[0m\n",temp_spath);
             d_copy(temp_spath,temp_tpath);
             strcpy(temp_tpath,tpath);
             strcpy(temp_spath,spath);
         }
     }
  }
    int DataPath;
    char TESTDATA[255];
    strcpy(TESTDATA,CpyPath);
    strcat(TESTDATA,"/TESTINFO");
    DataPath=mkdir(TESTDATA,S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
if(DataPath!=0)
 {
printf("\033[31m Not Create %s Folder\033[0m\n",TESTDATA);
return false;
 }
 system("echo .>/LINUX/cpy.ok");
   printf("\033[32m Copy Finish!!\033[0m\n");
   closedir(dir_t);
   closedir(dir_s);
return true;
}


void Copy(char *spathname,char *tpathname)
{
   int sfd,tfd;
   struct stat s,t;
   char c;
   sfd=open(spathname,O_RDONLY);
   tfd=open(tpathname,O_RDWR|O_CREAT);
   while(read(sfd,&c,1)>0)
        write(tfd,&c,1);
   fstat(sfd,&s);
   chown(tpathname,s.st_uid,s.st_gid);
   chmod(tpathname,s.st_mode);
   
   close(sfd);
   close(tfd);
}


void d_copy(char *spathname,char *tpathname)
{
   struct stat s,t,temp_s,temp_t;
   struct dirent *s_p;
   DIR *dirs,*dirt;
   
   stat(spathname,&s);
   mkdir(tpathname,s.st_mode);
   chown(tpathname,s.st_uid,s.st_gid);
   dirt=opendir(tpathname);
   dirs=opendir(spathname);
   strcpy(temp_paths,spathname);
   strcpy(temp_patht,tpathname);
   while((s_p=readdir(dirs))!=NULL)
   {
      if(strcmp(s_p->d_name,".")!=0&&strcmp(s_p->d_name,"..")!=0)
      {
          strcat(temp_paths,"/");
          strcat(temp_paths,s_p->d_name);
          strcat(temp_patht,"/");
          strcat(temp_patht,s_p->d_name);
          lstat(temp_paths,&s);
          temp_s.st_mode=s.st_mode;
          if(S_ISLNK(temp_s.st_mode))
          {
              printf("%s is a symbol link file\n",temp_paths);
          }
          else if(S_ISREG(temp_s.st_mode))
          {
              printf("Copy file %s ......\n",temp_paths);
              Copy(temp_paths,temp_patht);
              strcpy(temp_paths,spathname);
              strcpy(temp_patht,tpathname);
          }
          else if(S_ISDIR(temp_s.st_mode))
          {
              printf("Copy directory %s ......\n",temp_paths);
              d_copy(temp_paths,temp_patht);
              strcpy(temp_paths,spathname);
              strcpy(temp_patht,tpathname);
          }
      }
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值