Userspace: add echo

This commit is contained in:
Bananymous 2023-06-09 01:50:18 +03:00
parent 2c18adbddd
commit 7f34d00c95
3 changed files with 57 additions and 0 deletions

View File

@ -5,6 +5,7 @@ project(userspace CXX)
set(USERSPACE_PROJECTS set(USERSPACE_PROJECTS
Shell Shell
cat cat
echo
test test
yes yes
) )

View File

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

40
userspace/echo/main.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
void print_argument(const char* arg)
{
while (*arg)
{
if (*arg == '\\')
{
switch (*(++arg))
{
case 'a': fputc('\a', stdout); break;
case 'b': fputc('\b', stdout); break;
case 'c': exit(0);
case 'f': fputc('\f', stdout); break;
case 'n': fputc('\n', stdout); break;
case 'r': fputc('\r', stdout); break;
case 't': fputc('\t', stdout); break;
case 'v': fputc('\v', stdout); break;
case '\\': fputc('\\', stdout); break;
default: break;
}
}
else
fputc(*arg, stdout);
arg++;
}
}
int main(int argc, char** argv)
{
for (int i = 1; i < argc; i++)
{
print_argument(argv[i]);
if (i < argc - 1)
fputc(' ', stdout);
}
printf("\n");
return 1;
}