-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps-exec.c
96 lines (75 loc) · 1.66 KB
/
ps-exec.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Cross-Platform System Programming Guide: L1: execute a new program and forget about it */
#include <assert.h>
#ifdef _WIN32
#include <windows.h>
typedef HANDLE ps;
#define _PS_NULL INVALID_HANDLE_VALUE
ps ps_exec(const char *filename, const char **argv)
{
wchar_t wfn[1000];
if (0 == MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfn, 1000))
return _PS_NULL;
// convert command-line arguments array to a single UTF-16 string
wchar_t wargs[1000];
size_t n = 0;
for (size_t i = 0; argv[i] != NULL; i++) {
if (i != 0)
wargs[n++] = ' ';
int r = MultiByteToWideChar(CP_UTF8, 0, argv[i], -1, &wargs[n], 1000 - n);
if (r == 0)
return _PS_NULL;
r--;
n += r;
}
STARTUPINFOW si = {};
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION info;
if (!CreateProcessW(wfn, wargs, NULL, NULL, /*inherit handles*/ 0
, 0, /*environment*/ NULL, NULL, &si, &info))
return _PS_NULL;
CloseHandle(info.hThread);
return info.hProcess;
}
void ps_close(ps p)
{
CloseHandle(p);
}
#else // UNIX:
#include <unistd.h>
extern char **environ;
typedef int ps;
#define _PS_NULL (-1)
/** Create a new process.
Return process descriptor;
_PS_NULL on error */
ps ps_exec(const char *filename, const char **argv)
{
pid_t p = vfork();
if (p != 0)
return p;
execve(filename, (char**)argv, environ);
_exit(255);
return 0;
}
/** Close process descriptor */
void ps_close(ps p)
{
(void)p;
}
#endif
void main()
{
const char *path = "dir-list", *arg0 = "dir-list";
#ifdef _WIN32
path = "dir-list.exe";
#endif
// create a new process
const char *args[] = {
arg0,
NULL,
};
ps p = ps_exec(path, args);
assert(p != _PS_NULL);
// close process descriptor
ps_close(p);
}