util.c
changeset 14 51eb85ae4de4
parent 12 191b3c25974a
child 16 e6a640ad2cc2
equal deleted inserted replaced
13:23a242d7b7fa 14:51eb85ae4de4
    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" );
    42 	printf( "    -d <level> Show debug information on stderr\n" );
       
    43 	printf( "    -c <filename> Create the volta database from a rules file\n" );
    43 	printf( "    -f <filename> Specify the database file to use (default is './volta.db')\n");
    44 	printf( "    -f <filename> Specify the database file to use (default is './volta.db')\n");
    44 	printf( "    -h Usage (you're lookin' at it)\n" );
    45 	printf( "    -h Usage (you're lookin' at it)\n" );
    45 	printf( "    -v Display version\n" );
    46 	printf( "    -v Display version\n" );
    46 	printf( "\n" );
    47 	printf( "\n" );
    47 	return;
    48 	return;
    78 	return;
    79 	return;
    79 }
    80 }
    80 
    81 
    81 
    82 
    82 /*
    83 /*
    83  * Output to stdout for squid, unless the debug level is at or above 4.
    84  * Output to stdout for squid, unless the debug level is at or above 5.
    84  */
    85  */
    85 void
    86 void
    86 out( const char *str )
    87 out( const char *str )
    87 {
    88 {
    88 	if ( v.debugmode >= 4 ) return;
    89 	if ( v.debugmode >= 5 ) return;
    89 	fprintf( stdout, "%s", str );
    90 	fprintf( stdout, "%s", str );
    90 	return;
    91 	return;
    91 }
    92 }
    92 
    93 
    93 
    94 
   113 	return;
   114 	return;
   114 }
   115 }
   115 
   116 
   116 
   117 
   117 /*
   118 /*
       
   119  * Lowercase a string in place.
       
   120  *
       
   121  */
       
   122 void
       
   123 lowercase_str( char *str, unsigned short int len )
       
   124 {
       
   125 	unsigned short int i = 0;
       
   126 	char c;
       
   127 
       
   128 	for ( ; i < len; i++ ) {
       
   129 		c = str[i];
       
   130 		str[i] = tolower( c );
       
   131 	}
       
   132 
       
   133 	return;
       
   134 }
       
   135 
       
   136 
       
   137 /*
   118  * Append 'buf' to the end of 'line', a la strcat, except dynamically
   138  * Append 'buf' to the end of 'line', a la strcat, except dynamically
   119  * grow memory for the target string.
   139  * grow memory for the target string.
   120  *
   140  *
   121  * 'buf' should be null terminated.  Returns the modified line.
   141  * 'buf' should be null terminated.  Returns the modified line.
   122  */
   142  */
   140 
   160 
   141 	debug( 5, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset );
   161 	debug( 5, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset );
   142 	if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
   162 	if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
   143 		debug( 5, LOC, "Ignoring line %d, error while allocating memory: %s\n",
   163 		debug( 5, LOC, "Ignoring line %d, error while allocating memory: %s\n",
   144 				v.timer.lines+1, (line_realloc == NULL ? strerror(errno) : "Line too large") );
   164 				v.timer.lines+1, (line_realloc == NULL ? strerror(errno) : "Line too large") );
   145 		if ( line != NULL ) free( line ), line = NULL;
   165 		free( line ), line = NULL;
   146 		printf( "\n" );
   166 		printf( "\n" );
   147 	}
   167 	}
   148 	else {
   168 	else {
   149 		line = line_realloc;
   169 		line = line_realloc;
   150 		memcpy( line + offset, buf, LINE_BUFSIZE - 1 );
   170 		memcpy( line + offset, buf, LINE_BUFSIZE );
   151 	}
   171 	}
   152 
   172 
   153 	return( line );
   173 	return( line );
   154 }
   174 }
   155 
   175 
   220 /* 
   240 /* 
   221  * Allocate memory and copy +length+ bytes from the given dotted quad style
   241  * Allocate memory and copy +length+ bytes from the given dotted quad style
   222  * +ip_string+ into an in_addr struct, returning a pointer to it.
   242  * +ip_string+ into an in_addr struct, returning a pointer to it.
   223  * 
   243  * 
   224  */
   244  */
       
   245 /*
   225 struct in_addr *
   246 struct in_addr *
   226 copy_ipv4_token( char *ip_string, unsigned short int length )
   247 copy_ipv4_token( char *ip_string, unsigned short int length )
   227 {
   248 {
   228 	struct in_addr *alloc_ptr = NULL;
   249 	struct in_addr *alloc_ptr = NULL;
   229 	char c_ip[ INET_ADDRSTRLEN ];
   250 	char c_ip[ INET_ADDRSTRLEN ];
   244 		free( alloc_ptr ), alloc_ptr = NULL;
   265 		free( alloc_ptr ), alloc_ptr = NULL;
   245 	}
   266 	}
   246 
   267 
   247 	return( alloc_ptr );
   268 	return( alloc_ptr );
   248 }
   269 }
   249 
   270 */
   250 
   271 
   251 /*
   272 /*
   252  * Report how many lines were processed per second.
   273  * Report how many lines were processed per second.
   253  *
   274  *
   254  */
   275  */