/cgi-bin/getfile.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void htmlspecialchars(const string& s, string& ret)
{
ret = "";
for (string::const_iterator i = s.begin(); i != s.end(); i++)
{
switch (*i)
{
case '<':
ret += "<";
break;
case '>':
ret += ">";
break;
case '&':
ret += "&";
break;
case ' ':
ret += " ";
break;
default:
ret += *i;
}
}
}
int main()
{
cout << "Content-type: text/html; charset=UTF-8\n\n";
char *f = getenv("QUERY_STRING");
ifstream ifs(f);
do
{
if (!ifs.is_open())
{
cout << "file cannot open: " << f << endl;
break;
}
cout << "<h5>" << f << "</h5>\n";
cout << "<pre>\n";
for (string line; getline(ifs, line); )
{
string s;
htmlspecialchars(line, s);
cout << s << endl;
}
cout << "</pre>\n";
}
while (0);
return 0;
}