File descriptor output redirection implementation.
More...
#include "fd.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
|
static int | openfd (const char *name, const unsigned int id) |
| Opens, or creates, a truncated file using a formatted path derived from the provided name and id arguments.
|
|
int | fdinit (struct fdset_s *fds, const unsigned int id) |
| Initializes stdout/stderr files used for redirecting output from a child process. The file descriptors are stored in the provided file descriptor set. The files are opened in write-only mode and are created if they do not exist. If either file fails to open, the function will return -1 and both file descriptors will be set to -1.
|
|
void | fdclose (struct fdset_s *fds) |
| Closes the stdout and stderr file descriptors in the provided file descriptor set using close(2) . If either file descriptor is already closed, the function will not attempt to close it again.
|
|
File descriptor output redirection implementation.
◆ fdclose()
void fdclose |
( |
struct fdset_s * | fds | ) |
|
Closes the stdout and stderr file descriptors in the provided file descriptor set using close(2)
. If either file descriptor is already closed, the function will not attempt to close it again.
- Parameters
-
fds | The file descriptor set to close |
- Note
- The file descriptors must be initialized with
fdinit
before calling this function.
◆ fdinit()
int fdinit |
( |
struct fdset_s * | fds, |
|
|
unsigned int | id ) |
Initializes stdout/stderr files used for redirecting output from a child process. The file descriptors are stored in the provided file descriptor set. The files are opened in write-only mode and are created if they do not exist. If either file fails to open, the function will return -1 and both file descriptors will be set to -1.
- Parameters
-
fds | The file descriptor set to initialize |
id | Unique identifier to append to the file name |
- Returns
- 0 if both files were opened successfully, otherwise -1
◆ openfd()
static int openfd |
( |
const char * | name, |
|
|
const unsigned int | id ) |
|
static |
Opens, or creates, a truncated file using a formatted path derived from the provided name
and id
arguments.
- Parameters
-
name | The base name of the file to open |
id | The unique identifier to append to the file name |
- Returns
- The file descriptor of the opened file, or -1 on error
- Note
- Due to a fixed-size buffer, the formatted file path has a maximum length of 31 characters.