2 .\" Copyright (c) 2010 Apple Inc.  All rights reserved.
 
   4 .\" @APPLE_LICENSE_HEADER_START@
 
   6 .\" This file contains Original Code and/or Modifications of Original Code
 
   7 .\" as defined in and that are subject to the Apple Public Source License
 
   8 .\" Version 2.0 (the 'License'). You may not use this file except in
 
   9 .\" compliance with the License. Please obtain a copy of the License at
 
  10 .\" http://www.opensource.apple.com/apsl/ and read it before using this
 
  13 .\" The Original Code and all software distributed under the License are
 
  14 .\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 
  15 .\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 
  16 .\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 
  17 .\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 
  18 .\" Please see the License for the specific language governing rights and
 
  19 .\" limitations under the License.
 
  21 .\" @APPLE_LICENSE_HEADER_END@
 
  24 .\"     $NetBSD: open.2,v 1.8 1995/02/27 12:35:14 cgd Exp $
 
  26 .\" Copyright (c) 1980, 1991, 1993
 
  27 .\"     The Regents of the University of California.  All rights reserved.
 
  29 .\" Redistribution and use in source and binary forms, with or without
 
  30 .\" modification, are permitted provided that the following conditions
 
  32 .\" 1. Redistributions of source code must retain the above copyright
 
  33 .\"    notice, this list of conditions and the following disclaimer.
 
  34 .\" 2. Redistributions in binary form must reproduce the above copyright
 
  35 .\"    notice, this list of conditions and the following disclaimer in the
 
  36 .\"    documentation and/or other materials provided with the distribution.
 
  37 .\" 3. All advertising materials mentioning features or use of this software
 
  38 .\"    must display the following acknowledgement:
 
  39 .\"     This product includes software developed by the University of
 
  40 .\"     California, Berkeley and its contributors.
 
  41 .\" 4. Neither the name of the University nor the names of its contributors
 
  42 .\"    may be used to endorse or promote products derived from this software
 
  43 .\"    without specific prior written permission.
 
  45 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
  46 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
  47 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
  48 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
  49 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  50 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
  51 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
  52 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
  53 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
  54 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
  57 .\"     @(#)open.2      8.2 (Berkeley) 11/16/93
 
  64 .Nd open or create a file for reading or writing
 
  66 .\" OH??? .Fd #include <sys/stat.h>
 
  67 .Fd #include <fcntl.h>
 
  70 .Fa "const char *path"
 
  75 .Fn openat "int fd" "const char *path" "int oflag" "..."
 
  77 The file name specified by
 
  80 for reading and/or writing,
 
  81 as specified by the argument
 
  83 the file descriptor is returned to the calling process.
 
  87 argument may indicate that the file is to be
 
  88 created if it does not exist (by specifying the
 
  94 require an additional argument
 
  96 the file is created with mode
 
 100 and modified by the process' umask value (see
 
 105 function is equivalent to the
 
 107 function except in the case where the
 
 109 specifies a relative path.
 
 110 In this case the file to be opened is determined relative to the directory
 
 111 associated with the file descriptor
 
 113 instead of the current working directory.
 
 116 argument and the optional fourth argument correspond exactly to
 
 121 is passed the special value
 
 125 argument, the current working directory is used
 
 126 and the behavior is identical to a call to
 
 132 argument must include exactly one of the following file access modes:
 
 134 .Bd -literal -offset indent -compact
 
 135 O_RDONLY        open for reading only
 
 136 O_WRONLY        open for writing only
 
 137 O_RDWR          open for reading and writing
 
 140 In addition any combination of the following values can be
 
 144 .Bd -literal -offset indent -compact
 
 145 O_NONBLOCK      do not block on open or for data to become available
 
 146 O_APPEND        append on each write
 
 147 O_CREAT         create file if it does not exist
 
 148 O_TRUNC         truncate size to 0
 
 149 O_EXCL          error if O_CREAT and the file exists
 
 150 O_SHLOCK        atomically obtain a shared lock
 
 151 O_EXLOCK        atomically obtain an exclusive lock
 
 152 O_NOFOLLOW      do not follow symlinks
 
 153 O_SYMLINK       allow open of symlinks
 
 154 O_EVTONLY       descriptor requested for event notifications only
 
 155 O_CLOEXEC       mark as close-on-exec
 
 160 set causes each write on the file to be appended to the end.  If
 
 163 file exists, the file is truncated to zero length.
 
 172 This may be used to implement a simple exclusive-access locking mechanism.
 
 177 and the last component of the pathname is a symbolic link,
 
 179 will fail even if the symbolic link points to a non-existent name.
 
 183 flag is specified, do not wait for the device or file
 
 184 to be ready or available.  If the
 
 187 in the process being blocked for some reason
 
 188 (e.g., waiting for carrier on a dialup line),
 
 191 This flag also has the effect of making all subsequent I/O
 
 192 on the open file non-blocking.
 
 194 When opening a file, a lock with
 
 196 semantics can be obtained by setting
 
 198 for a shared lock, or
 
 200 for an exclusive lock.
 
 201 If creating a file with
 
 203 the request for the lock will never fail
 
 204 (provided that the underlying filesystem supports locking).
 
 208 is used in the mask and the target file passed to
 
 210 is a symbolic link then the
 
 216 is used in the mask and the target file passed to
 
 218 is a symbolic link then the
 
 220 will be for the symbolic link itself, not what it links to.
 
 224 flag is only intended for monitoring a file for changes (e.g. kqueue). Note: when 
 
 225 this flag is used, the opened file will not prevent an unmount 
 
 226 of the volume that contains the file.
 
 230 flag causes the file descriptor to be marked as close-on-exec,
 
 233 flag.  The state of the file descriptor flags can be inspected
 
 234 using the F_GETFD fcntl.  See
 
 239 returns a non-negative integer, termed a file descriptor.
 
 240 It returns -1 on failure.
 
 241 The file pointer (used to mark the current position within the file)
 
 242 is set to the beginning of the file.
 
 244 When a new file is created,
 
 245 it is given the group of the directory which contains it.
 
 247 The new descriptor is set to remain open across
 
 254 The system imposes a limit on the number of file descriptors
 
 255 that can be held open simultaneously by one process.
 
 257 returns the current system limit.
 
 261 returns a non-negative integer, termed a file descriptor.
 
 262 It returns -1 on failure, and sets
 
 264 to indicate the error.
 
 266 The named file is opened unless:
 
 270 Search permission is denied for a component of the path prefix.
 
 273 The required permissions (for reading and/or writing)
 
 274 are denied for the given flags.
 
 279 the file does not exist,
 
 280 and the directory in which it is to be created
 
 281 does not permit writing.
 
 285 is specified and write permission is denied.
 
 289 specifies the slave side of a locked pseudo-terminal device.
 
 294 the file does not exist,
 
 295 and the directory in which the entry for the new file
 
 296 is being placed cannot be extended because the
 
 297 user's quota of disk blocks on the file system
 
 298 containing the directory has been exhausted.
 
 303 the file does not exist,
 
 304 and the user's quota of inodes on the file system
 
 305 on which the file is being created has been exhausted.
 
 311 are specified and the file exists.
 
 315 points outside the process's allocated address space.
 
 320 operation is interrupted by a signal.
 
 328 An I/O error occurs while making the directory entry or
 
 329 allocating the inode for
 
 333 The named file is a directory, and the arguments specify
 
 334 that it is to be opened for writing.
 
 337 Too many symbolic links are encountered in translating the pathname.
 
 338 This is taken to be indicative of a looping symbolic link.
 
 341 The process has already reached its limit for open file descriptors.
 
 343 .It Bq Er ENAMETOOLONG
 
 344 A component of a pathname exceeds
 
 346 characters, or an entire path name exceeded 
 
 351 The system file table is full.
 
 355 was specified and the target is a symbolic link.
 
 359 is not set and the named file does not exist.
 
 362 A component of the path name that must exist does not exist.
 
 367 the file does not exist,
 
 368 and the directory in which the entry for the new file is being placed
 
 369 cannot be extended because there is no space left on the file
 
 370 system containing the directory.
 
 375 the file does not exist,
 
 376 and there are no free inodes on the file system on which the
 
 377 file is being created.
 
 380 A component of the path prefix is not a directory.
 
 383 The named file is a character-special or block-special file
 
 384 and the device associated with this special file does not exist.
 
 387 O_NONBLOCK and O_WRONLY are set, the file is a FIFO,
 
 388 and no process has it open for reading.
 
 394 is specified, but the underlying filesystem does not support locking.
 
 397 An attempt is made to open a socket (not currently implemented).
 
 400 The named file is a regular file
 
 401 and its size does not fit in an object of type off_t.
 
 404 The named file resides on a read-only file system,
 
 405 and the file is to be modified.
 
 408 The file is a pure procedure (shared text) file that is being
 
 411 call requests write access.
 
 415 argument does not specify an absolute path and the
 
 420 nor a valid file descriptor open for searching.
 
 424 argument is not an absolute path and
 
 428 nor a file descriptor associated with a directory.
 
 430 The filename does not match the encoding rules.
 
 434 on a terminal device (i.e., /dev/console)
 
 435 will now make that device a controlling terminal for the process.
 
 436 Use the O_NOCTTY flag to open a terminal device
 
 437 without changing your controlling terminal.
 
 442 .Xr getdtablesize 2 ,
 
 450 function call appeared in 
 
 454 function was introduced in OS X 10.10