Esempio n. 1
0
File: cgi.c Progetto: mmocean/shttpd
//CGI这个值得一看
int
run_cgi(struct conn *c, const char *prog)
{
	struct env_block	blk;
	char			dir[FILENAME_MAX], *p;
	int			ret, pair[2];

	prepare_environment(c, prog, &blk);

	/* CGI must be executed in its own directory */
	(void) my_snprintf(dir, sizeof(dir), "%s", prog);
	for (p = dir + strlen(dir) - 1; p > dir; p--)
		if (*p == '/') {
			*p++ = '\0';
			break;
		}
	
	if (my_socketpair(c, pair) != 0) {
		ret = -1;
	} else if (spawn_process(c, prog, blk.buf, blk.vars, pair[1], dir)) {
		ret = -1;
		(void) closesocket(pair[0]);
		(void) closesocket(pair[1]);
	} else {
		ret = 0;
		c->loc.chan.sock = pair[0];
	}

	return (ret);
}
int main() {
    FILE *file_template = fopen("template.in", "r");
    FILE *file_values = fopen("values.in", "r");

    char *line = NULL;
    size_t len = 0;
    ssize_t read;

    if (file_values == NULL) {
        printf("Can't open template.in file!");
        return 0;
    }

    if (file_values == NULL) {
        printf("Can't open values.in file!");
        return 0;
    }

    while ((read = getline(&line, &len, file_values)) != -1) {
       if (line[0] == '-') {
           /* Regenerate enviroment and create last document */
           create_document(file_template);
           prepare_environment();

           /* Set document number */
           strcpy(document_number, get_value(line, len));
       } else {
           /* Save current template value on binary tree */
           save_value(get_number(line), get_value(line, len), firstValue, len);
       }
    }

    /* Create last document */
    create_document(file_template);
    free_memory(firstValue);

    if (line) {
        free(line);
    }

    fclose(file_template);
    fclose(file_values);

    return 0;
}