summaryrefslogtreecommitdiff
path: root/process.h
blob: d8bb6ecbd813f0f2296642f79744b51d7b195dc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// like popen, but allowing for non-blocking reads
#ifndef PROCESS_H_
#define PROCESS_H_

typedef struct Process Process;

// zero everything except what you're using
typedef struct {
	bool stdin_blocking;
	bool stdout_blocking;
} ProcessSettings;

// execute the given command (like if it was passed to system()).
// returns false on failure
bool process_run_ex(Process *proc, const char *command, const ProcessSettings *props);
// like process_run_ex, but with the default settings
bool process_run(Process *process, char const *command);
// returns the error last error produced, or NULL if there was no error.
char const *process_geterr(Process *process);
// write to stdin
// returns:
// -2 on error
// or a non-negative number indicating the number of bytes written.
long long process_write(Process *process, const char *data, size_t size);
// read from stdout+stderr
// returns:
// -2 on error
// -1 if no data is available right now
// 0 on end of file
// or a positive number indicating the number of bytes read to data (at most size)
long long process_read(Process *process, char *data, size_t size);
// Checks if the process has exited. Returns:
// -1 if the process returned a non-zero exit code, or got a signal.
// 1  if the process exited successfully
// 0  if the process hasn't exited.
// If message is not NULL, it will be set to a description of what happened (e.g. "exited successfully")
int process_check_status(Process *process, char *message, size_t message_size);
// kills process if still running
void process_kill(Process *process);

#endif