xref: /illumos-gate/usr/src/boot/libsa/write.c (revision 22028508)
1199767f8SToomas Soome /*	$NetBSD: write.c,v 1.7 1996/06/21 20:29:30 pk Exp $	*/
2199767f8SToomas Soome 
3692bf522SToomas Soome /*
4199767f8SToomas Soome  * Copyright (c) 1993
5199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * This code is derived from software contributed to Berkeley by
8199767f8SToomas Soome  * The Mach Operating System project at Carnegie-Mellon University.
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
18692bf522SToomas Soome  * 3. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
20199767f8SToomas Soome  *    without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome  * SUCH DAMAGE.
33199767f8SToomas Soome  *
34199767f8SToomas Soome  *	@(#)write.c	8.1 (Berkeley) 6/11/93
35692bf522SToomas Soome  *
36199767f8SToomas Soome  *
37199767f8SToomas Soome  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38199767f8SToomas Soome  * All Rights Reserved.
39199767f8SToomas Soome  *
40199767f8SToomas Soome  * Author: Alessandro Forin
41692bf522SToomas Soome  *
42199767f8SToomas Soome  * Permission to use, copy, modify and distribute this software and its
43199767f8SToomas Soome  * documentation is hereby granted, provided that both the copyright
44199767f8SToomas Soome  * notice and this permission notice appear in all copies of the
45199767f8SToomas Soome  * software, derivative works or modified versions, and any portions
46199767f8SToomas Soome  * thereof, and that both notices appear in supporting documentation.
47692bf522SToomas Soome  *
48199767f8SToomas Soome  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49199767f8SToomas Soome  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50199767f8SToomas Soome  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51692bf522SToomas Soome  *
52199767f8SToomas Soome  * Carnegie Mellon requests users of this software to return to
53692bf522SToomas Soome  *
54199767f8SToomas Soome  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55199767f8SToomas Soome  *  School of Computer Science
56199767f8SToomas Soome  *  Carnegie Mellon University
57199767f8SToomas Soome  *  Pittsburgh PA 15213-3890
58692bf522SToomas Soome  *
59199767f8SToomas Soome  * any improvements or extensions that they make and grant Carnegie the
60199767f8SToomas Soome  * rights to redistribute these changes.
61199767f8SToomas Soome  */
62199767f8SToomas Soome 
63199767f8SToomas Soome #include <sys/cdefs.h>
64199767f8SToomas Soome 
65199767f8SToomas Soome #include <sys/param.h>
66199767f8SToomas Soome #include "stand.h"
67199767f8SToomas Soome 
68199767f8SToomas Soome ssize_t
write(int fd,const void * dest,size_t bcount)69cf837ed8SToomas Soome write(int fd, const void *dest, size_t bcount)
70199767f8SToomas Soome {
71a2027c5dSToomas Soome 	struct open_file *f;
72199767f8SToomas Soome 	size_t resid;
73199767f8SToomas Soome 
74a2027c5dSToomas Soome 	f = fd2open_file(fd);
75a2027c5dSToomas Soome 	if (f == NULL || !(f->f_flags & F_WRITE)) {
76199767f8SToomas Soome 		errno = EBADF;
77199767f8SToomas Soome 		return (-1);
78199767f8SToomas Soome 	}
79199767f8SToomas Soome 	if (f->f_flags & F_RAW) {
80199767f8SToomas Soome 		twiddle(4);
81199767f8SToomas Soome 		errno = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
82cf837ed8SToomas Soome 		    btodb(f->f_offset), bcount, __DECONST(void *, dest),
83cf837ed8SToomas Soome 		    &resid);
84199767f8SToomas Soome 		if (errno)
85199767f8SToomas Soome 			return (-1);
86199767f8SToomas Soome 		f->f_offset += resid;
87199767f8SToomas Soome 		return (resid);
88199767f8SToomas Soome 	}
89199767f8SToomas Soome 	resid = bcount;
90199767f8SToomas Soome 	if ((errno = (f->f_ops->fo_write)(f, dest, bcount, &resid)))
91199767f8SToomas Soome 		return (-1);
92199767f8SToomas Soome 	return (bcount - resid);
93199767f8SToomas Soome }
94