CGIãã©ã¡ã¼ã¿ã®èªåå¦ç
Cã§cgiãæ¸ãã¨ãã®ããã«ãã¨ããããåãå°ããªãã©ã¡ã¼ã¿å¦çé¢æ°ãä½ã£ãã
æªæ¥ã®èªååã使ãæ¹ã¡ã¢
é¢æ°cgi()ã«æåå(char*)ã渡ãã¨ãæååã®é
å(char**)ãè¿ãã
ä¾ãã°ãcgi("a=123&b=%23&c=1+3")ã¨å¼ã³åºãã¨ã{"a","123","b","#","c","1 3",NULL}ãè¿ãã
é
åã®0,2,4,â¦çªç®ã«ã¯åãã©ã¡ã¼ã¿ã®ååã¸ã®ãã¤ã³ã¿ãã1,3,5,â¦çªç®ã«ã¯åãã©ã¡ã¼ã¿ã®å¤ã¸ã®ãã¤ã³ã¿ãå
¥ãã
é
åã®æå¾ã®è¦ç´ ã«ã¯NULLãå
¥ãã
æååä¸ã«%nnã®å½¢å¼ããã£ãå ´åã«ã¯ããã«å¯¾å¿ããæåã«å¤æããã
æååä¸ã«+ããã£ãå ´åã«ã¯ã¹ãã¼ã¹ã«å¤æããã
ã¾ããå¼æ°ã空æåå("")ã ã£ãå ´åã«ã¯ãNULLã®ã¿ãå
¥ã£ãè¦ç´ 1ã®é
åãè¿ãã
å¼æ°ã§ä¸ããããæååèªä½ãå å·¥ããæ¹å¼ãªã®ã§å¿
è¦ãªãã°ã³ãã¼ã渡ããã¨ã
cgi()é¢æ°æ¬ä½
#include<stdlib.h> char **cgi(char *src){ char **m,*x,*y,i=0,n=0; for(x=src;*x;x++) if(*x=='=') n++; m = malloc(sizeof(char*)*(n*2+1)); for(x=y=m[0]=src;*x;x++,y++){ if(*x=='='||*x=='&'){ *y = '\0'; m[++i] = y+1; } else if(*x=='%') sscanf((x+=2)-1,"%2hhx",y); else *y = *x=='+' ? ' ' : *x; } *y = '\0'; m[n*2] = NULL; return m; }
ãµã³ãã«ã®mainé¢æ°
åãã©ã¡ã¼ã¿ãtableã«ãã¦è¡¨ç¤ºããä¾
#include<stdio.h> #include<stdlib.h> char **cgi(char*); main(){ char **q,i; puts("Content-Type: text/html\n"); puts("<form><input name=a><input name=b><input type=submit></form>"); q=cgi(getenv("QUERY_STRING")); if(q[0]!=NULL){ /* ãã©ã¡ã¼ã¿ã渡ããã¦ãããã®å¤å® */ puts("<table border><tr><th>key<th>value"); for(i=0;q[i]!=NULL;i+=2) printf("<tr><td>%s<td>%s\n",q[i],q[i+1]); /* q[i]ãååã§q[i+1]ãå¤ */ puts("</table>"); } }