Hello There,
I am new to C programming, and am not very good with pointers. However,
I was wondering what I did wrong with the following so that I can learn from
my mistakes. What I was trying to do, was get a substring from the referer
environment variable in a POST method CGI script. Then pass this variable
to gethostbyname. Following is the code that does NOT work. I got it to
work properly by converting *data into an array of type char. The main
problem is that when it gets to the first debug print, data contains the
information that it is supposed to. However, after calling gethostbyname,
the last character is a garbage character and host is NULL. What am I doing
wrong? Thanks in advance for any tips of any nature (including my sucky
coding style, though it IS properly indented on the actual code.).
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef struct hostent hostent;
int main ( void )
{
char *referer, *data;
hostent *host;
int i;
data = calloc(1, sizeof( char * ) );
printf ("Content-type: text/html\n\n");
referer = getenv("HTTP_REFERER");
referer += 7; /* skip past the 7 characters of "http://" */
for ( i = 0 ; *referer != '/'; )
*(data + i++) = *referer++;
*(data + i ) = '\0';
printf ("%s:<BR>\n", data ); /*First debug print to check data */
host = gethostbyname(data);
printf ("%s:<BR>\n", data ); /*Second debug print to check data */
return 0;
Quote:}