/*--------------------------------------------------------*/ /* cgi.c 08-aug-1997/bets */ /* based on code from pablo */ /* contains (modified) cgi processing routines, */ /* downloaded from ncsa.uiuc */ /*--------------------------------------------------------*/ #include #include #include "page.h" #include "cgi.h" #define LF 10 #define CR 13 /*--------------------------------------------------------*/ /* cgi_getword */ /* this routine gets the first word from line, where the */ /* word starts at the 0th character in line and ends with */ /* the first character in line that matches the stop */ /* character; then the routine advances line past the stop*/ /* character */ /*--------------------------------------------------------*/ void cgi_getword (char *word,char *line,char stop ) { int x = 0, y; for ( x=0; (( line[x] ) && ( line[x] != stop )); x++ ) word[x] = line[x]; word[x] = '\0'; if ( line[x] ) ++x; y = 0; while ( (line[y++] = line[x++]) != '\0' ) ; } /* end of cgi_getword () */ /*--------------------------------------------------------*/ /* cgi_makeword */ /*--------------------------------------------------------*/ char *cgi_makeword( char *line,char stop ) { int x = 0, y; char *word = (char *)malloc( sizeof(char) * (strlen( line ) + 1 )); if ( word == NULL ) { page_error( "CgiException: out of memory" ); exit( 1 ); } for ( x=0; ((line[x]) && (line[x] != stop)); x++ ) word[x] = line[x]; word[x] = '\0'; if ( line[x] ) ++x; y = 0; while( (line[y++] = line[x++]) != '\0' ); return word; } /* end of cgi_makeword () */ /*--------------------------------------------------------*/ /* cgi_fmakeword */ /*--------------------------------------------------------*/ char *cgi_fmakeword( FILE *f,char stop,int *cl ) { int wsize = 102400; int ll = 0; char *word = (char *)malloc( sizeof(char) * ( wsize + 1 )); while ( 1 ) { word[ll] = (char)fgetc( f ); if ( ll == wsize ) { word[ll+1] = '\0'; wsize += 102400; word = (char *)realloc( word, sizeof(char)*( wsize+1 )); } --(*cl); if (( word[ll] == stop ) || ( feof( f )) || ( ! ( *cl ))) { if ( word[ll] != stop ) ll++; word[ll] = '\0'; return word; } ++ll; } } /* end of cgi_fmakeword() */ /*--------------------------------------------------------*/ /* cgi_x2c */ /*--------------------------------------------------------*/ char cgi_x2c( char *what ) { register char digit; digit = ( what[0] >= 'A' ? (( what[0] & 0xdf ) - 'A' ) + 10 : ( what[0] - '0' )); digit *= 16; digit += ( what[1] >= 'A' ? (( what[1] & 0xdf ) - 'A' ) + 10 : ( what[1] - '0' )); return( digit ); } /* end of cgi_x2c() */ /*--------------------------------------------------------*/ /* cgi_unescape_url */ /*--------------------------------------------------------*/ void cgi_unescape_url( char *url ) { register int x, y; for ( x=0, y=0; url[y]; ++x, ++y ) { if (( url[x] = url[y] ) == '%' ) { url[x] = cgi_x2c( &url[y+1] ); y += 2; } } url[x] = '\0'; } /* end of cgi_unescape_url() */ /*--------------------------------------------------------*/ /* cgi_plus2space */ /* this routine replaces all plus signs (+) in str with */ /* blank spaces */ /*--------------------------------------------------------*/ void cgi_plus2space( char *str ) { register int x; for ( x=0; str[x]; x++ ) if ( str[x] == '+' ) str[x] = ' '; } /* end of cgi_plus2space() */ /*--------------------------------------------------------*/ /* cgi_index */ /* this routine returns the index of c in string s; */ /* it returns -1 if c is not found in s */ /*--------------------------------------------------------*/ int cgi_index( char *s,char c ) { register int x; for ( x=0; s[x]; x++ ) if ( s[x] == c ) return x; return -1; } /* end of cgi_index() */ /*--------------------------------------------------------*/ /* cgi_rindex */ /* this routine returns the index of c in s, searching s */ /* in reverse order; it returns -1 if c is not found in s */ /*--------------------------------------------------------*/ int cgi_rindex( char *s,char c ) { register int x; for( x=strlen(s) - 1; x != -1; x-- ) if ( s[x] == c ) return x; return -1; } /* end of cgi_rindex() */ /*--------------------------------------------------------*/ /* cgi_fgetline */ /* this routine reads and returns a line (in s) from file */ /* f of maximum length n; it strips off CR-LF characters */ /* and terminates s with '/0'. the return value of the */ /* routine is 1 if the end-of-file is read, else 0. */ /*--------------------------------------------------------*/ int cgi_fgetline( char *s,int n,FILE *f ) { register int i = 0; while ( 1 ) { s[i] = (char)fgetc( f ); if ( s[i] == CR ) s[i] = fgetc( f ); if (( s[i] == 0x4 ) || ( s[i] == LF ) || ( i == ( n - 1 ))) { s[i] = '\0'; return ( feof( f ) ? 1 : 0 ); } ++i; } } /* end of cgi_fgetline() */ /*--------------------------------------------------------*/ /* cgi_send_fd */ /* this routine copies the contents of file f to file fd */ /*--------------------------------------------------------*/ void cgi_send_fd( FILE *f,FILE *fd ) { char c; while ( 1 ) { c = fgetc( f ); if ( feof( f )) return; fputc( c,fd ); } } /* end of cgi_send_fd() */ /*--------------------------------------------------------*/ /* cgi_escape_shell_cmd */ /*--------------------------------------------------------*/ void cgi_escape_shell_cmd( char *cmd ) { register int x, y, l; l = strlen( cmd ); for ( x=0; cmd[x]; x++ ) { if ( cgi_index( "&;`'\"|*?~<>^()[]{}$\\", cmd[x] ) != -1 ) { for ( y=l+1; y>x; y-- ) cmd[y] = cmd[y-1]; l++; /* length has been increased */ cmd[x] = '\\'; x++; /* skip the character */ } } } /* end of cgi_escape_shell_cmd() */