common/writen.c

///////////////////////////////////////////////////////////////////////////////
// Filename: writen.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: simulate normal I/O write() for sockets, pipes,... 
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/03/12 01:11:10 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>



// Local headers ///////////////////////////////////////////// Local headers //
    /* NONE */



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //

/*

Taken from [Stev92], page 406: (code from page 408)

------------------------------------------------------------------------------
Some devices, notably terminals, networks, and any SVR4 streams devices,
have the following two properties.

 1. A "read" operation may return less than asked for, even though we have not
    encountered the end of file. This is not an error, and we should just
    continue reading from the device.

 2. A "write" operation cam also return less than we specified. This may be
    caused by flow control constraints by downstream modules, for example.
    Again, it's not an error, and we should continue writing the reminder of
    the data. (Normally this short return from "write" only occurs with a 
    nonblocking descriptor if a signal is caught.)
------------------------------------------------------------------------------

*/

// normal write arguments

int 
writen(int fd, char* ptr, int numbytes)
{
    int nleft, nwritten;

    nleft = numbytes;
    while (nleft > 0)
    {
        nwritten = write(fd, ptr, nleft);
        if (nwritten <= 0)
            return(nwritten);   /* error */

        nleft -= nwritten;
        ptr   += nwritten;
    }
    return(numbytes - nleft);
}



// Main /////////////////////////////////////////////////////////////// Main //
    /* NONE */