home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l440 / 2.ddi / CHAP5 / PUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-26  |  1.9 KB  |  90 lines

  1. /* PUT.C -- STDERR output routines, no malloc */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <dos.h>
  8. #include <bios.h>
  9.  
  10. #define STDERR          2
  11.  
  12. #include "put.h"
  13.  
  14. // returns length of far string
  15. #ifdef _MSC_VER
  16. #define fstrlen(s)      _fstrlen(s)     // MSC 6.0
  17. #else
  18. size_t fstrlen(const char far *s)       // MSC 5.1
  19. {
  20.     size_t len = 0;
  21.     while (*s++) len++;
  22.     return len;
  23. }
  24. #endif
  25.  
  26. unsigned doswrite(int handle, char far *s, unsigned len)
  27. {
  28.     unsigned bytes;
  29.     _dos_write(handle, s, len, &bytes);
  30.     return bytes;
  31. }
  32.  
  33. unsigned put_str(char far *s)
  34. {
  35.     return doswrite(STDERR, s, fstrlen(s));
  36. }
  37.  
  38. unsigned put_chr(int c)
  39. {
  40.     return doswrite(STDERR, (void far *) &c, 1);
  41. }
  42.  
  43. #define putstr(s)   { put_str(s); put_str("\r\n"); }
  44.  
  45. #define MAX_WID     12
  46.  
  47. unsigned put_num(unsigned long u, unsigned wid, unsigned radix)
  48. {
  49.     char buf[MAX_WID+1], *p;
  50.     int i, digit;
  51.     if (wid > MAX_WID)
  52.         return;
  53.     for (i=wid-1, p=&buf[wid-1]; i >= 0; i--, p--, u /= radix)
  54.     {
  55.         digit = u % radix;
  56.         *p = digit + ((digit < 10) ? '0' : 'A' - 10);
  57.     }
  58.     buf[wid] = 0;
  59.     return doswrite(STDERR, (void far *) buf, wid);
  60. }
  61.  
  62. #define put_hex(u)      put_num(u, 4, 16)
  63. #define put_long(ul)    put_num(ul, 9, 10)
  64.  
  65. int _FAR_ _cdecl printf(const char _FAR_ *fmt, ...)
  66. {
  67.     static char buf[128];
  68.     int len;
  69.     va_list marker;
  70.     va_start(marker, fmt);
  71.     len = vsprintf(buf, fmt, marker); 
  72.     va_end(marker);
  73.     return doswrite(STDERR, (void far *) buf, len);
  74. }
  75.  
  76. unsigned get_str(char far *s, unsigned len)
  77. {
  78.     extern void (interrupt far *old_int28)(void);
  79.     unsigned rcount;
  80.     
  81.     /* give TSRs a chance by calling INT 28h */
  82.     while (! _bios_keybrd(_KEYBRD_READY))
  83.         (*old_int28)();
  84.     
  85.     if ((_dos_read(STDERR, s, len, &rcount) != 0) || (rcount < 3))
  86.         return 0;
  87.     s[rcount-2] = '\0';
  88.     return rcount-2;
  89. }
  90.