title: Copy directory with symbolic links pointing to the copied files (inside directory tree)
body: <p>I want to copy a folder and all of its contents including sub-folders. I am using C on Ubuntu.</p>
  
  <p>Copying regular files, and folders is easy and done so far, but the specifications for copying links (symbolic for now) is that they should link to the copied files. For now I am only interested in links inside the directory tree. </p>
  
  <p>(Although links outside the tree I think should be easier - just copy the full path to the new link plus find out if they belong in the tree or not - that's hard although <a href="https://stackoverflow.com/users/377270/sarnold">sarnold</a> gave me a tip about using <code>rsync</code> to achieve that)</p>
  
  <p>So I have an absolute path returned by readlink:</p>
  
  <pre><code>/home/giorgos/Desktop/folder/folder1/a.pdf
  </code></pre>
  
  <p>Which in the worst case would be :</p>
  
  <pre><code>/home/giorgos/Desktop/folder/folder/folder/folder1/a.pdf
  </code></pre>
  
  <p>but I can't find a way to retrieve the relative path to my directory tree. If I could find it I could replace it with the copied directory's name:</p>
  
  <pre><code> /home/giorgos/Desktop/March/folder/myfolder/folder/folder1/a.pdf
  </code></pre>
  
  <p>I can't use cp or the system() function or functions of that sort, the solution has to be low level. I can use c libraries plus GNU, but please post an answer anyway I am interested.</p>