@@ -527,7 +527,9 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
527527
528528 xfile_t file ;
529529 xbool_t bDirty = XFALSE ;
530+
530531 char sLine [XLINE_MAX ];
532+ size_t nLinesProcessed = 0 ;
531533
532534 xbyte_buffer_t buffer ;
533535 XByteBuffer_Init (& buffer , XLINE_MAX , XSTDNON );
@@ -541,7 +543,7 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
541543 {
542544 xbool_t bDropLine = XFALSE ;
543545 xbool_t bModified = XFALSE ;
544- int nFirstSpace = XSTDERR ;
546+ int nFirstSpace = -1 ;
545547 int nPosit = 0 ;
546548
547549 while (nPosit < nLength )
@@ -559,7 +561,8 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
559561 if (nFirstSpace >= 0 && nFirstSpace < nLength )
560562 {
561563 nNextLength = XFile_GetLine (& file , sNextLine , sizeof (sNextLine ));
562- bDropLine = !nFirstSpace && nNextLength <= 0 ? XTRUE : XFALSE ;
564+ bDropLine = !nFirstSpace && nNextLength <= 0 && nLinesProcessed &&
565+ sLine [nLength - 1 ] != '\n' ? XTRUE : XFALSE ;
563566
564567 if (nFirstSpace == 0 ||
565568 nNextLength <= 0 ||
@@ -579,31 +582,33 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
579582 XByteBuffer_Clear , & buffer , XFile_Close , & file , XSTDERR );
580583 }
581584
582- if (pArgs -> bFinalNewline && nNextLength <= 0 &&
583- nLength > 0 && sLine [nLength - 1 ] != '\n' )
584- {
585- XASSERT_CALL2 ((XByteBuffer_Add (& buffer , (const uint8_t * )"\n" , 1 ) > 0 ),
586- XByteBuffer_Clear , & buffer , XFile_Close , & file , XSTDERR );
587-
588- bDirty = XTRUE ;
589- }
590-
591585 if (bModified )
592586 {
593- xstrncpy (sLine , sizeof (sLine ), sNextLine );
587+ if (nNextLength > 0 )
588+ xstrncpy (sLine , sizeof (sLine ), sNextLine );
589+
594590 nLength = nNextLength ;
591+ nLinesProcessed ++ ;
595592 continue ;
596593 }
597594
598595 nLength = XFile_GetLine (& file , sLine , sizeof (sLine ));
596+ nLinesProcessed ++ ;
599597 }
600598
601599 // Finished reading
602600 XFile_Close (& file );
603601
604- if (!bDirty )
602+ if (pArgs -> bFinalNewline && (!buffer .nUsed || buffer .pData [buffer .nUsed - 1 ] != '\n' ))
603+ {
604+ XASSERT_CALL ((XByteBuffer_Add (& buffer , (const uint8_t * )"\n" , 1 ) > 0 ),
605+ XByteBuffer_Clear , & buffer , XSTDERR );
606+
607+ bDirty = XTRUE ;
608+ }
609+
610+ if (!bDirty || !buffer .nUsed )
605611 {
606- // No need to change anything
607612 XByteBuffer_Clear (& buffer );
608613 return XSTDNON ;
609614 }
@@ -616,7 +621,7 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
616621
617622 if (XPath_Exists (sEntryBackup ))
618623 {
619- if (XFile_Open (& file , sEntry , "cwt" , NULL ) < 0 )
624+ if (XFile_OpenM (& file , sEntry , "cwt" , pEntry -> nMode ) < 0 )
620625 {
621626 xloge ("Failed to open file: %s (%s)" , sEntry , XSTRERR );
622627 rename (sEntryBackup , sEntry );
@@ -636,7 +641,6 @@ int XSearch_StripEmptySpaces(xsearch_args_t *pArgs, xsearch_entry_t *pEntry)
636641
637642 XFile_Close (& file );
638643 unlink (sEntryBackup );
639- chmod (sEntry , pEntry -> nMode );
640644 }
641645
642646 XByteBuffer_Clear (& buffer );
0 commit comments