How to redirect stdout on Atari?

C and PASCAL (or any other high-level languages) in here please

Moderators: Zorro 2, Moderator Team

RA_pdx
Captain Atari
Captain Atari
Posts: 222
Joined: Sun Feb 02, 2003 12:01 pm
Location: Nuernberg/GERMANY

How to redirect stdout on Atari?

Post by RA_pdx »

Hi,

I want to redirect stdout to a log file and write with printf to this file.

How can i do this on an Atari machine?

I have tried to use Gemdos functions Fforce and Fdup but with no success. Text was not anymore on screen but also not in my created file (it is created but empty).

Code: Select all

FILE*	pFile;
pFile = fopen(strFile, "w");
	
if (pFile == NULL){
	return false;
}
	
Fforce(1, Fdup(fileno(pFile)));

printf("Test");
	
fclose(pFile);
>> > raZen/Paradox < <<

Atari 1040STE, TOS 2.06, 4MB, MC68010, IDE 8GB SSD, Gigafile
m0n0
Captain Atari
Captain Atari
Posts: 481
Joined: Mon Oct 05, 2009 3:13 am

Re: How to redirect stdout on Atari?

Post by m0n0 »

With mintlib it's easy (don't know about PureC lib, or AHCC...., but you can look at the mintlib source to see if you can implement it for yourself):


freopen("stdout.log", "a+", stdout);
freopen("stderr.log", "a+", stderr);

eventually you want to disable output buffering:
setbuf(stderr, NULL);
setbuf(stdout, NULL);

now you can printf to stdout / stderr and it will be written to the file....
User avatar
BlankVector
Atari Super Hero
Atari Super Hero
Posts: 607
Joined: Wed Oct 24, 2007 7:52 pm
Location: France

Re: How to redirect stdout on Atari?

Post by BlankVector »

m0n0's answer is the right one.

BTW:
RA_pdx wrote:

Code: Select all

Fforce(1, Fdup(fileno(pFile)));
You should remove Fdup() there.
http://toshyp.atari.org/en/005009.html#Fdup
http://toshyp.atari.org/en/005009.html#Fforce
Subscribe to my Vretrocomputing channel on YouTube and Facebook. Latest video: Display a monochrome pixel in assembly language on Atari ST.
RA_pdx
Captain Atari
Captain Atari
Posts: 222
Joined: Sun Feb 02, 2003 12:01 pm
Location: Nuernberg/GERMANY

Re: How to redirect stdout on Atari?

Post by RA_pdx »

I have already tried it without Fdup but both versions didn´t work.

Thanks, i will have a look on the source of mintlib.
Even i had the hope that there is an easy way with TOS.
>> > raZen/Paradox < <<

Atari 1040STE, TOS 2.06, 4MB, MC68010, IDE 8GB SSD, Gigafile
User avatar
BlankVector
Atari Super Hero
Atari Super Hero
Posts: 607
Joined: Wed Oct 24, 2007 7:52 pm
Location: France

Re: How to redirect stdout on Atari?

Post by BlankVector »

RA_pdx wrote:I have already tried it without Fdup but both versions didn´t work.
Do you use Hatari with GEMDOS hard disk emulation?
Redirection does not seem to work in that case. The result is always an empty file.
However, if the redirected file is located on the floppy, it works as expected.
Subscribe to my Vretrocomputing channel on YouTube and Facebook. Latest video: Display a monochrome pixel in assembly language on Atari ST.
User avatar
wongck
Ultimate Atarian
Ultimate Atarian
Posts: 13546
Joined: Sat May 03, 2008 2:09 pm
Location: Far East

Re: How to redirect stdout on Atari?

Post by wongck »

This works for me, compiled under Pure C.

Code: Select all

#include <tos.h>
#include <stdio.h>

void main(void)
{
  long fno;
  
  fno = Fcreate("myoutput.txt", 0);
  Fforce( 1, (long) fno);    /* redirect screen to file */
  printf("hello world");
  Fclose(fno);
  
} 
hello world goes into the file called myoutput.txt.
So may be it is Hatari like what Vincent said.
My Stuff: FB/Falcon CT63 CTPCI ATI RTL8139 USB 512MB 30GB HDD CF HxC_SD/ TT030 68882 4+32MB 520MB Nova/ 520STFM 4MB Tos206 SCSI
Shared SCSI Bus:ScsiLink ethernet, 9GB HDD,SD-reader @ http://phsw.atari.org
My Atari stuff that are no longer for sale due to them over 30 years old - click here for list
RA_pdx
Captain Atari
Captain Atari
Posts: 222
Joined: Sun Feb 02, 2003 12:01 pm
Location: Nuernberg/GERMANY

Re: How to redirect stdout on Atari?

Post by RA_pdx »

BlankVector wrote:
RA_pdx wrote:I have already tried it without Fdup but both versions didn´t work.
Do you use Hatari with GEMDOS hard disk emulation?
Redirection does not seem to work in that case. The result is always an empty file.
However, if the redirected file is located on the floppy, it works as expected.
Yes thanks, you are right. I am using Hatari with GEMDOS hard disk.
Now i know that it was not my fault that it doesn´t work - i spent several hours on this problem... :cry:

Thanks to everybody for your help!
>> > raZen/Paradox < <<

Atari 1040STE, TOS 2.06, 4MB, MC68010, IDE 8GB SSD, Gigafile
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 3999
Joined: Sun Jul 31, 2011 1:11 pm

Re: How to redirect stdout on Atari?

Post by Eero Tamminen »

RA_pdx wrote:Yes thanks, you are right. I am using Hatari with GEMDOS hard disk.
Now i know that it was not my fault that it doesn´t work - i spent several hours on this problem...
When having problems with OS calls, it helps to check their return values. TOS should be returning "invalid handle" error in this case.

The reason why Fforce() isn't implemented is that Hatari GEMDOS emulation doesn't (currently) have any knowledge of programs, it just catches GEMDOS calls.

Implications of this mean some differences to TOS:
* GEMDOS emulation file handles are global to the whole system, not specific to the program.
Other programs can access them if they just use correct handle (GEMDOS emulation handle IDs start from 64).
* Files that program leaves open aren't closed on program termination like TOS does.
I.e. GEMDOS emulated file handles could run out if exiting programs leave them open (unlike with TOS).
* Forced file handles wouldn't be automatically un-redirected on program exit (when it forgets to close it).
This which would be pretty much catastrophe. :-)

I'm looking whether these could be worked around for next Hatari version.
RA_pdx
Captain Atari
Captain Atari
Posts: 222
Joined: Sun Feb 02, 2003 12:01 pm
Location: Nuernberg/GERMANY

Re: How to redirect stdout on Atari?

Post by RA_pdx »

Eero Tamminen wrote:I'm looking whether these could be worked around for next Hatari version.
That would be great :!:
>> > raZen/Paradox < <<

Atari 1040STE, TOS 2.06, 4MB, MC68010, IDE 8GB SSD, Gigafile
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 3999
Joined: Sun Jul 31, 2011 1:11 pm

Re: How to redirect stdout on Atari?

Post by Eero Tamminen »

RA_pdx wrote:
Eero Tamminen wrote:I'm looking whether these could be worked around for next Hatari version.
That would be great :!:
I've looked now into that and I don't think it's possible. There doesn't seem to be any reliable way to catch all the possible ways of program starting (GEMDOS pexec, AES shel_write etc) and terminating (3 Pterm functions, crashing etc) so that GEMDOS emulation current program information would be reliably in sync with TOS.

If you're sure that all your programs close files they Fforced, I've mailed a patch to hatari-devel mailing list to get Fforce() files redirected under GEMDOS emulation. It's not something that can be put into release though, as redirections are removed only when Fclose is called on related handle (not on program termination).

To release I'll probably add just console warnings when trying to Fforce() GEMDOS emulated files and when files are left open at Pterm*().
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 3999
Joined: Sun Jul 31, 2011 1:11 pm

Re: How to redirect stdout on Atari?

Post by Eero Tamminen »

Eero Tamminen wrote:I'll probably add just console warnings when trying to Fforce() GEMDOS emulated files and when files are left open at Pterm*().
I've commited that to Mercurial and added notes about this to GEMDOS emulation section in Hatari manual:
http://hg.tuxfamily.org/mercurialroot/h ... _emulation

I'd recommend reading the documentation on that, Fforce() & file closing on program termination are not the only issues for GEMDOS HD emulation...
RA_pdx
Captain Atari
Captain Atari
Posts: 222
Joined: Sun Feb 02, 2003 12:01 pm
Location: Nuernberg/GERMANY

Re: How to redirect stdout on Atari?

Post by RA_pdx »

I always close all opened files so that´s no problem.
It´s a good workaround for me. Thanks Eero. :cheers:
>> > raZen/Paradox < <<

Atari 1040STE, TOS 2.06, 4MB, MC68010, IDE 8GB SSD, Gigafile
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 3999
Joined: Sun Jul 31, 2011 1:11 pm

Re: How to redirect stdout on Atari?

Post by Eero Tamminen »

RA_pdx wrote:I always close all opened files so that´s no problem.
It´s a good workaround for me. Thanks Eero. :cheers:
Note that even when using GEMDOS emulation (without the attached fforce.diff against Hatari Mercurial tip [1]), any files that reside on floppy or HD images can still be redirected as those are forwarded to TOS.

[1] http://hg.tuxfamily.org/mercurialroot/h ... i/shortlog
You do not have the required permissions to view the files attached to this post.
User avatar
Eero Tamminen
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 3999
Joined: Sun Jul 31, 2011 1:11 pm

Re: How to redirect stdout on Atari?

Post by Eero Tamminen »

I think I found a way to do redirecting so that it's safe (gets unredirected when needed). Code to do that is commited to Hatari repository.

Hatari will now also tell about how many file handles were left open / re-directed when program terminates normally (through Pterm*()).
RA_pdx
Captain Atari
Captain Atari
Posts: 222
Joined: Sun Feb 02, 2003 12:01 pm
Location: Nuernberg/GERMANY

Re: How to redirect stdout on Atari?

Post by RA_pdx »

Great news, thanks again.
>> > raZen/Paradox < <<

Atari 1040STE, TOS 2.06, 4MB, MC68010, IDE 8GB SSD, Gigafile

Return to “C / PASCAL etc.”