home *** CD-ROM | disk | FTP | other *** search
- /*
- * parseurl.c
- * A portable, simple but inefficient way of parsing URLs from searches
- *
- * i = parseurl(qs, v, r)
- * qs is the encoded query string (getenv("QUERY_STRING"))
- * f is the name of the field to look up
- * r is where to return the result
- * rs is the number of bytes allocated in r
- * i returns
- */
-
- #include <stdlib.h>
- #include <stdio.h>
-
- static int hexdig(i)
- int i;
- {
- if ('0' <= i && i <= '9')
- return i-'0';
- else if ('A' <= i && i <= 'F')
- return i-'A'+10;
- else return i-'a'+10;
- }
-
- static int decode(qs, r, rs)
- char *qs, *r;
- int rs;
- {
- /* Decode one value up to the & or \0 */
- while (1 < rs && *qs && *qs != '&') {
- if (*qs == '+') {
- *r++ = ' ';
- qs++ ; rs--;
- }
- else if (*qs == '%') {
- *r++ = hexdig(qs[1])*16+hexdig(qs[2]);
- qs += 3; rs--;
- }
- else {
- *r++ = *qs++; rs--;
- }
- }
- *r = 0;
- if (0 < rs) return 1;
- return 2;
- }
-
- int parseurl(qs, v, r, rs)
- char *qs, *v, *r;
- int rs;
- {
- if (qs == NULL) {
- qs = getenv("QUERY_STRING");
- }
- if (qs == NULL || *qs == 0) return -1;
- while (*qs) {
- if (0==strncmp(v, qs, strlen(v)) &&
- qs[strlen(v)]=='=')
- return decode(qs+strlen(v)+1, r, rs);
- while (*qs && *qs != '&') qs++;
- if (*qs == '&') qs++;
- }
- return 0;
- }
-
- #if 0
- void main()
- {
- char email[20];
- int i;
-
- printf("content-type: text/plain\n\n");
- printf("TEST RESULTS\n");
- printf("getenv=%s\n", getenv("QUERY_STRING"));
- i = parseurl(NULL, "emailaddr", email, sizeof(email));
- printf("i=%d\n", i);
- printf("email=%s\n", email);
- exit(0);
- }
- #endif
-