0

I'm trying to write a CGI script in c++ which prints the reverse network path (using traceroute) from the web server to the IP address of the client invoking the CGI script.

When I run the program in Visual Studio, it works fine(creates the process, prints the result into "C:/result.out" file, opens the file, prints each line from file, closes file) BUT after compiling and trying to run just its .exe file, it throws an exception. What could I do to make the .exe work properly ? Just as a note, I'm using Windows XP and Visual C++ 2008

Here`s the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include <conio.h>

int main()
{
    char *line, *command, *userIp;

    printf("Content-Type:text/html\n\n");
    printf("<html><head></head><br/>");
    printf("<body><br/>");

    line    = (char*)malloc(255*sizeof(char));
    command = (char*)malloc(10*sizeof(char));
    userIp  = (char*)malloc(30*sizeof(char));
    //userIp = getenv("REMOTE_ADDR"); // use a default IP until program works 
    strcpy(command,"tracert ");
    strcpy(userIp,"74.125.87.104");

    strcat(command,userIp);
    strcat(command," > C:/result.out");
    // create command "tracert 74.125.87.104 > C:/result.out"

    printf("%s",command);
    system(command);
    FILE *f;
    f = fopen("C:/result.out","r");  // open C:/result.out and read line - by - line
    strcpy(line,"");
    while(!feof(f)){
      fgets(line,255,f);
      printf("%s\n",line);
    }
    fclose(f);
    printf("<br/>Test running OK<br/>");
    printf("</body></html>");
    getch();
    return 0;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Liviu
  • 149
  • 1
  • 8
  • 2
    What is the exception it throws please? –  Nov 30 '10 at 03:34
  • I`m sorry, I`m not very good at Exception handling in C, so I couldn`t figure out exactly. It only prompted me that my program threw an unhandled win32 exception. Never the less, I managed to solve my problem with a popen. But thank you for your response – Liviu Nov 30 '10 at 10:33

2 Answers2

3

Most likely your web server (sanely) doesn't have permission to write to c:\. Either use a proper location for temporary files, or have tracert stream the results back to the executable so you can capture them.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Well the following two lines cause buffer over-runs

strcat(command,userIp);
strcat(command," > C:/result.out");

So this is likely the result of a crash.

Please do not use the term "Exception" unless an exception is being thrown, and since you are writing C code that is unlikely. So it is not an exception.

Rather than running a command using system() and piping the result to a file use the popen() command this will run a command and pipe the output to a stream that you can read just like a file (but without the security implications of writing to the file system).

FILE *f;
f = popen(command,"r");  // run the command. The std out goes to the FILE stream
    ^^^^^^
strcpy(line,"");
while(!feof(f)){
  fgets(line,255,f);
  printf("%s\n",line);
}
fclose(f);
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • First of all, thank you very much for your suggestion. That worked perfectly. – Liviu Nov 30 '10 at 10:28
  • Second of all, about the "Exception" term ... I only used it because Visual Studio kept popping up an error saying that my program threw an unhandled win32 exception. Sorry if that is not corrcect, I just said what my computer prompted :-?? – Liviu Nov 30 '10 at 10:30