United States-English |
|
|
HP-UX Reference > Wwrite(2)HP-UX 11i Version 3: February 2007 |
|
NAMEwrite, writev, pwrite — write on a file SYNOPSIS#include <unistd.h> ssize_t write(int fildes, const void *buf, size_t nbyte); ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset); #include <sys/uio.h> ssize_t writev(int fildes, const struct iovec *iov, int iovcnt); DESCRIPTIONThe write() function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes. If nbyte is 0, write() will return 0 and have no other results if the file is a regular file. Otherwise, the results are unspecified. On a regular file or other file capable of seeking, the actual writing of data proceeds from the position in the file indicated by the file offset associated with fildes. Before successful return from write(), the file offset is incremented by the number of bytes actually written. On a regular file, if this incremented file offset is greater than the length of the file, the length of the file will be set to this file offset. For ordinary files, if the O_DSYNC file status flag is set, the write does not return until both the file data and the file attributes required to retrieve the data are physically updated. If the O_SYNC flag is set, the behavior is identical to that of O_DSYNC, with the addition that all file attributes changed by the write operation, including access time, modification time and status change time, are also physically updated before returning to the calling process. For block special files, if the O_DSYNC or the O_SYNC flag is set, the write does not return until the data is physically updated. How the data reaches the physical media is implementation and hardware dependent. A write to an ordinary file is prevented if enforcement-mode file and record locking is set, another process owns a lock on the segment of the file being written, and the following apply:
If the O_APPEND flag of the file status flags is set, the file offset will be set to the end of the file prior to each write and no intervening file modification operation will occur between changing the file offset and the write operation. If a write() requests that more bytes be written than there is room for, for example, the ulimit or the physical end of a medium, only as many bytes as there is room for will be written. For example, suppose there is space for 20 bytes more in a file before reaching a limit. A write of 512 bytes will return 20. The next write of a non-zero number of bytes will give a failure return (except as noted below) and the implementation will generate a SIGXFSZ signal for the process. If write() is interrupted by a signal before it writes any data, it will return -1 with errno set to EINTR. If write() is interrupted by a signal after it successfully writes some data, it will return the number of bytes written. If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation dependent. After a write() to a regular file has successfully returned:
Write requests to a pipe or FIFO will be handled the same as a regular file with the following exceptions:
When writing a pipe with the O_NDELAY or O_NONBLOCK file status flag set, the following apply:
When writing a pipe and the O_NDELAY and O_NONBLOCK file status flags are clear, the write() always executes correctly (blocking as necessary), and returns the number of bytes written. When attempting to write to a file descriptor (other than a pipe or FIFO) that supports non-blocking writes and cannot accept the data immediately, the following apply:
Upon successful completion, where nbyte is greater than 0, write() will mark for update the st_ctime and st_mtime fields of the file, and if the file is a regular file, the S_ISUID and S_ISGID bits of the file mode may be cleared. For character special devices, if the stopio() call was used on the same device after it was opened, write() returns -1, sets errno to EBADF, and issues the SIGHUP signal to the process. write() also clears the potential and granted privilege vectors on the file. If fildes refers to a STREAM, the operation of write() is determined by the values of the minimum and maximum nbyte range ("packet size") accepted by the STREAM. These values are determined by the topmost STREAM module. If nbyte falls within the packet size range, nbyte bytes will be written. If nbyte does not fall within the range and the minimum packet size value is 0, write() will break the buffer into maximum packet size segments prior to sending the data downstream (the last segment may contain less than the maximum packet size). If nbyte does not fall within the range and the minimum value is non-zero, write() will fail with errno set to ERANGE. Writing a zero-length buffer ( nbyte is 0) to a STREAMS device sends 0 bytes with 0 returned. However, writing a zero-length buffer to a STREAMS-based pipe or FIFO sends no message and 0 is returned. The process may issue I_SWROPT ioctl() to enable zero-length messages to be sent across the pipe or FIFO. When writing to a STREAM, data messages are created with a priority band of 0. When writing to a STREAM that is not a pipe or FIFO, the following apply:
In addition, write() and writev() will fail if the STREAM head had processed an asynchronous error before the call. In this case, the value of errno does not reflect the result of write() or writev() but reflects the prior error. If the write is performed by any user other than the owner or a user who has appropriate privileges, write() clears the set-user-ID, set-group-ID, and sticky bits on all nondirectory files. If the write is performed by the owner or a user who has appropriate privileges, the behavior is file-system dependent. In some file systems, the write clears the set-user-ID, set-group-ID, and sticky bits on a nondirectory file. In other file systems, the write does not clear these bits on a nondirectory file. For directories, write() does not clear the set-user-ID, set-group-ID, and sticky bits. The writev() function is equivalent to write(), but gathers the output data from the iovcnt buffers specified by the members of the iov array: iov[0], iov[1], ..., iov[iovcnt-1]. iovcnt is valid if greater than 0 and less than or equal to {IOV_MAX}, as defined in <limits.h>. Each iovec entry specifies the base address and length of an area in memory from which data should be written. The writev() function will always write a complete area before proceeding to the next. The iovec structure is defined in /usr/include/sys/uio.h. If fildes refers to a regular file and all of the iov_len members in the array pointed to by iov are 0, writev() will return 0 and have no other effect. For other file types, the behavior is unspecified. If the sum of the iov_len values is greater than SSIZE_MAX, the operation fails and no data is transferred. The pwrite() function performs the same action as write(), except that it writes into a given position without changing the file pointer. The first three arguments to pwrite() are the same as write() with the addition of a fourth argument offset for the desired position inside the file. When a write() system call executes on a local file system and needs a new buffer to be allocated to hold the data, the buffer maps onto the disk at that time. If the disk is full, write() returns an error and errno is set to ENOSPC. When a write() system call executes on an NFS file system, the write() allocates the new buffer without communicating with the NFS server to see if there is space for the buffer (to improve NFS performance). The disk-full condition is checked, only when the buffer is written to the server (at file close or the buffer is full). If the disk is full, close() returns an error. See close(2). RETURN VALUEUpon successful completion, write() and pwrite() will return the number of bytes actually written to the file associated with fildes. This number will never be greater than nbyte. Otherwise, -1 is returned and errno is set to indicate the error. Upon successful completion, writev() returns the number of bytes actually written. Otherwise, it returns a value of -1, the file pointer remains unchanged, and errno is set to indicate an error. A write to a STREAMS file may fail if an error message has been received at the STREAM head. In this case, errno is set to the value included in the error message. ERRORSUnder the following conditions, write(), pwrite() and writev() fail and set errno to:
Under the following conditions, writev() fails and sets errno to:
Under the following conditions, the writev() function may fail and set errno to:
Under the following conditions, the pwrite() function fails, the file pointer remains unchanged and errno is set to:
Under the following conditions, write() or writev() fails, the file offset is updated to reflect the amount of data transferred and errno is set to:
EXAMPLESAssuming a process opened a file for writing, the following call to write() attempts to write mybufsize bytes to the file from the buffer to which mybuf points. #include <string.h> int fildes; size_t mybufsize; ssize_t nbytes; char *mybuf = "aeiou and sometimes y"; mybufsize = (size_t)strlen (mybuf); nbytes = write (fildes, (void *)mybuf, mybufsize); WARNINGSCharacter special devices, and raw disks in particular, apply constraints on how write() can be used. See specific Section 7 manual entries for details on particular devices. |
Printable version | ||
|