home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.atari.st.tech
- Path: sparky!uunet!mcsun!sun4nl!spider.research.ptt.nl!ull!marcob
- From: marcob@ull.research.ptt.nl (Blom M.A.)
- Subject: Re: Porting UNIX Code to the ST
- Message-ID: <1993Jan8.123049.27647@spider.research.ptt.nl>
- Sender: usenet@spider.research.ptt.nl (USEnet News)
- Nntp-Posting-Host: ull.research.ptt.nl
- Organization: PTT Research
- References: <1993Jan5.214420.8268@sae.com> <1993Jan7.124324.27805@its.bt.co.uk>
- Date: Fri, 8 Jan 1993 12:30:49 GMT
- Lines: 51
-
- In article <1993Jan7.124324.27805@its.bt.co.uk> jvt@its.bt.co.uk (John Trickey) writes:
- >In article <1993Jan5.214420.8268@sae.com> malay@sae.com writes:
- >>
- >>I have some code that generates a binary file - I compiled it with GCC 2.3.1
- >>and ran it on the ST - it works fine, it generates a binary file; however, the
- >>print statements seem to be outputting a CNTRL-M (^M) - I don't need these
- >>extra characters what should I do to get rid of them???
- >
- >
- >The problem you have encountered is that Unix treats all files alike while
- >DOS, TOS etc do not. In Unix the open is fopen(filename,mode) where mode is
- >a char * of "a","r","w","r+","w+","a+". In TOS these are for text files with
- >CRLF being converted to LF on input and LF being converted to CRLF on output.
- >Note CR is ^M.
- >To switch this off you need to open the file in binary mode. These modes
- >are "ab","rb","wb","rb+","wb+","ab+". The format "r+b" etc is also acceptable.
- >
-
- Actually, in the Mint library there is a function for this called _binmode().
-
- Quote from the source of _binmode():
- * if called with TRUE, then subsequently all fopens have _IOBIN
- * by default on open. This will make life much easier for
- * people who have been using the Gnu lib (any my job with the compiler
- * much easier too, don't have to go hunting for fopens())
-
- In other words, _binmode(TRUE) makes sure that all calls to fopen()
- automatically open files in binary mode.
-
- Unfortunately, this doesn't solve the problem for programs that use stdout
- to write (binary) results to a file, i.e. programs that simply use printf()
- and rely on IO-redirection from within a shell. Many Unix programs do this
- (pbmplus, cjpeg, djpeg, etc.) The problem with this is that stdout is
- already open, and NOT in binary mode. There doesn't seem to be a (legal) way
- to set stdout to binary mode. Still, there is a trick, but it only works with
- gcc (I still use version 1.39 or so) and the MiNT library. It may NOT even work
- with future versions of the MiNT libraries. OK, now for the trick:
- In main(), before any output is sent to stdout, I add this to the code:
-
- stdout -> _flag |= _IOBIN;
-
- This sets stdout to binary mode. It relies on how the MiNT library internally
- represents and deals with files. I've successfully compiled djpeg en cjepg
- (version 4) with this trick without changing anything else in the code.
- I also used it to compile the previous (=old) version of pbmplus.
-
- Perhaps someone working on the MiNT library could add a function to the library
- that sets stdout to binary. This would make porting Unix programs somewhat
- easier. And it would turn this trick into a feature :-)
-
- Marco Blom.
-