home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / msdos / programm / 9384 < prev    next >
Encoding:
Text File  |  1992-09-15  |  2.1 KB  |  61 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsc!cbfsb!cbnewsg.cb.att.com!rnichols
  3. From: rnichols@cbnewsg.cb.att.com (robert.k.nichols)
  4. Subject: Re: Print to stdout in binary mode?
  5. Message-ID: <1992Sep16.013220.10987@cbfsb.cb.att.com>
  6. Summary: Need to force the driver into binary mode via int21, func 4401h
  7. Keywords: stdout,binary
  8. Sender: news@cbfsb.cb.att.com
  9. Organization: AT&T
  10. References: <BuLKyI.Jz8@news.cso.uiuc.edu>
  11. Date: Wed, 16 Sep 1992 01:32:20 GMT
  12. Lines: 47
  13.  
  14. In article <BuLKyI.Jz8@news.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:
  15. >
  16. >    Hi.  I am trying to make a C program write to stdout in binary mode,
  17. >and can't figure out how to do it.  The problem is that I am writing to a
  18. >device which expects lines to be terminated by a linefeed, *not* an LF/CR
  19. >pair.  I would like to do this to stdout, to be able to pipe it.  I suppose 
  20. >I could write to an intermediate file, if I had to, though.  Can anyone
  21. >figure out a way?  I tried writing to "con", but that can't be redirected.
  22. ...
  23.  
  24. I fought this battle, too, and finally discovered that I had to make an
  25. int21 call to force the driver into binary mode.  Just using normal I/O
  26. calls to set binary mode didn't work.  Here's my code fragment:
  27.  
  28.     int c,n;
  29.     FILE *prn;    /* usually opened as device "PRN" in "wb" mode,
  30.                but might be stdout */
  31.  
  32.     c = setmode(fileno(prn),O_BINARY);    /* One would think that this
  33.                            would suffice */
  34.  
  35.         /* Ensure that the driver is REALLY in binary mode */
  36.     n = fileno(prn);
  37.     _asm {
  38.         mov    bx,n
  39.         mov    ax,4400h        /* 4400: get device data */
  40.         int    21h
  41.         mov    c,dx
  42.     }
  43.     if(!(c & 0x20)) {    /* If not in binary mode (and it NEVER is) */
  44.         c = c & 0xff | 0x20;        /* set the BINARY mode bit */
  45.         _asm {
  46.             mov    bx,n
  47.             mov    ax,4401h    /* 4401: set device data */
  48.             mov    dx,c
  49.             int    21h
  50.         }
  51.     }
  52.  
  53.  
  54. Yes, I know that the code could have been simpler, but I was experimenting,
  55. and once I found the magic combination I never bothered to clean it up.
  56. (Why optimize code that executes once during a program's initialization?)
  57.  
  58. Bob Nichols
  59. AT&T Bell Laboratories
  60. rnichols@ihlpm.ih.att.com
  61.