Char ** getvars ()
{
Int I;
Int content_length;
Char ** postvars;
Char * postinput;
Char ** pairlist;
Int paircount = 0;
Char * nvpair;
Char * eqpos;
Postinput = getenv ("content_length ");
If (! Postinput)
Exit (1 );
If (! (Content_length = atoi (postinput )))
Exit (1 );
If (! (Postinput = (char *) malloc (content_length + 1 )))
Exit (1 );
If (! Fread (postinput, content_length, 1, stdin ))
Exit (1 );
Postinput [content_length] = '/0 ';
For (I = 0; postinput [I]; I ++)
If (postinput [I] = '+ ')
Postinput [I] = '';
Pairlist = (char **) malloc (256 * sizeof (char **));
Paircount = 0;
Nvpair = strtok (postinput ,"&");
While (nvpair)
{
Pairlist [paircount ++] = strdup (nvpair );
If (! (Paircount % 256 ))
Pairlist = (char **) realloc (pairlist, (paircount + 256) * sizeof (char **));
Nvpair = strtok (null ,"&");
}
N = paircount * 2 + 1;
Pairlist [paircount] = 0;
Postvars = (char **) malloc (paircount * 2 + 1) * sizeof (char **));
For (I = 0; I <paircount; I ++)
{
Eqpos = strchr (pairlist [I], '= ');
If (eqpos)
{
* Eqpos = '/0 ';
Unescape_url (postvars [I * 2 + 1] = strdup (eqpos + 1 ));
} Else
{
Unescape_url (postvars [I * 2 + 1] = strdup (""));
}
Unescape_url (postvars [I * 2] = strdup (pairlist [I]);
}
Postvars [paircount * 2] = 0;
For (I = 0; pairlist [I]; I ++)
Free (pairlist [I]);
Free (pairlist );
Free (postinput );
Return postvars;
}
Among them, the strtok () and realloc () functions are exquisite and practical, making the string processing very simple ......