1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00

fputs() and puts() now share the same common code. Added abort checks in order to avoid turning the memcpy() operations into an uninterruptable sequence.

This commit is contained in:
Olaf Barthel
2023-09-06 13:24:15 +02:00
parent 45d118101a
commit a7389454bb
2 changed files with 135 additions and 109 deletions

View File

@@ -44,11 +44,7 @@
int
puts(const char *s)
{
FILE * stream = stdout;
struct iob * file = (struct iob *)stream;
int result = EOF;
int buffer_mode;
int c;
ENTER();
@@ -56,11 +52,6 @@ puts(const char *s)
assert( s != NULL );
if(__check_abort_enabled)
__check_abort();
flockfile(stream);
#if defined(CHECK_FOR_NULL_POINTERS)
{
if(s == NULL)
@@ -71,58 +62,13 @@ puts(const char *s)
}
#endif /* CHECK_FOR_NULL_POINTERS */
assert( __is_valid_iob(file) );
assert( FLAG_IS_SET(file->iob_Flags,IOBF_IN_USE) );
assert( file->iob_BufferSize > 0 );
/* If buffering is disabled for an interactive stream
switch to line buffering to improve the readability of
the output: instead of pumping out the entire buffer
break it up into individual lines. */
buffer_mode = (file->iob_Flags & IOBF_BUFFER_MODE);
if(buffer_mode == IOBF_BUFFER_MODE_NONE)
{
struct fd * fd = __fd[file->iob_Descriptor];
__fd_lock(fd);
if(FLAG_IS_SET(fd->fd_Flags,FDF_IS_INTERACTIVE))
buffer_mode = IOBF_BUFFER_MODE_LINE;
__fd_unlock(fd);
}
if(__fputc_check(stream) < 0)
goto out;
while((c = (*s++)) != '\0')
{
if(__putc(c,stream,buffer_mode) == EOF)
goto out;
}
if(__putc('\n',stream,buffer_mode) == EOF)
if (__fputs(s, '\n', stdout) == EOF)
goto out;
result = OK;
out:
/* Note: if buffering is disabled for this stream, then we still
may have buffered data around, queued to be printed right now.
This is intended to improve performance as it takes more effort
to write a single character to a file than to write a bunch. */
if(result == 0 && (file->iob_Flags & IOBF_BUFFER_MODE) == IOBF_BUFFER_MODE_NONE)
{
if(__iob_write_buffer_is_valid(file) && __flush_iob_write_buffer(file) < 0)
{
SHOWMSG("couldn't flush the write buffer");
result = EOF;
}
}
funlockfile(stream);
RETURN(result);
return(result);
}