usb-drive.c
int writeFileToUSBDrive(char *fileName){
  /* The USB drive mount point */
  char usbDriveName[ 100 ];
  memset( usbDriveName, 0, 100 );
  getUSBDriveName( usbDriveName );

  /* Stuff having to do with searching through directories */
  DIR *usbDrive;
  struct dirent *ep;
  
  /* Input file (source) and output file (destination) */
  FILE *inFile;
  FILE *outFile;
  /* Output file name */
  char outFileName[ 12 ] = "IMG0001.JPG";
  char outFileName2[ 12 ] = "";
  
  /* The sub-directory name to create on the USB stick */
  char *dirname = malloc(20 * sizeof(char));
  strcpy(dirname, "DigitalPhotoBooth");
  int dirExists = 0;

  int retVal = 0;
  
  /* Storage for data bytes being copied */
  unsigned char data;

  /* User:RWX, Group:RWX, Other:RWX */
  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;

  if( usbDriveName != NULL ){ /* USB drive not detected by getUSBDriveName */

    /* Open the USB drive for listing */
    usbDrive = opendir( usbDriveName );

    /* Figure out what the final sub-directory name is */
    if( usbDrive != NULL ){ /* USB drive could not be opened */
      while( (ep = readdir( usbDrive )) ){
        if( !strcmp( ep->d_name, dirname ) ){ /* "if ep->d_name == dirname" */
          dirExists ++;
        } /* if */
      }

      /* Close the directory listing */
      closedir( usbDrive );
    
      /* Catenate all the stuff together for the path to the output file */
      strcat(usbDriveName, "/");
      strcat(usbDriveName, dirname);
      strcat(usbDriveName, "/");

      if( !dirExists ){

        /* Make the directory on the USB drive */
        retVal = mkdir(usbDriveName, mode);
      } else {
        /* Check whether the default filename exists and adjust as necessary */
        usbDrive = opendir( usbDriveName );
        if( usbDrive != NULL ){
          while( (ep = readdir( usbDrive )) ){
            if( !strcmp( toUpperCase( ep->d_name ), outFileName ) ){
              incrementFileName( outFileName, outFileName2 );
              strncpy( outFileName, outFileName2, 11 );
              rewinddir( usbDrive );
            }
          }
        }
        retVal = 0;
      }

      /* Catenate the output file name */
      strcat(usbDriveName, outFileName);

      inFile = fopen(fileName, "rb");
      outFile = fopen(usbDriveName, "wb");

      /* Copy the input file to the output file */
      int errFlag = 0;
      if( (inFile != NULL) && (outFile != NULL) ){
        errFlag |= ferror(inFile);
        errFlag |= ferror(outFile);
        while( !feof(inFile) && !errFlag ){
          errFlag = 0; /* This should be fresh every time */
          data = fgetc(inFile);
          errFlag = ferror(inFile);
          if( !errFlag ){
            fputc(data, outFile); 
            errFlag |= ferror(outFile);
          }
        }
      }

      fclose(inFile);
      fclose(outFile);


    } else {
      retVal = -1;
    } 

    
  } /* end of chech for whether usb drive was found */
  else {
    retVal = -1;
  }

  return retVal;
}
/* writeFileToUSBDrive()
 * Writes a file to the currently connected USB drive. This function uses the
 *  getUSBDriveName function to determine the mountpoint for the USB drive.
 *  If a folder named "DigitalPhotoBooth" does not yet exist on the root of the
 *  drive, it will be created and the specified file will be placed in it. If
 *  it does exist, a check will be performed to see whether the default output
 *  filename (IMG0001.JPG) is already present. The number at the end of the
 *  filename will be incremented until it does not match an already existing
 *  filename.
 *  
 * Parameters:
 *  fileName: the name of the file (current directory or path) to write
 *
 * Returns 0 if successful, nonzero if an error occurred.
 */
writeFileToUSBDrive()
Writes a file to the currently connected USB drive. This function uses the
getUSBDriveName function to determine the mountpoint for the USB drive.
If a folder named "DigitalPhotoBooth" does not yet exist on the root of the
drive, it will be created and the specified file will be placed in it. If
it does exist, a check will be performed to see whether the default output
filename (IMG0001.JPG) is already present. The number at the end of the
filename will be incremented until it does not match an already existing
filename.

the name of the file (current directory or path) to write

Returns 0 if successful, nonzero if an error occurred.
writeFileToUSBDrive () Writes a file to the currently connected USB drive . This function uses the getUSBDriveName function to determine the mountpoint for the USB drive . If a folder named " DigitalPhotoBooth " does not yet exist on the root of the drive it will be created and the specified file will be placed in it . If it does exist a check will be performed to see whether the default output filename ( IMG0001 . JPG ) is already present . The number at the end of the filename will be incremented until it does not match an already existing filename . the name of the file ( current directory or path ) to write Returns 0 if successful nonzero if an error occurred .
int writeFileToUSBDrive(char *fileName){
  char usbDriveName[ 100 ];
  memset( usbDriveName, 0, 100 );
  getUSBDriveName( usbDriveName );
  DIR *usbDrive;
  struct dirent *ep;
  FILE *inFile;
  FILE *outFile;
  char outFileName[ 12 ] = "IMG0001.JPG";
  char outFileName2[ 12 ] = "";
  char *dirname = malloc(20 * sizeof(char));
  strcpy(dirname, "DigitalPhotoBooth");
  int dirExists = 0;
  int retVal = 0;
  unsigned char data;
  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
  if( usbDriveName != NULL ){ 
    usbDrive = opendir( usbDriveName );
    if( usbDrive != NULL ){ 
      while( (ep = readdir( usbDrive )) ){
        if( !strcmp( ep->d_name, dirname ) ){ 
          dirExists ++;
        } 
      }
      closedir( usbDrive );
      strcat(usbDriveName, "/");
      strcat(usbDriveName, dirname);
      strcat(usbDriveName, "/");
      if( !dirExists ){
        retVal = mkdir(usbDriveName, mode);
      } else {
        usbDrive = opendir( usbDriveName );
        if( usbDrive != NULL ){
          while( (ep = readdir( usbDrive )) ){
            if( !strcmp( toUpperCase( ep->d_name ), outFileName ) ){
              incrementFileName( outFileName, outFileName2 );
              strncpy( outFileName, outFileName2, 11 );
              rewinddir( usbDrive );
            }
          }
        }
        retVal = 0;
      }
      strcat(usbDriveName, outFileName);
      inFile = fopen(fileName, "rb");
      outFile = fopen(usbDriveName, "wb");
      int errFlag = 0;
      if( (inFile != NULL) && (outFile != NULL) ){
        errFlag |= ferror(inFile);
        errFlag |= ferror(outFile);
        while( !feof(inFile) && !errFlag ){
          errFlag = 0; 
          data = fgetc(inFile);
          errFlag = ferror(inFile);
          if( !errFlag ){
            fputc(data, outFile); 
            errFlag |= ferror(outFile);
          }
        }
      }
      fclose(inFile);
      fclose(outFile);
    } else {
      retVal = -1;
    } 
  } 
  else {
    retVal = -1;
  }
  return retVal;
}
int writeFileToUSBDrive ( char * fileName ) { char usbDriveName [ 100 ] ; memset ( usbDriveName , 0 , 100 ) ; getUSBDriveName ( usbDriveName ) ; DIR * usbDrive ; struct dirent * ep ; FILE * inFile ; FILE * outFile ; char outFileName [ 12 ] = " " ; char outFileName2 [ 12 ] = " " ; char * dirname = malloc ( 20 * sizeof ( char ) ) ; strcpy ( dirname , " " ) ; int dirExists = 0 ; int retVal = 0 ; unsigned char data ; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO ; if ( usbDriveName != NULL ) { usbDrive = opendir ( usbDriveName ) ; if ( usbDrive != NULL ) { while ( ( ep = readdir ( usbDrive ) ) ) { if ( ! strcmp ( ep -> d_name , dirname ) ) { dirExists ++ ; } } closedir ( usbDrive ) ; strcat ( usbDriveName , " " ) ; strcat ( usbDriveName , dirname ) ; strcat ( usbDriveName , " " ) ; if ( ! dirExists ) { retVal = mkdir ( usbDriveName , mode ) ; } else { usbDrive = opendir ( usbDriveName ) ; if ( usbDrive != NULL ) { while ( ( ep = readdir ( usbDrive ) ) ) { if ( ! strcmp ( toUpperCase ( ep -> d_name ) , outFileName ) ) { incrementFileName ( outFileName , outFileName2 ) ; strncpy ( outFileName , outFileName2 , 11 ) ; rewinddir ( usbDrive ) ; } } } retVal = 0 ; } strcat ( usbDriveName , outFileName ) ; inFile = fopen ( fileName , " " ) ; outFile = fopen ( usbDriveName , " " ) ; int errFlag = 0 ; if ( ( inFile != NULL ) && ( outFile != NULL ) ) { errFlag |= ferror ( inFile ) ; errFlag |= ferror ( outFile ) ; while ( ! feof ( inFile ) && ! errFlag ) { errFlag = 0 ; data = fgetc ( inFile ) ; errFlag = ferror ( inFile ) ; if ( ! errFlag ) { fputc ( data , outFile ) ; errFlag |= ferror ( outFile ) ; } } } fclose ( inFile ) ; fclose ( outFile ) ; } else { retVal = -1 ; } } else { retVal = -1 ; } return retVal ; }
writeFileToUSBDrive()
 Writes a file to the currently connected USB drive.
writeFileToUSBDrive () Writes a file to the currently connected USB drive .
/* The USB drive mount point */ /* Stuff having to do with searching through directories */ /* Input file (source) and output file (destination) */ /* Output file name */ /* The sub-directory name to create on the USB stick */ /* Storage for data bytes being copied */ /* User:RWX, Group:RWX, Other:RWX */ /* USB drive not detected by getUSBDriveName */ /* Open the USB drive for listing */ /* Figure out what the final sub-directory name is */ /* USB drive could not be opened */ /* "if ep->d_name == dirname" */ /* if */ /* Close the directory listing */ /* Catenate all the stuff together for the path to the output file */ /* Make the directory on the USB drive */ /* Check whether the default filename exists and adjust as necessary */ /* Catenate the output file name */ /* Copy the input file to the output file */ /* This should be fresh every time */ /* end of chech for whether usb drive was found */