util.c
changeset 10 d07309450285
parent 5 7718f04c8cd1
child 12 191b3c25974a
equal deleted inserted replaced
9:bdf20e6eefd7 10:d07309450285
    29 */
    29 */
    30 
    30 
    31 
    31 
    32 #include "volta.h"
    32 #include "volta.h"
    33 
    33 
       
    34 
    34 /*
    35 /*
    35  * Output basic usage information.
    36  * Output basic usage information.
    36  */
    37  */
    37 void
    38 void
    38 usage( char *prg )
    39 usage( char *prg )
    67 	struct tm *now = localtime( &t );
    68 	struct tm *now = localtime( &t );
    68 	strftime( timestamp, 20, "%F %T", now );
    69 	strftime( timestamp, 20, "%F %T", now );
    69 
    70 
    70 	va_list args;
    71 	va_list args;
    71 	va_start( args, fmt );
    72 	va_start( args, fmt );
    72 	fprintf( stderr, "%s [%s] #%d (%s:%04d): ",
    73 	fprintf( stderr, "%s [%s] #%d %d (%s:%d): ",
    73 			 PROG, timestamp, getpid(), file, line );
    74 			 PROG, timestamp, getpid(), level, file, line );
    74 	vfprintf( stderr, fmt, args );
    75 	vfprintf( stderr, fmt, args );
    75 	va_end( args );
    76 	va_end( args );
    76 
    77 
    77 	return;
    78 	return;
    78 }
    79 }
    86  */
    87  */
    87 char *
    88 char *
    88 extend_line( char *line, const char *buf )
    89 extend_line( char *line, const char *buf )
    89 {
    90 {
    90 	char *line_realloc;
    91 	char *line_realloc;
    91 
       
    92 	unsigned short int offset;
    92 	unsigned short int offset;
    93 	size_t new_len;
    93 	size_t new_len;
    94 
    94 
    95 	/* find offset and lengths, first assignment */
    95 	/* find offset and lengths, first assignment */
    96 	if ( line == NULL ) {
    96 	if ( line == NULL ) {
   101 	else {
   101 	else {
   102 		offset  = strlen( line ); /* not including '\0' */
   102 		offset  = strlen( line ); /* not including '\0' */
   103 		new_len = offset + LINE_BUFSIZE;
   103 		new_len = offset + LINE_BUFSIZE;
   104 	}
   104 	}
   105 
   105 
   106 	debug( 3, LOC, "Extending line to %d bytes at offset %d...\n", new_len, offset );
   106 	debug( 4, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset );
   107 	if ( (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
   107 	if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
   108 		/* cleanup on allocation errors */
   108 		debug( 1, LOC, "Ignoring line, error while allocating memory: %s\n",
   109 		debug( 3, LOC, "Ignoring line, error while allocating memory: %s\n", strerror(errno) );
   109 				(line_realloc == NULL ? strerror(errno) : "Line too large") );
   110 		if ( line != NULL ) free( line );
   110 		if ( line != NULL ) free( line ), line = NULL;
   111 		line = NULL;
   111 		printf( "\n" );
   112 	}
   112 	}
   113 	else {
   113 	else {
   114 		line = line_realloc;
   114 		line = line_realloc;
   115 		memcpy( line + offset, buf, LINE_BUFSIZE - 1 );
   115 		memcpy( line + offset, buf, LINE_BUFSIZE - 1 );
   116 	}
   116 	}
   118 	return( line );
   118 	return( line );
   119 }
   119 }
   120 
   120 
   121 
   121 
   122 /*
   122 /*
   123  * Read an entire file into memory, returning a pointer the contents.
   123  * Read an entire file into memory, returning a pointer to the contents.
   124  * Returns NULL on error.
   124  * Returns NULL on error.
   125  *
   125  *
   126  */
   126  */
   127 char *
   127 char *
   128 slurp_file( char *file )
   128 slurp_file( char *file )
   147 		debug( 1, LOC, "Could not open file for reading '%s': %s\n",
   147 		debug( 1, LOC, "Could not open file for reading '%s': %s\n",
   148 				file, strerror(errno) );
   148 				file, strerror(errno) );
   149 		return( NULL );
   149 		return( NULL );
   150 	}
   150 	}
   151 
   151 
   152 	if ( fread( contents, sizeof(char), sb.st_size, fh ) != sb.st_size ) {
   152 	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 );
   153 		debug( 1, LOC, "Short read for file '%s'?: %s\n", file );
   154 		fclose( fh );
   154 		fclose( fh );
   155 		return( NULL );
   155 		return( NULL );
   156 	}
   156 	}
   157 
   157 
   158 	fclose( fh );
   158 	fclose( fh );
   159 	return( contents );
   159 	return( contents );
   160 }
   160 }
   161 
   161 
   162 
   162 
   163 
   163 /* 
       
   164  * Allocate memory and copy +length+ bytes (plus 1 for null) from the given
       
   165  * +string+ into a new string, returning a pointer to it.
       
   166  * 
       
   167  */
       
   168 char *
       
   169 copy_string_token( char *string, unsigned short int length )
       
   170 {
       
   171 	char *alloc_ptr = NULL;
       
   172 	if ( string == NULL || length == 0 ) return ( NULL );
       
   173 
       
   174 	if ( (alloc_ptr = calloc( length + 1, sizeof(char) )) == NULL ) {
       
   175 		debug( 1, LOC, "Unable to allocate memory for token: %s\n", strerror(errno) );
       
   176 		return( NULL );
       
   177 	}
       
   178 
       
   179 	(void)memcpy( alloc_ptr, string, length );
       
   180 
       
   181 	return( alloc_ptr );
       
   182 }
       
   183 
       
   184 
       
   185 /* 
       
   186  * Allocate memory and copy +length+ bytes from the given dotted quad style
       
   187  * +ip_string+ into an in_addr struct, returning a pointer to it.
       
   188  * 
       
   189  */
       
   190 struct in_addr *
       
   191 copy_ipv4_token( char *ip_string, unsigned short int length )
       
   192 {
       
   193 	struct in_addr *alloc_ptr = NULL;
       
   194 	char c_ip[ INET_ADDRSTRLEN ];
       
   195 
       
   196 	if ( ip_string == NULL ) return ( NULL );
       
   197 
       
   198 	(void)strncpy( c_ip, ip_string, length );
       
   199 	c_ip[ length ] = '\0';
       
   200 
       
   201 	if ( (alloc_ptr = calloc( length, sizeof(struct in_addr) )) == NULL ) {
       
   202 		debug( 1, LOC, "Unable to allocate memory for ip '%s': %s\n",
       
   203 				c_ip, strerror(errno) );
       
   204 	}
       
   205 
       
   206 	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",
       
   208 				c_ip, strerror(errno) );
       
   209 		free( alloc_ptr ), alloc_ptr = NULL;
       
   210 	}
       
   211 
       
   212 	return( alloc_ptr );
       
   213 }
       
   214 
       
   215 
       
   216 /*
       
   217  * Report how many lines were processed per second.
       
   218  *
       
   219  */
       
   220 void
       
   221 report_speed( void )
       
   222 {
       
   223 	if ( v.debugmode < 3 ) return;
       
   224 
       
   225 	time_t end_time = time( NULL );
       
   226 	double elapsed  = difftime( end_time, v.timer.start );
       
   227 
       
   228 	if ( elapsed > 0 )  {
       
   229 		debug( 3, LOC, "Processed %lu lines in %0.1f seconds. (%0.1f lines/sec)\n",
       
   230 				v.timer.lines, elapsed, v.timer.lines/elapsed );
       
   231 	}
       
   232 	else {
       
   233 		debug( 3, LOC, "Processed %lu lines in under a second.\n", v.timer.lines );
       
   234 	}
       
   235 
       
   236 	return;
       
   237 }
       
   238