Mstdlib-1.24.0
|
Typedefs | |
typedef struct M_popen_handle | M_popen_handle_t |
Enumerations | |
enum | M_popen_fd_t { M_POPEN_FD_READ = 0 , M_POPEN_FD_WRITE , M_POPEN_FD_ERR } |
enum | M_popen_err_t { M_POPEN_ERR_NONE = 0 , M_POPEN_ERR_INVALIDUSE , M_POPEN_ERR_CMDNOTFOUND , M_POPEN_ERR_PERM , M_POPEN_ERR_NOEXEC , M_POPEN_ERR_KILLSIGNAL , M_POPEN_ERR_PIPE , M_POPEN_ERR_WAIT , M_POPEN_ERR_SPAWN } |
enum | M_popen_status_t { M_POPEN_STATUS_RUNNING = 0 , M_POPEN_STATUS_ERROR , M_POPEN_STATUS_DONE } |
Functions | |
M_popen_handle_t * | M_popen (const char *cmd, M_popen_err_t *errorid) |
ssize_t | M_popen_read (M_popen_handle_t *mp, M_popen_fd_t fd, char *out, size_t out_len, M_uint64 timeout_ms) |
ssize_t | M_popen_write (M_popen_handle_t *mp, M_popen_fd_t fd, const char *in, size_t in_len) |
int | M_popen_closefd (M_popen_handle_t *mp, M_popen_fd_t fd) |
M_popen_status_t | M_popen_check (M_popen_handle_t *mp) |
int | M_popen_close_ex (M_popen_handle_t *mp, char **stdout_buf, size_t *stdout_buf_len, char **stderr_buf, size_t *stderr_buf_len, M_popen_err_t *errorid, M_uint64 timeout) |
int | M_popen_close (M_popen_handle_t *mp, M_popen_err_t *errorid) |
const char * | M_popen_strerror (M_popen_err_t err) |
Open and interact with a process.
Example:
typedef struct M_popen_handle M_popen_handle_t |
enum M_popen_fd_t |
enum M_popen_err_t |
Possible error reason codes
enum M_popen_status_t |
M_popen_handle_t * M_popen | ( | const char * | cmd, |
M_popen_err_t * | errorid | ||
) |
Start the specified command and open stdin (write), stdout (read), and stderr (read) file descriptors for communication.
Must call M_popen_close() to clean up the returned handle.
[in] | cmd | Command to execute. |
[out] | errorid | Pointer to store error id if an error occurs. |
ssize_t M_popen_read | ( | M_popen_handle_t * | mp, |
M_popen_fd_t | fd, | ||
char * | out, | ||
size_t | out_len, | ||
M_uint64 | timeout_ms | ||
) |
Read from a file descriptor
[in] | mp | Open M_popen_t object. |
[in] | fd | Which FD to read from. |
[out] | out | Buffer to hold read data. |
[in] | out_len | Length of out buffer. |
[in] | timeout_ms | Time in ms to wait for data. M_TIMEOUT_INF will cause this to block. Note: Windows only has 15 ms resolution. |
ssize_t M_popen_write | ( | M_popen_handle_t * | mp, |
M_popen_fd_t | fd, | ||
const char * | in, | ||
size_t | in_len | ||
) |
Write to a file descriptor
[in,out] | mp | Open M_popen_t object. |
[in] | fd | Which FD to write to. |
[in] | in | Buffer to holding data to be written. |
[in] | in_len | Length of data to be written. |
int M_popen_closefd | ( | M_popen_handle_t * | mp, |
M_popen_fd_t | fd | ||
) |
Close the provided file descriptor.
This is used mainly to close the stdin stream to signal to the command being executed that there is no more data left to be read. Any open file descriptors are automatically closed by M_popen_close().
[in,out] | mp | Open M_popen_t object. |
[in] | fd | Which FD to close. |
M_popen_status_t M_popen_check | ( | M_popen_handle_t * | mp | ) |
Checks the current state of the command being executed and returns a code identifying the state.
Even if the state returns DONE or ERROR, M_popen_close() must be called.
[in] | mp | Open M_popen_t object. |
int M_popen_close_ex | ( | M_popen_handle_t * | mp, |
char ** | stdout_buf, | ||
size_t * | stdout_buf_len, | ||
char ** | stderr_buf, | ||
size_t * | stderr_buf_len, | ||
M_popen_err_t * | errorid, | ||
M_uint64 | timeout | ||
) |
Close the M_popen_t object.
This will perform a blocking wait for the process to exit before returning control to the caller.
[in] | mp | M_popen_t object |
[out] | stdout_buf | Optional parameter. Will return allocated buffer containing the contents of the process's stdout. If specified, must also specify stdout_buf_len. |
[out] | stdout_buf_len | Optional parameter. Will return the length of stdout_buf. |
[out] | stderr_buf | Optional parameter. Will return allocated buffer containing the contents of the process's stderr. If specified, must also specify stderr_buf_len. |
[out] | stderr_buf_len | Optional parameter. Will return the length of stderr_buf. |
[out] | errorid | if an error has occurred, will populate with a reason code. |
[in] | timeout | Time in ms to wait for the processes to exit. If the process has not finished after the timeout expires it will be killed. M_TIMEOUT_INF will cause this to block until the process exits. Note: the time out only has 15 ms resolution. |
int M_popen_close | ( | M_popen_handle_t * | mp, |
M_popen_err_t * | errorid | ||
) |
Close the M_popen_t object.
This is a simplified wrapper around M_popen_close_ex(). This command blocks forever until the child process is done. If you need to force-kill the process after a given timeout, use M_popen_close_ex() instead of this function.
[in] | mp | M_popen_t object |
[out] | errorid | if an error has occurred, will populate with a reason code. |
const char * M_popen_strerror | ( | M_popen_err_t | err | ) |
Output human-readable error string.
[in] | err | Error as returned by M_popen() or M_popen_close(). |