1 /* 2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * By using this file, you agree to the terms and conditions set 11 * forth in the LICENSE file which can be found at the top level of 12 * the sendmail distribution. 13 */ 14 15 #pragma ident "%Z%%M% %I% %E% SMI" 16 17 #include <sm/gen.h> 18 SM_RCSID("@(#)$Id: fwrite.c,v 1.22 2001/04/03 01:46:40 ca Exp $") 19 #include <errno.h> 20 #include <sm/io.h> 21 #include <sm/assert.h> 22 #include "local.h" 23 #include "fvwrite.h" 24 25 /* 26 ** SM_IO_WRITE -- write to a file pointer 27 ** 28 ** Parameters: 29 ** fp -- file pointer writing to 30 ** timeout -- time to complete the write 31 ** buf -- location of data to be written 32 ** size -- number of bytes to be written 33 ** 34 ** Result: 35 ** Failure: returns 0 _and_ sets errno 36 ** Success: returns >=0 with errno unchanged, where the 37 ** number returned is the number of bytes written. 38 */ 39 40 size_t 41 sm_io_write(fp, timeout, buf, size) 42 SM_FILE_T *fp; 43 int timeout; 44 const void *buf; 45 size_t size; 46 { 47 struct sm_uio uio; 48 struct sm_iov iov; 49 50 SM_REQUIRE_ISA(fp, SmFileMagic); 51 52 if (fp->f_write == NULL) 53 { 54 errno = ENODEV; 55 return 0; 56 } 57 58 iov.iov_base = (void *) buf; 59 uio.uio_resid = iov.iov_len = size; 60 uio.uio_iov = &iov; 61 uio.uio_iovcnt = 1; 62 63 /* The usual case is success (sm_fvwrite returns 0) */ 64 if (sm_fvwrite(fp, timeout, &uio) == 0) 65 return size; 66 67 /* else return number of bytes actually written */ 68 return size - uio.uio_resid; 69 } 70