Skip to content

Commit

Permalink
man: use cat as pager to print man pages
Browse files Browse the repository at this point in the history
This reduces the required code in man and additionally,
replaces the old broken read/puts implementation.

Puts expects a null-terminated string, but write does not return
a null-terminted string.
  • Loading branch information
fischerling committed Feb 13, 2024
1 parent cad4804 commit beacbde
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions programs/man.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,19 @@ int main(int argc, char *argv[])
}
else if (argc == 2)
{
char *pager = "cat";
char filepath[PATH_MAX];
strcpy(filepath, "/usr/share/man/");
strcat(filepath, argv[1]);
strcat(filepath, ".man");
int fd = open(filepath, O_RDONLY, 42);
if (fd < 0)
{
printf("%s: No manual entry for %s\n\n", argv[0], argv[1]);
}
else
{
// Prepare the buffer for reading the man file.
char buffer[BUFSIZ];
// Put on the standard output the characters.
while (read(fd, buffer, BUFSIZ) > 0)
{
puts(buffer);
}
// Close the file descriptor.
close(fd);
// Terminate with a pair of newlines.
putchar('\n');
putchar('\n');
printf("%s: No manual entry for %s\n", argv[0], argv[1]);
exit(1);
}
close(fd);
execlp(pager, pager, filepath);
}
return 0;
}

0 comments on commit beacbde

Please sign in to comment.