home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / c / actlib11 / tools / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-25  |  6.6 KB  |  256 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. /*
  4.  * File         : file.c
  5.  *
  6.  * Functions    : filencopy
  7.  *          Filencopy
  8.  *                filetrunc
  9.  *          Filetrunc
  10.  *          Filecat
  11.  *
  12.  * Description  : File management functions.
  13.  *
  14.  * Decisions    :
  15.  *
  16.  */
  17.  
  18.  
  19. #include "strings.h"
  20. #include "tools.h"
  21. #include <malloc.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>  /* because used by stat.h in Microsoft */
  25. #include <sys/stat.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29.  
  30. #define BUFSIZE         1024L     /*  Buffer size : to be optimized        */
  31.                 /*  Very context dependant !!            */
  32.                 /*  Under Sun 3.0 : 1 page = 8192 bytes  */
  33.                 /*  Under MS-DOS  : 1 page =  512 bytes  */
  34.  
  35. #define PERMISSION    0666    /*  RW for owner, R for everybody else  */
  36.  
  37.  
  38.  
  39. /***
  40.  *  Function    :  filencopy
  41.  *
  42.  *  Description :  Copy n characters from a file on another.
  43.  *
  44.  *  Parameters  :  in   int            target    target file descriptor
  45.  *                 in   int            source    source file descriptor
  46.  *                 in   unsigned long  length    number of bytes to copy
  47.  *
  48.  *  Precond     :  The two files must be opened.
  49.  *
  50.  *  Decisions   :  The copy can stop when eof is reached.
  51.  *
  52.  *  Warning     :  no rewind is performed on either file!!!
  53.  *
  54.  *  Return      :  0 if OK
  55.  *                 errno if error
  56.  *
  57.  *  OS/Compiler :  All
  58.  ***/
  59.  
  60. int filencopy( int source, int target, unsigned long length )
  61.  
  62. { int nread, nwrite, nb;
  63.   char *buffer;
  64. /*  int bufsize = (int) min( farcoreleft(), 0xFFFEUL );
  65.   if ( bufsize < 128 ) return -1;
  66.   buffer = (char *) malloc(bufsize);
  67.   if ( ! buffer ) return -1;
  68. */
  69.   int bufsize;
  70.   for ( bufsize = 0x7FFF; bufsize >= 128; bufsize /= 2 )
  71.       if ( buffer = (char *) malloc(bufsize) ) break;
  72.   if ( bufsize < 128 ) return -1;
  73.  
  74.   while ( nread = read(source, buffer, bufsize) )
  75.         {
  76.           if ( nread == -1 ) { free( buffer );
  77.                                return errno;
  78.                              }
  79.  
  80.           for ( nwrite = 0; nwrite < nread; nwrite += nb )
  81.               if ( (nb = write(target,&buffer[nwrite],nread-nwrite)) == -1 )
  82.                  {
  83.                    free( buffer );
  84.                    return errno;
  85.                  }
  86.           if ( (length < nread) || eof(source) )
  87.              {
  88.                free( buffer );
  89.                return 0;
  90.               }
  91.  
  92.           length -= nread;
  93.         }
  94.  
  95.   free( buffer );
  96.   return 0;
  97. }
  98.  
  99. /***
  100.  *  Function    :  Filencopy
  101.  *
  102.  *  Description :  Copy n characters from a file on another.
  103.  *
  104.  *  Parameters  :  in   char *         source    source filename
  105.  *                 in   char *         target    target filename
  106.  *                 in   unsigned long  length    number of bytes to copy
  107.  *
  108.  *  Decisions   :  The copy can stop when eof is reached.
  109.  *
  110.  *  Return      :  0 if OK
  111.  *                 errno if error
  112.  *
  113.  *  OS/Compiler :  All
  114.  ***/
  115.  
  116. int Filencopy( const char *source, const char *target, unsigned long length )
  117.  
  118. { int targetfile, sourcefile;
  119.   int error;
  120.   char *ptr1, targetname[_MAX_PATH];
  121.  
  122.   if ( strcmp(target, source) == 0 ) return Filetrunc(source, length);
  123.  
  124.   strcpy( targetname, target );
  125.   if ( *(ptr1 = strend(targetname) - 1) == '\\' )
  126.      {
  127.        const char *ptr2 = max( source, strrchr(source, '\\') + 1 );
  128.        strcpy( ++ptr1, ptr2 );
  129.      }
  130.  
  131.   /* change the default file mode from text to binary */
  132.   _fmode = O_BINARY;
  133.   if ( (sourcefile = open(source, O_RDONLY | O_BINARY)) == -1 ||
  134.        (targetfile = creat(targetname, S_IWRITE)) == -1
  135.      ) return errno;
  136.  
  137.   if ( error = filencopy(sourcefile, targetfile, length) ) return error;
  138.  
  139.   if ( (close(targetfile) == -1) || (close(sourcefile) == -1) ) return errno;
  140.  
  141.   return 0;
  142. }
  143.  
  144.  
  145.  
  146. /***
  147.  *  Function    :  Filecat
  148.  *
  149.  *  Description :  Concatenate two files on another.
  150.  *
  151.  *  Parameters  :  in   char *     newpath     result filename
  152.  *                 in   char *     path1       path1 filename
  153.  *                 in   char *     path2       path2 filename
  154.  *
  155.  *  Return      :  0 if OK
  156.  *                 errno if error
  157.  *
  158.  *  OS/Compiler :  All
  159.  ***/
  160.  
  161. int Filecat( const char *newpath, const char *path1, const char *path2 )
  162.  
  163. { int fd1, fd2, newfd;
  164.   int error;
  165.  
  166.   if ( strcmp(path2, newpath) ) return -1; /*  new path equal to second one  */
  167.  
  168.   if ( (fd2 = open(path2, O_RDONLY | O_BINARY)) == -1 ) return errno;
  169.  
  170.   /* change the default file mode from text to binary */
  171.   _fmode = O_BINARY;
  172.  
  173.   if ( strcmp(path1, newpath) )
  174.      {                /*  new path different than first one  */
  175.        if ( (fd1 = open(path1, O_RDONLY | O_BINARY)) == -1 ||
  176.             (newfd = creat(newpath, S_IWRITE)) == -1
  177.           ) return errno;
  178.        if ( error = filecopy(fd1, newfd) ) return error;
  179.        if ( close(fd1) == -1) return errno;
  180.      }
  181.   else if ( (newfd = open(path1, O_APPEND | O_BINARY)) == -1 ) return errno;
  182.  
  183.   if ( error = filecopy(newfd, fd2) ) return error;
  184.  
  185.   if ( (close(fd2) == -1) || (close(newfd) == -1) ) return errno;
  186.  
  187.   return 0;
  188. }
  189.  
  190. /***
  191.  *  Function    :  filetrunc
  192.  *
  193.  *  Description :  Truncate a file to a specified length.
  194.  *
  195.  *  Parameters  :  in   int            fd        file descriptor
  196.  *                 in   unsigned long  length    number of bytes to copy
  197.  *
  198.  *  Precond     :  The file must be opened with writing permission
  199.  *
  200.  *  Warning     :  no rewind is performed on the file!!!
  201.  *
  202.  *  Return      :  0 if OK
  203.  *                 errno if error
  204.  *
  205.  *  OS/Compiler :  All
  206.  ***/
  207.  
  208. int filetrunc( int fd, unsigned long length )
  209.  
  210. {
  211. /*
  212.   int nread;
  213.   char buffer[BUFSIZE + 1];
  214.  
  215.   while ( nread = read(fd, buffer, min(BUFSIZE,length)) )
  216.         {
  217.           if ( nread == -1 ) return errno;
  218.           length -= nread;
  219.         }
  220.  
  221.   if ( write(fd, buffer, 0) == -1 ) return errno;
  222. */
  223.   if ( chsize(fd, length) ) return errno;
  224.   return 0;
  225. }
  226.  
  227. /***
  228.  *  Function    :  Filetrunc
  229.  *
  230.  *  Description :  Truncate a file to a specified length.
  231.  *
  232.  *  Parameters  :  in   char *         path      file descriptor
  233.  *                 in   unsigned long  length    number of bytes to copy
  234.  *
  235.  *  Return      :  0 if OK
  236.  *                 errno if error
  237.  *
  238.  *  OS/Compiler :  All
  239.  ***/
  240.  
  241. int Filetrunc( const char *path, int length )
  242.  
  243. { int fd, error;
  244.  
  245.   /* change the default file mode from text to binary */
  246.   _fmode = O_BINARY;
  247.  
  248.   if ( (fd = creat(path, S_IWRITE)) == -1 ) return errno;
  249.  
  250.   if ( error = filetrunc( fd, length ) ) return error;
  251.  
  252.   if ( close(fd) == -1 ) return errno;
  253.  
  254.   return 0;
  255. }
  256.