@@ -1150,6 +1150,7 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
1150
1150
JanetAbstract orig_in = NULL , orig_out = NULL , orig_err = NULL ;
1151
1151
JanetHandle new_in = JANET_HANDLE_NONE , new_out = JANET_HANDLE_NONE , new_err = JANET_HANDLE_NONE ;
1152
1152
JanetHandle pipe_in = JANET_HANDLE_NONE , pipe_out = JANET_HANDLE_NONE , pipe_err = JANET_HANDLE_NONE ;
1153
+ const char * chdir_path = NULL ;
1153
1154
int stderr_is_stdout = 0 ;
1154
1155
int pipe_errflag = 0 ; /* Track errors setting up pipes */
1155
1156
int pipe_owner_flags = (is_spawn && (flags & 0x8 )) ? JANET_PROC_ALLOW_ZOMBIE : 0 ;
@@ -1182,6 +1183,17 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
1182
1183
}
1183
1184
}
1184
1185
1186
+ /* Optional working directory. Available for both os/execute and os/spawn. */
1187
+ if (argc > 2 ) {
1188
+ JanetDictView tab = janet_getdictionary (argv , 2 );
1189
+ Janet workdir = janet_dictionary_get (tab .kvs , tab .cap , janet_ckeywordv ("cd" ));
1190
+ if (janet_checktype (workdir , JANET_STRING )) {
1191
+ chdir_path = (const char * ) janet_unwrap_string (workdir );
1192
+ } else if (!janet_checktype (workdir , JANET_NIL )) {
1193
+ janet_panicf ("expected string for `:cd` argumnet, got %v" , workdir );
1194
+ }
1195
+ }
1196
+
1185
1197
/* Clean up if any of the pipes have any issues */
1186
1198
if (pipe_errflag ) {
1187
1199
if (pipe_in != JANET_HANDLE_NONE ) close_handle (pipe_in );
@@ -1212,6 +1224,10 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
1212
1224
}
1213
1225
const char * path = (const char * ) janet_unwrap_string (exargs .items [0 ]);
1214
1226
1227
+ if (chdir_path != NULL ) {
1228
+ startupInfo .lpCurrentDirectory = chdir_path ;
1229
+ }
1230
+
1215
1231
/* Do IO redirection */
1216
1232
1217
1233
if (pipe_in != JANET_HANDLE_NONE ) {
@@ -1305,6 +1321,9 @@ static Janet os_execute_impl(int32_t argc, Janet *argv, JanetExecuteMode mode) {
1305
1321
/* Posix spawn setup */
1306
1322
posix_spawn_file_actions_t actions ;
1307
1323
posix_spawn_file_actions_init (& actions );
1324
+ if (chdir_path != NULL ) {
1325
+ posix_spawn_file_actions_addchdir_np (& actions , chdir_path );
1326
+ }
1308
1327
if (pipe_in != JANET_HANDLE_NONE ) {
1309
1328
posix_spawn_file_actions_adddup2 (& actions , pipe_in , 0 );
1310
1329
posix_spawn_file_actions_addclose (& actions , pipe_in );
0 commit comments