ref: 3bf29c945492fa42e9681794504b640ccbc04fe9
parent: 06cb1082b7b827cbdcc0c68e07ea57d6b7a97ff1
author: zamfofex <zamfofex@twdb.moe>
date: Sun Apr 21 20:24:58 EDT 2024
accept optional working directory for ‘moonfish_spawn’
--- a/tools/analyse.c
+++ b/tools/analyse.c
@@ -646,7 +646,7 @@
fancy->x = 0;
fancy->y = 0;
- moonfish_spawn(argv[0], command, &fancy->in, &fancy->out);
+ moonfish_spawn(argv[0], command, &fancy->in, &fancy->out, NULL);
fancy->i = 0;
fancy->count = 1;
--- a/tools/battle.c
+++ b/tools/battle.c
@@ -183,7 +183,7 @@
if (*command != NULL && !strcmp(*command, "--"))
command++;
- moonfish_spawn(argv0, command, &bot->in, &bot->out);
+ moonfish_spawn(argv0, command, &bot->in, &bot->out, NULL);
if (bot->ugi) fprintf(bot->in, "ugi\n");
else fprintf(bot->in, "uci\n");
--- a/tools/chat.c
+++ b/tools/chat.c
@@ -174,7 +174,7 @@
moonfish_chess(&chess);
- moonfish_spawn(argv0, command, &in, &out);
+ moonfish_spawn(argv0, command, &in, &out, NULL);
fprintf(in, "uci\n");
moonfish_wait(out, "uciok");
--- a/tools/lichess.c
+++ b/tools/lichess.c
@@ -254,7 +254,7 @@
game = data;
- moonfish_spawn(game->argv0, game->argv, &in, &out);
+ moonfish_spawn(game->argv0, game->argv, &in, &out, NULL);
tls = moonfish_connect(game->argv0, game->host, game->port);
snprintf(request, sizeof request, "GET /api/bot/game/stream/%s", game->id);
--- a/tools/play.c
+++ b/tools/play.c
@@ -319,7 +319,7 @@
name = names;
- moonfish_spawn(argv[0], command, &in, &out);
+ moonfish_spawn(argv[0], command, &in, &out, NULL);
if (tcgetattr(0, &moonfish_termios))
{--- a/tools/tools.h
+++ b/tools/tools.h
@@ -15,7 +15,7 @@
char *description;
};
-void moonfish_spawn(char *argv0, char **argv, FILE **in, FILE **out);
+void moonfish_spawn(char *argv0, char **argv, FILE **in, FILE **out, char *directory);
char *moonfish_next(FILE *file);
char *moonfish_wait(FILE *file, char *name);
char **moonfish_args(struct moonfish_arg *args, char *rest_format, int argc, char **argv);
--- a/tools/ugi.c
+++ b/tools/ugi.c
@@ -44,7 +44,7 @@
void *(*start1)(void *data);
void *(*start2)(void *data);
- moonfish_spawn(argv0, argv, &in, &out);
+ moonfish_spawn(argv0, argv, &in, &out, NULL);
for (;;)
{--- a/tools/utils.c
+++ b/tools/utils.c
@@ -9,10 +9,10 @@
#include "tools.h"
-static int moonfish_fork(char *argv0, char **argv, int *in_fd, int *out_fd)
+static int moonfish_fork(char *argv0, char **argv, int *in_fd, int *out_fd, char *directory)
{int p1[2], p2[2];
- int pid, fd;
+ int pid;
if (pipe(p1) < 0) return 1;
if (pipe(p2) < 0) return 1;
@@ -29,11 +29,13 @@
return 0;
}
- fd = open("/dev/null", O_RDONLY);- if (fd < 0)
+ if (directory != NULL)
{- fprintf(stderr, "%s: %s: %s\n", argv0, argv[0], strerror(errno));
- exit(1);
+ if (chdir(directory))
+ {+ perror(argv0);
+ exit(1);
+ }
}
dup2(p1[0], 0);
@@ -50,11 +52,11 @@
exit(1);
}
-void moonfish_spawn(char *argv0, char **argv, FILE **in, FILE **out)
+void moonfish_spawn(char *argv0, char **argv, FILE **in, FILE **out, char *directory)
{int in_fd, out_fd;
- if (moonfish_fork(argv0, argv, &in_fd, &out_fd))
+ if (moonfish_fork(argv0, argv, &in_fd, &out_fd, directory))
{perror(argv0);
exit(1);
--
⑨