util.c
changeset 4 5701b7859a31
parent 2 8c88756f81b0
child 5 7718f04c8cd1
equal deleted inserted replaced
3:97f767832c52 4:5701b7859a31
    35  * Output basic usage information.
    35  * Output basic usage information.
    36  */
    36  */
    37 void
    37 void
    38 usage( char *prg )
    38 usage( char *prg )
    39 {
    39 {
    40 	printf( "%s [-vh] [-d <level>] [-a <init>]\n", prg );
    40 	printf( "%s [-vh] [-f <filename>] [-d <level>]\n", prg );
    41 	printf( "    -v Display version\n" );
    41 	printf( "    -v Display version\n" );
    42 	printf( "    -d <level> Show debug information on stderr\n" );
    42 	printf( "    -d <level> Show debug information on stderr\n" );
    43 	printf( "    -h Usage (you're lookin' at it)\n" );
    43 	printf( "    -h Usage (you're lookin' at it)\n" );
    44 	printf( "    -a Perform an action, being one of:\n" );
    44 	printf( "    -f <filename> Specify the database to use (default is 'volta.db' in the cwd.)\n");
    45 	printf( "         init: Initialize a new, empty database\n" );
       
    46 	printf( "         dump: DUMP IT\n" );
       
    47 	printf( "\n" );
    45 	printf( "\n" );
    48 	return;
    46 	return;
    49 }
    47 }
    50 
    48 
    51 
    49 
    60  * 	... :  any printf style strings and formats that constitute the log message
    58  * 	... :  any printf style strings and formats that constitute the log message
    61  */
    59  */
    62 void
    60 void
    63 debug( int level, char *file, int line, const char *fmt, ... )
    61 debug( int level, char *file, int line, const char *fmt, ... )
    64 {
    62 {
    65 	if ( debugmode < level ) return;
    63 	if ( v.debugmode < level ) return;
    66 
    64 
    67 	char timestamp[20];
    65 	char timestamp[20];
    68 	time_t t = time( NULL );
    66 	time_t t = time( NULL );
    69 	struct tm *now = localtime( &t );
    67 	struct tm *now = localtime( &t );
    70 	strftime( timestamp, 20, "%F %T", now );
    68 	strftime( timestamp, 20, "%F %T", now );
   112 		if ( line != NULL ) free( line );
   110 		if ( line != NULL ) free( line );
   113 		line = NULL;
   111 		line = NULL;
   114 	}
   112 	}
   115 	else {
   113 	else {
   116 		line = line_realloc;
   114 		line = line_realloc;
   117 		memcpy( line + offset, buf, strlen(buf) );
   115 		memcpy( line + offset, buf, LINE_BUFSIZE - 1 );
   118 	}
   116 	}
   119 
   117 
   120 	return( line );
   118 	return( line );
   121 }
   119 }
   122 
   120 
       
   121 
       
   122 /*
       
   123  * Read an entire file into memory, returning a pointer the contents.
       
   124  * Returns NULL on error.
       
   125  *
       
   126  */
       
   127 char *
       
   128 slurp_file( char *file )
       
   129 {
       
   130 	FILE *fh       = NULL;
       
   131 	char *contents = NULL;
       
   132 	struct stat sb;
       
   133 
       
   134 	if ( stat( file, &sb ) != 0 ) {
       
   135 		debug( 1, LOC, "Unable to stat() file '%s': %s\n",
       
   136 				file, strerror(errno) );
       
   137 		return( NULL );
       
   138 	}
       
   139 
       
   140 	if ( (contents = malloc( sb.st_size + 1 )) == NULL ) {
       
   141 		debug( 1, LOC, "Unable to allocate memory for file '%s': %s\n",
       
   142 				file, strerror(errno) );
       
   143 		return( NULL );
       
   144 	}
       
   145 
       
   146 	if ( (fh = fopen( file, "r" )) == NULL ) {
       
   147 		debug( 1, LOC, "Could not open file for reading '%s': %s\n",
       
   148 				file, strerror(errno) );
       
   149 		return( NULL );
       
   150 	}
       
   151 
       
   152 	fread( contents, sizeof(char), sb.st_size, fh );
       
   153 	fclose( fh );
       
   154 
       
   155 	return( contents );
       
   156 }
       
   157 
       
   158 
       
   159