LinuxSir.cn,穿越时空的Linuxsir!

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

字符串的问题,又来了

[复制链接]
发表于 2003-9-26 21:59:57 | 显示全部楼层 |阅读模式
想法:将变量t2中的"\"后的内容定义为一个文件名的变量fname,
      然后将fname定义为一个一维数组fname[2],
      fname[0]="index.html",fname[1]="index.htm";
      判断:
     

  1.       if (strcmp(t2, strcat( "/", fname)) == 0)
  2.           {
  3.                printf("200 OK.\n");
  4.                exec_command(connectfd, fname);
  5.           }
  6.       else
  7.           printf( "403 Bad Request.\n");

  8.          
  9.       
复制代码

函数exec_command:
     

  1.       int exec_command( int fd, char *fname)
  2.       {
  3.           char buf[BUFSIZ];
  4.           int n;
  5.           fd = open(fname, O_RDONLY);
  6.           if(fd == -1) {
  7.               perror("open file");
  8.               exit(1);
  9.               }
  10.          while((n=read(fd, buf, BUFSIZ)) > 0)
  11.              write(1, buf, n);
  12.        close(fd);
  13.        exit(0);
  14.      }
  15.       
复制代码

还想用send将读取的index.html文件返回到客户端。
我另外发一帖好了…………
发表于 2003-9-26 22:30:14 | 显示全部楼层
strcat( "/", fname)
这样写不对,strcat把第二个参数连接到第一个参数后面,第一个参数必须保证有足够大的空间,你写的"/"是个常字符串
int exec_command( int fd, char *fname)
这个fd不对,按你的代码应该另取一个名字,函数内部再声明另一个文件描述符
 楼主| 发表于 2003-9-27 09:50:01 | 显示全部楼层
我思路有点乱,等我有头绪了再来问问你。
谢谢先!
发表于 2003-9-27 12:43:36 | 显示全部楼层
strcat
=
{
  it i = strlen(d);
  strcpy(d+i,s);
}
 楼主| 发表于 2003-9-27 12:50:17 | 显示全部楼层
无双,我不太明白你的意思啊。
我想法是这样的,当客户段输入http://ip:port/filename时,
我把filename获取了,但是我不知道怎么把它结合到一个读取文件
并将文件返回到客户端的界面上。
用strcat我是想把“/”和文件名称分开来,因为我上面用变量获取的值,设置为t2=/filename,而读取文件的程序总是没有“/”的,我不知道怎么做这个。
发表于 2003-9-27 13:07:28 | 显示全部楼层
sprintf(d,"/%s",s);
 楼主| 发表于 2003-9-27 13:36:59 | 显示全部楼层
d,s代表什么?
发表于 2003-9-27 14:38:19 | 显示全部楼层
d: 目标
s: 源
why not google for function prototype to comprehend meaning of each parameter.
 楼主| 发表于 2003-9-27 18:31:04 | 显示全部楼层
thank you,but i just don't konw how to find the way.
Maybe i am a freshman of the area.
发表于 2003-9-27 19:04:42 | 显示全部楼层
使用google是最基本的功能

建议你先学会这个
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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