Userspace: Add u8sum

This program caluculates the sum of bytes in file mod 256
This commit is contained in:
Bananymous 2023-06-12 20:36:16 +03:00
parent 32e1473c94
commit 326a30d1af
3 changed files with 46 additions and 0 deletions

View File

@ -10,6 +10,7 @@ set(USERSPACE_PROJECTS
ls
Shell
test
u8sum
yes
)

View File

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

29
userspace/u8sum/main.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char** argv)
{
for (int i = 1; i < argc; i++)
{
FILE* fp = fopen(argv[i], "r");
if (fp == nullptr)
{
perror("nullptr");
continue;
}
uint8_t sum = 0;
uint8_t buffer[1024];
while (size_t ret = fread(buffer, 1, sizeof(buffer), fp))
for (int j = 0; j < ret; j++)
sum += buffer[j];
if (ferror(fp))
perror("fread");
else
printf("%s: %02x\n", argv[i], sum);
fclose(fp);
}
}