home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / GETFILE.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  5KB  |  149 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) getfile.c 2.7 88/01/24 12:44:23";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  8. */
  9.  
  10. #include "options.h"
  11. /*
  12. This function copies n characters from the source file to the destination
  13.  
  14. Input:   out_f:            destination file
  15.          in_f:             source file
  16.          count:         count of characters to copy
  17.          docrc:         0 if crc not wanted
  18.  
  19. If count is -1, copying is done until eof is encountered.
  20.  
  21. The source file is transferred to the current file pointer position in the
  22. destination file, using the handles provided.  Function return value is 0
  23. if no error, 2 if write error, and 3 if read error.
  24.  
  25. If docrc is not 0, the global variable crccode is updated via addbfcrc().
  26. This is done even if the output is to the null device.
  27.  
  28. If UNBUF_IO is defined, and if more than UNBUF_LIMIT bytes are
  29. being transferred or copying is to end of file, the data transfer
  30. is done using low-level read() and write() functions, which must
  31. be defined elsewhere.  File descriptors are obtained for this
  32. purpose using the fileno() macro, which must be provided elsewhere
  33. too.  This is meant to provide greater efficiency on some systems.
  34. The files of type ZOOFILE are synchronized with their file
  35. descriptors by doing a reasonable number of seeks and other
  36. miscellaneous operations before and after the transfer.  Such
  37. simultaneous use of buffered and unbuffered files is not
  38. portable and should not be used without extensive testing.
  39. */
  40.  
  41. #ifdef UNBUF_IO
  42. int read PARMS ((int, VOIDPTR, unsigned));
  43. int write PARMS ((int, VOIDPTR, unsigned));
  44. long lseek PARMS ((int, long, int));
  45. long tell PARMS ((int));
  46. #endif /* UNBUF_IO */
  47.  
  48. #include "zoo.h"        /* satisfy declarations in zooio.h */
  49. #include "zooio.h"
  50. #include "various.h"
  51. #include "zoofns.h"
  52. #include "zoomem.h"
  53.  
  54. int getfile (in_f, out_f, count, docrc)
  55. ZOOFILE in_f, out_f;
  56. long count;
  57. int docrc;
  58. {
  59.    register int how_much;
  60. #ifdef UNBUF_IO
  61.     int in_d, out_d;    /* file descriptors for unbuffered I/O */
  62. #endif /* UNBUF_IO */
  63.  
  64. #ifdef UNBUF_IO
  65.     if (out_f != NULLFILE && (count == -1 || count > UNBUF_LIMIT)) {
  66.         in_d = fileno (in_f);        /* get ..                        */
  67.         out_d = fileno (out_f);        /* ... file descriptors        */
  68.  
  69.         /* Synchronize buffered and unbuffered files */
  70.         zooseek (in_f, zootell (in_f), 0);
  71.         zooseek (out_f, zootell (out_f), 0);
  72.  
  73. #if 0
  74.         lseek (in_d, zootell (in_f), 0);
  75.         lseek (out_d, zootell (out_f), 0);
  76. #endif
  77.  
  78.         if (count == -1) {
  79.             while ((how_much = read (in_d, out_buf_adr, MEM_BLOCK_SIZE)) > 0) {
  80.                 if (how_much == -1 ||
  81.                         write (out_d, out_buf_adr, how_much) != how_much)
  82.                     return (2);
  83.                 if (docrc)
  84.                     addbfcrc (out_buf_adr,how_much);
  85.             }
  86.             zooseek (in_f, tell (in_d), 0);        /* resynch    */
  87.             zooseek (out_f, tell (out_d), 0);    /* resynch    */
  88.  
  89. #ifndef NDEBUG
  90.             if (ftell (in_f) != tell (in_d) || ftell (out_f) != tell (out_d)) {
  91.                 prterror ('w', "seek mismatch in copy to EOF\n");
  92.                 printf ("in_f =%6ld, in_d =%6ld\n", ftell (in_f),  tell (in_d));
  93.                 printf ("out_f=%6ld, out_d=%6ld\n", ftell (out_f), tell (out_d));
  94.             }
  95. #endif /* NDEBUG */
  96.  
  97.             return (0);
  98.         }
  99.  
  100.         while (count > 0) {
  101.             if (count > MEM_BLOCK_SIZE)
  102.                 how_much = MEM_BLOCK_SIZE;
  103.             else
  104.                 how_much = (int) count;
  105.             count -= how_much;
  106.             if (read (in_d, out_buf_adr, how_much) != how_much)
  107.                 return (3);
  108.             if (docrc)
  109.                 addbfcrc (out_buf_adr, how_much);
  110.             if (write (out_d, out_buf_adr, how_much) != how_much)
  111.                 return (2);
  112.         }
  113.         zooseek (in_f, tell (in_d), 0);        /* resynch    */
  114.         zooseek (out_f, tell (out_d), 0);    /* resynch    */
  115. #ifndef NDEBUG
  116.         if (ftell (in_f) != tell (in_d) || ftell (out_f) != tell (out_d))
  117.              prterror ('w', "seek mismatch in fixed length copy\n");
  118. #endif /* NDEBUG */
  119.         return (0);
  120.     }
  121. #endif /* UNBUF_IO */
  122.  
  123.    if (count == -1) {
  124.       while ((how_much = zooread (in_f, out_buf_adr, MEM_BLOCK_SIZE)) > 0) {
  125.          if (how_much == -1 ||
  126.                zoowrite (out_f, out_buf_adr, how_much) != how_much)
  127.             return (2);
  128.          if (docrc)
  129.             addbfcrc (out_buf_adr,how_much);
  130.       }
  131.       return (0);
  132.    }
  133.  
  134.    while (count > 0) {
  135.       if (count > MEM_BLOCK_SIZE)
  136.          how_much = MEM_BLOCK_SIZE;
  137.       else
  138.          how_much = (int) count;
  139.       count -= how_much;
  140.       if (zooread (in_f, out_buf_adr, how_much) != how_much)
  141.          return (3);
  142.       if (docrc)
  143.          addbfcrc (out_buf_adr, how_much);
  144.       if (zoowrite (out_f, out_buf_adr, how_much) != how_much)
  145.          return (2);
  146.    }
  147.    return (0);
  148. }
  149.