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

This forum is for general developer support questions.
Post Reply
User avatar
gdridi
Posts: 94
Joined: Sat Aug 11, 2012 10:17 am

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

Post 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
Last edited by gdridi on Wed Feb 12, 2025 8:15 pm, edited 1 time in total.
User avatar
thomasrapp
Posts: 315
Joined: Sat Jun 18, 2011 11:22 pm

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

Post 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.
User avatar
gdridi
Posts: 94
Joined: Sat Aug 11, 2012 10:17 am

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

Post 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
User avatar
thomasrapp
Posts: 315
Joined: Sat Jun 18, 2011 11:22 pm

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

Post 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);
User avatar
gdridi
Posts: 94
Joined: Sat Aug 11, 2012 10:17 am

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

Post 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
User avatar
gdridi
Posts: 94
Joined: Sat Aug 11, 2012 10:17 am

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

Post 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
Post Reply