cat/cat-mmap: print newline if file doesn't end in one

This commit is contained in:
Bananymous 2024-01-02 23:27:13 +02:00
parent 9fa13079f2
commit 1bd33e76e5
2 changed files with 8 additions and 0 deletions

View File

@ -23,6 +23,10 @@ bool cat_file(int fd)
if (nwrite == -1)
perror("write");
if (static_cast<uint8_t*>(addr)[st.st_size - 1] != '\n')
if (write(STDOUT_FILENO, "\n", 1) == -1)
perror("write");
if (munmap(addr, st.st_size) == -1)
{
perror("munmap");

View File

@ -3,6 +3,7 @@
bool cat_file(int fd)
{
char last = '\0';
char buffer[1024];
while (ssize_t n_read = read(fd, buffer, sizeof(buffer)))
{
@ -12,7 +13,10 @@ bool cat_file(int fd)
return false;
}
write(STDOUT_FILENO, buffer, n_read);
last = buffer[n_read - 1];
}
if (last != '\n')
write(STDOUT_FILENO, "\n", 1);
return true;
}