Welcome to little lamb

Code » limb » release » tree

[release] / src / doc / posixplz.h / mkfiletemp.3.md

% limb manual
% mkfiletemp(3)
% limb 0.1.0
% 2023-07-24

# NAME

mkfiletemp, mkfiletempat - open a new temporary file

# SYNOPSIS

    #include <limb/posixplz.h>

```pre hl
typedef int (*create_fn) (const char *<em>file</em>, mode_t <em>mode</em>, void *<em>data</em>)
typedef int (*createat_fn) (int <em>fd</em>, const char *<em>file</em>, mode_t <em>mode</em>, void *<em>data</em>)

int mkfiletemp(char *<em>name</em>, create_fn <em>fn</em>, mode_t <em>mode</em>, void *<em>data</em>)
int mkfiletempat(int <em>bfd</em>, char *<em>name</em>, createat_fn <em>fnat</em>, mode_t <em>mode</em>, void *<em>data</em>)
```

# DESCRIPTION

The `mkfiletemp`() function will open a new temporary file using the function
pointed by `fn`, passing it a file name based on the template pointed by
`name` - which must bend with at least 6 characters `X` that will be replaced
with random (printable) characters - as well as the `mode` and `data` arguments.

The memory pointed by `name` will be modified, replacing the trailing `X`
characters so that `name` contains the name of the file actually opened.

The `fn` function should create & open a file whose name is pointed by its first
argument. It should return the file descriptor of the newly created file on
success, otherwise returning -1 and setting `errno` appropriately. In case of
failure due to *EEXIST* a new file name will automatically be generated and a
new call made.


The `mkfiletempat`() function is similar to `mkfiletemp`() except it takes
a file descriptor `bfd` which will be passed as first argument to `fnat`.

The `fnat` function should create & open a file whose name is pointed by its
second argument, which, when specifying a relative path, should be determined
relative to the directory associated with the file descriptor passed as its
first argument.
If passed the special value *AT_FDCWD* in the `bfd` parameter, the current
working directory should be used used.

It should otherwise behave as `fn`, notably returning the file descriptor of the
newly created file on success, otherwise -1 and setting `errno` appropriately 

# RETURN VALUE

These functions return the file descriptor to the newly created temporary file,
as returned by `fn` or `fnat`, on success. Otherwise they return -1 and set
`errno` to indicate the error.

# ERRORS

These functions may fail if :

: *EINVAL*
:: The template filename pointed by `name` did not end with at least 6 `X`.

They may also fail and set `errno` for any of the errors described for the
function pointed by `fn` or `fnat`, save for *EEXIST*.