util.c
changeset 18 d4ce82194b64
parent 16 e6a640ad2cc2
child 22 822094314703
equal deleted inserted replaced
17:bd746609ba46 18:d4ce82194b64
    80 	return;
    80 	return;
    81 }
    81 }
    82 
    82 
    83 
    83 
    84 /*
    84 /*
    85  * Output to stdout for squid, unless the debug level is at or above 5.
       
    86  */
       
    87 void
       
    88 out( const char *str )
       
    89 {
       
    90 	if ( v.debugmode >= 5 ) return;
       
    91 	fprintf( stdout, "%s", str );
       
    92 	fflush( stdout );
       
    93 	return;
       
    94 }
       
    95 
       
    96 
       
    97 /*
       
    98  * Given a string, reverse it in place.
    85  * Given a string, reverse it in place.
    99  */
    86  */
   100 void
    87 void
   101 reverse_str( char *str )
    88 reverse_str( char *str )
   102 {
    89 {
   175 
   162 
   176 	return( line );
   163 	return( line );
   177 }
   164 }
   178 
   165 
   179 
   166 
   180 /*
       
   181  * Read an entire file into memory, returning a pointer to the contents.
       
   182  * Returns NULL on error.
       
   183  *
       
   184  */
       
   185 char *
       
   186 slurp_file( char *file )
       
   187 {
       
   188 	FILE *fh       = NULL;
       
   189 	char *contents = NULL;
       
   190 	struct stat sb;
       
   191 
       
   192 	if ( stat( file, &sb ) != 0 ) {
       
   193 		debug( 1, LOC, "Unable to stat() file '%s': %s\n",
       
   194 				file, strerror(errno) );
       
   195 		return( NULL );
       
   196 	}
       
   197 
       
   198 	if ( (contents = malloc( sb.st_size + 1 )) == NULL ) {
       
   199 		debug( 5, LOC, "Unable to allocate memory for file '%s': %s\n",
       
   200 				file, strerror(errno) );
       
   201 		return( NULL );
       
   202 	}
       
   203 
       
   204 	if ( (fh = fopen( file, "r" )) == NULL ) {
       
   205 		debug( 1, LOC, "Could not open file for reading '%s': %s\n",
       
   206 				file, strerror(errno) );
       
   207 		return( NULL );
       
   208 	}
       
   209 
       
   210 	if ( fread( contents, sizeof(char), sb.st_size, fh ) != (unsigned int)sb.st_size ) {
       
   211 		debug( 5, LOC, "Short read for file '%s'?: %s\n", file );
       
   212 		fclose( fh );
       
   213 		return( NULL );
       
   214 	}
       
   215 
       
   216 	fclose( fh );
       
   217 	return( contents );
       
   218 }
       
   219 
       
   220 
       
   221 /* 
   167 /* 
   222  * Allocate memory and copy +length+ bytes (plus 1 for null) from the given
   168  * Allocate memory and copy +length+ bytes (plus 1 for null) from the given
   223  * +string+ into a new string, returning a pointer to it.
   169  * +string+ into a new string, returning a pointer to it.
   224  * 
   170  * 
   225  */
   171  */