home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TOOLS.ZIP / FILE.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  6KB  |  248 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 set
  56.  *                 -1 otherwise
  57.  *
  58.  *  OS/Compiler :  All
  59.  ***/
  60.  
  61. int filencopy( int source, int target, unsigned long length )
  62.  
  63. { int nread, bufsize;
  64.   char *buffer;
  65.  
  66.   /* Build buffer as big as possible */
  67.   for ( bufsize = min(0x7FFF, length); bufsize >= 128; bufsize /= 2 )
  68.       if ( buffer = (char *) malloc(bufsize) ) break;
  69.   if ( bufsize < 128 ) return -1;
  70.  
  71.   while ( nread = read(source, buffer, min(length, bufsize)) )
  72.         {
  73.           if ( nread == -1 || nread != write(target, buffer, nread) )
  74.              {
  75.                free( buffer );
  76.                if ( errno ) return errno;
  77.                        else return -1;
  78.              }
  79.  
  80.           if ( (nread == length) || eof(source) ) break;
  81.  
  82.           length -= nread;
  83.         }
  84.  
  85.   free( buffer );
  86.   return 0;
  87. }
  88.  
  89. /***
  90.  *  Function    :  Filencopy
  91.  *
  92.  *  Description :  Copy n characters from a file on another.
  93.  *
  94.  *  Parameters  :  in   char *         source    source filename
  95.  *                 in   char *         target    target filename
  96.  *                 in   unsigned long  length    number of bytes to copy
  97.  *
  98.  *  Decisions   :  The copy can stop when eof is reached.
  99.  *
  100.  *  Return      :  0 if OK
  101.  *                 errno if set
  102.  *                 -1 otherwise
  103.  *
  104.  *  OS/Compiler :  All
  105.  ***/
  106.  
  107. int Filencopy( const char *source, const char *target, unsigned long length )
  108.  
  109. { int targetfile, sourcefile;
  110.   int error;
  111.   char *ptr1, targetname[_MAX_PATH];
  112.  
  113.   if ( strcmp(target, source) == 0 ) return Filetrunc(source, length);
  114.  
  115.   strcpy( targetname, target );
  116.   if ( *(ptr1 = strend(targetname) - 1) == '\\' )
  117.      {
  118.        const char *ptr2 = max( source, strrchr(source, '\\') + 1 );
  119.        strcpy( ++ptr1, ptr2 );
  120.      }
  121.  
  122.   /* change the default file mode from text to binary */
  123.   _fmode = O_BINARY;
  124.   if ( (sourcefile = open(source, O_RDONLY | O_BINARY)) == -1 ||
  125.        (targetfile = creat(targetname, S_IWRITE)) == -1
  126.      ) return errno;
  127.  
  128.   if ( error = filencopy(sourcefile, targetfile, length) ) return error;
  129.  
  130.   if ( (close(targetfile) == -1) || (close(sourcefile) == -1) ) return errno;
  131.  
  132.   return 0;
  133. }
  134.  
  135.  
  136.  
  137. /***
  138.  *  Function    :  Filecat
  139.  *
  140.  *  Description :  Concatenate two files on another.
  141.  *
  142.  *  Parameters  :  in   char *     newpath     result filename
  143.  *                 in   char *     path1       path1 filename
  144.  *                 in   char *     path2       path2 filename
  145.  *
  146.  *  Return      :  0 if OK
  147.  *                 errno if set
  148.  *                 -1 otherwise
  149.  *
  150.  *  OS/Compiler :  All
  151.  ***/
  152.  
  153. int Filecat( const char *newpath, const char *path1, const char *path2 )
  154.  
  155. { int fd1, fd2, newfd;
  156.   int error;
  157.  
  158.   if ( strcmp(path2, newpath) ) return -1; /*  new path equal to second one  */
  159.  
  160.   if ( (fd2 = open(path2, O_RDONLY | O_BINARY)) == -1 ) return errno;
  161.  
  162.   /* change the default file mode from text to binary */
  163.   _fmode = O_BINARY;
  164.  
  165.   if ( strcmp(path1, newpath) )
  166.      {                /*  new path different than first one  */
  167.        if ( (fd1 = open(path1, O_RDONLY | O_BINARY)) == -1 ||
  168.             (newfd = creat(newpath, S_IWRITE)) == -1
  169.           ) return errno;
  170.        if ( error = filecopy(fd1, newfd) ) return error;
  171.        if ( close(fd1) == -1) return errno;
  172.      }
  173.   else if ( (newfd = open(path1, O_APPEND | O_BINARY)) == -1 ) return errno;
  174.  
  175.   if ( error = filecopy(newfd, fd2) ) return error;
  176.  
  177.   if ( (close(fd2) == -1) || (close(newfd) == -1) ) return errno;
  178.  
  179.   return 0;
  180. }
  181.  
  182. /***
  183.  *  Function    :  filetrunc
  184.  *
  185.  *  Description :  Truncate a file to a specified length.
  186.  *
  187.  *  Parameters  :  in   int            fd        file descriptor
  188.  *                 in   unsigned long  length    number of bytes to copy
  189.  *
  190.  *  Precond     :  The file must be opened with writing permission
  191.  *
  192.  *  Warning     :  no rewind is performed on the file!!!
  193.  *
  194.  *  Return      :  0 if OK
  195.  *                 errno if error
  196.  *
  197.  *  OS/Compiler :  All
  198.  ***/
  199.  
  200. int filetrunc( int fd, unsigned long length )
  201.  
  202. {
  203. /*
  204.   int nread;
  205.   char buffer[BUFSIZE + 1];
  206.  
  207.   while ( nread = read(fd, buffer, min(BUFSIZE,length)) )
  208.         {
  209.           if ( nread == -1 ) return errno;
  210.           length -= nread;
  211.         }
  212.  
  213.   if ( write(fd, buffer, 0) == -1 ) return errno;
  214. */
  215.   if ( chsize(fd, length) ) return errno;
  216.   return 0;
  217. }
  218.  
  219. /***
  220.  *  Function    :  Filetrunc
  221.  *
  222.  *  Description :  Truncate a file to a specified length.
  223.  *
  224.  *  Parameters  :  in   char *         path      file descriptor
  225.  *                 in   unsigned long  length    number of bytes to copy
  226.  *
  227.  *  Return      :  0 if OK
  228.  *                 errno if error
  229.  *
  230.  *  OS/Compiler :  All
  231.  ***/
  232.  
  233. int Filetrunc( const char *path, int length )
  234.  
  235. { int fd, error;
  236.  
  237.   /* change the default file mode from text to binary */
  238.   _fmode = O_BINARY;
  239.  
  240.   if ( (fd = creat(path, S_IWRITE)) == -1 ) return errno;
  241.  
  242.   if ( error = filetrunc( fd, length ) ) return error;
  243.  
  244.   if ( close(fd) == -1 ) return errno;
  245.  
  246.   return 0;
  247. }
  248.