util.c
changeset 2 8c88756f81b0
child 4 5701b7859a31
equal deleted inserted replaced
1:823d42546cea 2:8c88756f81b0
       
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
       
     2 /*
       
     3 Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
       
     4 All rights reserved.
       
     5 Redistribution and use in source and binary forms, with or without
       
     6 modification, are permitted provided that the following conditions are met:
       
     7 
       
     8     * Redistributions of source code must retain the above copyright
       
     9       notice, this list of conditions and the following disclaimer.
       
    10 
       
    11     * Redistributions in binary form must reproduce the above copyright
       
    12       notice, this list of conditions and the following disclaimer in the
       
    13       documentation and/or other materials provided with the distribution.
       
    14 
       
    15     * Neither the name of Mahlon E. Smith nor the names of his
       
    16       contributors may be used to endorse or promote products derived
       
    17       from this software without specific prior written permission.
       
    18 
       
    19 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
       
    20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
       
    23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29 */
       
    30 
       
    31 
       
    32 #include "volta.h"
       
    33 
       
    34 /*
       
    35  * Output basic usage information.
       
    36  */
       
    37 void
       
    38 usage( char *prg )
       
    39 {
       
    40 	printf( "%s [-vh] [-d <level>] [-a <init>]\n", prg );
       
    41 	printf( "    -v Display version\n" );
       
    42 	printf( "    -d <level> Show debug information on stderr\n" );
       
    43 	printf( "    -h Usage (you're lookin' at it)\n" );
       
    44 	printf( "    -a Perform an action, being one of:\n" );
       
    45 	printf( "         init: Initialize a new, empty database\n" );
       
    46 	printf( "         dump: DUMP IT\n" );
       
    47 	printf( "\n" );
       
    48 	return;
       
    49 }
       
    50 
       
    51 
       
    52 /*
       
    53  * Debug function, only output to stderr if the debug level is
       
    54  * equal or greater to the set output level.
       
    55  *
       
    56  * 	level: The minimum debug level that must be set for the 
       
    57  * 	       line to be logged.
       
    58  * 	file:  The current code file that is emitting the log
       
    59  * 	line:  The line number of the code file that is emitting the log
       
    60  * 	... :  any printf style strings and formats that constitute the log message
       
    61  */
       
    62 void
       
    63 debug( int level, char *file, int line, const char *fmt, ... )
       
    64 {
       
    65 	if ( debugmode < level ) return;
       
    66 
       
    67 	char timestamp[20];
       
    68 	time_t t = time( NULL );
       
    69 	struct tm *now = localtime( &t );
       
    70 	strftime( timestamp, 20, "%F %T", now );
       
    71 
       
    72 	va_list args;
       
    73 	va_start( args, fmt );
       
    74 	fprintf( stderr, "%s [%s] #%d (%s:%04d): ",
       
    75 			 PROG, timestamp, getpid(), file, line );
       
    76 	vfprintf( stderr, fmt, args );
       
    77 	va_end( args );
       
    78 
       
    79 	return;
       
    80 }
       
    81 
       
    82 
       
    83 /*
       
    84  * Append 'buf' to the end of 'line', a la strcat, except dynamically
       
    85  * grow memory for the target string.
       
    86  *
       
    87  * 'buf' should be null terminated.  Returns the modified line.
       
    88  */
       
    89 char *
       
    90 extend_line( char *line, const char *buf )
       
    91 {
       
    92 	char *line_realloc;
       
    93 
       
    94 	unsigned short int offset;
       
    95 	size_t new_len;
       
    96 
       
    97 	/* find offset and lengths, first assignment */
       
    98 	if ( line == NULL ) {
       
    99 		offset  = 0;
       
   100 		new_len = strlen( buf );
       
   101 	}
       
   102 	/* find offset and lengths, append to existing string */
       
   103 	else {
       
   104 		offset  = strlen( line ); /* not including '\0' */
       
   105 		new_len = offset + LINE_BUFSIZE;
       
   106 	}
       
   107 
       
   108 	debug( 3, LOC, "Extending line to %d bytes at offset %d...\n", new_len, offset );
       
   109 	if ( (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) {
       
   110 		/* cleanup on allocation errors */
       
   111 		debug( 3, LOC, "Ignoring line, error while allocating memory: %s\n", strerror(errno) );
       
   112 		if ( line != NULL ) free( line );
       
   113 		line = NULL;
       
   114 	}
       
   115 	else {
       
   116 		line = line_realloc;
       
   117 		memcpy( line + offset, buf, strlen(buf) );
       
   118 	}
       
   119 
       
   120 	return( line );
       
   121 }
       
   122