cat/cat-mmap: print newline if file doesn't end in one
This commit is contained in:
parent
9fa13079f2
commit
1bd33e76e5
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue