I'm following the project TINY SERVER and there is a peice of code shown as below.
This code is used to passing the cgiargs to filename which is a cgi program.
void serve_dynamic(int fd, char *filename, char *cgiargs)
{
char buf[MAXLINE], *emptylist[] = { NULL };
/* Return first part of HTTP response */
sprintf(buf, "HTTP/1.0 200 OK\r\n");
rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Server: Tiny Web Server\r\n");
rio_writen(fd, buf, strlen(buf));
if (Fork() == 0) { /* child */ //line:netp:servedynamic:fork
/* Real server would set all CGI vars here */
setenv("QUERY_STRING", cgiargs, 1); //line:netp:servedynamic:setenv
dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */ //line:netp:servedynamic:dup2
execve(filename, emptylist, environ); /* Run CGI program */ //line:netp:servedynamic:execve
}
wait(NULL); /* Parent waits for and reaps child */ //line:netp:servedynamic:wait
}
In the adder.c which is the target cgi program, the code is shown as below.
int main(void) {
char *buf, *p;
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
int n1 = 0, n2 = 0;
/* Extract the two arguments */
if ((buf = getenv("QUERY_STRING")) != NULL) {
p = strchr(buf, ’&’);
*p = ’\0’;
strcpy(arg1, buf);
strcpy(arg2, p + 1);
n1 = atoi(arg1);
n2 = atoi(arg2);
}
/* Make the response body */
sprintf(content, "QUERY_STRING=%s", buf);
sprintf(content, "Welcome to add.com: ");
sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>",
content, n1, n2, n1 + n2);
sprintf(content, "%sThanks for visiting!\r\n", content);
/* Generate the HTTP response */
printf("Connection: close\r\n");
printf("Content-length: %d\r\n", (int)strlen(content));
printf("Content-type: text/html\r\n\r\n");
printf("%s", content);
fflush(stdout);
exit(0);
}
In my opinion, the serve_dynamic function sets the "QUERY_STRING", an environment variable, and the adder.c gets the "QUERY_STRING" and use it. But actually in adder.c, the buf is NULL.
Then I try to set environ to pass the QUERY_STRING and it works. But it's very inconvenient.
I didn't think out why setenv function didn't work, and I hope someone can solve my problem.
I read the environ variable agian, and I found I lost to use
extern char **environ. After adding this code, the setenv and getenv can work normally.