// MKCP183catc03 20240721
// description: cat command, read stdin if no argument given.
// using system call on LINUX
// board: Milk-V DUO 256MB
// MPU:RISC-V CPU 1 x C906@1GHz + 1 x C906@700MHz
// SD card: DUOV1.1.1.img
// os cat /etc/os-release NAME=Buildroot VERSION=20240528-2053
// device:
// language: C ,cross compile riscv64-unknown-linux-musl-gcc
// IDE: VScode using wsl ubuntu on windows11
// duo-example/MKCP203catc03
// | makefile
// | catc03.c
// | text.txt // 0123456789
//$ cd duo-examples
//$ source envsetup.sh
//$ cd MKCP203catc03
//$ make
//$ nano text.txt >> 0123456789
//--------------------------------------
// target folder mkdir MKCP203catc03
//$ scp -O catc03 root@192.168.42.1:/root/MKCP203catc03 >> password: milkv
//$ scp -O text.txt root@192.168.42.1:/root/MKCP203catc03 >> password: milkv
//$ ssh root@192.168.42.1 >> password: milkv
//# cd MKCP203catc03
//# ./catc03 text.txt >> 0123456789 // read file
//# .catc03 \n 123 unitl ^C >> 123 // read stdin
#include // for standard input/output (like fgetc, putchar).
#include // for exit function.
void doCat(FILE *f);// Reads characters from pointer,Prints each character
int main(int argc, char *argv[]){
if (argc == 1) { // no argument given.
doCat(stdin); // read standard input
}else{
for (int i = 1; i < argc; i++) {
FILE *f;
f = fopen(argv[i], "r"); //file open read mode
if (!f) { // Error Handling
perror(argv[i]); // error message
exit(1); // exit
}
doCat(f); // read file
fclose(f); // file close
}
}
exit(0);
}
//-------------------------------------
void doCat(FILE *f){
int c;
while ((c = fgetc(f)) != EOF) {
if (putchar(c) < 0) exit(1);
}
}