Skip to content

Commit c825d79

Browse files
authored
Merge pull request swiftlang#32514 from 3405691582/OpenBSD_CommandLine
Command line routines for OpenBSD.
2 parents 603ca95 + b1f29d9 commit c825d79

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

stdlib/public/stubs/CommandLine.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,42 @@ char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
242242

243243
return argv;
244244
}
245+
#elif defined(__OpenBSD__)
246+
#include <sys/types.h>
247+
#include <sys/sysctl.h>
248+
#include <sys/exec.h>
249+
250+
SWIFT_RUNTIME_STDLIB_API
251+
char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {
252+
assert(outArgLen != nullptr);
253+
if (_swift_stdlib_ProcessOverrideUnsafeArgv) {
254+
*outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc;
255+
return _swift_stdlib_ProcessOverrideUnsafeArgv;
256+
}
257+
258+
int mib[2] = {CTL_VM, VM_PSSTRINGS};
259+
struct _ps_strings _ps;
260+
size_t len = sizeof(_ps);
261+
262+
if (sysctl(mib, 2, &_ps, &len, NULL, 0) == -1) {
263+
char **empty_argv = static_cast<char **>(calloc(1, sizeof(char *)));
264+
empty_argv[0] = nullptr;
265+
*outArgLen = 0;
266+
return empty_argv;
267+
}
268+
269+
struct ps_strings *ps = static_cast<struct ps_strings *>(_ps.val);
270+
*outArgLen = ps->ps_nargvstr;
271+
272+
char **argv_copy =
273+
static_cast<char **>(calloc(ps->ps_nargvstr + 1, sizeof(char *)));
274+
for(int i = 0; i < ps->ps_nargvstr; i++) {
275+
argv_copy[i] = strdup(ps->ps_argvstr[i]);
276+
}
277+
argv_copy[ps->ps_nargvstr] = nullptr;
278+
279+
return argv_copy;
280+
}
245281
#else // Add your favorite OS's command line arg grabber here.
246282
SWIFT_RUNTIME_STDLIB_API
247283
char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) {

0 commit comments

Comments
 (0)