Copying Folder, C program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Copying all the normal files from 1 folder to another on a unix platform. I pass in 1 or 2 arguments, the folder to be copied and folder copied to.
    I don't know why my code is not working. I open up a directory, and copying file by file. I don't know why it won't work.

  2. Relevant commands, code, scripts, algorithms:
    some cp files you can find easily all over the internet, and part of a ls file.

  3. The attempts at a solution (include all code and scripts):
    //here is my code

#include	<stdio.h>
#include	<sys/types.h>
#include	<dirent.h>
#include	<sys/stat.h>
#include	<pwd.h>
#include	<grp.h>
#include    <string.h>
#include	<unistd.h>
#include	<stdlib.h>
#include	<fcntl.h>

#define BUFFERSIZE      4096
#define COPYMODE        0644


void open_dir(char[], char[]);
void copy_file(char *, char[]);
void oops(char *, char *);

main(int ac, char *av[]){
	if ( ac == 2 )
		open_dir( ".", av[1]);
	if ( ac == 3)
		open_dir( av[1], av[2]);
}		
						
/*******************************************
 *	list files in directory called dirname */
 void open_dir( char dirname[], char tardir[] ){
	DIR		*dir_ptr;		/* the directory */
	struct dirent	*direntp;	/* each entry	 */

	if ( ( dir_ptr = opendir( dirname ) ) == NULL )
		fprintf(stderr,"ls1: cannot open %s\n", dirname);
	else {
		while ( ( direntp = readdir( dir_ptr ) ) != NULL )
			copy_file( direntp->d_name, tardir);
		closedir(dir_ptr);
	}
}

/********************************************
 *  copys file								*/
void copy_file( char *filename, char dirname[]){
		
		int     in_fd, out_fd, n_chars;
        char    buf[BUFFERSIZE];
		DIR		*dir_ptr;		/* the directory */
		struct dirent	*direntp;	/* each entry	 */      						
						/* open files	*/
        if ( (in_fd=open(filename, O_RDONLY)) == -1 )
                oops("Cannot open ", filename);
		if ( ( dir_ptr = opendir( dirname ) ) == NULL )
		fprintf(stderr,"ls1: cannot open %s\n", dirname);
		else {
			if ( (out_fd=creat( filename, COPYMODE)) == -1 )
                oops( "Cannot creat", filename);
		}
						/* copy files	*/

        while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
                if ( write( out_fd, buf, n_chars ) != n_chars )
                        oops("Write error to ", filename);
	if ( n_chars == -1 )
			oops("Read error from ", filename);

						/* close files	*/

        if ( close(in_fd) == -1 || close(out_fd) == -1 )
                oops("Error closing files","");
}
void oops(char *s1, char *s2)
{
        fprintf(stderr,"Error: %s ", s1);
        perror(s2);
        exit(1);
}
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    CSUEB, Hayward, CA, USA, Prof. Lee, CS3640

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

---------- Post updated at 03:41 PM ---------- Previous update was at 01:51 AM ----------

I've found out that I have to use malloc() to put it in another directory, but I'm very confused to how to use it for this program.