#include <fcntl.h> int open(const char *path, int oflag, . . .);Service Program Name: QP0LLIB1
The open() function opens a file and returns a number called a file descriptor. You can use this file descriptor to refer to the file in subsequent I/O operations such as read() or write(). In these subsequent operations, the file descriptor is commonly identified by the argument fildes or descriptor. Each file opened by a job gets a new file descriptor.
If the last element of the path is a symbolic link, the open() function resolves the contents of the symbolic link.
open() positions the file offset (an indicator showing where the next read or write will take place in the file) at the beginning of the file. However, there are options that can change the position.
open() clears the FD_CLOEXEC file descriptor flag for the new file descriptor. Refer to fcntl()--Perform File Control Command for additional information about the FD_CLOEXEC flag.
The open() function also can be used to open a directory. The resulting file descriptor can be used in some functions that have a fildes parameter.
If the file being opened has been saved and its storage freed, the file is restored during this open() function. The storage extension exit program registered against the QIBM_QTA_STOR_EX400 exit point is called to restore the object. (See the Storage Extension Exit Program for details). If the file cannot successfully be restored, open() fails with the EOFFLINE error number.
For information about the exit point which can be associated with open(), see Integrated File System Scan on Open Exit Programs.
This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.
When a new file is created, the new file name is assumed to be represented in the language and country currently in effect for the job.
See QlgOpen()--Open a File (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.
Note: The open64() API sets the O_LARGEFILE flag internally.
If the O_CCSID flag is set, this parameter specifies a CCSID. If the O_CODEPAGE flag is set, this parameter specifies a code page used to derive a CCSID.
The specified or derived CCSID is assumed to be the CCSID of the data in the file, when a new file is created. This CCSID is associated with the file during file creation.
When the O_TEXT_CREAT flag and its prerequisite flags are not set, the specified or derived CCSID is the CCSID in which data is to be returned (when reading from a file), or the CCSID in which data is being supplied (when writing to a file).
See Using CCSIDs and code pages for more details.
When O_CCSID flag is set, this parameter specifies a CCSID. If the O_CODEPAGE flag is set, this parameter specifies a code page used to derive a CCSID.
The specified or derived CCSID will be used as the CCSID of this open instance. Therefore, this will be the CCSID in which data is to be returned (when reading from a file), or the CCSID in which data is being supplied (when writing to a file). Data will be stored in the CCSID associated with the open file. Note: if the file was not created by this open operation, the file's CCSID may be different than the CCSID associated with the conversion ID parameter.
See Using CCSIDs and code pages for more details.
One of the following values must be specified in oflag:
One or more of the following also can be specified in oflag:
If the file being opened already exists, O_CREAT has no effect, except when O_EXCL is also specified (see the following description of O_EXCL.
If the file being opened does not exist, it is created and then opened. Since the create and open operations occur as separate steps, error [EBUSY] could be received if another user opened the object with a conflicting file sharing mode after the create step, but before the open step. The user ID (uid) of the file is set to the effective uid of the job. If the object is being created in the "root" (/), QOpenSys, and user-defined file systems, the following applies. If the S_ISGID bit of the parent directory is off, the group ID (GID) is set to the effective GID of the thread creating the object. If the S_ISGID bit of the parent directory is on, the group ID (GID) of the new object is set to the GID of the parent directory. For all other file systems, the group ID (GID) of the file is set to the GID of the directory in which the file is created. File permission bits are set according to mode, except for those set in the file mode creation mask of the job. The S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits are also set according to mode. The file type bits in mode are ignored. All other bits in mode must be cleared (not set) or a [EINVAL] error is returned.
Truncation of the file will return the [EOVERFLOW] error if the file is larger than 2 GB minus 1 byte and if the O_LARGEFILE oflag is not also specified on the open() call. (Note that open64() sets the O_LARGEFILE oflag automatically.)
If the file exists and it is a regular file, the S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits of the file mode are cleared.
If the file has a digital signature, open() with the O_TRUNC oflag causes the signature to be deleted.
The data is read from the file and written to the file assuming it is in textual form. When the data is read from the file, it is converted from the CCSID of the file to the CCSID of the job or the CCSID specified by the application receiving the data. When data is written to the file, it is converted to the CCSID of the file from the CCSID of the job or the CCSID specified by the application.
For true stream files, any line-formatting characters (such as carriage return, tab, and end-of-file) are just converted from one CCSID to another.
When reading from a record file that is being used as a stream file, end-of-line characters are added to the end of the data in each record. When writing to the record file:
See Using CCSIDs and code pages for more details on text conversions.
This flag cannot be specified with the O_CODEPAGE flag.
See Using CCSIDs and code pages for more details.
This flag cannot be specified with the O_CCSID flag.
See Using CCSIDs and code pages for more details.
If the O_TEXT_CREAT flag is specified, all of the following flags must also be specified: O_CREAT, O_TEXTDATA, and either O_CODEPAGE or O_CCSID. If all of these prerequisite flags are not specified when O_TEXT_CREAT is specified, then the call to open will fail with error condition [EINVAL].
This flag indicates that the textual data read from or written to this file will be converted between the CCSID specified or derived from the text file creation conversion ID and the CCSID of the file. When data is read from the file, it is converted from the CCSID of the file to the CCSID specified or derived from the text file creation conversion ID. When data is written to the file, it is converted to the CCSID of the file from the CCSID specified or derived from the text file creation conversion ID.
See Using CCSIDs and code pages for more details.
The "root" (/), QOpenSys, QSYS.LIB, independent ASP QSYS.LIB, and QDLS file systems support this flag on an open() with the O_CREAT flag set. The QOPT file system ignores this flag because files in this file system do not have data authorities.
If O_NONBLOCK is specified when opening a FIFO:
If O_NONBLOCK is not specified when opening a FIFO:
The O_NONBLOCK open flag is ignored for all other object types.
A file sharing mode may also be specified in the oflag. If none are specified, a default sharing mode of O_SHARE_RDWR is used. No more than one of the following may be specified:
Once opened with this sharing mode, any request to open this file for writing fails with the [EBUSY] error.
Once opened with this sharing mode, any request to open this file for reading fails with the [EBUSY] error.
All other bits in oflag must be cleared (not set).
Notes:
If O_TRUNC is specified and the file previously existed, a successful open() updates the change time and modification time for the file.
If a sharing mode is not specified in the oflag parameter, a default sharing mode of O_SHARE_RDWR is used. The open() may fail with the [EBUSY] error number if the file is already open with a sharing mode that conflicts with the access intent of this open() request.
Directories may only be opened with a sharing mode of O_SHARE_RDWR. If any other sharing mode is specified, the open() fails with error number [EINVAL].
For *CHRSF files, a sharing mode of O_SHARE_RDWR is used regardless of the sharing mode specified in the oflag parameter. The sharing mode specified in the oflag parameter is ignored.
The following table shows when conflicts will occur:
File Sharing Conflicts | ||||
---|---|---|---|---|
Access Intent | Sharing Mode | |||
Readers Only | Writers Only | Readers and Writers |
No Others (Exclusive) |
|
O_RDONLY | OK | EBUSY | OK | EBUSY |
O_WRONLY | EBUSY | OK | OK | EBUSY |
O_RDWR | EBUSY | EBUSY | OK | EBUSY |
If the O_CCSID or O_CODEPAGE flag is specified, but O_CREAT is not, the mode parameter must be specified, but its value will be ignored.
The value of conversion ID must be less than 65536. The [EINVAL] error will be returned if it is not.
When a new file is created:
When an existing file is opened and O_TEXTDATA is not specified:
When an existing file is opened and O_TEXTDATA is specified:
See Examples for a sample program that creates a new file and then opens it for data conversion.
Note: Adopted authority is not used.
Authorization Required for open() (excluding QSYS.LIB, independent ASP QSYS.LIB,and QDLS) | ||
---|---|---|
Object Referred to | Authority Required |
errno |
Each directory in the path name preceding the object to be opened | *X | EACCES |
Existing object when access mode is O_RDONLY | *R | EACCES |
Existing object when access mode is O_WRONLY | *W | EACCES |
Existing object when access mode is O_RDWR | *RW | EACCES |
Existing object when O_TRUNC is specified | *W | EACCES |
Parent directory of object to be created when object does not exist and O_CREAT is specified | *WX | EACCES |
Authorization Required for open() in the QSYS.LIB and independent ASP QSYS.LIB File Systems | ||
---|---|---|
Object Referred to | Authority Required |
errno |
Each directory in the path name preceding the object to be opened | *X | EACCES |
Existing object when access mode is O_RDONLY | *R | EACCES |
Existing object when access mode is O_WRONLY | *W | EACCES |
Existing object when access mode is O_RDWR | *RW | EACCES |
Existing object when object is a save file | *RWX | EACCES |
Existing object when O_TRUNC is specified | *W | EACCES |
Parent directory of object to be created when object does not exist and O_CREAT is specified | *OBJMGT or *OBJALTER |
EACCES |
Parent directory of object to be created when object does not exist and object type is *USRSPC or save file | *RX and *Add | EACCES |
Parent directory of the parent directory of object to be created when object does not exist, O_CREAT is specified, and object being created is a physical file member | *ADD | EACCES |
Authorization Required for open() in the QDLS File System | ||
---|---|---|
Object Referred to | Authority Required |
errno |
Each directory in the path name preceding the object to be opened | *X | EACCES |
Existing object when access mode is O_RDONLY | *R | EACCES |
Existing object when access mode is O_WRONLY | *W | EACCES |
Existing object when access mode is O_RDWR | *RW | EACCES |
Existing object when O_TRUNC is specified | *W | EACCES |
Parent directory of object to be created when object does not exist and O_CREAT is specified | *CHANGE | EACCES |
value | open() was successful. The value returned is the file descriptor. |
-1 | open() was not successful. The errno global variable is set to indicate the error. |
If open() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADFID] | |
[EBADNAME] | |
[EBUSY] |
The open sharing mode may conflict with another open of this file, or O_WRONLY or O_RDWR is specified and the file is checked out by another user. In the QSYS.LIB and independent ASP QSYS.LIB file systems, if the O_TEXTDATA flag was specified, the file may be already open in this job or another job where the O_TEXTDATA flag was not specified. Or if the O_TEXTDATA flag was not specified, the file may be already open in this job or another job where the O_TEXTDATA flag was specified. |
[ECONVERT] | |
[EDAMAGE] | |
[EDEADLK] | |
[EEXIST] | |
[EFAULT] | |
[EFILECVT] | |
[EINTR] | |
[EINVAL] | For example
|
[EIO] | |
[EISDIR] |
The path name given is a directory. Write access or O_TRUNC has been specified and is not valid for a directory. |
[EJRNDAMAGE] | |
[EJRNENTTOOLONG] | |
[EJRNINACTIVE] | |
[EJRNRCVSPC] | |
[ELOOP] | |
[EMFILE] | |
[ENAMETOOLONG] | |
[ENEWJRN] | |
[ENEWJRNRCV] | |
[ENFILE] | |
[ENOENT] | |
[ENOMEM] | |
[ENOSPC] | |
[ENOSYS] | |
[ENOSYSRSC] | |
[ENOTAVAIL] | |
[ENOTDIR] | |
[ENOTSUP] | |
[ENXIO] |
O_NONBLOCK and O_WRONLY open flags are specified, path refers to a FIFO, and no job has the FIFO open for reading. |
[EOFFLINE] | |
[EOVERFLOW] |
The size of the specified file cannot be represented correctly in a variable of type off_t (the file is larger than 2GB minus 1 byte). |
[EPERM] | |
[EROOBJ] | |
[ESCANFAILURE] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETXTBSY] | |
[EUNKNOWN] |
Additionally, if interaction with a file server is required to access the object, errno could also indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ETIMEDOUT] | |
[EUNATCH] |
The following messages may be sent from this function:
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
The user who creates the file becomes its owner. The S_ISGID bit of the directory affects what the group ID (GID) is for objects that are created in the directory. If the S_ISGID bit of the parent directory is off, the group ID (GID) is set to the effective GID of the thread creating the object. If the S_ISGID bit of the parent directory is on, the group ID is copied from the parent directory in which the file is created.
When you do not specify O_INHERITMODE for the oflag parameter, the owner, primary group, and public object authorities (*OBJEXIST, *OBJMGT, *OBJALTER, and *OBJREF) are copied from the parent directory's owner, primary group, and public object authorities. This occurs even when the new file has a different owner than the parent directory. The owner, primary group, and public data authorities (*R, *W, and *X) are derived from the permissions specified in the mode (except for those permissions that are also set in the file mode creation mask). The new file does not have any private authorities or authorization list. It only has authorities for the owner, primary group, and public.
When you specify O_INHERITMODE for the oflag parameter, the owner, primary group, and public data and object authorities (*R, *W, *X, *OBJEXIST, *OBJMGT, *OBJALTER, and *OBJREF) are copied from the parent directory's owner, primary group, and public data and object authorities. In addition, the private authorities (if any) and authorization list (if any) are copied from the parent directory. If the new file has a different owner than the parent directory and the new file's owner has a private authority in the parent directory, that private authority is not copied from the parent directory. The authority for the owner of the new file is copied from the owner of the parent directory.
There are some restrictions when opening a FIFO for text conversion and the CCSIDs involved are not strictly single-byte:
The following object types are allowed to be opened:
When creating a member, the ownership, group profile, and authorities are all derived from the member's parent physical file. The input mode value is ignored.
The group ID is obtained from the primary user profile, if a group profile exists.
The primary group authorities specified in mode are not saved if no primary group exists.
You cannot open a member in a file that has a mixed data CCSID.
The file access time for a database member is updated using the normal rules that apply to database files. At most, the access time is updated once per day.
Due to the restriction that only one job may have a database member open for writing at a time, the sharing modes O_SHARE_WRONLY and O_SHARE_RDWR do not provide the requested level of sharing.
The open() of a database member fails with an [EBUSY] error under any of the following conditions:
This function will fail with error number [ENOTSAFE] if the object on which this function is operating is a save file and multiple threads exist in the job.
This function will fail with error number [ENOTSUP] if the file specified is a save file and the O_RDWR flag is specified. A save file can be opened for either reading only or writing only.
This function will fail with error number [ENOTSUP] if the file specified is a save file and the O_TEXTDATA flag is specified.
If a save file containing data is opened for writing, the O_APPEND or O_TRUNC flag must be specified. Otherwise, the open() will fail with error number [ENOTSUP].
There are some restrictions on sharing modes when opening a save file.
Note: Unpredictable results, including loss of data, could occur if, in the same job, a user tries to open the same file for writing at the same time by using both open() API for stream file access and a data management open API for record access.
When O_CREAT is specified and a new file is created:
The primary group authorities specified in mode are not saved if no primary group exists.
QDLS does not store the language ID and country ID with its files. When this information is requested (using the readdir() function), QDLS returns the language ID and country ID of the system.
When the volume on which the file is being opened is formatted in Universal Disk Format (UDF):
When the volume on which the file is being opened is not formatted in Universal Disk Format (UDF):
Local access to remote files through the Network File System may produce unexpected results due to conditions at the server. The creation of a file may fail if permissions and other attributes that are stored locally by the Network File System are more restrictive than those at the server. A later attempt to create a file can succeed when the locally stored data has been refreshed. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) The creation can also succeed after the file system has been remounted.
If you try to re-create a file that was recently deleted, the request may fail because data that was stored locally by the Network File System still has a record of the file's existence. The creation succeeds when the locally stored data has been updated.
Once a file is open, subsequent requests to perform operations on the file can fail because file attributes are checked at the server on each request. If permissions on the file are made more restrictive at the server or the file is unlinked or made unavailable by the server for another client, your operation on an open file descriptor will fail when the local Network File System receives these updates. The local Network File System also impacts operations that retrieve file attributes. Recent changes at the server may not be available at your client yet, and old values may be returned from operations.
The sharing modes O_SHARE_RDONLY, O_SHARE_WRONLY, and O_SHARE_NONE do not provide the requested level of sharing. If any one of these share modes is specified, the resulting share mode semantic will be equivalent to O_SHARE_RDWR.
The QNetWare file system does not fully support mode bits. See Netware on iSeries in the iSeries Information Center for more information.
See Code disclaimer information for information pertaining to code examples.
The following example opens an output file for appending. Because no sharing mode is specified, the O_SHARE_RDWR sharing mode is used.
int fildes; fildes = open("outfile",O_WRONLY | O_APPEND);
The following example creates a new file with read, write, and execute permissions for the user creating the file. If the file already exists, the open() fails. If the open() succeeds, the file is opened for sharing with readers only.
fildes = open("newfile",O_WRONLY|O_CREAT|O_EXCL|O_SHARE_RDONLY,S_IRWXU);
This example first creates an output file for with a specified CCSID. The file is then closed and opened again with data conversion. The open() function is called twice because no data conversion would have occurred when using the first open's descriptor on read or write operations, even if O_TEXTDATA had been specified on that open; however, the second open could be eliminated entirely by using O_TEXT_CREAT on the first open. This is demonstrated in the code example immediately following this example. In this example, EBCDIC data is written to the file and converted to ASCII.
#include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <stdio.h> int main(int argc, char *argv[]) { int fd; int rc; char name[]="/test.dat"; char data[]="abcdefghijk"; int oflag1 = O_CREAT | O_RDWR | O_CCSID; int oflag2 = O_RDWR | O_TEXTDATA | O_CCSID; mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; unsigned int file_ccsid = 819; unsigned int open_ccsid = 37; /***************************************************/ /* First create the file with the CCSID 819. */ /***************************************************/ if ((fd=open(name,oflag1,mode,file_ccsid)) < 0) { perror("open() for create failed"); return(0); } if (close(fd) < 0) { perror("close() failed."); return(0); } /***************************************************/ /* Now open the file so EBCDIC (CCSID 37) data */ /* written will be converted to ASCII (CCSID 819).*/ /***************************************************/ if ((fd=open(name,oflag2,mode,open_ccsid)) < 0) { perror("open() with translation failed"); return(0); } /***************************************************/ /* Write some EBCDIC data. */ /***************************************************/ if (-1 == (rc=write(fd, data, strlen(data)))) { perror("write failed"); return(0); } if (0 != (rc=close(fd))) { perror("close failed"); return(0); } }
In this second example, EBCDIC data is written to the file and converted to ASCII. This will produce the same results as the first example, except that it did it by only using one open instead of two.
#include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <stdio.h> int main(int argc, char *argv[]) { int fd; int rc; char name[]="/test.dat"; char data[]="abcdefghijk"; int oflag1 = O_CREAT | O_RDWR | O_CCSID | O_TEXTDATA | O_TEXT_CREAT | O_EXCL; mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; unsigned int file_ccsid = 819; unsigned int open_ccsid = 37; /***************************************************/ /* First create the file with the CCSID 819, and */ /* open it such that the data is converted */ /* between the the open CCSID of 37 and the */ /* file's CCSID of 819 when writing data to it. */ /***************************************************/ if ((fd=open(name,oflag1,mode,file_ccsid,open_ccsid)) < 0) { perror("open() for create failed"); return(0); } /***************************************************/ /* Write some EBCDIC data. */ /***************************************************/ if (-1 == (rc=write(fd, data, strlen(data)))) { perror("write failed"); return(0); } /***************************************************/ /* Close the file. */ /***************************************************/ if (0 != (rc=close(fd))) { perror("close failed"); return(0); } }
Top | UNIX-Type APIs | APIs by category |