util.c
changeset 12 191b3c25974a
parent 10 d07309450285
child 14 51eb85ae4de4
equal deleted inserted replaced
11:9aa5114326e8 12:191b3c25974a
    37  */
    37  */
    38 void
    38 void
    39 usage( char *prg )
    39 usage( char *prg )
    40 {
    40 {
    41 	printf( "%s [-vh] [-f <filename>] [-d <level>]\n", prg );
    41 	printf( "%s [-vh] [-f <filename>] [-d <level>]\n", prg );
       
    42 	printf( "    -d <level> Show debug information on stderr\n" );
       
    43 	printf( "    -f <filename> Specify the database file to use (default is './volta.db')\n");
       
    44 	printf( "    -h Usage (you're lookin' at it)\n" );
    42 	printf( "    -v Display version\n" );
    45 	printf( "    -v Display version\n" );
    43 	printf( "    -d <level> Show debug information on stderr\n" );
       
    44 	printf( "    -h Usage (you're lookin' at it)\n" );
       
    45 	printf( "    -f <filename> Specify the database to use (default is 'volta.db' in the cwd.)\n");
       
    46 	printf( "\n" );
    46 	printf( "\n" );
    47 	return;
    47 	return;
    48 }
    48 }
    49 
    49 
    50 
    50 
    78 	return;
    78 	return;
    79 }
    79 }
    80 
    80 
    81 
    81 
    82 /*
    82 /*
       
    83  * Output to stdout for squid, unless the debug level is at or above 4.
       
    84  */
       
    85 void
       
    86 out( const char *str )
       
    87 {
       
    88 	if ( v.debugmode >= 4 ) return;
       
    89 	fprintf( stdout, "%s", str );
       
    90 	return;
       
    91 }
       
    92 
       
    93 
       
    94 /*
       
    95  * Given a string, reverse it in place.
       
    96  */
       
    97 void
       
    98 reverse_str( char *str )
       
    99 {
       
   100 	int i = 0;
       
   101 	int tmp = 0;
       
   102 	int j = strlen( str ) - 1;
       
   103 
       
   104 	while ( i < j ) {
       
   105 		tmp    = str[i];
       
   106 		str[i] = str[j];
       
   107 		str[j] = tmp;
       
   108 
       
   109 		i++;
       
   110 		j--;
       
   111 	}
       
   112 
       
   113 	return;
       
   114 }
       
   115 
       
   116 
       
   117 /*
    83  * Append 'buf' to the end of 'line', a la strcat, except dynamically
   118  * Append 'buf' to the end of 'line', a la strcat, except dynamically
    84  * grow memory for the target string.
   119  * grow memory for the target string.
    85  *
   120  *
    86  * 'buf' should be null terminated.  Returns the modified line.
   121  * 'buf' should be null terminated.  Returns the modified line.
    87  */
   122  */
   101 	else {
   136 	else {
   102 		offset  = strlen( line ); /* not including '\0' */
   137 		offset  = strlen( line ); /* not including '\0' */
   103 		new_len = offset + LINE_BUFSIZE;
   138 		new_len = offset + LINE_BUFSIZE;
   104 	}
   139 	}
   105 
   140 
   106 	debug( 4, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset );
   141 	debug( 5, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset );
   107 	if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
   142 	if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
   108 		debug( 1, LOC, "Ignoring line, error while allocating memory: %s\n",
   143 		debug( 5, LOC, "Ignoring line %d, error while allocating memory: %s\n",
   109 				(line_realloc == NULL ? strerror(errno) : "Line too large") );
   144 				v.timer.lines+1, (line_realloc == NULL ? strerror(errno) : "Line too large") );
   110 		if ( line != NULL ) free( line ), line = NULL;
   145 		if ( line != NULL ) free( line ), line = NULL;
   111 		printf( "\n" );
   146 		printf( "\n" );
   112 	}
   147 	}
   113 	else {
   148 	else {
   114 		line = line_realloc;
   149 		line = line_realloc;
   136 				file, strerror(errno) );
   171 				file, strerror(errno) );
   137 		return( NULL );
   172 		return( NULL );
   138 	}
   173 	}
   139 
   174 
   140 	if ( (contents = malloc( sb.st_size + 1 )) == NULL ) {
   175 	if ( (contents = malloc( sb.st_size + 1 )) == NULL ) {
   141 		debug( 1, LOC, "Unable to allocate memory for file '%s': %s\n",
   176 		debug( 5, LOC, "Unable to allocate memory for file '%s': %s\n",
   142 				file, strerror(errno) );
   177 				file, strerror(errno) );
   143 		return( NULL );
   178 		return( NULL );
   144 	}
   179 	}
   145 
   180 
   146 	if ( (fh = fopen( file, "r" )) == NULL ) {
   181 	if ( (fh = fopen( file, "r" )) == NULL ) {
   148 				file, strerror(errno) );
   183 				file, strerror(errno) );
   149 		return( NULL );
   184 		return( NULL );
   150 	}
   185 	}
   151 
   186 
   152 	if ( fread( contents, sizeof(char), sb.st_size, fh ) != (unsigned int)sb.st_size ) {
   187 	if ( fread( contents, sizeof(char), sb.st_size, fh ) != (unsigned int)sb.st_size ) {
   153 		debug( 1, LOC, "Short read for file '%s'?: %s\n", file );
   188 		debug( 5, LOC, "Short read for file '%s'?: %s\n", file );
   154 		fclose( fh );
   189 		fclose( fh );
   155 		return( NULL );
   190 		return( NULL );
   156 	}
   191 	}
   157 
   192 
   158 	fclose( fh );
   193 	fclose( fh );
   170 {
   205 {
   171 	char *alloc_ptr = NULL;
   206 	char *alloc_ptr = NULL;
   172 	if ( string == NULL || length == 0 ) return ( NULL );
   207 	if ( string == NULL || length == 0 ) return ( NULL );
   173 
   208 
   174 	if ( (alloc_ptr = calloc( length + 1, sizeof(char) )) == NULL ) {
   209 	if ( (alloc_ptr = calloc( length + 1, sizeof(char) )) == NULL ) {
   175 		debug( 1, LOC, "Unable to allocate memory for token: %s\n", strerror(errno) );
   210 		debug( 5, LOC, "Unable to allocate memory for token: %s\n", strerror(errno) );
   176 		return( NULL );
   211 		return( NULL );
   177 	}
   212 	}
   178 
   213 
   179 	(void)memcpy( alloc_ptr, string, length );
   214 	(void)memcpy( alloc_ptr, string, length );
   180 
   215 
   197 
   232 
   198 	(void)strncpy( c_ip, ip_string, length );
   233 	(void)strncpy( c_ip, ip_string, length );
   199 	c_ip[ length ] = '\0';
   234 	c_ip[ length ] = '\0';
   200 
   235 
   201 	if ( (alloc_ptr = calloc( length, sizeof(struct in_addr) )) == NULL ) {
   236 	if ( (alloc_ptr = calloc( length, sizeof(struct in_addr) )) == NULL ) {
   202 		debug( 1, LOC, "Unable to allocate memory for ip '%s': %s\n",
   237 		debug( 5, LOC, "Unable to allocate memory for ip '%s': %s\n",
   203 				c_ip, strerror(errno) );
   238 				c_ip, strerror(errno) );
   204 	}
   239 	}
   205 
   240 
   206 	if ( inet_pton( AF_INET, c_ip, alloc_ptr ) < 1 ) {
   241 	if ( inet_pton( AF_INET, c_ip, alloc_ptr ) < 1 ) {
   207 		debug( 1, LOC, "Unable to create in_addr struct for client ip '%s': %s\n",
   242 		debug( 1, LOC, "Unable to create in_addr struct for client ip '%s': %s\n",