190792Sgshapiro/*
2261363Sgshapiro * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
390792Sgshapiro *      All rights reserved.
490792Sgshapiro * Copyright (c) 1990, 1993
590792Sgshapiro *	The Regents of the University of California.  All rights reserved.
690792Sgshapiro *
790792Sgshapiro * This code is derived from software contributed to Berkeley by
890792Sgshapiro * Chris Torek.
990792Sgshapiro *
1090792Sgshapiro * By using this file, you agree to the terms and conditions set
1190792Sgshapiro * forth in the LICENSE file which can be found at the top level of
1290792Sgshapiro * the sendmail distribution.
1390792Sgshapiro */
1490792Sgshapiro
1590792Sgshapiro#include <sm/gen.h>
16266692SgshapiroSM_RCSID("@(#)$Id: fwrite.c,v 1.25 2013-11-22 20:51:43 ca Exp $")
1790792Sgshapiro#include <errno.h>
1890792Sgshapiro#include <sm/io.h>
1990792Sgshapiro#include <sm/assert.h>
2090792Sgshapiro#include "local.h"
2190792Sgshapiro#include "fvwrite.h"
2290792Sgshapiro
2390792Sgshapiro/*
2490792Sgshapiro**  SM_IO_WRITE -- write to a file pointer
2590792Sgshapiro**
2690792Sgshapiro**	Parameters:
2790792Sgshapiro**		fp -- file pointer writing to
2890792Sgshapiro**		timeout -- time to complete the write
2990792Sgshapiro**		buf -- location of data to be written
3090792Sgshapiro**		size -- number of bytes to be written
3190792Sgshapiro**
3290792Sgshapiro**	Result:
3390792Sgshapiro**		Failure: returns 0 _and_ sets errno
3490792Sgshapiro**		Success: returns >=0 with errno unchanged, where the
3590792Sgshapiro**			number returned is the number of bytes written.
3690792Sgshapiro*/
3790792Sgshapiro
3890792Sgshapirosize_t
3990792Sgshapirosm_io_write(fp, timeout, buf, size)
4090792Sgshapiro	SM_FILE_T *fp;
4190792Sgshapiro	int timeout;
4290792Sgshapiro	const void *buf;
4390792Sgshapiro	size_t size;
4490792Sgshapiro{
4590792Sgshapiro	struct sm_uio uio;
4690792Sgshapiro	struct sm_iov iov;
4790792Sgshapiro
4890792Sgshapiro	SM_REQUIRE_ISA(fp, SmFileMagic);
4990792Sgshapiro
5090792Sgshapiro	if (fp->f_write == NULL)
5190792Sgshapiro	{
5290792Sgshapiro		errno = ENODEV;
5390792Sgshapiro		return 0;
5490792Sgshapiro	}
5590792Sgshapiro
5690792Sgshapiro	iov.iov_base = (void *) buf;
5790792Sgshapiro	uio.uio_resid = iov.iov_len = size;
5890792Sgshapiro	uio.uio_iov = &iov;
5990792Sgshapiro	uio.uio_iovcnt = 1;
6090792Sgshapiro
6190792Sgshapiro	/* The usual case is success (sm_fvwrite returns 0) */
6290792Sgshapiro	if (sm_fvwrite(fp, timeout, &uio) == 0)
6390792Sgshapiro		return size;
6490792Sgshapiro
6590792Sgshapiro	/* else return number of bytes actually written */
6690792Sgshapiro	return size - uio.uio_resid;
6790792Sgshapiro}
68