yuuji@0: /* ======================================================================== yuuji@0: * Copyright 1988-2006 University of Washington yuuji@0: * yuuji@0: * Licensed under the Apache License, Version 2.0 (the "License"); yuuji@0: * you may not use this file except in compliance with the License. yuuji@0: * You may obtain a copy of the License at yuuji@0: * yuuji@0: * http://www.apache.org/licenses/LICENSE-2.0 yuuji@0: * yuuji@0: * yuuji@0: * ======================================================================== yuuji@0: */ yuuji@0: yuuji@0: /* yuuji@0: * Program: Write data, treating partial writes as an error yuuji@0: * yuuji@0: * Author: Mark Crispin yuuji@0: * Networks and Distributed Computing yuuji@0: * Computing & Communications yuuji@0: * University of Washington yuuji@0: * Administration Building, AG-44 yuuji@0: * Seattle, WA 98195 yuuji@0: * Internet: MRC@CAC.Washington.EDU yuuji@0: * yuuji@0: * Date: 26 May 1995 yuuji@0: * Last Edited: 30 August 2006 yuuji@0: */ yuuji@0: yuuji@0: /* The whole purpose of this unfortunate routine is to deal with DOS and yuuji@0: * certain cretinous versions of UNIX which decided that the "bytes actually yuuji@0: * written" return value from write() gave them license to use that for things yuuji@0: * that are really errors, such as disk quota exceeded, maximum file size yuuji@0: * exceeded, disk full, etc. yuuji@0: * yuuji@0: * BSD won't screw us this way on the local filesystem, but who knows what yuuji@0: * some NFS-mounted filesystem will do. yuuji@0: */ yuuji@0: yuuji@0: #undef write yuuji@0: yuuji@0: /* Write data to file yuuji@0: * Accepts: file descriptor yuuji@0: * I/O vector structure yuuji@0: * number of vectors in structure yuuji@0: * Returns: number of bytes written if successful, -1 if failure yuuji@0: */ yuuji@0: yuuji@0: long maxposint = (long)((((unsigned long) 1) << ((sizeof(int) * 8) - 1)) - 1); yuuji@0: yuuji@0: long safe_write (int fd,char *buf,long nbytes) yuuji@0: { yuuji@0: long i,j; yuuji@0: if (nbytes > 0) for (i = nbytes; i; i -= j,buf += j) { yuuji@0: while (((j = write (fd,buf,(int) min (maxposint,i))) < 0) && yuuji@0: (errno == EINTR)); yuuji@0: if (j < 0) return j; yuuji@0: } yuuji@0: return nbytes; yuuji@0: }