home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / GETFILE.C < prev    next >
C/C++ Source or Header  |  1987-02-07  |  2KB  |  68 lines

  1. /* copyfile.c */
  2. /*
  3. Copyright (C) 1986 Rahul Dhesi -- All rights reserved
  4. */
  5.  
  6. #include "options.h"
  7. /* 
  8. This function copies n characters from the source file to the destination
  9.  
  10. Input:   output_han:    handle of destination file.
  11.          input_han:     handle of source file.
  12.          count:         count of characters to copy.
  13.          docrc:         0 if crc not wanted
  14.  
  15. If count is -1, copying is done until eof is encountered.
  16.  
  17. The source file is transferred to the current file pointer position in the
  18. destination file, using the handles provided.  Function return value is 0 
  19. if no error, 2 if write error, and 3 if read error.  
  20.  
  21. Note, however, that if the output handle is -2, it is taken to be the
  22. null device and no output actually occurs.  
  23.  
  24. If docrc is not 0, the global variable crccode is updated via addbfcrc().
  25. This is done even if the output is to the null device.
  26. */
  27.  
  28. #include <stdio.h>
  29. #include "various.h"
  30. #include "zoofns.h"
  31. #include "zoomem.h"
  32.  
  33. int getfile(input_han, output_han, count, docrc)
  34. int input_han, output_han;
  35. long count;
  36. int docrc;
  37. {
  38.    register int how_much;
  39.  
  40.    if (count == -1) {
  41.       while ((how_much = read (input_han, out_buf_adr, MEM_BLOCK_SIZE)) > 0) {
  42.          if (how_much == -1 || 
  43.                write (output_han, out_buf_adr, how_much) != how_much)
  44.             return (2);
  45.          if (docrc)
  46.             addbfcrc (out_buf_adr,how_much);
  47.       }
  48.       return (0);
  49.    }
  50.  
  51.    while (count > 0) {
  52.       if (count > MEM_BLOCK_SIZE)
  53.          how_much = MEM_BLOCK_SIZE;
  54.       else
  55.          how_much = count;
  56.       count -= how_much;
  57.       if (read (input_han, out_buf_adr, how_much) != how_much)
  58.          return (3);
  59.       if (docrc)
  60.          addbfcrc (out_buf_adr, how_much);
  61.       if (output_han != -2)
  62.          if (write (output_han, out_buf_adr, how_much) != how_much)
  63.             return (2);
  64.    }
  65.    return (0);
  66. }
  67.  
  68.