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

📄 fmerge.c

📁 该代码包为INTEL IXP2400单板的BSP,同时经过修改,可以对MSN和ixf1104做自定义配置
💻 C
📖 第 1 页 / 共 3 页
字号:
/*ZXR10: Append a newfile to the ZAR file, delete and extract files
         existed from the ZAR file, and display the information about
         the files(filename, file size, and last modify date) in the
         ZAR file.
  -A(a)  Append a new file to the ZAR file. If there is a file with
         the same name has already in the ZAR file, fmerger will prompt
         you whether you will override it.
-AZ(az)  deflate a new file to a compact file ,then Append the compact
         file to the ZAR file.
  -L(l)  Display the information about the files existed in the ZAR file.
  -R(r)  Delete the file(s) specified by the user from the ZAR file.
  -E     Extract all the files in the ZAR file to the current directory.
  -Eu    Extract all the files in the ZAR file to the current directory
		 and inflate all the files.
  -e     Extract the file(s) specified by the user from the ZAR file
         to the current directory.
  -eu     Extract the file(s) specified by the user from the ZAR file
         to the current directory and inflate it.*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include "ctype.h"
#include "sys/stat.h"


#ifndef VxWorks
#define VxWorks
#endif

#ifndef VxWorks
/*Header files used in the VC++6.0*/
#include "direct.h"
#include "sys/utime.h"
#endif

/*Header files used in the Borlandc++ 5.02*/

#undef   MAXDRIVE
#undef   MAXDIR
#undef   MAXFILE
#undef   MAXEXT

#define  RW_BYTES   512
#define  MAXDRIVE     4
#define  MAXDIR       66
#define  MAXFILE      9
#define  MAXEXT       7
#define  SOF          0x9    /*Tag used to identify the ZAR file*/

extern int Extract_Inflate_File(int Num_Files, char **FileName);
extern int Extract_Inflate_Files(int Num_Files, char **FileName);
extern int inflate_mem(char* p_src_mem,UINT src_mem_size,char* p_dst_mem,UINT dst_mem_size);

long int sourceFileLength = 0;

/*The Img file header in the ZAR file*/
typedef struct fileHeadZAR
                    {
                      char          fname[MAXFILE+MAXEXT];
					  long int      flength;	/* compact file length */
					  long int      srcflength;	/* source file length */
/*                      time_t        creatTime;*/
                      time_t        modifyTime;
                     }HdZAR;


int Delete_IMGfile(int Num_Files, char **FileName);

#ifdef VxWorks
#include "stdarg.h"
#include "ioLib.h"
#include "utime.h"
#endif

/*#include "io.h"
#include "dir.h"*/

#ifdef VxWorks
/*Compare two strings
  Input: char * s1, char *s2
  Return:s1 equal to s2: 0, else: 1*/
int stricmp(char *s1, char *s2)
{   int ret_value=1;
    int s1_length, s2_length, length, i;

    s1_length = strlen(s1);
    s2_length = strlen(s2);
    if (s1_length!=s2_length)
        return ret_value;
    length = s1_length;
    for (i=0; i<length; i++)
    {   if (isupper((int)(s1[i]))!=0)
            s1[i] = tolower(s1[i]);
        if (isupper((int)(s2[i]))!=0)
            s2[i] = tolower(s2[i]);
    }
    if (strcmp(s1,s2)==0)
        ret_value = 0;
    return ret_value;
}

/* if s2 is a part of s1 ,return 0 . hu.shaohua 2002-11-12 17:29 */

int strspcmp(char *s1, char *s2)
{   int ret_value=1;
    int length, i;

    length = strlen(s2);

    for (i=0; i<length; i++)
    {   if (isupper((int)s1[i])!=0)
            s1[i] = tolower(s1[i]);
        if (isupper((int)s2[i])!=0)
            s2[i] = tolower(s2[i]);
    }

    if ( bcmp(s1,s2,length)==0 )
        ret_value = 0;
    return ret_value;
}


#endif

/*write the long type data to file use the little-endian format
  to assure the portability
  Input: i, the data write to the file
         destFile, the file pointer
  Output: void*/
void Put_To_File(long i, FILE *destFile)
{   fputc((i>>24)&0xff, destFile);
    fputc((i>>16)&0xff, destFile);
    fputc((i>>8)&0xff, destFile);
    fputc(i&0xff, destFile);
}

/*read the long type data from file use the little-endian format
  to assure the portability
  Input: i, the data write to the file
         destFile, the file pointer
  Output: void*/
long Read_From_File(FILE *destFile)
{
    long i;

    i  =(long)( (fgetc(destFile)<<24) & 0xFF000000 ) ;
    i |=(long)( (fgetc(destFile)<<16) & 0xFF0000 );
    i |=(long)( (fgetc(destFile)<<8)  & 0xFF00 );
    i |=(long)( fgetc(destFile) & 0xFF);
    return i;
}

/*Check the destination file, and return the length of the file*/
/*Input: File Pointer: pDestFile*/
/*       Status of the file: staBuf*/
long int  checkDestFile(FILE * pDestFile,struct stat * staBuf)
{
        int fNo;/*To get the file handle of the file pointer*/
        long int  lthDestFile;/*To save the length of the destination file*/
        int pChar;/*Used to save the character read from the file*/

        fNo=fileno(pDestFile);
        fstat(fNo, staBuf);
        lthDestFile= staBuf->st_size;
        if  (lthDestFile<=1)
        {
            printf ("There are no img file in the dest file!\n");
            return 0;
        }
        pChar=getc( pDestFile) ;
        if (pChar!=SOF)
        {
            printf ("Wrong dest file format!\n");
            return 0;
        }
        return (lthDestFile);
}

/*Look up the destination file, to determine if the Img file has existed
  in the destination file
  Input : File Pointer: destFile
  Img file name: imgFileName
  return : yes:1, no:0*/
int Lookup_File(FILE *destFile, char *imgFileName)
{   int chi, ret_value=0;/*Used to save the character read from the file*/
                         /*ret_value: to save the return value*/
    HdZAR header;        /*The header of the Img file in the ZAR file*/

    fseek(destFile, 1, SEEK_SET);
    chi = fgetc(destFile);
    while (chi!=EOF)
        {   fseek(destFile, -1, SEEK_CUR);
            fread (&header.fname,1,MAXFILE+MAXEXT,destFile);
            header.flength = Read_From_File(destFile);
            header.srcflength = Read_From_File(destFile);
/*            header.creatTime=Read_From_File(destFile);*/
            header.modifyTime=Read_From_File(destFile);
            if (stricmp(header.fname, imgFileName)==0)
                {    ret_value=1;
                     return ret_value;
                }
            else
                {   fseek(destFile, header.flength, SEEK_CUR);
                    chi = fgetc(destFile);
                }
        }
    return ret_value;
}

int z_seek_pointer(FILE* fp, char *imgFileName, unsigned int *srcFileLen)
{
    HdZAR header;        /*合并后各IMG文件的文件头。*/
    struct stat file_stat;
    char   ch;
    int    nCnt;

    fstat(fileno(fp),&file_stat);
    fseek(fp, 0, SEEK_SET);
    fread(&ch, 1,1,fp);

    while (1)
    {
        if( fread ((char*)&header.fname,1, MAXFILE+MAXEXT,fp) != (MAXFILE+MAXEXT))
            break;

        header.flength = Read_From_File(fp);
        header.srcflength = Read_From_File(fp);
/*        header.creatTime=Read_From_File(fp);*/
        header.modifyTime=Read_From_File(fp);

        if (stricmp(header.fname, imgFileName)==0)
        {
            if(srcFileLen != NULL)
                *srcFileLen = header.srcflength;
            return header.flength;
        }
        else
        {
            if( (ftell(fp)+header.flength) >= file_stat.st_size)
                break;
            else
            {
                for(nCnt=0;nCnt<header.flength;nCnt++)
                    fread(&ch, 1,1,fp);
/*               lseek(fd, header.flength, SEEK_CUR);*/
            }
        }
    }

    return -1;
}

/*To split the path and the filename
  1 Look up the last "\" or "/" from the input
  2 Save the characters after the last "\" or "/" to 'filename'*/
void Split_Pathname(char *Fullname, char *filename)
{
    int             k = 0;
    unsigned int    j;
    char            *lpath_flag1;
    char            *lpath_flag;

    lpath_flag = strrchr(Fullname, '\\');
    lpath_flag1 = strrchr(Fullname, '/');

    if (lpath_flag==NULL && lpath_flag1 == NULL)
        lpath_flag = Fullname-1;
	else
	{
		if(lpath_flag != NULL && lpath_flag1 != NULL)
		{
			if(strlen(lpath_flag1) < strlen(lpath_flag))
				lpath_flag = lpath_flag1;
		}
		else if(lpath_flag == NULL)
			lpath_flag = lpath_flag1;
	}

    for (j=lpath_flag - Fullname+1; j<strlen(Fullname); j++)
    {   filename[k]=Fullname[j];
        k++;
    }
    filename[k] = '\0';
}

/*Set the file pointer to the given position
  Input: the file pointer of the destination file, Img filename
  Output: Success: 1, Fail: 0*/
int Fd_Position(FILE *destFile, char *IMGfname)
{   HdZAR           header;
    int             ret_value=0, chi;
    long            offset, nCnt;
    char            ch;

    fseek(destFile, 1, SEEK_SET);
    chi = fgetc(destFile);
    if (chi == EOF)
        {   printf("There is no any IMG files in the destfile\n");
            return ret_value;
        }
    while (chi != EOF)
        {   fseek(destFile, -1, SEEK_CUR);
            fread (&header.fname,1,MAXFILE+MAXEXT,destFile);
            header.flength = Read_From_File(destFile);
            header.srcflength = Read_From_File(destFile);
/*            header.creatTime=Read_From_File(destFile);*/
            header.modifyTime=Read_From_File(destFile);
            offset= header.flength;
            if (stricmp(header.fname, IMGfname) == 0)
                {   fseek(destFile, -(long)sizeof(HdZAR), SEEK_CUR);
                    ret_value =1;
                    break;
                }
            else
            {   /*fseek(destFile, offset, SEEK_CUR);*/
                for (nCnt=0; nCnt<offset; nCnt++)
                    fread(&ch, 1, 1, destFile);
                chi = fgetc(destFile);
                if (chi == EOF)
                    break;
            }
        }/*while (chi != EOF)*/
    return ret_value;
}

/*Append a new Img file to the ZAR file
  Input: The number of the arguments: Num_Files
         The string  of each input:   FileName
  Return: Success:1, Failed: 0*/
int Append_IMGfile(int Num_Files, char **FileName)
{
    FILE       *destFile, *imgFile;/*The file pointer of the destination file and the Img file*/
    int        ret_value=0;/*return value*/
    int        i=0, find_file=0, ch;
    char        *tmpFileName[4],*imgFileName, *rw_buf;
    struct  stat    File_Statbuf;

    HdZAR       header;
    char            filename[MAXFILE+MAXEXT];
    long        temp;

    /*Open the destination file to append a new Img file. The destination
      file was opened use Read_Only to determin whether the file has existed.*/
    destFile = fopen(FileName[Num_Files-1], "rb");

    /*Created the file if it is not existed*/
    if (destFile == NULL)
    {   printf ("Creating the new destfile: --%s-- now\n", FileName[Num_Files-1]);
        destFile = fopen(FileName[Num_Files-1], "ab+");

        /*Created the new file was failure*/
        if (destFile == NULL)
        {   printf("Cannot Creating the new destfile: --%s-- \n", FileName[Num_Files-1]);
            return ret_value;
        }
        /*success*/
        else
        {   fputc (SOF,  destFile);
            fclose(destFile);
        }
    }
    fclose(destFile);

    for (i=0; i<4; i++) /*rex 2002-11-22 11:47*/
    {
        tmpFileName[i]=(char *) NULL;
        tmpFileName[i]=(char *) malloc (100);
        if(NULL == tmpFileName[i])
        {
            for(;i>=0;i--)
                free(tmpFileName[i]);
            return ret_value;
        }
    }
    strcpy(tmpFileName[0], FileName[0]);
    strcpy(tmpFileName[1], FileName[1]);
    strcpy(tmpFileName[3], FileName[Num_Files-1]);


    /*Open the destination file to append the new Img file(s)*/
    destFile = fopen(FileName[Num_Files-1], "r+b");
    if (destFile == NULL)
    {   printf("Cannot open the destfile for writing\n");
        return ret_value;
    }
    for (i=2; i<Num_Files-1; i++)
    {       Split_Pathname(FileName[i], filename);
        find_file = Lookup_File(destFile, filename);
        if (find_file==1)
        {   printf("file %s was found in the destFile\n", FileName[i]);
            printf("Overwrite it or not? y/n:");
            /*if the new Img file has existed in the destination file,
               and the user want to override it, the Img file in the ZAR
               file should be deleted firstly. Otherwise does nothing
            */
            ch = getchar();
            if ((tolower(ch)=='y')||(iscntrl(ch)!=0))
            {   strcpy(tmpFileName[2], filename);
                fclose(destFile);
                Delete_IMGfile(4, tmpFileName);

                /*Open the destination file to append the new Img file(s)*/
                destFile = fopen(FileName[Num_Files-1], "r+b");
                if (destFile == NULL)
                {   printf("Cannot open the destfile for writing\n");
                    return ret_value;
                }
            }
            else
                FileName[i]=NULL;
        }
    }/*end of for (i=2; i<Num_Files-1; i++)*/

    for (i=0; i<4; i++)
        free(tmpFileName[i]);

    if ((rw_buf=(char *)malloc(RW_BYTES))==NULL)
    {   printf("Extract Files: Not enough memory!\n");
        fclose(destFile);
        return ret_value;
    }

    /*Append the new Img file to the destination file*/
    for (i=2; i<Num_Files-1; i++)
    {   imgFileName = FileName[i];
        if (imgFileName != NULL)
        {   fseek(destFile, 0, SEEK_END);
            Split_Pathname(FileName[i], filename);
            imgFile = fopen(imgFileName, "rb");
            if (imgFile == NULL)
            {
                printf("Cannot open the IMG file: %s\n", imgFileName);
                free(rw_buf);/*rex 2002-11-22 11:47*/
                fclose(destFile);
                return ret_value;
            }
            fstat( fileno(imgFile), &File_Statbuf );/*Read the information of the file*/

            /*write the header of the Img file to the destination file*/
            header.fname[0]  =   '\0';
            strcpy (header.fname,filename);
            fwrite(header.fname, 1, MAXFILE+MAXEXT, destFile);
            header.flength   = File_Statbuf.st_size;
            Put_To_File(header.flength,destFile);
            header.srcflength= sourceFileLength;
            Put_To_File(header.srcflength,destFile);
/*            header.creatTime = File_Statbuf.st_ctime;
            Put_To_File(header.creatTime,destFile);*/
            header.modifyTime= File_Statbuf.st_mtime;
            Put_To_File(header.modifyTime,destFile);

            printf ("File: %s is now being added into %s!\n",imgFileName,FileName[Num_Files-1]);
            /*write the Img file to the destination file*/
            for (temp=0;temp< header.flength/RW_BYTES;temp++)
            {   fread(rw_buf,1,RW_BYTES,imgFile);
                fwrite(rw_buf, 1, RW_BYTES, destFile);
            }
            if ((header.flength%RW_BYTES)!=0)
            {   fread(rw_buf,1,header.flength%RW_BYTES,imgFile);
                fwrite(rw_buf, 1, header.flength%RW_BYTES, destFile);
            }
            fclose (imgFile);
        }/*end if (imgFileName != NULL)*/
    }/*end for (i=2; i<Num_Files-1; i++)*/
    ret_value = 1;
    free(rw_buf);
    fclose(destFile);
    return ret_value;
}/*end of int Append_IMGfile(int Num_Files, char **FileName)*/

/*Delete the specified file(s) from the ZAR file
  Input: The number of the arguments: Num_Files

⌨️ 快捷键说明

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