diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-17 20:57:05 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-17 20:57:05 +0200 |
commit | 361bb55f01ba28960a8127b0a16c1e7a7a8de61a (patch) | |
tree | ff0479af1c9283c8a0546025022ebc54616a9a75 /query.c | |
parent | 2810ad72aeb973a898034d6875922284062c5ce0 (diff) | |
download | cforum-361bb55f01ba28960a8127b0a16c1e7a7a8de61a.tar.gz |
Rewrite mktpl script in C
The Perl version didn't handle multiple interpolations on the same line.
Diffstat (limited to 'query.c')
-rw-r--r-- | query.c | 29 |
1 files changed, 21 insertions, 8 deletions
@@ -10,13 +10,14 @@ * truncation occurred, the -1th character of the string is set to 1. */ char * -nextparam(int max) +nextparam(enum method method, int max) { char *buf; int i, sz; static int j = 0; - if(!query.post && !query.string[j-1]) + /* Return NULL if at end of parameter string. */ + if(method == GET && !query.string[j-1]) return NULL; #define STEP 256 @@ -32,17 +33,28 @@ nextparam(int max) buf++; TRUNCATED(buf) = 0; -#define READ(b) (query.post? fread(b, 1, 1, stdin): (*(b) = query.string[j++])) +#define READ(b) (method == GET? \ + (*(b) = query.string[j++]): \ + fread(b, 1, 1, stdin)) - /* Return NULL if first character cannot be read. */ + /* + * The parameter is read in a rather lopsided loop, treating + * the first character specially. + */ +first: + /* Return NULL if at end of parameter string. */ if(!READ(buf)) return NULL; + /* Skip empty parameters. */ + if(*buf == '&') + goto first; + i = 0; - goto loop; + goto rest; for(; READ(buf+i); i++){ -loop: +rest: if(buf[i] == '&'){ buf[i] = 0; break; @@ -74,12 +86,13 @@ setquery() fprintf(stderr, "no QUERY_STRING\n"); exit(1); } - query.post = strcmp(getenv("REQUEST_METHOD"), "POST") == 0; + query.method = strcmp(getenv("REQUEST_METHOD"), "POST")? GET: POST; } /* * Split parameter string on equals sign and return value portion; - * return NULL if none found. + * return NULL if none found. For example, v = split(p) results in + * p pointing to the parameter key and v pointing to the value. */ char * split(char *param) |