Userspace: Implement cat-mmap

This behaves exactly as cat, but uses mmap to read the file.
This commit is contained in:
Bananymous 2023-09-29 17:24:55 +03:00
parent d54c6b7f6b
commit 7a5bb6a56b
3 changed files with 80 additions and 0 deletions

View File

@ -4,6 +4,7 @@ project(userspace CXX)
set(USERSPACE_PROJECTS
cat
cat-mmap
dd
echo
id

View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.26)
project(cat-mmap CXX)
set(SOURCES
main.cpp
)
add_executable(cat-mmap ${SOURCES})
target_compile_options(cat-mmap PUBLIC -O2 -g)
target_link_libraries(cat-mmap PUBLIC libc)
add_custom_target(cat-mmap-install
COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/cat-mmap ${BANAN_BIN}/
DEPENDS cat-mmap
USES_TERMINAL
)

View File

@ -0,0 +1,62 @@
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
bool cat_file(int fd)
{
struct stat st;
if (fstat(fd, &st) == -1)
{
perror("stat");
return false;
}
void* addr = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
{
perror("mmap");
return false;
}
ssize_t nwrite = write(STDOUT_FILENO, addr, st.st_size);
if (nwrite == -1)
perror("write");
if (munmap(addr, st.st_size) == -1)
{
perror("munmap");
return false;
}
return true;
}
int main(int argc, char** argv)
{
int ret = 0;
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
int fd = open(argv[i], O_RDONLY);
if (fd == -1)
{
perror(argv[i]);
ret = 1;
continue;
}
if (!cat_file(fd))
ret = 1;
close(fd);
}
}
else
{
if (!cat_file(STDIN_FILENO))
ret = 1;
}
return ret;
}