--- a/util.c Wed Sep 28 09:04:16 2011 -0700
+++ b/util.c Mon Oct 17 09:12:00 2011 -0700
@@ -31,6 +31,7 @@
#include "volta.h"
+
/*
* Output basic usage information.
*/
@@ -69,8 +70,8 @@
va_list args;
va_start( args, fmt );
- fprintf( stderr, "%s [%s] #%d (%s:%04d): ",
- PROG, timestamp, getpid(), file, line );
+ fprintf( stderr, "%s [%s] #%d %d (%s:%d): ",
+ PROG, timestamp, getpid(), level, file, line );
vfprintf( stderr, fmt, args );
va_end( args );
@@ -88,7 +89,6 @@
extend_line( char *line, const char *buf )
{
char *line_realloc;
-
unsigned short int offset;
size_t new_len;
@@ -103,12 +103,12 @@
new_len = offset + LINE_BUFSIZE;
}
- debug( 3, LOC, "Extending line to %d bytes at offset %d...\n", new_len, offset );
- if ( (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
- /* cleanup on allocation errors */
- debug( 3, LOC, "Ignoring line, error while allocating memory: %s\n", strerror(errno) );
- if ( line != NULL ) free( line );
- line = NULL;
+ debug( 4, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset );
+ if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
+ debug( 1, LOC, "Ignoring line, error while allocating memory: %s\n",
+ (line_realloc == NULL ? strerror(errno) : "Line too large") );
+ if ( line != NULL ) free( line ), line = NULL;
+ printf( "\n" );
}
else {
line = line_realloc;
@@ -120,7 +120,7 @@
/*
- * Read an entire file into memory, returning a pointer the contents.
+ * Read an entire file into memory, returning a pointer to the contents.
* Returns NULL on error.
*
*/
@@ -149,7 +149,7 @@
return( NULL );
}
- if ( fread( contents, sizeof(char), sb.st_size, fh ) != sb.st_size ) {
+ if ( fread( contents, sizeof(char), sb.st_size, fh ) != (unsigned int)sb.st_size ) {
debug( 1, LOC, "Short read for file '%s'?: %s\n", file );
fclose( fh );
return( NULL );
@@ -160,4 +160,79 @@
}
+/*
+ * Allocate memory and copy +length+ bytes (plus 1 for null) from the given
+ * +string+ into a new string, returning a pointer to it.
+ *
+ */
+char *
+copy_string_token( char *string, unsigned short int length )
+{
+ char *alloc_ptr = NULL;
+ if ( string == NULL || length == 0 ) return ( NULL );
+ if ( (alloc_ptr = calloc( length + 1, sizeof(char) )) == NULL ) {
+ debug( 1, LOC, "Unable to allocate memory for token: %s\n", strerror(errno) );
+ return( NULL );
+ }
+
+ (void)memcpy( alloc_ptr, string, length );
+
+ return( alloc_ptr );
+}
+
+
+/*
+ * Allocate memory and copy +length+ bytes from the given dotted quad style
+ * +ip_string+ into an in_addr struct, returning a pointer to it.
+ *
+ */
+struct in_addr *
+copy_ipv4_token( char *ip_string, unsigned short int length )
+{
+ struct in_addr *alloc_ptr = NULL;
+ char c_ip[ INET_ADDRSTRLEN ];
+
+ if ( ip_string == NULL ) return ( NULL );
+
+ (void)strncpy( c_ip, ip_string, length );
+ c_ip[ length ] = '\0';
+
+ if ( (alloc_ptr = calloc( length, sizeof(struct in_addr) )) == NULL ) {
+ debug( 1, LOC, "Unable to allocate memory for ip '%s': %s\n",
+ c_ip, strerror(errno) );
+ }
+
+ if ( inet_pton( AF_INET, c_ip, alloc_ptr ) < 1 ) {
+ debug( 1, LOC, "Unable to create in_addr struct for client ip '%s': %s\n",
+ c_ip, strerror(errno) );
+ free( alloc_ptr ), alloc_ptr = NULL;
+ }
+
+ return( alloc_ptr );
+}
+
+
+/*
+ * Report how many lines were processed per second.
+ *
+ */
+void
+report_speed( void )
+{
+ if ( v.debugmode < 3 ) return;
+
+ time_t end_time = time( NULL );
+ double elapsed = difftime( end_time, v.timer.start );
+
+ if ( elapsed > 0 ) {
+ debug( 3, LOC, "Processed %lu lines in %0.1f seconds. (%0.1f lines/sec)\n",
+ v.timer.lines, elapsed, v.timer.lines/elapsed );
+ }
+ else {
+ debug( 3, LOC, "Processed %lu lines in under a second.\n", v.timer.lines );
+ }
+
+ return;
+}
+