LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 780|回复: 3

那位帮我看看,为什么执行到这里会了错了

[复制链接]
发表于 2006-11-10 12:33:26 | 显示全部楼层 |阅读模式
  1. #ifdef HAVE_CONFIG_H
  2. #include <config.h>
  3. #endif
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include<sys/types.h>
  8. #include<sys/stat.h>
  9. int fcopy(char *p,char *p2)
  10. {
  11.   char cub[1024];
  12.   int readi;
  13.   FILE *fp,*fp1;
  14.   chmod(p,S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  15.   fp=fopen(p,"r");
  16.   fp1=fopen(p2,"w");
  17.   while(!feof(fp))
  18.     {
  19.       readi=fread(cub,sizeof(char),1024,fp);
  20.       fwrite(cub,sizeof(char),readi,fp1);
  21.     }
  22.   fclose(fp);
  23.   fclose(fp1);
  24. return 0;
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28.   FILE *fp,*fplog;
  29.   char cub[1024],*usermail, sTiff[1024],sDtmf[4],newdir[2000],webdir[1024]="/home/billyz/data/user";
  30.    char userdir[1024];
  31. char *mailcmd1,*tmpstr;
  32.   int CodeType,j;
  33.   if (argc <2 )
  34.     return EXIT_SUCCESS;
  35. chmod(argv[2],S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  36.   fp=fopen(argv[2],"r");
  37.   fseek(fp,0,SEEK_END);
  38.    if (ftell(fp)<1024)
  39.   {
  40.    fclose(fp);
  41.    printf("file is smal");
  42.    return EXIT_SUCCESS;
  43.   }
  44.   fclose(fp);
  45. mailcmd1=malloc(2024);
  46. tmpstr=malloc(2024);
  47. usermail=malloc(1024);
  48.   memset(sTiff,'\0',1024);
  49.   memset(sDtmf,'\0',4);
  50.   memset(usermail,'\0',1024);
  51.   memset(userdir,'\0',1024);
  52.   strcpy(sDtmf,argv[1]);
  53.   strcpy(sTiff,argv[2]);
  54.   //SMTP邮件设置
  55.   fp=fopen("/home/billyz/data/sendEmail","r");
  56.   if(fp==NULL)
  57.     {
  58.       printf("open file err");
  59.     }
  60.   fgets(mailcmd1,2024,fp);
  61.   //traceServer("mailcmd:%s",mailcmd );
  62.   fclose(fp);
  63.   
  64. fplog=fopen("/home/billyz/data/sendmail.log","a+");
  65.   fp=fopen("/home/billyz/data/CodeType","r");
  66.   CodeType=fgetc(fp)-48;
  67.   //traceServer("CodeType:%d",CodeType );
  68.   fclose(fp);
  69.   fp=fopen("/home/billyz/data/user.dat", "r");
  70.   do
  71.     {
  72.       memset(cub,'\0',1024);
  73.       fgets(cub,1024,fp);
  74.       if(strlen(cub)>0)
  75.         if(strcmp(strtok(cub,":"),sDtmf)==0)
  76.           {
  77.             strtok(NULL,":");
  78.             strcpy(userdir,strtok(NULL,":"));
  79.             strtok(NULL,":");
  80.             strtok(NULL,":");
  81.             strtok(NULL,":");
  82.             strcpy(usermail,strtok(NULL,":"));
  83.             printf("%s:%s\n",userdir,usermail);
  84.           }
  85.     }
  86.   while(!feof(fp));
  87.   fclose(fp);
  88.   if(strlen(userdir)>0)
  89.     {
  90.       sprintf(newdir,"%s/%s/inbox/NEW%d.tiff",webdir,userdir,time((time_t*)NULL));
  91.       //sendmail1(usermail,newdir);
  92.     printf("mail: %s\n",usermail);   
  93.   fcopy(sTiff,newdir);
  94. j=strlen(usermail);
  95. printf("%c\nj:%d",mailcmd1[0],j);
  96. printf("\nd:%d", j);
  97. //执行到这里时会出错了
  98. if (mailcmd1[0]!='0' && j!=0)
  99.     {
  100.        
  101.       printf("d");
  102.       memset(tmpstr,'\0',2024);
  103.       sprintf(tmpstr,"%s -t "%s" -u "you have a new fax" -m "%d" -a %s >  /tmp/%d.log",mailcmd1,usermail,time((time_t*)NULL),newdir,time((time_t*)NULL));
  104.       printf("%s",tmpstr);
  105.       j=0;
  106.       do
  107.         {
  108.           memset(cub,'\0',1024);
  109.           system(tmpstr);
  110.           j++;
  111.           fp=fopen("/tmp/sendmail.log","r");
  112.           fgets(cub,1024,fp);
  113.           fclose(fp);
  114.           sprintf(tmpstr,"%s:%s:%s:%s",time((time_t*)NULL),userdir,usermail,cub);
  115.           fputs(tmpstr,fplog);
  116.           printf("%s\n%s",tmpstr,cub);
  117.         }
  118.       while(!(strstr(cub,"Email was sent successfully!")!=0) && j<10);
  119.     }
  120.   chmod("/home/billyz/data/sendmail.log",S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  121.   fclose(fplog);
  122.      
  123.       
  124.     }
  125.   else
  126.     {
  127.       switch(CodeType)
  128.         {
  129.         case 1:
  130.           fp=fopen("/home/billyz/data/default","r");
  131.           fgets(usermail,1024,fp);
  132.           strcpy(userdir,strtok(NULL,":"));
  133.           strcpy(usermail,strtok(NULL,":"));
  134.           fclose(fp);
  135.           sprintf(newdir,"%s/%s",webdir,userdir);
  136.           fcopy(sTiff,newdir);
  137.           j=strlen(usermail);
  138. if (mailcmd1[0]!='0' &&j!=0)
  139.     {
  140.       sprintf(tmpstr,"%s -t "%s" -u "you have a new fax" -m "%d" -a %s >  /tmp/sendmail.log",mailcmd1,usermail,time((time_t*)NULL),newdir);
  141.       j=0;
  142.       do
  143.         {
  144.           memset(cub,'\0',2024);
  145.           system(tmpstr);
  146.           j++;
  147.           fp=fopen("/tmp/sendmail.log","r");
  148.           fgets(cub,2024,fp);
  149.           fclose(fp);
  150.           sprintf(tmpstr,"%s:%s:%s:%s",time((time_t*)NULL),userdir,usermail,cub);
  151.           fputs(tmpstr,fplog);
  152.           printf("%s\n%s",tmpstr,cub);
  153.         }
  154.       while(!(strstr(cub,"Email was sent successfully!")!=0) && j<10);
  155.     }
  156.   chmod("/home/billyz/data/sendmail.log",S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  157. fclose(fplog);
  158.           break;
  159.         case 2:
  160.           fp=fopen("/home/billyz/data/user.dat", "r");
  161.           do
  162.             {
  163.               memset(cub,'\0',1024);
  164.               fgets(cub,1024,fp);
  165.               if(strlen(cub)>0)
  166.                 if(strcmp(strtok(cub,":"),"1")!=0)
  167.                   {
  168.                     strtok(NULL,":");
  169.                     strcpy(userdir,strtok(NULL,":"));
  170.                     strtok(NULL,":");
  171.                     strtok(NULL,":");
  172.                     strtok(NULL,":");
  173.                     strcpy(usermail,strtok(NULL,":"));
  174.                     sprintf(newdir,"%s/%s/inbox/NEW%d.tiff",webdir,userdir,time((time_t*)NULL));
  175.                     fcopy(sTiff,newdir);
  176.                     //sendmail1(usermail,newdir,userdir);
  177.           j=strlen(usermail);
  178. if (mailcmd1[0]!='0' &&j!=0)
  179.     {
  180.       sprintf(tmpstr,"%s -t "%s" -u "you have a new fax" -m "%d" -a %s >  /tmp/sendmail.log",mailcmd1,usermail,time((time_t*)NULL),newdir);
  181.       j=0;
  182.       do
  183.         {
  184.           memset(cub,'\0',2024);
  185.           system(tmpstr);
  186.           j++;
  187.           fp=fopen("/tmp/sendmail.log","r");
  188.           fgets(cub,2024,fp);
  189.           fclose(fp);
  190.           sprintf(tmpstr,"%s:%s:%s:%s",time((time_t*)NULL),userdir,usermail,cub);
  191.           fputs(tmpstr,fplog);
  192.           printf("%s\n%s",tmpstr,cub);
  193.         }
  194.       while(!(strstr(cub,"Email was sent successfully!")!=0) && j<10);
  195.     }
  196.   chmod("/home/billyz/data/sendmail.log",S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  197. fclose(fplog);
  198.                   }
  199.             }
  200.           while(!feof(fp));
  201.           fclose(fp);
  202.           break;
  203.         }
  204.     }
  205. free(mailcmd1);
  206. free(tmpstr);
  207. free(usermail);
  208.   return EXIT_SUCCESS;
  209. }
复制代码
运行./copytiffto 806 /var/spool/fax/recvq/fax000000026.tif
的以下提示
--------
billyz:pzblwxjj@163.com
mail: pzblwxjj@163.com
s
j:16
段错误
发表于 2006-11-10 23:24:18 | 显示全部楼层

  1. int fcopy(char *p,char *p2)
  2. {
  3.   char cub[1024];
  4.   int readi;
  5.   FILE *fp,*fp1;
  6.   chmod(p,S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  7.   fp=fopen(p,"r");
  8.   fp1=fopen(p2,"w");
  9.   while(!feof(fp))
  10.     {
  11.       readi=fread(cub,sizeof(char),1024,fp);//这儿快你的文件内容要不是正好1024整数倍的话,最后一次读不是肯定越界吗?
  12.       fwrite(cub,sizeof(char),readi,fp1);
  13.     }
  14.   fclose(fp);
  15.   fclose(fp1);

  16. return 0;
  17. }
复制代码

这个函数逻辑就有问题吧!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-11 16:40:59 | 显示全部楼层

不会越界的,如果到最后,他会返回读取到的字节数到readi中的。

Post by haohao_h

  1. int fcopy(char *p,char *p2)
  2. {
  3.   char cub[1024];
  4.   int readi;
  5.   FILE *fp,*fp1;
  6.   chmod(p,S_IROTH | S_IWOTH |  S_IWGRP | S_IRGRP);
  7.   fp=fopen(p,"r");
  8.   fp1=fopen(p2,"w");
  9.   while(!feof(fp))
  10.     {
  11.       readi=fread(cub,sizeof(char),1024,fp);//这儿快你的文件内容要不是正好1024整数倍的话,最后一次读不是肯定越界吗?
  12.       fwrite(cub,sizeof(char),readi,fp1);
  13.     }
  14.   fclose(fp);
  15.   fclose(fp1);

  16. return 0;
  17. }
复制代码

这个函数逻辑就有问题吧!

如果到最后,他会返回读取到的字节数到readi中的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-11 18:39:54 | 显示全部楼层
  1. sprintf(newdir,"%s/%s/inbox/NEW%d.tiff",webdir,userdir,time((time_t*)NULL));
复制代码
找到了,是这里出错了,userdir为空时sprintf就会出错,出现溢出。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表