24 lines
552 B
C++
24 lines
552 B
C++
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include "swrite.h"
|
|
|
|
int swrite( int fd, const char *str, ssize_t len )
|
|
{
|
|
ssize_t total_bytes_written = 0;
|
|
ssize_t bytes_to_write = ( len >= 0 ) ? len : strlen( str );
|
|
while ( total_bytes_written < bytes_to_write ) {
|
|
ssize_t bytes_written = write( fd, str + total_bytes_written,
|
|
bytes_to_write - total_bytes_written );
|
|
if ( bytes_written <= 0 ) {
|
|
perror( "write" );
|
|
return -1;
|
|
} else {
|
|
total_bytes_written += bytes_written;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|