Hi, I am trying to copy all contents from one directory over to another directory. This is the code I have so far:

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <sys/stat.h> 
#define MAXNAME 80

void *copydirectory(void *arg);
void *copyfilepass(void *arg);
// DIR *opendir(const char *dirname);
// struct dirent *readdir(DIR *dirp);
// int closedir(DIR *dirp);

typedef struct {
	int args[3];
	pthread_t tid;
} copy_t;

int main(int argc, char *argv[]) {
	if (argc != 3) {
		fprintf(stderr, "Incorrect input\n");
		return 1;

	copydirectory(strcat(argv[1], argv[2]));

	return 0;

void *copydirectory(void *arg) {
	int error;
	struct dirent *direntp;
	DIR *dirp_source, *dirp_destination;	
	char *source, *destination;
	copy_t copy;
	char filename[MAXNAME];
	  1.) Split strings (COMPLETE)
	  2.) Open Directory (COMPLETE)
	  3.) Read Directory for files
	  4.) Copy file from source to destination
	  5.) Close Files and Directory

	// 1.) Split the two directory strings:	
	source = strtok(arg, "\\");
	destination = strtok(NULL, "\\");
	fprintf(stderr, "Source: %s and Destination: %s\n", source, destination);
	// 2.) Open Directory:
	if ((dirp_source = opendir(source)) == NULL) {
		fprintf(stderr, "Failed to open %s\n", source);
		return 1;
	if ((dirp_destination = opendir(destination)) == NULL) {
		perror("Failed to open destination directory");
		return 1;
	// 3.) Read directory for files:
	while ((direntp = readdir(dirp_source)) != NULL)
		copy.tid = pthread_self();	// Cannot be value for new thread
		fprintf(stderr, "got to 71\n");
		// Create source filename to copy:
		if (snprintf(filename, MAXNAME, direntp->d_name) == MAXNAME)
			fprintf(stderr, "Input filename %s is too long", direntp->d_name);
		fprintf(stderr, "got to 78\n");
		// Open the file to read from:
		if ((copy.args[0] = open(filename, R_FLAGS)) == -1) {
			fprintf(stderr, "Failed to open source file %s: %s\n",
							filename, strerror(errno));

		// Create destination filename to write to:
		if (snprintf(filename, MAXNAME, "%s", direntp->d_name) == MAXNAME)
			fprintf(stderr, "Output filename %s is too long\n", direntp->d_name);

		// Open the file to write to:
		if ((copy.args[1] = open(filename, W_FLAGS, W_PERMS)) == -1) {
			fprintf(stderr, "Failed to oepn destination file %s: %s\n",
							filename, strerror(errno));
		if (error = pthread_create((&copy.tid), NULL, copyfilepass, copy.args)) {
			fprintf(stderr, "Failed to create thread: %s\n", strerror(error));
			copy.tid = pthread_self();
	// 4.) Close directory:
	while ((closedir(dirp_source) == -1) && (errno == EINTR)) ;

	fprintf(stderr, "Successfully copied files! Yay\n");

I am to use threads each time I call copyfilepass. Copyfilepass will copy each file. You send it the file descriptor of the next file you want copied, copying one at a time. The problem I am having is right after the print statement saying "got to 78", I get a segmentation fault.

The input I am sending it is "./copydirectory.c first1/\\first2/"

The program correctly parses the two directory names, I am thinking that perhaps when I change filename it is just putting the filename and not the exact path to that file. So it is trying to grab that file from the current directory that the Copydirectory.c program is in (which is a different directory than first1 and first2 of course).

If anyone can put in some input that'd be great. I am wanting to LEARN, not to get a SOLUTION exactly, though code bits are helpful and appreciated. I can't use fgets or fputs, I must use copyfilepass method to copy the file, and I must create a thread and wait for the current thread to finish before copying the next file.

Thank you and let me know if you need anything cleared up.

6 Years
Discussion Span
Last Post by fkjhadslf

hey did you ever finish this program i am having a similar problem with the same sort of program :)


Edited by deceptikon: Snipped request for help by email

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.