-1

I'm trying to implement cp -r command so that when user types in cp -r dir dir1, dir gets copied and pasted inside dir1. Below is what i have so far and it does copy the files and directories inside the directory, but it doesn't copy directory itself. For example, when there is a file1 and a directory 'a'inside dir then it will only copy and paste file1 and 'a' inside of dir1, but not the directory dir itself. Any suggestions?

james jung
  • 21
  • 1
  • 4

1 Answers1

0

You start your algorithm one step too deep into the directory you are copying: dir_entry = readdir(dir) reads all the entrys inside your source directory and therefore the source directory itself is not copied.

The function is fine, you just need one extra step before you call it.

Instead of calling dirCopy("a", "b") you need to start by executing

mkdir("b/a", convertMode("a")); 

and then

dirCopy("a", "b/a");

So you are going to need code that extracts the last filename part from your path so you can then append it to newPath. If you need assistance for that you can look at this question: Get a file name from a path

Community
  • 1
  • 1
A Person
  • 1,062
  • 9
  • 17