1: /*
     2:  *  Guitar-ZyX(tm)::MasterControlProgram - portable guitar F/X controller
     3:  *  Copyright (C) 2009  Douglas McClendon
     4:  *
     5:  *  This program is free software: you can redistribute it and/or modify
     6:  *  it under the terms of the GNU General Public License as published by
     7:  *  the Free Software Foundation, version 3 of the License.
     8:  *
     9:  *  This program is distributed in the hope that it will be useful,
    10:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12:  *  GNU General Public License for more details.
    13:  *
    14:  *  You should have received a copy of the GNU General Public License
    15:  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16: */
    17: /*
    18: #############################################################################
    19: #############################################################################
    20: ## 
    21: ## gzmcpc::libdmc: Doug's Misc C Library
    22: ##
    23: #############################################################################
    24: ##
    25: ## Copyright 2008-2009 Douglas McClendon <dmc AT filteredperception DOT org>
    26: ##
    27: #############################################################################
    28: #############################################################################
    29: #
    30: */
    31: 
    32: 
    33: 
    34: 
    35: 
    36: 
    37: #include <stdio.h> 
    38: #include <stdlib.h> 
    39: #include <string.h> 
    40: #include <dirent.h> 
    41: #include <sys/stat.h> 
    42: #include <unistd.h> 
    43: 
    44: 
    45: #include <fat.h> 
    46: 
    47: 
    48: #include "dmc.h" 
    49: 
    50: #include "debug.h" 
    51: 
    52: 
    53: 
    54: 
    55: 
    56: 
    57: 
    58: int dmc_fs_cp(const char *src,
    59: 	   const char *dst) {
    60: 
    61:   unsigned char *cp_buf;
    62:   FILE *src_file;
    63:   FILE *dst_file;
    64:   int done = 0;
    65:   int bytes_read = -1;
    66:   int bytes_written = -1;
    67: 
    68:   // alloc a buffer
    69:   cp_buf = malloc(DMC_FS_CP_BUF);
    70:   if (cp_buf == NULL) {
    71:     return -1;
    72:   }
    73: 
    74:   // delete dest if present
    75:   unlink(dst);
    76: 
    77:   // open files
    78:   // TODO: error check
    79:   src_file = fopen(src, "r");
    80:   if (src_file == NULL) {
    81:     free(cp_buf);
    82:     return -1;
    83:   }
    84:   dst_file = fopen(dst, "w");
    85:   if (dst_file == NULL) {
    86:     free(cp_buf);
    87:     fclose(src_file);
    88:     return -1;
    89:   }
    90: 
    91:   // data copy loop
    92:   while (!done) {
    93: 
    94:     bytes_read = fread(cp_buf,
    95: 		       1,
    96: 		       DMC_FS_CP_BUF,
    97: 		       src_file);
    98: 
    99:     // libfat workaround
   100:     fseek(dst_file,
   101:           0,
   102:           SEEK_SET);
   103:     fseek(dst_file,
   104:           0,
   105:           SEEK_END);
   106: 
   107:     bytes_written = fwrite(cp_buf,
   108: 			   1,
   109: 			   bytes_read,
   110: 			   dst_file);
   111: 
   112:     if (bytes_written != bytes_read) {
   113:       // assuming no possible acceptable short writes (for now)
   114:       die();
   115:     }
   116: 
   117:     // check for eof condition
   118:     if (feof(src_file)) {
   119:       done = 1;
   120:     }
   121: 
   122:   }
   123: 
   124:   // free resources
   125:   fclose(src_file);
   126:   fclose(dst_file);
   127:   free(cp_buf);
   128: 
   129:   // success
   130:   return 0;
   131: 
   132: }
   133: