0

我只是无法弄清楚为什么 CGI 脚本的输出不稳定,尽管输入在所有运行中都是相同的。

我有这个 CGI 脚本 [in C],它生成一个html页面,该页面有一个表,该表再现了文件系统(Ubuntu)的树/层次视图。它在 arm 平台上运行并且是迷你的(使用 lighthttpd 开发)。所以 o /p of script 是一个包含目录名和文件名的表。我的问题是它在运行时成功列出了该表中的文件系统(文件和文件夹列表),比如现在,并且在运行 10 分钟左右时它不显示包含文件和文件夹的表,保持输入相同(文件系统和文件仍然相同)[浏览器只显示错误消息Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data. 或空白页。]

可能是什么问题?我已经在服务器上运行了脚本并验证了输出是一个有效的 HTML 页面,其中文件和文件夹的路径有效并且它们都存在。但有时相同的脚本会成功显示表格,否则我上面提到的任何错误都会出现。我怀疑这可能与权限问题有关,但如果是这样,它是如何偶尔显示的?我已将我正在处理的所有文件和文件夹的权限更改为 full.But 仍然没有帮助!!:(

脚本如下...

#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main()
{
  int dir_count = 0;
  int file_count = 0;
  int dir_index=0;
  int file_index=0;
  int inner_loop=0;
  int len=0;
  int i=0;
  DIR * dirp;
  DIR * dirp2;
  DIR * dirp3;
  char realbase[35]="/home";
  char final_report_path[30];
  char base[20];
  char source[20];
  char patient_id[15];
  char patient_name[15];
  int j=0;
  struct dirent * entry;
  char dir_name[500][20];
  char* file_name[500];
  dirp = opendir(realbase); 
  if(dirp)
  while ((entry = readdir(dirp)) != NULL) 
    {
             if (entry->d_type == DT_DIR) 
        { /* If the entry is a regular file */
                 if(entry->d_name[0]!='.' && entry->d_name[1]!='.')
            {
                 dir_count++;
                         strcpy(dir_name[dir_index++],entry->d_name);

            }
            }
    }
  closedir(dirp);
  printf("Content-type: text/html\n\n");
  printf("<html>\n");
  printf("<body>\n");
  printf("<h1>Blah Blah!!</h1>\n");
  printf("\n");
  printf("    \n");
  printf("Click on the links to view Reports\n");
  printf("    \n");
  printf("<table border=\"3\" bordercolor=\"#c86260\" bgcolor=\"#ffffcc\" width=\"50%\" cellspacing=\"5\" cellpadding=\"3\">\n");
  printf("<tr><td><b>Patient Name</b></td><td><b>Patient ID</b></td><td><b>Reports</b></td></tr>\n");
  for(i=0;i<dir_count;i++)
  {
   strncpy(source, "", sizeof(source));
   strncpy(base, "", sizeof(base));
   strncpy(patient_name, "", sizeof(patient_name));
   strncpy(patient_id, "", sizeof(patient_id));
   strcpy(source,dir_name[i]);
   if(source[0]!='.')
{
   strcpy(base,"/home");
                      for(len=0;len<sizeof(source);len++)
                    {
                        if(source[len]!='_')
                        patient_name[len]=source[len];
                        else
                        {
                                                len++;

                        break;
                        }
                    }
                 for(inner_loop=0;len<sizeof(source);len++,inner_loop++)
                    {
                        if(source[len]!='\0')
                        patient_id[inner_loop]=source[len];
                        else
                        {

                        break;
                        }
                    }
   printf("<tr><td valign=\"center\">%s</td>",patient_name);
   printf("<td valign=\"center\">%s</td>",patient_id);

   strcat(base,source);
   strcat(base,"/Reports/");

   dirp2 = opendir(base); 
   if(dirp2)
   while ((entry = readdir(dirp2)) != NULL) 
    {

             if (entry->d_type == DT_REG) 
        { /* If the entry is a regular file */
                 if(entry->d_name[0]!='.')
            {
                                 if(file_count==0)
                                 {
                                  printf("<td>");
                                 }
                                 if(file_count!=0)
                                 {
                                  printf("<br>");
                                 }
                 file_count++;
                         file_name[file_index++]=entry->d_name;

                 printf("<a href=\"/home/%s/Reports/%s\">",dir_name[i],file_name[file_index-1]);
                 printf("%s",file_name[file_index-1]);
                 printf("</a>");
            }
            }
    }
   if(file_count>0)
   printf("</td>");
   printf("</tr>\n");
   file_count=0;
   file_index=0;
   closedir(dirp2);
}
  }

  printf("</table>");

  printf("</body>\n");
  printf("</html>\n");
  return 0;
}
4

0 回答 0