BuildSystem: Cleanup userspace directory layout

userspace programs are now in userspace/programs
userspace tests are now in userspace/tests

This makes listing userspace projects much cleaner. Libraries were
already separated to their own directory, so other programs should also.
This commit is contained in:
2024-07-03 09:15:22 +03:00
parent 5dc441c4af
commit 8ddab05ed3
107 changed files with 78 additions and 67 deletions

View File

@@ -0,0 +1,8 @@
set(SOURCES
main.cpp
)
add_executable(cat ${SOURCES})
banan_link_library(cat libc)
install(TARGETS cat OPTIONAL)

View File

@@ -0,0 +1,50 @@
#include <fcntl.h>
#include <stdio.h>
bool cat_file(int fd)
{
char last = '\0';
char buffer[1024];
while (ssize_t n_read = read(fd, buffer, sizeof(buffer)))
{
if (n_read == -1)
{
perror("read");
return false;
}
write(STDOUT_FILENO, buffer, n_read);
last = buffer[n_read - 1];
}
if (last != '\n')
write(STDOUT_FILENO, "\n", 1);
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;
}