Page 1 of 1

How SDK:/Local/C/cat works? [SOLVED]

Posted: Fri Jan 31, 2025 7:33 am
by gdridi
Hello,

I have bug with 'cat' when running on my new Shell without parameter and when input terminated with a line without a new line (character).

I was wondering how cat works internally ?

I have the trouble with my Shell only and not in AmigaShell. Perhaps I forgot something on ACTION_WRITE (somewhere a null) because without no new line at the end of the input, cat displays the content of the file and keep going on trash the screen with 'garbage' characters.

It works fine with a getchar() putchar() program.

Perhaps, cat use bloc by bloc output but my ACTION_WRITE needs a null at the end of bloc/line.

example : cat <filewithnoNewlineAtEnd

Works fine with AmigaShell
But none on my Shell (limit 4096 chars on Read) perhaps cat reads a bloc more than 4Ko ?

I’m lost!

Thank you,
DGILLES

Re: How SDK:/Local/C/cat works?

Posted: Fri Jan 31, 2025 2:30 pm
by thomasrapp
ACTION_WRITE surely does not null-terminate its buffer. The buffer can be completely filled with data, up to the last byte. The number of bytes in the dp_Arg3 field determines the length of the buffer, not some terminating byte. It's just coincidence that you always found a null byte in the buffer. Actually a program can include null bytes in the middle of its output. These should just be skipped or rather printed like an unprintable character but not terminate the output.

Re: How SDK:/Local/C/cat works?

Posted: Fri Jan 31, 2025 3:00 pm
by gdridi
Hello,

Perhaps 'cat' use fstat() to know the size of buffer and the appropriate ACTION_INFO or whatever is not implemented in my handler?

Sincerely yours, thank you for your reply Thomas(Repp)

DGILLES

Re: How SDK:/Local/C/cat works?

Posted: Fri Jan 31, 2025 5:14 pm
by thomasrapp
I don't think it needs to know the buffer size. It just reads from the file and writes to the console in large chunks.

Try the AmigaDOS command:

copy filewithnoNewlineAtEnd to console:

it should have the same effect.

Or write a program which does something like this:

Code: Select all

char buffer[250000];

BPTR f = Open("sys:documentation/roadshow/Roadshow.doc",MODE_OLDFILE);
long actual = Read (f,buffer,sizeof(buffer));
Close (f);
f = Open ("console:",MODE_NEWFILE);
Write (f,buffer,actual-2); /* cut off the last two \n */
Close (f);

Re: How SDK:/Local/C/cat works?

Posted: Fri Jan 31, 2025 7:28 pm
by gdridi
I think it uses stat properties so call fstat() somewhere :

coreutils/src/ioblksize.h at master · coreutils/coreutils · GitHub

https://github.com/coreutils/coreutils/ ... oblksize.h

Now the explanation:
dd - How does cat 'know' the optimum block size to use? - Unix & Linux Stack Exchange

https://unix.stackexchange.com/question ... ize-to-use

Sincerely yours,
DGILLES

Re: How SDK:/Local/C/cat works? [solved]

Posted: Wed Feb 12, 2025 8:14 pm
by gdridi
Hello!

The problem was when the last line(last char) is not ended by a NewLine.
With 'cat' the last line of characters when not ended even with a null character '\0'.
So it displays garbage after the correct lines.

Sorry for the inconvenience,
DGILLES