char * util_alloc_envvar( const char * value ) { if (value == NULL) return NULL; else { buffer_type * buffer = buffer_alloc( 1024 ); /* Start by filling up a buffer instance with the current content of @value. */ buffer_fwrite_char_ptr( buffer , value ); buffer_fwrite_char( buffer , '\0' ); buffer_rewind( buffer ); while (true) { if (buffer_strchr( buffer , '$')) { const char * data = buffer_get_data( buffer ); int offset = buffer_get_offset( buffer ) + 1; /* Points at the first character following the '$' */ int var_length = 0; /* Find the length of the variable name */ while (true) { char c; c = data[offset + var_length]; if (!(isalnum( c ) || c == '_')) /* Any character which is NOT in the set [a-Z,0-9_] marks the end of the variable. */ break; if (c == '\0') /* The end of the string. */ break; var_length += 1; } { char * var_name = util_alloc_substring_copy( data , offset - 1 , var_length + 1); /* Include the leading $ */ const char * var_value = getenv( &var_name[1] ); if (var_value != NULL) buffer_search_replace( buffer , var_name , var_value); /* The actual string replacement. */ else buffer_fseek( buffer , var_length , SEEK_CUR ); /* The variable is not defined, and we leave the $name. */ free( var_name ); } } else break; /* No more $ to replace */ } buffer_shrink_to_fit( buffer ); { char * expanded_value = buffer_get_data( buffer ); buffer_free_container( buffer ); return expanded_value; } } }
char * subst_list_alloc_string_representation( const subst_list_type * subst_list ) { int size = subst_list_get_size( subst_list ); char * return_string = NULL; if (size > 0) { buffer_type * buffer = buffer_alloc( 512 ); int i; for (i=0; i < size; i++) { buffer_fwrite_char_ptr( buffer , subst_list_iget_key( subst_list , i)); buffer_fwrite_char(buffer , '='); buffer_fwrite_char_ptr( buffer , subst_list_iget_value( subst_list , i)); if (i < (size - 1)) buffer_fwrite_char_ptr( buffer , ", "); } buffer_fwrite_char( buffer , '\0'); buffer_shrink_to_fit( buffer ); return_string = buffer_get_data( buffer ); buffer_free_container( buffer ); } return return_string; }
void subst_list_filter_file(const subst_list_type * subst_list , const char * src_file , const char * target_file) { char * backup_file = NULL; buffer_type * buffer = buffer_fread_alloc( src_file ); buffer_fseek( buffer , 0 , SEEK_END ); /* Ensure that the buffer is a \0 terminated string. */ buffer_fwrite_char( buffer , '\0'); /*****************************************************************/ /* Backup file ?? */ if (util_same_file(src_file , target_file)) { char * backup_prefix = util_alloc_sprintf("%s-%s" , src_file , __func__); backup_file = util_alloc_tmp_file("/tmp" , backup_prefix , false); free(backup_prefix); } /* Writing backup file */ if (backup_file != NULL) { FILE * stream = util_fopen(backup_file , "w"); buffer_stream_fwrite_n( buffer , 0 , -1 , stream ); /* -1: Do not write the trailing \0. */ fclose(stream); } /* Doing the actual update */ subst_list_update_buffer(subst_list , buffer); /* Writing updated file */ { FILE * stream = util_mkdir_fopen(target_file , "w"); buffer_stream_fwrite_n( buffer , 0 , -1 , stream ); /* -1: Do not write the trailing \0. */ fclose(stream); } /* OK - all went hunka dory - unlink the backup file and leave the building. */ if (backup_file != NULL) { remove( backup_file ); free( backup_file ); } free(buffer); }
/** Will append a \0 to the buffer; before appending the last character of the buffer will be checked, and no new \0 will be added if the buffer is already \0 terminated. */ void buffer_terminate_char_ptr( buffer_type * buffer ) { if (buffer->data[ buffer->content_size - 1] != '\0') buffer_fwrite_char( buffer , '\0'); }