nightly: remove unused BINARCHIVE
[unleashed.git] / share / man / man2 / write.2
blob08c8a102f29c27820def4a6524fee702675fd61f
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2007, Sun Microsystems, Inc.  All Rights Reserved.
46 .\" Copyright (c) 2015, Joyent, Inc.  All rights reserved.
47 .\"
48 .TH WRITE 2 "Jan 15, 2015"
49 .SH NAME
50 write, pwrite, writev, pwritev \- write on a file
51 .SH SYNOPSIS
52 .LP
53 .nf
54 #include <unistd.h>
56 \fBssize_t\fR \fBwrite\fR(\fBint\fR \fIfildes\fR, \fBconst void *\fR\fIbuf\fR, \fBsize_t\fR \fInbyte\fR);
57 .fi
59 .LP
60 .nf
61 \fBssize_t\fR \fBpwrite\fR(\fBint\fR \fIfildes\fR, \fBconst void *\fR\fIbuf\fR, \fBsize_t\fR \fInbyte\fR,
62      \fBoff_t\fR \fIoffset\fR);
63 .fi
65 .LP
66 .nf
67 #include <sys/uio.h>
69 \fBssize_t\fR \fBwritev\fR(\fBint\fR \fIfildes\fR, \fBconst struct iovec *\fR\fIiov\fR, \fBint\fR \fIiovcnt\fR);
70 .fi
71 .LP
72 .nf
73 \fBssize_t\fR \fBpwritev\fR(\fBint\fR \fIfildes\fR, \fBconst struct iovec *\fR\fIiov\fR, \fBint\fR \fIiovcnt\fR, \fBoff_t\fR \fIoffset\fR);
74 .fi
76 .SH DESCRIPTION
77 .LP
78 The \fBwrite()\fR function attempts to write \fInbyte\fR bytes from the buffer
79 pointed to by \fIbuf\fR to the file associated with the open file descriptor,
80 \fIfildes\fR.
81 .sp
82 .LP
83 If \fInbyte\fR is 0, \fBwrite()\fR will return 0 and have no other results if
84 the file is a regular file; otherwise, the results are unspecified.
85 .sp
86 .LP
87 On a regular file or other file capable of seeking, the actual writing of data
88 proceeds from the position in the file indicated by the file offset associated
89 with \fIfildes\fR. Before successful return from \fBwrite()\fR, the file offset
90 is incremented by the number of bytes actually written. On a regular file, if
91 this incremented file offset is greater than the length of the file, the length
92 of the file will be set to this file offset.
93 .sp
94 .LP
95 If the \fBO_SYNC\fR bit has been set, write I/O operations on the file
96 descriptor complete as defined by synchronized I/O file integrity completion.
97 .sp
98 .LP
99 If \fIfildes\fR refers to a socket, \fBwrite()\fR is equivalent to
100 \fBsend\fR(3SOCKET) with no flags set.
103 On a file not capable of seeking, writing always takes place starting at the
104 current position.  The value of a file offset associated with such a device is
105 undefined.
108 If the \fBO_APPEND\fR flag of the file status flags is set, the file offset
109 will be set to the end of the file prior to each write and no intervening file
110 modification operation will occur between changing the file offset and the
111 write operation.
114 For regular files, no data transfer will occur past the offset maximum
115 established in the open file description with \fIfildes\fR.
118 A \fBwrite()\fR to a regular file is blocked if mandatory file/record locking
119 is set (see \fBchmod\fR(2)), and there is a record lock owned by another
120 process  on the segment of the file to be written:
121 .RS +4
123 .ie t \(bu
124 .el o
125 If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set, \fBwrite()\fR returns \fB\(mi1\fR
126 and sets \fBerrno\fR to \fBEAGAIN\fR.
128 .RS +4
130 .ie t \(bu
131 .el o
132 If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are clear, \fBwrite()\fR sleeps until
133 all blocking locks are removed or the \fBwrite()\fR is terminated by a signal.
137 If a  \fBwrite()\fR requests that more bytes be written than there is room
138 for\(emfor example, if the write would exceed the process file size limit (see
139 \fBgetrlimit\fR(2) and \fBulimit\fR(2)), the system file size limit, or the
140 free space on the device\(emonly as many bytes as there is room for will be
141 written. For example, suppose there is space for 20 bytes more in a file before
142 reaching a limit. A \fBwrite()\fR of 512-bytes returns 20. The next
143 \fBwrite()\fR of a non-zero number of bytes gives a failure return (except as
144 noted for pipes and FIFO below).
147 If \fBwrite()\fR is interrupted by a signal before it writes any data, it will
148 return \(mi1 with \fBerrno\fR set to \fBEINTR\fR.
151 If \fBwrite()\fR is interrupted by a signal after it successfully writes some
152 data, it will return the number of bytes written.
155 If \fBwrite()\fR exceeds the process file size limit, the application generates
156 a \fBSIGXFSZ\fR signal, whose default behavior is to dump core.
159 After a \fBwrite()\fR to a regular file has successfully returned:
160 .RS +4
162 .ie t \(bu
163 .el o
164 Any successful \fBread\fR(2) from each byte position in the file that was
165 modified by that write will return the data specified by the \fBwrite()\fR for
166 that position until such byte positions are again modified.
168 .RS +4
170 .ie t \(bu
171 .el o
172 Any subsequent successful \fBwrite()\fR to the same byte position in the file
173 will overwrite that file data.
177 Write requests to a pipe or FIFO are handled the same as a regular file with
178 the following exceptions:
179 .RS +4
181 .ie t \(bu
182 .el o
183 There is no file offset associated with a pipe, hence each write request
184 appends to the end of the pipe.
186 .RS +4
188 .ie t \(bu
189 .el o
190 Write requests of \fB{PIPE_BUF}\fR bytes or less are guaranteed not to be
191 interleaved with data from other processes doing writes on the same pipe.
192 Writes of greater than \fB{PIPE_BUF}\fR bytes may have data interleaved, on
193 arbitrary boundaries, with writes by other processes, whether or not the
194 \fBO_NONBLOCK\fR or \fBO_NDELAY\fR flags are set.
196 .RS +4
198 .ie t \(bu
199 .el o
200 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, a write request may cause the
201 process to block, but on normal completion it returns \fInbyte\fR.
203 .RS +4
205 .ie t \(bu
206 .el o
207 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are set, \fBwrite()\fR does not block
208 the process. If a \fBwrite()\fR request for \fBPIPE_BUF\fR or fewer bytes
209 succeeds completely  \fBwrite()\fR returns \fInbyte\fR. Otherwise, if
210 \fBO_NONBLOCK\fR is set, it returns \fB\(mi1\fR and sets \fBerrno\fR to
211 \fBEAGAIN\fR or if \fBO_NDELAY\fR is set, it returns \fB0\fR.  A \fBwrite()\fR
212 request for greater than \fB{PIPE_BUF}\fR bytes  transfers what it can and
213 returns the number of bytes written or it transfers no data and, if
214 \fBO_NONBLOCK\fR is set, returns \fB\(mi1\fR with \fBerrno\fR set to
215 \fBEAGAIN\fR or if \fBO_NDELAY\fR is set, it returns \fB0\fR.  Finally, if a
216 request is greater than \fBPIPE_BUF\fR bytes and all data previously written to
217 the pipe has been read, \fBwrite()\fR transfers at least \fBPIPE_BUF\fR bytes.
221 When attempting to write to a file descriptor (other than a pipe, a FIFO, a
222 socket, or a stream) that supports nonblocking writes and cannot accept the
223 data immediately:
224 .RS +4
226 .ie t \(bu
227 .el o
228 If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, \fBwrite()\fR blocks until
229 the data can be accepted.
231 .RS +4
233 .ie t \(bu
234 .el o
235 If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, \fBwrite()\fR does not block the
236 process. If some data can be written without blocking the process,
237 \fBwrite()\fR writes what it can and returns the number of bytes written.
238 Otherwise, if \fBO_NONBLOCK\fR is set, it returns \fB\(mi1\fR  and sets
239 \fBerrno\fR to \fBEAGAIN\fR or if \fBO_NDELAY\fR is set,  it returns \fB0\fR.
243 Upon successful completion, where \fInbyte\fR is greater than 0, \fBwrite()\fR
244 will mark for update the \fBst_ctime\fR and \fBst_mtime\fR fields of the file,
245 and if the file is a regular file, the \fBS_ISUID\fR and \fBS_ISGID\fR bits of
246 the file mode may be cleared.
249 For streams files (see \fBIntro\fR(2) and \fBstreamio\fR(7I)), the operation of
250 \fBwrite()\fR is determined by the values of the minimum and maximum
251 \fInbyte\fR range ("packet size") accepted by the stream. These values are
252 contained in the topmost stream module, and can not be set or tested from user
253 level. If \fInbyte\fR falls within the packet size range, \fInbyte\fR bytes are
254 written. If \fInbyte\fR does not fall within the range and the minimum packet
255 size value  is zero, \fBwrite()\fR breaks the buffer into maximum packet size
256 segments prior  to sending the data downstream (the last segment may be smaller
257 than the maximum  packet size). If \fInbyte\fR does not fall within the range
258 and the minimum value is non-zero, \fBwrite()\fR fails and sets \fBerrno\fR to
259 \fBERANGE\fR. Writing a zero-length buffer (\fInbyte\fR is zero) to a streams
260 device sends  a zero length message with zero returned. However, writing a
261 zero-length buffer to a pipe or FIFO sends no message  and zero is returned.
262 The user program may issue the \fBI_SWROPT\fR \fBioctl\fR(2) to enable
263 zero-length messages to be sent across the pipe or FIFO (see
264 \fBstreamio\fR(7I)).
267 When writing to a stream, data messages are created with a priority band of
268 zero. When writing to a socket or to a stream that is not a pipe or a FIFO:
269 .RS +4
271 .ie t \(bu
272 .el o
273 If \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are not set, and the stream cannot
274 accept data (the stream write queue is full due to internal flow control
275 conditions), \fBwrite()\fR blocks until data can be accepted.
277 .RS +4
279 .ie t \(bu
280 .el o
281 If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set and the stream cannot accept data,
282 \fBwrite()\fR returns \fB-1\fR and sets \fBerrno\fR to \fBEAGAIN\fR.
284 .RS +4
286 .ie t \(bu
287 .el o
288 If \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is set and part of the buffer has already
289 been written when a condition occurs in which the stream cannot accept
290 additional data, \fBwrite()\fR terminates and returns the number of bytes
291 written.
295 The \fBwrite()\fR and \fBwritev()\fR functions will fail if the stream head had
296 processed an asynchronous error before the call.  In this case, the value of
297 \fBerrno\fR does not reflect the result of \fBwrite()\fR or \fBwritev()\fR but
298 reflects the prior error.
299 .SS "\fBpwrite()\fR"
301 The \fBpwrite()\fR function is equivalent to \fBwrite()\fR, except that it
302 writes into a given position and does not change the file offset (regardless of
303 whether \fBO_APPEND\fR is set). The first three arguments to \fBpwrite()\fR are
304 the same as \fBwrite()\fR, with the addition of a fourth argument \fIoffset\fR
305 for the desired position inside the file.
306 .SS "\fBwritev()\fR"
308 The \fBwritev()\fR function performs the same action as \fBwrite()\fR, but
309 gathers the output data from the \fIiovcnt\fR buffers specified by the members
310 of the \fIiov\fR array: \fIiov\fR[0], \fIiov\fR[1], \&.\|.\|.,
311 \fIiov\fR[\fIiovcnt\fR\|\(mi\|1]. The \fIiovcnt\fR buffer is valid if greater
312 than 0 and less than or equal to \fB{IOV_MAX}\fR. See \fBIntro\fR(2) for a
313 definition of \fB{IOV_MAX}\fR.
315 .SS "\fBpwritev()\fR"
317 The \fBpwritev()\fR function is equivalent to \fBwritev()\fR, except that it
318 writes into a given position and does not change the file offset (regardless of
319 whether \fBO_APPEND\fR is set). The first three arguments to \fBpwritev()\fR are
320 the same as \fBwritev()\fR, with the addition of a fourth argument \fIoffset\fR
321 for the desired position inside the file.
323 The \fBiovec\fR structure contains the following members:
325 .in +2
327 caddr_t  iov_base;
328 int      iov_len;
330 .in -2
334 Each \fBiovec\fR entry specifies the base address and length of an area in
335 memory from which data should be written.  The \fBwritev()\fR function always
336 writes all data from an area before proceeding to the next.
339 If \fIfildes\fR refers to a regular file and all of the \fBiov_len\fR members
340 in the array pointed to by \fIiov\fR are 0, \fBwritev()\fR will return 0 and
341 have no other effect.  For other file types, the behavior is unspecified.
344 If the sum of the \fBiov_len\fR values is greater than \fBSSIZE_MAX\fR, the
345 operation fails and no data is transferred.
346 .SH RETURN VALUES
348 Upon successful completion, \fBwrite()\fR returns the number of bytes actually
349 written to the file associated with \fIfildes\fR. This number is never greater
350 than \fInbyte\fR. Otherwise, \fB\(mi1\fR is returned, the file-pointer remains
351 unchanged, and \fBerrno\fR is set to indicate the error.
354 Upon successful completion, \fBwritev()\fR returns the number of bytes actually
355 written.  Otherwise, it returns  \fB\(mi1\fR, the file-pointer remains
356 unchanged, and \fBerrno\fR is set to indicate an error.
357 .SH ERRORS
359 The \fBwrite()\fR, \fBpwrite()\fR, \fBwritev()\fR, and \fBpwritev()\fR
360 functions will fail if:
362 .ne 2
364 \fB\fBEAGAIN\fR\fR
366 .RS 11n
367 Mandatory file/record locking is set, \fBO_NDELAY\fR or \fBO_NONBLOCK\fR is
368 set, and there is a blocking record lock; an attempt is made to write to a
369 stream that can not accept data with the \fBO_NDELAY\fR or \fBO_NONBLOCK\fR
370 flag set; or a write to a pipe or FIFO of \fBPIPE_BUF\fR bytes or less is
371 requested and less than \fInbytes\fR of free space is available.
375 .ne 2
377 \fB\fBEBADF\fR\fR
379 .RS 11n
380 The \fIfildes\fR argument is not a valid file descriptor open for writing.
384 .ne 2
386 \fB\fBECONNRESET\fR\fR
388 .RS 11n
389 The \fIfiledes\fR argument refers to a connection oriented socket and the
390 connection was forcibly closed by the peer and is no longer valid.  I/O can no
391 longer be performed to \fIfiledes\fR.
395 .ne 2
397 \fB\fBEDEADLK\fR\fR
399 .RS 11n
400 The write was going to go to sleep  and cause a deadlock situation to occur.
404 .ne 2
406 \fB\fBEDQUOT\fR\fR
408 .RS 11n
409 The user's quota of disk blocks on the file system containing the file has been
410 exhausted.
414 .ne 2
416 \fB\fBEFBIG\fR\fR
418 .RS 11n
419 An attempt is made to write a file that exceeds the process's file size limit
420 or the maximum file size (see \fBgetrlimit\fR(2) and \fBulimit\fR(2)).
424 .ne 2
426 \fB\fBEFBIG\fR\fR
428 .RS 11n
429 The file is a regular file, \fInbyte\fR is greater than 0, and the starting
430 position is greater than or equal to the offset maximum established in the file
431 description associated with \fIfildes\fR.
435 .ne 2
437 \fB\fBEINTR\fR\fR
439 .RS 11n
440 A signal was caught during the write  operation and no data was transferred.
444 .ne 2
446 \fB\fBEIO\fR\fR
448 .RS 11n
449 The process is in the background and is attempting to write to its controlling
450 terminal whose \fBTOSTOP\fR flag is set, or the process is  neither ignoring
451 nor blocking \fBSIGTTOU\fR signals and the process  group of the process is
452 orphaned.
456 .ne 2
458 \fB\fBENOLCK\fR\fR
460 .RS 11n
461 Enforced record locking was enabled and \fB{LOCK_MAX}\fR regions  are already
462 locked in the system, or the system record lock table was full and the write
463 could not go to sleep  until the blocking record lock was removed.
467 .ne 2
469 \fB\fBENOLINK\fR\fR
471 .RS 11n
472 The \fIfildes\fR argument is on a remote machine and the link to that machine
473 is no longer active.
477 .ne 2
479 \fB\fBENOSPC\fR\fR
481 .RS 11n
482 During a write to an ordinary file, there is no  free space left on the device.
486 .ne 2
488 \fB\fBENOSR\fR\fR
490 .RS 11n
491 An attempt is made to write to a streams with insufficient streams memory
492 resources  available in the system.
496 .ne 2
498 \fB\fBENXIO\fR\fR
500 .RS 11n
501 A hangup occurred on the stream being written to.
505 .ne 2
507 \fB\fBEPIPE\fR\fR
509 .RS 11n
510 An attempt is made to write to a pipe or a \fBFIFO\fR that is not open for
511 reading by any process, or that has only one end open (or to a file descriptor
512 created by \fBsocket\fR(3SOCKET), using type \fBSOCK_STREAM\fR that is no
513 longer connected to a peer endpoint). A \fBSIGPIPE\fR signal will also be sent
514 to the thread. The process dies unless special provisions were taken to catch
515 or ignore the signal.
519 .ne 2
521 \fB\fBERANGE\fR\fR
523 .RS 11n
524 The transfer request size was outside the range supported by the streams file
525 associated with \fIfildes\fR.
530 The \fBwrite()\fR and \fBpwrite()\fR functions will fail if:
532 .ne 2
534 \fB\fBEFAULT\fR\fR
536 .RS 10n
537 The \fIbuf\fR argument points to an illegal address.
541 .ne 2
543 \fB\fBEINVAL\fR\fR
545 .RS 10n
546 The \fInbyte\fR argument overflowed an \fBssize_t\fR.
551 The \fBpwrite()\fR and \fBpwritev()\fR functions fail and the file pointer
552 remains unchanged if:
554 .ne 2
556 \fB\fBESPIPE\fR\fR
558 .RS 10n
559 The \fIfildes\fR argument is associated with a pipe or FIFO.
564 The \fBwrite()\fR and \fBwritev()\fR functions may fail if:
566 .ne 2
568 \fB\fBEINVAL\fR\fR
570 .RS 10n
571 The stream or multiplexer referenced by \fIfildes\fR is linked (directly or
572 indirectly) downstream from a multiplexer.
576 .ne 2
578 \fB\fBENXIO\fR\fR
580 .RS 10n
581 A request was made of a non-existent device, or the request was outside the
582 capabilities of the device.
586 .ne 2
588 \fB\fBENXIO\fR\fR
590 .RS 10n
591 A hangup occurred on the stream being written to.
596 A write to a streams file may fail if an error message has been received at the
597 stream head.  In this case, \fBerrno\fR is set to the value included in the
598 error message.
601 The \fBwritev()\fR and \fBpwritev()\fR functions may fail if:
603 .ne 2
605 \fB\fBEINVAL\fR\fR
607 .RS 10n
608 The \fIiovcnt\fR argument was less than or equal to 0 or greater than
609 {\fBIOV_MAX\fR}; one of the \fBiov_len\fR values in the \fIiov\fR array was
610 negative; or the sum of the \fBiov_len\fR values in the \fIiov\fR array
611 overflowed an \fBssize_t\fR.
614 .SH USAGE
616 The \fBpwrite()\fR function has a transitional interface for 64-bit file
617 offsets.  See \fBlf64\fR(5).
618 .SH ATTRIBUTES
620 See \fBattributes\fR(5) for descriptions of the following attributes:
625 box;
626 c | c
627 l | l .
628 ATTRIBUTE TYPE  ATTRIBUTE VALUE
630 Interface Stability     Committed
632 MT-Level        \fBwrite()\fR is Async-Signal-Safe
634 Standard        See \fBstandards\fR(5).
637 .SH SEE ALSO
639 \fBIntro\fR(2), \fBchmod\fR(2), \fBcreat\fR(2), \fBdup\fR(2), \fBfcntl\fR(2),
640 \fBgetrlimit\fR(2), \fBioctl\fR(2), \fBlseek\fR(2), \fBopen\fR(2),
641 \fBpipe\fR(2), \fBulimit\fR(2), \fBsend\fR(3SOCKET), \fBsocket\fR(3SOCKET),
642 \fBattributes\fR(5), \fBlf64\fR(5), \fBstandards\fR(5), \fBstreamio\fR(7I)