@@ -110,7 +110,9 @@ static const char* Pointer;
110
110
[[ noreturn ]] static void cleanUp (int exitCode )
111
111
{
112
112
if (InsertFile ) {
113
- fclose (InsertFile );
113
+ if (InsertFile != stdin ) {
114
+ fclose (InsertFile );
115
+ }
114
116
InsertFile = nullptr ;
115
117
}
116
118
@@ -162,7 +164,9 @@ static long long countLines(FILE* inputFile)
162
164
lines ++ ;
163
165
line = Buffer ;
164
166
}
165
- rewind (inputFile );
167
+ if (inputFile != stdin ) {
168
+ rewind (inputFile );
169
+ }
166
170
167
171
return lines ;
168
172
}
@@ -171,8 +175,6 @@ static long long countLines(FILE* inputFile)
171
175
// ###### Write contents of insert file #####################################
172
176
static void copyInsertFileIntoOutputFile ()
173
177
{
174
- rewind (InsertFile );
175
-
176
178
char buffer [65536 ];
177
179
ssize_t bytesRead ;
178
180
while ( (bytesRead = fread ((char * )& buffer , 1 , sizeof (buffer ), InsertFile )) > 0 ) {
@@ -184,6 +186,9 @@ static void copyInsertFileIntoOutputFile()
184
186
fputs ("\n" , stderr );
185
187
cleanUp (1 );
186
188
}
189
+ if (InsertFile != stdin ) {
190
+ rewind (InsertFile );
191
+ }
187
192
}
188
193
189
194
@@ -594,6 +599,9 @@ int main (int argc, char** argv)
594
599
595
600
// ====== Open input file ================================================
596
601
InputFile = stdin ;
602
+ if ( (InputFileName != nullptr ) && (strcmp (InputFileName , "-" ) == 0 ) ) {
603
+ InputFileName = nullptr ; // Special case: - => stdin
604
+ }
597
605
if (InputFileName != nullptr ) {
598
606
InputFile = fopen (InputFileName , "r" );
599
607
if (InputFile == nullptr ) {
@@ -633,6 +641,9 @@ int main (int argc, char** argv)
633
641
634
642
// ====== Open output file ===============================================
635
643
OutputFile = stdout ;
644
+ if ( (OutputFileName != nullptr ) && (strcmp (OutputFileName , "-" ) == 0 ) ) {
645
+ OutputFileName = nullptr ; // Special case: - => stdout
646
+ }
636
647
if (OutputFileName != nullptr ) {
637
648
OutputFile = fopen (OutputFileName , (OpenOutputAppend == false) ? "w" : "a" );
638
649
if (OutputFile == nullptr ) {
@@ -648,6 +659,15 @@ int main (int argc, char** argv)
648
659
}
649
660
650
661
// ====== Open insert file ===============================================
662
+ if ( (InsertFileName != nullptr ) && (strcmp (InsertFileName , "-" ) == 0 ) ) {
663
+ InsertFileName = nullptr ; // Special case: - => stdin
664
+ if (InputFile == stdin ) {
665
+ fputs (gettext ("ERROR: Insert from stdin requires an input file!" ), stderr );
666
+ fputs ("\n" , stderr );
667
+ cleanUp (1 );
668
+ }
669
+ InsertFile = stdin ;
670
+ }
651
671
if (InsertFileName != nullptr ) {
652
672
InsertFile = fopen (InsertFileName , "r" );
653
673
if (InsertFile == nullptr ) {
@@ -660,7 +680,7 @@ int main (int argc, char** argv)
660
680
posix_fadvise (fileno (InputFile ), 0 , 0 , POSIX_FADV_SEQUENTIAL |POSIX_FADV_WILLNEED );
661
681
#endif
662
682
}
663
- else {
683
+ if ( InputFile == nullptr ) {
664
684
if ( (Mode == InsertFront ) || (Mode == InsertBack ) || (Mode == Replace ) ) {
665
685
fputs (gettext ("ERROR: No insert file provided!" ), stderr );
666
686
fputs ("\n" , stderr );
0 commit comments