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

  1. /* FILE.C */
  2. #include <dos.h>
  3. #include <conio.h>
  4. #include <fcntl.h>
  5. #include <share.h>
  6.  
  7. char file_prompt[] = "File? ";
  8. char cant_open[] = "Can't open file\r\n";
  9. char error_reading[] = "Error reading file\r\n";
  10. char insuff_mem[] = "Insufficient memory; Press any key...\r\n";
  11. char crlf[] = "\r\n";
  12.  
  13. #define PUTSTR(s)   \
  14.     _dos_write(STDERR, (char far *) s, sizeof(s)-1, &wcount)
  15.  
  16. #define MIN_PARAS   4
  17. #define WANT_PARAS  64
  18. #define BYTES       (paras << 4)
  19.  
  20. #define STDERR      2
  21.  
  22. #ifdef TSR
  23. application(void)
  24. #else
  25. main(void)
  26. #endif
  27. {
  28.     char buf[81];
  29.     char far *s;
  30.     unsigned rcount, wcount, ret, paras, seg;
  31.     int f;
  32.     
  33.     /* prompt for filename */
  34.     if (PUTSTR(file_prompt) != 0)
  35.         return;
  36.  
  37.     /* get filename */
  38.     if ((_dos_read(STDERR, buf, 80, &rcount) != 0) || (rcount < 3))
  39.         return;
  40.     /* replace CRLF with NULL */
  41.     buf[rcount-2] = '\0';
  42.     
  43.     /* try to allocate: first try a lot, then a little */
  44.     if (_dos_allocmem(WANT_PARAS, &seg) == 0)
  45.         paras = WANT_PARAS;
  46.     else if (_dos_allocmem(MIN_PARAS, &seg) == 0)
  47.         paras = MIN_PARAS;
  48.     else
  49.     {
  50.         PUTSTR(insuff_mem);
  51.         return;
  52.     }
  53.     FP_SEG(s) = seg;
  54.     FP_OFF(s) = 0;
  55.     
  56.     /* open file */
  57.     if (_dos_open(buf, O_RDWR | SH_DENYNO, &f) != 0)
  58.         return PUTSTR(cant_open);
  59.     
  60.     /* display file */
  61.     while (((ret = _dos_read(f, s, BYTES, &rcount)) == 0) && rcount)
  62.         if (_dos_write(STDERR, s, rcount, &wcount) != 0)
  63.             break;
  64.     /* write one more CRLF */
  65.     PUTSTR(crlf);
  66.     if (ret)
  67.         PUTSTR(error_reading);
  68.     
  69.     /* free memory */
  70.     _dos_freemem(seg);
  71.     
  72.     /* close file */
  73.     _dos_close(f);
  74.     
  75.     PUTSTR("Press any key...");
  76. }
  77.