home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / atari / st / tech / 6540 < prev    next >
Encoding:
Text File  |  1993-01-08  |  3.1 KB  |  64 lines

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