Back to index

courier  0.68.2
cgihttpscriptptr.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2007 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 /*
00007 */
00008 #include      "cgi.h"
00009 
00010 #if    HAVE_UNISTD_H
00011 #include      <unistd.h>
00012 #endif
00013 
00014 #include      <string.h>
00015 #include      <stdlib.h>
00016 #include      <stdio.h>
00017 
00018 static char *scriptptr=0;
00019 
00020 extern void error(const char *);
00021 
00022 void cgihttpscriptptr_init()
00023 {
00024        /* Reinitialisation required when running as fastcgi */
00025        if (scriptptr) {
00026               free(scriptptr);
00027               scriptptr=0;
00028        }
00029 }
00030 
00031 const char *cgihttpscriptptr()
00032 {
00033        if (!scriptptr)
00034        {
00035        char   *p=getenv("SCRIPT_NAME");
00036        char   *h=getenv("HTTP_HOST");
00037        char   *q;
00038 
00039               if (!h)       h="";
00040               if (!p)       p="";
00041 
00042               q=malloc(strlen(p)+strlen(h)+sizeof("http://"));
00043               if (!q)       error("Out of memory.");
00044               sprintf(q, "http:%s%s%s", (*h ? "//":""), h, p);
00045               scriptptr=q;
00046        }
00047        return (scriptptr);
00048 }