@ -151,19 +151,23 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
char * opt_pos = find_option ( * buf , * buf_len , option ) ;
if ( opt_pos ) {
// Find option line end
char * line_end = strchr ( opt_pos , ' \n ' ) ;
if ( ! line_end ) line_end = * buf + * buf_len ;
// Determine if old line has trailing \n
char * nl = strchr ( opt_pos , ' \n ' ) ;
int has_trailing_nl = ( nl ! = NULL ) ;
char * line_end = nl ? nl : * buf + * buf_len ;
// Prepare new line
char new_line [ MAX_LINE_LEN ] ;
int new_len = snprintf ( new_line , sizeof ( new_line ) , " %s=%s \n " , option , value ) ;
int new_len ;
if ( has_trailing_nl ) {
new_len = snprintf ( new_line , sizeof ( new_line ) , " %s=%s \n " , option , value ) ;
} else {
new_len = snprintf ( new_line , sizeof ( new_line ) , " %s=%s " , option , value ) ;
}
if ( new_len < = 0 | | new_len > = ( int ) sizeof ( new_line ) ) return - 1 ;
// Calculate length difference
size_t old_line_len = line_end - opt_pos + 1 ;
size_t old_line_len = has_trailing_nl ? ( line_end - opt_pos + 1 ) : ( line_end - opt_pos ) ;
long len_diff = new_len - old_line_len ;
// Ensure buffer capacity
@ -177,9 +181,9 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
opt_pos = find_option ( * buf , * buf_len , option ) ;
if ( ! opt_pos ) return - 1 ;
line_end = strchr ( opt_pos , ' \n ' ) ;
if ( ! line_end ) line_end = * buf + * buf_len ;
nl = strchr ( opt_pos , ' \n ' ) ;
has_trailing_nl = ( nl ! = NULL ) ;
line_end = nl ? nl : * buf + * buf_len ;
}
// Move content and insert new line
@ -197,9 +201,13 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
// Find end of global section (next [ or end of buffer)
char * insert_pos = strchr ( section_end , ' [ ' ) ;
int need_leading_nl = 0 ;
if ( ! insert_pos ) insert_pos = * buf + * buf_len ;
else {
if ( ! insert_pos ) {
insert_pos = * buf + * buf_len ;
if ( * buf_len > 0 & & ( * buf ) [ * buf_len - 1 ] ! = ' \n ' )
need_leading_nl = 1 ;
} else {
// Move to beginning of next line
char * prev_nl = insert_pos ;
while ( prev_nl > * buf & & * prev_nl ! = ' \n ' ) prev_nl - - ;
@ -208,8 +216,11 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
// Prepare new line
char new_line [ MAX_LINE_LEN ] ;
int new_len = snprintf ( new_line , sizeof ( new_line ) , " %s=%s \n " , option , value ) ;
int new_len ;
if ( need_leading_nl )
new_len = snprintf ( new_line , sizeof ( new_line ) , " \n %s=%s \n " , option , value ) ;
else
new_len = snprintf ( new_line , sizeof ( new_line ) , " %s=%s \n " , option , value ) ;
if ( new_len < = 0 | | new_len > = ( int ) sizeof ( new_line ) ) return - 1 ;
// Ensure buffer capacity
@ -224,9 +235,13 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
if ( ! section_end ) return - 1 ;
insert_pos = strchr ( section_end , ' [ ' ) ;
need_leading_nl = 0 ;
if ( ! insert_pos ) insert_pos = * buf + * buf_len ;
else {
if ( ! insert_pos ) {
insert_pos = * buf + * buf_len ;
if ( * buf_len > 0 & & ( * buf ) [ * buf_len - 1 ] ! = ' \n ' )
need_leading_nl = 1 ;
} else {
char * prev_nl = insert_pos ;
while ( prev_nl > * buf & & * prev_nl ! = ' \n ' ) prev_nl - - ;
if ( * prev_nl = = ' \n ' ) insert_pos = prev_nl + 1 ;