📄 isindex.c
字号:
/* CGI program for handling simple ISINDEX queries *//* This program assumes that the query is passed to the CGI program in the environment variable QUERY_STRING (as a result of a GET http operation) and that there are no spaces or other characters that the browser would encode (urlencoding). */#include <stdio.h>#include <stdlib.h> /* for getenv */#include <string.h>#include <ctype.h>#define MAXQUERY 1000char *http_header = "content-type: text/html\n\n";char *error_message = "<H2>Some kind of error occured while processing your query</H2>\n";void fixstring( unsigned char *s);int main( ) { char *query; FILE *cmd; char command[2001]; char results[2000]; /* get the query - we assume it is in the environment variable QUERY_STRING */ query = getenv("QUERY_STRING"); fixstring( query ); if ((query==NULL)||(strlen(query)==0)) { /* No QUERY or it is null - generate error message and quit*/ printf("%s%s\n",http_header,error_message); exit(1); } if (strcspn(query,"&;!@#$()~\"") != strlen(query)) { printf("%s",http_header); printf("<H2>Nice try wise guy, but it's not gonna happen!</H2>\n"); return(0); } /* create the grep command */ snprintf(command,2000,"grep %s /usr/dict/words",query); /* start up the grep and pipe the result back here (to cmd) */ cmd = popen(command,"r"); if (cmd==NULL) { /* problem starting up the grep command */ printf("%s%s\n",http_header,error_message); exit(1); } /* Grep is running - print out the results */ printf("%s",http_header); printf("<H2>Words found that match \"%s\":</H2>\n",query); printf("<UL>\n"); while (fgets(results,1000,cmd) !=NULL ) { printf("<LI>%s\n",results); } printf("</UL>\n"); return(0);}/* hexval converts a ascii encoded hex digit to the corresponding binary value. If we get a numeric character ('0' - '9') we return the corresponding value 0-9. If we get an alphabetic character we return; 'a' or 'A' -> 10 'b' or 'B' -> 11 ... 'f' or 'F' -> 15*/char hexval ( char x ) { if (isdigit(x)) { /* converting '0' - '9' */ return(x-'0'); } else if ( (x>='a') && (x<='f')) { /* lowercase 'a' - 'f' */ return(10+(x-'a')); } else if ( (x>='A') && (x<='F')) { /* uppercase 'A' - 'F' */ return(10+(x-'A')); } /* we got something we didn't expect - return -1 */ return(-1);} /* fixstring converts a urlencoded string to a normal string. This includes: replaces all '+' character by ' ' replaces all hex equivalents by the single character encoded. The size of the string may change - but it will only shrink, so we change it in place (change the original string).*/void fixstring( unsigned char *s) { char *p = strdup(s); /* make a copy of the string to work with */ char *orig=p; /* keep track of this copy */ while (*p) { if (*p=='+') { /* substitute a blank for a plus */ *s=' '; p++; s++; /* Look for a hex encoded character - don't trust the user (make really sure this is hex encoded!) */ } else if ((strlen(p)>=3) && (*p=='%') /*&& (isxdigit(p+1)) && (isxdigit(p+2))*/ ) { /* hex encoded - convert to a char */ *s = hexval(p[1])<<4 | hexval(p[2]); s++; p+=3; } else { /* normal character - just copy it without changing */ *s=*p; s++; p++; } } /* terminate the new string */ *s=0; /* and get rid of the duplicate */ free(orig);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -