xref: /freebsd/contrib/sendmail/libsm/fwrite.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*
2  * Copyright (c) 2000-2001 Proofpoint, 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 #include <sm/gen.h>
16 SM_RCSID("@(#)$Id: fwrite.c,v 1.25 2013-11-22 20:51:43 ca Exp $")
17 #include <errno.h>
18 #include <sm/io.h>
19 #include <sm/assert.h>
20 #include "local.h"
21 #include "fvwrite.h"
22 
23 /*
24 **  SM_IO_WRITE -- write to a file pointer
25 **
26 **	Parameters:
27 **		fp -- file pointer writing to
28 **		timeout -- time to complete the write
29 **		buf -- location of data to be written
30 **		size -- number of bytes to be written
31 **
32 **	Result:
33 **		Failure: returns 0 _and_ sets errno
34 **		Success: returns >=0 with errno unchanged, where the
35 **			number returned is the number of bytes written.
36 */
37 
38 size_t
39 sm_io_write(fp, timeout, buf, size)
40 	SM_FILE_T *fp;
41 	int timeout;
42 	const void *buf;
43 	size_t size;
44 {
45 	struct sm_uio uio;
46 	struct sm_iov iov;
47 
48 	SM_REQUIRE_ISA(fp, SmFileMagic);
49 
50 	if (fp->f_write == NULL)
51 	{
52 		errno = ENODEV;
53 		return 0;
54 	}
55 
56 	iov.iov_base = (void *) buf;
57 	uio.uio_resid = iov.iov_len = size;
58 	uio.uio_iov = &iov;
59 	uio.uio_iovcnt = 1;
60 
61 	/* The usual case is success (sm_fvwrite returns 0) */
62 	if (sm_fvwrite(fp, timeout, &uio) == 0)
63 		return size;
64 
65 	/* else return number of bytes actually written */
66 	return size - uio.uio_resid;
67 }
68