Hi,
Declared usage() as __dead since it won't return, instructions() called
a handrolled pager (/bin/cat) using fork which I replaced for a simple
cicle of while(fgets) then fputs (greatly inspired, to not saying it was
bluntly copied, by a previous patch sent by tedu@ for another application).
After instructions() function is run then fish(6) only needs pledge "stdio".
Index: fish.c
===================================================================
RCS file: /cvs/src/games/fish/fish.c,v
retrieving revision 1.17
diff -u -p -u -r1.17 fish.c
--- fish.c 18 Feb 2015 23:20:45 -0000 1.17
+++ fish.c 23 Nov 2015 13:09:38 -0000
@@ -79,7 +79,7 @@ int nrandom(int);
void printhand(const int *);
void printplayer(int);
int promove(void);
-void usage(void);
+__dead void usage(void);
int usermove(void);
int
@@ -99,6 +99,10 @@ main(int argc, char *argv[])
}
instructions();
+
+ if (pledge("stdio", NULL) == -1)
+ err(1, "pledge");
+
init();
if (nrandom(2) == 1) {
@@ -444,46 +448,26 @@ getans(const char *prompt)
void
instructions(void)
{
- const char *pager;
- pid_t pid;
- int status;
int input;
- int fd;
+ char buf[1024];
+ FILE *fp;
if (getans("Would you like instructions (y or n)? ") == 0)
return;
- if ((fd = open(_PATH_INSTR, O_RDONLY)) == -1)
+ if ((fp = fopen(_PATH_INSTR, "r")) == NULL)
(void)printf("No instruction file found!\n");
else {
- switch (pid = fork()) {
- case 0: /* child */
- if (!isatty(1))
- pager = "/bin/cat";
- else {
- if (!(pager = getenv("PAGER")) || (*pager == 0))
- pager = _PATH_MORE;
- }
- if (dup2(fd, 0) == -1)
- err(1, "dup2");
- (void)execl(_PATH_BSHELL, "sh", "-c", pager, (char
*)NULL);
- err(1, "exec sh -c %s", pager);
- /* NOT REACHED */
- case -1:
- err(1, "fork");
- /* NOT REACHED */
- default:
- (void)waitpid(pid, &status, 0);
- close(fd);
- break;
- }
+ while (fgets(buf, sizeof(buf), fp))
+ fputs(buf, stdout);
+ fclose(fp);
}
(void)printf("Hit return to continue...\n");
while ((input = getchar()) != EOF && input != '\n');
}
-void
+__dead void
usage(void)
{
(void)fprintf(stderr, "usage: fish [-p]\n");