return to first page linux journal archive
keywordscontents

Listing 3. Handling Short Writes

/*
 * Listing 3:
 * Handling short writes
 * Ivan Griffin (ivan.griffin@ul.ie)
 */
int bytesToSend = 0,
    bytesWritten = 0,
    num = 0;

/*
 * somewhere here bytesToSend, buffer, and 
 * fileDesc must be set up.
 */

for (bytesWritten = 0; bytesWritten < bytesToSend;
     bytesWritten += num)
{
    num = write(fileDesc,
        (void *)( (char *)buffer + 
		(char *)bytesWritten ),
        bytesToSend - bytesWritten);

    if (num < 0)
    {
        perror("write()");

        if (errno != EINTR)
        {
            exit(1);
        }
    }
}