xref: /netbsd-src/sys/lib/libsa/write.c (revision db898c555af51fb73d5d1db5e435a7fc6f3784b4)
1*db898c55Stsutsui /*	$NetBSD: write.c,v 1.15 2007/12/02 04:59:26 tsutsui Exp $	*/
26668f51cScgd 
336b52a82Sbrezak /*-
436b52a82Sbrezak  * Copyright (c) 1993
536b52a82Sbrezak  *	The Regents of the University of California.  All rights reserved.
636b52a82Sbrezak  *
736b52a82Sbrezak  * This code is derived from software contributed to Berkeley by
836b52a82Sbrezak  * The Mach Operating System project at Carnegie-Mellon University.
936b52a82Sbrezak  *
1036b52a82Sbrezak  * Redistribution and use in source and binary forms, with or without
1136b52a82Sbrezak  * modification, are permitted provided that the following conditions
1236b52a82Sbrezak  * are met:
1336b52a82Sbrezak  * 1. Redistributions of source code must retain the above copyright
1436b52a82Sbrezak  *    notice, this list of conditions and the following disclaimer.
1536b52a82Sbrezak  * 2. Redistributions in binary form must reproduce the above copyright
1636b52a82Sbrezak  *    notice, this list of conditions and the following disclaimer in the
1736b52a82Sbrezak  *    documentation and/or other materials provided with the distribution.
18aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
1936b52a82Sbrezak  *    may be used to endorse or promote products derived from this software
2036b52a82Sbrezak  *    without specific prior written permission.
2136b52a82Sbrezak  *
2236b52a82Sbrezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2336b52a82Sbrezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2436b52a82Sbrezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2536b52a82Sbrezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2636b52a82Sbrezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2736b52a82Sbrezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2836b52a82Sbrezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2936b52a82Sbrezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3036b52a82Sbrezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3136b52a82Sbrezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3236b52a82Sbrezak  * SUCH DAMAGE.
3336b52a82Sbrezak  *
346668f51cScgd  *	@(#)write.c	8.1 (Berkeley) 6/11/93
3536b52a82Sbrezak  *
3636b52a82Sbrezak  *
3736b52a82Sbrezak  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
3836b52a82Sbrezak  * All Rights Reserved.
3936b52a82Sbrezak  *
4036b52a82Sbrezak  * Author: Alessandro Forin
4136b52a82Sbrezak  *
4236b52a82Sbrezak  * Permission to use, copy, modify and distribute this software and its
4336b52a82Sbrezak  * documentation is hereby granted, provided that both the copyright
4436b52a82Sbrezak  * notice and this permission notice appear in all copies of the
4536b52a82Sbrezak  * software, derivative works or modified versions, and any portions
4636b52a82Sbrezak  * thereof, and that both notices appear in supporting documentation.
4736b52a82Sbrezak  *
4836b52a82Sbrezak  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
4936b52a82Sbrezak  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
5036b52a82Sbrezak  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5136b52a82Sbrezak  *
5236b52a82Sbrezak  * Carnegie Mellon requests users of this software to return to
5336b52a82Sbrezak  *
5436b52a82Sbrezak  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5536b52a82Sbrezak  *  School of Computer Science
5636b52a82Sbrezak  *  Carnegie Mellon University
5736b52a82Sbrezak  *  Pittsburgh PA 15213-3890
5836b52a82Sbrezak  *
5936b52a82Sbrezak  * any improvements or extensions that they make and grant Carnegie the
6036b52a82Sbrezak  * rights to redistribute these changes.
6136b52a82Sbrezak  */
6236b52a82Sbrezak 
63b461ef1dSpk #include <sys/param.h>
6436b52a82Sbrezak #include "stand.h"
6536b52a82Sbrezak 
6684c517c1Spk ssize_t
write(int fd,const void * destp,size_t bcount)671c038e68Sisaki write(int fd, const void *destp, size_t bcount)
6836b52a82Sbrezak {
691279e67bSaugustss 	struct open_file *f = &files[fd];
7084c517c1Spk 	size_t resid;
71a6d61f67Schristos 	void *dest = __UNCONST(destp);
7236b52a82Sbrezak 
7330921347Scgd #if !defined(LIBSA_NO_FD_CHECKING)
74*db898c55Stsutsui 	if ((unsigned int)fd >= SOPEN_MAX || !(f->f_flags & F_WRITE)) {
7536b52a82Sbrezak 		errno = EBADF;
761c038e68Sisaki 		return -1;
7736b52a82Sbrezak 	}
7830921347Scgd #endif
7930921347Scgd #if !defined(LIBSA_NO_RAW_ACCESS)
8036b52a82Sbrezak 	if (f->f_flags & F_RAW) {
8130921347Scgd #if !defined(LIBSA_NO_TWIDDLE)
822d7464c4Smycroft 		twiddle();
8330921347Scgd #endif
8430921347Scgd 		errno = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_WRITE,
85b461ef1dSpk 			btodb(f->f_offset), bcount, dest, &resid);
8636b52a82Sbrezak 		if (errno)
871c038e68Sisaki 			return -1;
8868f420b5Spk 		f->f_offset += resid;
891c038e68Sisaki 		return resid;
9036b52a82Sbrezak 	}
9130921347Scgd #endif
9236b52a82Sbrezak 	resid = bcount;
9330921347Scgd 	if ((errno = FS_WRITE(f->f_ops)(f, dest, bcount, &resid)))
941c038e68Sisaki 		return -1;
951c038e68Sisaki 	return 0;
9636b52a82Sbrezak }
97