Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

creat, creat64 - create new file or overwrite existing one

&pagelevel(4)&pagelevel

Name

Syntax

creat, creat64

#include <fcntl.h>

Optional
#include <sys/types.h>
#include <sys/stat.h>

int creat(const char *path, mode_t mode);
int creat64(const char *path, mode_t mode);

BS2000
int creat(const char *path, int mode);
int creat64(const char *path, int mode);

Description

If POSIX files are created, the behavior of this function conforms to the XPG standard as described below:

creat() creates a new file or prepares to rewrite an existing file named by the pathname pointed to by path.

If the file exists, its length is truncated to 0, and the mode and owner are unchanged.

If the file does not exist, the file's owner ID is set to the effective user ID of the process. The group ID of the file is set to the effective group ID of the process, unless the S_ISGID bit is set in the parent directory, in which case the group ID of the file is inherited from the parent directory. The access permission bits of the file mode are set to the value of mode modified as follows:

If the group ID of the new file does not match the effective group ID or one of the supplementary group IDs, the S_ISGID bit is cleared.

All bits set in the process's file mode creation mask are cleared (see umask()).

The 'save text image after execution bit' of the mode is cleared (see chmod()).

Upon successful completion, a write-only file descriptor is returned, and the file is opened for writing even if the mode does not permit writing. The file position indicator is set to the beginning of the file. The file descriptor is set to remain open across exec system calls (see fcntl()). A new file may be created with a mode that forbids writing.

The call creat(path, mode) is equivalent to:

open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)

There is no difference in functionality between creat() and creat64() except that the identifier for a large file is stored in the file description linked to the file descriptor, i.e. the O_LARGEFILE bit is set. A file identifier is returned that can be used to increase the size of the file beyond 2 GB.

BS2000 The following must be noted when creating BS2000 files:

path can be:

  • any valid BS2000 file name

  • "link=linkname" linkname denotes a BS2000 link name.

mode: Only the lbp switch, the Nosplit switch, and the O_RECORD specification are evaluated in this parameter. All other specifications in this parameter are ignored. However, it is required for the creation of portable programs since it controls the protection bit assignment in the UNIX operating system.

lbp switch

The lbp switch controls handling of the Last Byte Pointer (LBP). It is only relevant for binary files with PAM access mode and can be combined with all specifications permissible for open. If O_LBP is specified as the lbp switch, a check is made to see whether LBP support is possible. If this is not the case, the creat(), creat64() function will fail and errno is set to ENOSYS. The switch has further effects only when the file is closed.

O_LBP

When a file which has been newly created is closed, no marker is written and a valid LBP is set. In the case of NK files the last logical block is padded with binary zeros, in the case of K files the file is padded to the physical end of file.

O_NOLBP

When a file which has been newly created is closed, the LBP is set to zero (= invalid). A marker is written. In the case of NK files the last logical block is padded with binary zeros, in the case of K files the file is padded to the physical end of file.In the case of NK files the last logical block is padded with binary zeros, in the case of K files the file is padded to the physical end of file.

If the lbp switch is specified in both variants (O_LBP and O_NOLBP), the creat(), creat64() function fails and errno is set to EINVAL.

If the lbp switch is not specified, the behavior depends on the environment variable LAST_BYTE_POINTER (see also section “Environment variables”):

LAST_BYTE_POINTER=YES

The function behaves as if O_LBP were specified.

LAST_BYTE_POINTER=NO

The function behaves as if O_NOLBP were specified.

Nosplit switch

This switch controls the processing of text files with SAM access mode and variable record length when a maximum record length is also specified. It can be combined with any of the other constants.

O_NOSPLIT

When writing with write(), records which are longer than the maximum record length are truncated to the maximum record length.

If the switch is not specified, the following applies when writing:

A record which is longer than the maximum record length will be split into multiple records. If a record has precisely the maximum record length, a record of the length zero is written after it.

The constant O_RECORD can be specified in the modus parameter to open files with recordoriented input/output (record I/O). It can always be combined with every other constant except O_LBP.

O_RECORD

This switch functions as follows:

The write function writes a record to the file. In the case of SAM and PAM files the record is written to the current file position. In the case of ISAM files the record is written to the position which corresponds to the key value in the record. If the number n of the characters to be written is greater than the maximum record length, only a record with the maximum record length is written. The remaining data is lost. In the case of ISAM files a record is written only if it contains at least a complete key. If in the case of files with a fixed record length n is less than the record length, binary zeros are used for padding. When a record is updated in a SAM or PAM file, the length of the record may not be modified. The write function returns the number of actually written characters also in the case of record I/O.

The BS2000 file name or link name may be written in lowercase and uppercase letters. It is automatically converted to uppercase letters.

If the file does not exist, the following file is created by default:
for KR functionality (only available with C/C++ versions lower than V3), a SAM file with variable record length and standard block length; for ANSI functionality, an ISAM file with variable record length and standard block length.

When using a link name, the following file attributes can be changed by means of the SET-FILE-LINK command: access method, record length, record format, block length and block format.

If an existing file is truncated to length 0, the catalog attributes of the file are preserved.

A maximum of _NFILE files may be open simultaneously. _NFILE is defined as 2048 in stdio.h

Return val.

File descriptor 


if successful.

-1

if an error occurs. errno is set to indicate the error. No file is opened or modified.

Errors

creat() will fail if:

 

EACCES

Search permission is denied on a component of the path.

The file does not exist and the directory in which the file is to be created does not permit writing.

The file exists and write permission is denied.

 

Extension

 

EAGAIN

The file exists, mandatory file/record locking is set, and there are outstanding record locks on the file (see chmod()). (End)

 

EEXIST

O_CREAT and O_EXCL are set, and the file name already exists.

 

Extension

 

EFAULT

path points outside the allocated address space of the process. (End)

 

EINTR

A signal was caught during the creat() system call.

 

EISDIR

The specified file is a directory.

 

Extension 

 

ELOOP

Too many symbolic links were encountered in resolving path.

 

EMFILE

The process has too many open files (see getrlimit()).

 

ENAMETOOLONG



The length of the path argument exceeds {PATH_MAX} or a path component is longer than {NAME_MAX}.

 

ENFILE

The system file table is full.


ENOENT

A component of the pathname does not exist or path points to an empty string.


ENOSPC

The file system is out of inodes.


ENOTDIR

A component of the pathname is not a directory.


ENXIO

The named file is a character special or block special file, and the device associated with this special file does not exist.


EROFS

The specified file resides or would reside on a read-only file system.


ETXTBSY

The file is a pure program file that is currently being executed.

Notes

The program environment determines whether a BS2000 or POSIX file is created. 

See also

chmod(), close(), dup(), fcntl(), getrlimit(), lseek(), open(), read(), umask(), write(), stat(), fcntl.h, sys/stat.h, sys/types.h..