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