The write is one of the most basic routines provided by a Unix-like operating system kernel. It writes data from a buffer declared by the user to a given device, such as a file. This is the primary way to output data from a program by directly using a system call. The destination is identified by a numeric code. The data to be written, for instance a piece of text, is defined by a pointer and a size, given in number of bytes.
<code>write</code> thus takes three arguments:
The write call interface is standardized by the POSIX specification. Data is written to a file by calling the write function. The function prototype is:
In above syntax, <code>ssize_t</code> is a <code>typedef</code>. It is a signed data type defined in <code>stddef.h</code>. Note that <code>write()</code> does not return an unsigned value; it returns -1 if an error occurs so it must return a signed value.<br> The write function returns the number of bytes successfully written into the file, which may at times be less than the specified nbytes. It returns -1 if an exceptional condition is encountered, see section on errors below.
Historically, Linux would use different system call tables for different architectures. <code>write</code> has the call number 1 on x86-64, but 4 on ARM. However, more recent architectures supported by Linux have adopted a universal system call table, in which <code>write</code>'s call number is 64.
When compiling software, the kernel exposes the call numbers for the target architecture as integer constants in the C header <code><linux/unistd.h></code>. Several macros are defined in the form of <code>__NR_xxx</code>, which expand to the call number for the system call <code>xxx</code>. As such, <code>write</code>'s call number is exposed as <code>__NR_write</code>. This header may also be included by assembler code using the C preprocessor.