LINUX (ubuntu) C 显卡测试框架

#ifndef _DELETE_FILE
#define FALSE 0
#define TRUE 1
#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>


typedef struct cfginfo
{
    char DownCode[9];
    char ProductName[255];
    char dangrading[255];
    char WinBootPath[255];
    char WcpyPath[255];
    char DownProgramPath[255];
}CFGIF;


typedef struct Inputinfo
{
    char LineBody[255];
    char JobNumber[255];
}INPIF;


typedef struct ReadBiosInfo       /*读取BIOS信息存储*/
{
    char ProductName[255];         /*Bios 名称*/
    char BiosConfigFile[255];      /*Bios 配置文件*/
    char Bios_P_N[255];            /*Bios P_N*/
    char BiosVersion[255];         /*Bios Version*/
    char BiosDate[255];            /*Bios Date*/
}RDBSIF;


char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];
char DataPath[255];


void Title();
void Help();
char *ReadCfgStr(char *FccStr);
bool FcJobNumber(char *FcStr,char *PathName);
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);
bool DownProgram(char *FolderName,bool IsCreate,char *SourcePath,char *CpyPath);
bool CreateData(char *LineBodyStr,char *JobNumberStr,char *dangradingStr,char *BootPathStr);
bool WriteText(char *PathName,char *str);                                        
bool InputCode(CFGIF *Storecf);
bool RunJobNumber(INPIF *Storeif);            /*Check 工号并存储*/
bool RunStation(INPIF *Storeif);                          /*Check 线体并存储*/
bool mkdirs(char *muldir);
bool ScanSn(char *SavePathName,int len);
bool ReadWriteBiosInfo(char *ReadFileName,char *WritePathName);


int main(int argc,char *argv[])
{
    if(argc>1)
    {
        Title();
        if((strcmp(argv[1],"/ID")==0)&&(argc==2))       /*录入员工信息和Down测试程式*/
        {
            CFGIF *str;
            str=(CFGIF *)malloc(sizeof(CFGIF));
            INPIF *str1;
            str1=(INPIF *)malloc(sizeof(INPIF));
            char dpath[255];
            char wpath[255];
            bool IsInputCode=false;
            bool IsRunJobNumber,IsRunStation,IsDownDiag,IsDownWin;
            IsInputCode=InputCode(str);
            if(IsInputCode)
            {    
                memset(dpath,0,sizeof(dpath));
                memset(wpath,0,sizeof(wpath));
                IsRunJobNumber=false;
                IsRunStation=false;
                IsDownDiag=false;
                IsDownWin=false;
                IsRunJobNumber=RunJobNumber(str1);
                IsRunStation=RunStation(str1);             
                strcpy(dpath,str->DownProgramPath);
                strcpy(wpath,str->DownProgramPath);
                strcat(dpath,"/DIAG");
                strcat(wpath,"/WIN");
                dpath[strlen(dpath)]='\0';
                wpath[strlen(wpath)]='\0'; 
                IsDownDiag=DownProgram("/root/test",true,dpath,"/root/test");
                IsDownWin=DownProgram("/WIN/MBAT",true,wpath,str->WcpyPath);
                if((IsDownDiag)&&(IsDownWin))
                {
                    if(CreateData(str1->LineBody,str1->JobNumber,str->dangrading,str->WinBootPath)==true)return 0;
                }
            }
            free(str);
            free(str1);
            return 0;
        }
        else if((strcmp(argv[1],"/UP")==0)&&(argc==2))  /*上传测试数据*/
        {
        
        }
        else if((strcmp(argv[1],"/RB")==0)&&(argc==4))  /*读取BIOS信息并生成文档*/
        {
            char Read_FileName[255];
            char Write_FilePathName[255];
            memset(Read_FileName,0,sizeof(Read_FileName));
            memset(Write_FilePathName,0,sizeof(Write_FilePathName));
            bool IsReadWriteBiIf=false;
            strcpy(Read_FileName,argv[2]);
            strcpy(Write_FilePathName,argv[3]);
            IsReadWriteBiIf=ReadWriteBiosInfo(Read_FileName,Write_FilePathName);
            if(IsReadWriteBiIf)
            {
                printf("\033[32m Bios Information Read/&Write Succeed\033[0m\n");
                return 0;
            }
            else
            {
                printf("\033[31m Bios Infomation Read/&Write Failed\033[0m\n");
                return 1;
            }
        }
        else if((strcmp(argv[1],"/SN")==0)&&(argc==4))  /*读取SN条码*/
        {
            int len=0;
            len=atoi(argv[2]);
            bool IsScanSn=false;
            char SavePath[255];
            memset(SavePath,0,sizeof(SavePath));
            strcpy(SavePath,argv[3]);
            /*IsScanSn=ScanSn("/WIN/RECORD/SN.DAT",len);*/
            IsScanSn=ScanSn(SavePath,len);
            if(IsScanSn)
            {
                printf("\033[32m SN Scan Input/&Write Succeed\033[0m\n");
                return 0;
            }
            else
            {
                printf("\033[31m SN Scan Input/&Writel Failed\033[0m\n");
                return 1;
            }
        }
    }
    Help();
    return 1;
}


/*程式标头函数*/
void Title()
{
    printf("\033c");
    printf("                                       Build Date:2018-04-03                                                                      \n");
    printf("                             EnvirSet   Copyright by SHENBO TDown Version:1.00                                                    \n");
    printf("================================================================================================================================\n");
    printf("\n");
}




/*帮助信息*/
void Help()                    
{
    printf("\033c");
    printf("================================================================================================================================\n");
    printf("LINUX Ubuntu TDown Version:1.00 Program Tool --Ferex Incorporation,By Shenbo,2018-04-03\n");
    printf("Usage          :TDown[Parameter]\n");
    printf("[Command]:\n");
    printf("                  \033[37;44m TDown  /ID                    Down Program Information Input And Down Test Program; \033[0m\n");
    printf("                  \033[37;44m TDown  /SN 13 SavePath        Sn barcode scanning and input,generate;               \033[0m\n");
    printf("                  \033[37;44m TDown  /RB Bios.txt SavePath  Read Bios Information Create log;                     \033[0m\n");
    printf("                  \033[37;44m TDown  /UP Config.txt         Upload the Test Records to the Server                 \033[0m\n");
    printf("[Example]:\n");
    printf("           TDown /ID\n");
    printf("           TDown /SN 13 \"/WIN/RECORD/SN.DAT\"");
    printf("           TDown /RB BsIf.log \"/WIN/RECORD\"");
    printf("================================================================================================================================\n");
    printf("\n");
}


bool ReadWriteBiosInfo(char *ReadFileName,char *WritePathName)
{
    char ReadLine[255],Temp_Str[255],PathName[255];
    int na=0;
    FILE *fp;
    char DataFileName[5][255]={"Product_Name.DAT","Bios_Config_File.DAT","Bios_PN.DAT","Bios_Version.DAT","Bios_Date.DAT"};
    Title();
    fp=fopen(ReadFileName,"r");
    if(fp==NULL)
    {
        printf("\033[31m Not open %s File Read Bios Information!!\033[0m\n",ReadFileName);
        return false;
    }
    while((fgets(ReadLine,sizeof(ReadLine),fp))!=NULL)
    {
        int stlen=0,n=0;
        memset(Temp_Str,0,sizeof(Temp_Str));
        memset(PathName,0,sizeof(PathName));
        strcpy(PathName,WritePathName);
        strcat(PathName,"/");
        strcat(PathName,DataFileName[na]);
        while(ReadLine[stlen]!=':')stlen++;
        stlen=stlen+3;
        for(;stlen<strlen(ReadLine);stlen++,n++)
        {
            Temp_Str[n]=ReadLine[stlen];
        }
        Temp_Str[n]='\0';      
        bool IsWT=false;
        IsWT=WriteText(PathName,Temp_Str);
        if(!IsWT)return false;
        memset(ReadLine,0,sizeof(ReadLine));
        na++;
    }
    fclose(fp);
    return true;
}


bool ScanSn(char *SavePathName,int len)
{
    char SnBuffer[255];
    FILE *fp;
    memset(SnBuffer,0,sizeof(SnBuffer));
    Title();
    printf("\033[32m Please Scan The %d Sn bar codes:\033[0m",len);
    scanf("%s",SnBuffer);
    if(strlen(SnBuffer)!=len)
    {
        printf("\033[31m Scanning Sn bar codes Length is not equal to %d!!\033",len);
        return false;
    }
    fp=fopen(SavePathName,"w");
    if(fp==NULL)
    {
        printf("\033[31m Not Open %s Write Sn bar codes.\033[0m\n",SavePathName);
        return false;
    }
    fputs(SnBuffer,fp);
    fclose(fp);
    return true;
}
 
/*生成Down数据文档*/
bool CreateData(char *LineBodyStr,char *JobNumberStr,char *dangradingStr,char *BootPathStr)
{
    if(strcmp(dangradingStr,"021")==0)
    {
       int Del,status;    /*删除目录信息*/
    if(opendir("/WIN/RECORD")!=NULL)
    {
     delete_file("/WIN/RECORD");
       Del=remove("/WIN/RECORD");
       if(Del!=0)
       {
         printf("\033[31m Not Delete /WIN/RECORD Folder\033[0m\n");
         return false;
       }
    }
    status=mkdir("/WIN/RECORD",S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
    if((WriteText("/WIN/RECORD/LineBody.DAT",LineBodyStr)==true)&&
       (WriteText("/WIN/RECORD/JobNumber.DAT",JobNumberStr)==true)&&
       (WriteText("/WIN/RECORD/dangrading.DAT",dangradingStr)==true)&&
       (WriteText("/WIN/RECORD/BootPath.CFG",BootPathStr)==true))
       return true;
    }
    else
    {
        if((WriteText("/root/LineBody.DAT",LineBodyStr)==true)&&
       (WriteText("/root/JobNumber.DAT",JobNumberStr)==true)&&
       (WriteText("/root/dangrading.DAT",dangradingStr)==true)&&
       (WriteText("/root/BootPath.CFG",BootPathStr)==true))
       return true;
    }
    return false;
}


/*写数据*/ 
bool WriteText(char *PathName,char *str)
{
    FILE *fp;
    fp=fopen(PathName,"w");
    if(fp==NULL)
    {
        printf("\033[31m Not Open %s File\033[0m ",PathName);
        return false;
    }
    fputs(str,fp);
    fclose(fp);
    return true;
}


/*查找字符串并比较*/
char *ReadCfgStr(char *FccStr)
{
    static char Buffer[255];
    FILE *fp;
    fp=fopen("/mnt/PUBLIC/FEREX.INI","r");
    if(fp==NULL)
    {
         printf("\033[31m Not Open /mnt/PUBLIC/FEREX.INI File\033[0m ");
         exit (1);
    }
    while((fgets(Buffer,sizeof(Buffer),fp))!=NULL)
    {
        if(Buffer[0]!=':')
        {
            char str[255];
            int n=0;
            memset(str,0,sizeof(str));
            while(Buffer[n]!='|')
            {
                str[n]=toupper(Buffer[n]);
                n++;
            }
            str[n]='\0'; 
            if(strcmp(str,FccStr)==0)
            {
                fclose(fp);
                return Buffer;
            }
        }
    }
    fclose(fp);
    return "123";
}


/*输入工号和服务器配份的工号比较*/
bool FcJobNumber(char *FcStr,char *PathName)
{
    FILE *fp;
    char Buffer[255];
    memset(Buffer,0,sizeof(Buffer)); 
    fp=fopen(PathName,"r");
    if(fp==NULL)
    {
        printf("\033[31m Not Open %s File\033[0m ",PathName);
exit (1);
    }
    while(fgets(Buffer,sizeof(Buffer),fp)!=NULL)
    {
        if(Buffer[0]!='#')
        {
            int n=0,i=0,x=0,len=0;
            while(Buffer[n]!='\0')
            {
                if(Buffer[n]==';')len++;
                n++;
            }
            n=0;
            for(;x<len;x++)
            {
                char Temp[255];
                memset(Temp,0,sizeof(Temp));
                while(Buffer[n]!=';')
                {
                    Temp[i]=toupper(Buffer[n]);
                    n++;
                    i++;
                }
                Temp[i]='\0';
                n=n+1;
                i=0; 
                if(strcmp(FcStr,Temp)==0) return true;
            }
        }
    }
    return false;
}


/*判断是否为目录*/
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);
}
}
}


/*Cpy程式*/
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);
          }
      }
   }
}


bool mkdirs(char *muldir)
{
    int i,len;
    char str[512];
    strncpy(str,muldir,512);
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i]=='/')
        {
            str[i]='\0';
            if(access(str,0)!=0)
            {
                mkdir(str,0777);
            }
            str[i]='/';
        }
    }
    if(len>0 && access(str,0)!=0)
    {
        mkdir(str,0777);
    }
    return true;
}


/*创建目录并COPY程式*/
bool DownProgram(char *FolderName,bool IsCreate,char *SourcePath,char *CpyPath)
{
int Del,status;
if(opendir(FolderName)!=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)
{
            if(toupper(CpyPath[1])=='W')
            {
                mkdirs(CpyPath);
            }
            else
            {
       status=mkdir(CpyPath,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);
         }
     }
  }
   printf("\033[32m Copy Finish!!\033[0m\n");
   closedir(dir_t);
   closedir(dir_s);
return true;
}


bool InputCode(CFGIF *Storecf)
{
    char Icode[255];
    char CfgStr[255];
    char Option='N';
    while(Option!='Y')
    {
        Title();
        memset(Icode,0,sizeof(Icode));
        memset(CfgStr,0,sizeof(CfgStr));
        int n=0,i=0,x=0,y=0;
        memset(Icode,0,sizeof(Icode));
        printf("\033[37;44m Please Input Part Number:\033[0m");
        fgets(Icode,sizeof(Icode),stdin);
        Icode[strlen(Icode)-1]='\0'; 
        for(;n<strlen(Icode);n++)Icode[n]=toupper(Icode[n]);
        strcpy(CfgStr,ReadCfgStr(Icode)); 
        if(strlen(CfgStr)>18)
        {
            for(;y<4;y++)
            {
                char KeyValue[255];
                memset(KeyValue,0,sizeof(KeyValue));
                while(CfgStr[i]!='|')
                {
                    KeyValue[x]=CfgStr[i];
                    x++;
                    i++;
                }
                KeyValue[x]='\0'; 
                switch(y)
                {
                    case 0:
                    {
                        strcpy(Storecf->DownCode,KeyValue);
                        break;
                    }
                    case 1:
                    {
                        strcpy(Storecf->ProductName,KeyValue);
                        break;
                    }
                    case 2:
                    {
                        strcpy(Storecf->dangrading,KeyValue);
                        break;
                    }
                    case 3:
                    {
                        strcpy(Storecf->WcpyPath,KeyValue);
                        break;
                    }
                    default:
                        break;
                }
                i=i+1;
                x=0;
            }
            while(CfgStr[i]!=' ')
            {
                Storecf->WinBootPath[x]=CfgStr[i];
                i++;
                x++;
            }
            Storecf->WinBootPath[x]='\0';
            i=i+2;
            x=0;
            while(CfgStr[i]!='\0')
            {
                Storecf->DownProgramPath[x]=CfgStr[i];
                x++;
                i++;
            }
            Storecf->DownProgramPath[x]='\0';
            printf("%s [Y,N]?",Storecf->ProductName);
            scanf("%c",&Option);
            Option=toupper(Option);      
        }
        if(Option=='Y')
        {
            return true;
        }
    } 
    return false;
}


bool RunJobNumber(INPIF *Storeif)
{
    bool IsJobNumber=false;
    char IJobNumber[10];
    while(IsJobNumber==false)
    {        
        Title();
        memset(IJobNumber,0,sizeof(IJobNumber));
        printf("Please Keyin or Scan your Operator ID:");
        scanf("%s",IJobNumber);
        IsJobNumber=FcJobNumber(IJobNumber,"/mnt/PUBLIC/OPID.TXT");
    }
    strcpy(Storeif->JobNumber,IJobNumber);
    return true; 
}


bool RunStation(INPIF *Storeif)
{
    bool IsLineBody=false;
    char ILineBody[11];
    while(IsLineBody==false)
    {
        Title();
        memset(ILineBody,0,sizeof(ILineBody));
        printf("Please use Barcode reader to scan Station:");
        /*gets(ILineBody);*/
        scanf("%s",ILineBody);
        int n=0;
        while(ILineBody[n]!='\0')
        {
            ILineBody[n]=toupper(ILineBody[n]);
            n++;
        }
        IsLineBody=FcJobNumber(ILineBody,"/mnt/PUBLIC/STATION.TXT");
    }
    strcpy(Storeif->LineBody,ILineBody); 
    return true;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值