小程序

/*
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * [email protected]
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CHGNOLEN  (11)

int write_neworchg_src(FILE* fp_dst, char tmp_buf[], int cvsorsvn, const char *newchg)
{
   FILE* fp = NULL;
    char buff[256];
 char bk_buf[256];
 int newflg = 0;

 fp = fopen("cvs-nup.txt", "r");
 if ( (NULL != fp) && (NULL != tmp_buf) )
 {
  while ( !feof(fp) )
        {
   memset(bk_buf, 0x00, sizeof(bk_buf));
            memset(buff, 0x00, sizeof(buff));
            fgets(buff, sizeof(buff), fp);
            if ( !( memcmp(buff, "C ", 2)) )
            {
                printf("Error:Confict file exist,Please check!!!/n");
                return (-1);
            }
   else if (!(memcmp(buff, "U ", 2)) )
            {
                printf("Error:File need to be Updated,Please check!!!/n");
                return (-1);
            }
   else if ( !(memcmp( buff, "Mer", 3)) )
            {
                printf("Error:Merged file exist,Please check and retry!!!/n");
                return (-1);
            }
            else if (!(memcmp( buff, newchg, 2)) )
            {
    newflg = 1;
    strncpy(bk_buf, tmp_buf, sizeof(bk_buf));
               // printf("bk_buf:%s/n",bk_buf);
               // printf("buff:%s/n",buff);
                if ( cvsorsvn )
    {
                 strcat(bk_buf, &buff[7]);
    }
                else
    {
                 strcat(bk_buf, &buff[2] );
    }
                if ( NULL != fp_dst )
                {
                //  printf("bk_buf:%s/n",bk_buf);
                    fputs(bk_buf, fp_dst);
                }
            }
   else
   {
   }
        }
  fclose(fp);
  if ( !newflg )
  {
   fputs("./n", fp_dst);
  }
  return 0;
    }
 return (-1);
}

int write_chgline_adddel(FILE* fp_dst, const char* dststr)
{
 FILE* fp = NULL;
 char bk_buf[256];
    char buff[256];
 char* p = NULL;
 int i = 0;

    fp = fopen("changeline.txt", "r");
    if ( NULL != fp )
    {
  memset(bk_buf, 0x00, sizeof(bk_buf));
  memset(buff, 0x00, sizeof(buff));
        fgets(buff, sizeof(buff), fp);
  p = strstr(buff, dststr);
  if ( p )
  {
   p += strlen(dststr);
   while ( (char)0x20 != *p )
   {
    bk_buf[i++] = *p++;
   }
   fputs(bk_buf, fp_dst);
   fputs("/n", fp_dst);
  }
  else
  {
   fputs("./n", fp_dst);
  }
  fclose(fp);
  return 0;
 }
 return (-1);
}

int main(int argc , char ** argv)
{
 FILE* fp = NULL;
 FILE* fp_dst = NULL;
 FILE* fp_cfg = NULL;
    char buff[256];
 char tmp_buf[256];
 char chgfilenm[256];
 char cfg_buf[256];
 char cfg_dir[256];
 int len = 0;
 int cvsorsvn = 0;
 int ret = 0;
 
 if ( (argc < 4) || (NULL == argv) || (NULL == argv[1]) || (NULL == argv[2]) || (NULL == argv[3]) )
 {
  printf("format :autocrechg cfgfiledir changno flg/n");
  printf("example:autocrechg /vns/work_neu/User/chenbd/changes/ V574AD-0007 1/n");
  goto ERROR;
 }

 memset(cfg_dir, 0x00, sizeof(cfg_dir));
    strcpy(cfg_dir, argv[1]);
 strcat(cfg_dir, "AUTOCRECHG.CFG");
// printf("cfg_dir:%s/n", cfg_dir);

 fp_cfg = fopen(cfg_dir, "r");
 if (NULL == fp_cfg)
 {
  printf("%s exist? please check!!!/n", cfg_dir);
  goto ERROR;
 }
 printf("AUTOCRECHG.CFG :%s/n",cfg_dir);

 memset(cfg_buf, 0x00, sizeof(cfg_buf));
 fgets(cfg_buf, sizeof(cfg_buf), fp_cfg);
 if ( !(memcmp( buff, "svn", 3)) )
    {
  cvsorsvn = 1;
    }
 if ( (0x31) == argv[3][0] )
 {
  if ( cvsorsvn )
  {
   system("svn status > cvs-nup.txt");
   printf("Repository Type:SVN /n ");
  }
  else
  {
   system("cvs -n up > cvs-nup.txt");
   printf("Repository Type:CVS /n");
  }
 }
 memset(cfg_buf, 0x00, sizeof(cfg_buf));
 memset(tmp_buf, 0x00, sizeof(tmp_buf));
 fgets(cfg_buf, sizeof(cfg_buf), fp_cfg);
 strncpy(tmp_buf, cfg_buf,(strlen(cfg_buf) -1));
 
 printf("maindir:%s/n",tmp_buf);

 memset(chgfilenm, 0x00, sizeof(chgfilenm));
 memset(cfg_buf, 0x00, sizeof(cfg_buf));
 
 strcpy(&chgfilenm[0], argv[1]);
 strncat(chgfilenm, argv[2], CHGNOLEN);

 printf("changefile:%s/n",chgfilenm);

 fp_dst = fopen(chgfilenm, "w");
 if ( NULL != fp_dst )
 {
  printf("*********** Proc start **********/n");
  
  memset(cfg_buf, 0x00, sizeof(cfg_buf));
     fgets(cfg_buf, sizeof(cfg_buf), fp_cfg);
  fputs(cfg_buf, fp_dst);
  fputs("/n", fp_dst);
  printf("*********** Changer Name OK **********/n"); 

  fputs("#僠乕儉柤丗/n", fp_dst);
  memset(cfg_buf, 0x00, sizeof(cfg_buf));
        fgets(cfg_buf, sizeof(cfg_buf), fp_cfg);
        fputs(cfg_buf, fp_dst);
        fputs("/n", fp_dst);
  printf("*********** Team Name OK **********/n");

  fputs("#曄峏src:/n", fp_dst);
  
  {
   ret = write_neworchg_src(fp_dst, tmp_buf, cvsorsvn, "M ");
   if (ret)
   {
    goto ERROR;
   }
   fputs("/n", fp_dst);
   printf("*********** Changed SRC OK **********/n");
  }

  fputs("#怴婯src:/n", fp_dst);
  
  {
   ret = write_neworchg_src(fp_dst, tmp_buf, cvsorsvn, "A ");
   if (ret)
            {
               goto ERROR;
            }
   fputs("/n", fp_dst);
   printf("*********** NEW SRC OK **********/n");
        }

  fputs("#<捛壛峴悢>:/n", fp_dst);
  if ( (0x31) == argv[3][0] )
  {
   memset(buff, 0x00, sizeof(buff));
   strcpy(buff, "changeline ");
   strncat(buff, argv[2], CHGNOLEN);
   strcat(buff, " ");
   strcat(buff, tmp_buf);
   printf("changeline:%s/n",buff);
   strcat(buff, " | grep total > changeline.txt");
   system(buff);
   write_chgline_adddel(fp_dst, "plus:");
   fputs("/n", fp_dst);
  }
  else
  {
   fputs("/n/n", fp_dst);
  }
  printf("*********** Changed Line OK **********/n");

  fputs("#<嶍彍峴悢>:/n", fp_dst);
  if ( (0x31) == argv[3][0] )
  {
   write_chgline_adddel(fp_dst, "minus:");
   fputs("/n", fp_dst);
  }
  else
  {
            fputs("/n/n", fp_dst);
        }
  printf("*********** Deleted Line OK **********/n");

  fputs("#曄峏撪梕:/n/n", fp_dst);
  printf("*********** Add 曄峏撪梕 Manualy or Give some Suggestions **********/n");

  fputs("#奐敪娗棟No丗/n/n", fp_dst);
  printf("*********** Add 奐敪娗棟No Manualy or Give some Suggestions **********/n");
  
  fputs("#廋惓懳徾僾儘僕僃僋僩丗/n", fp_dst);
  memset(cfg_buf, 0x00, sizeof(cfg_buf));
        fgets(cfg_buf, sizeof(cfg_buf), fp_cfg);
        fputs(cfg_buf, fp_dst);
        fputs("/n", fp_dst); 

  fputs("#旛峫:/n.", fp_dst);
  
  printf("*********** OK **********/n");
 }

ERROR:
 if (NULL != fp)
    {
  fclose( fp );
 }
 if (NULL != fp_cfg)
    {
        fclose( fp_cfg );
    }
 if ( NULL != fp_dst )
 {
  fclose(fp_dst);
 }
   
 return 0;
}

你可能感兴趣的:(小程序)