Chinaunix

标题: 求助:CGI脚本不能打开文件 这是为什么啊 ??? [打印本页]

作者: mj壮壮    时间: 2011-07-22 16:56
标题: 求助:CGI脚本不能打开文件 这是为什么啊 ???
以下是我写的 CGI脚本代码 可是在打开文件时不能成功。。。高手帮忙看下 另外我想问下 用C写CGI 脚本是不是一定要用如CGIC这样的函数 库呢?还是直接用linux——C写就行来呢   不过我写的几个CGI 都没有成功实现   最简单的问题就是这个打开文件都失败了。。。
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

char* getcgidata(FILE* fp, char* requestmethod);
int main(int argc,char *argv[])

{
int fp01;
  char *input;

  char *req_method;

  char cont[64];

// char pass[64];

  int i = 0;

  int j = 0;

// printf("Content-type: text/plain; charset=iso-8859-1\n\n";

  printf("Content-type: text/html\n\n";

  req_method = getenv("REQUEST_METHOD";

  input = getcgidata(stdin, req_method);

for ( i =5, j = 0; i < (int)strlen(input); i++ )

  {

  cont[j++] = input;

  }

  cont[j] = '\0';
printf("%s\n<br>",cont);
// char *P=pass;

if((fp01=open("xieru.txt",O_RDWR))==-1)
{

printf("cannot open the file\n<br>";
printf("%d<br>",fp01);
exit(0);
}
else
{
// fp01=open("xieru.txt",O_RDWR);
write(fp01,cont,;
}

close(fp01);
return 0;
}


char* getcgidata(FILE* fp, char* requestmethod)

{

  char *input;

  int len;

  int size=1024;

  int i=0;

   

  if (!strcmp(requestmethod,"GET")

  {

  input = getenv("QUERY_STRING";

  return input;

  }

  else if (!strcmp(requestmethod,"OST")

  {

  len = atoi(getenv("CONTENT_LENGTH");

  input = (char*)malloc(sizeof(char)*(size + 1));

   

  if(len == 0)

  {

  input[0] = '\0';

  return input;

  }

   

  while(1)

  {

  input = (char)fgetc(fp);

  if(i == size)

  {

  input[i+1] = '\0';

  return input;

  }

   

  --len;

  if (feof(fp)||(!(len)))

  {

  i++;

  input = '\0';

  return input;

  }

  i++;

   

  }

  }

  return NULL;

}




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2