title: How do you convert a relative path to an absolute path in C?
body: <p>I am trying to make a suid application that will only execute ruby scripts located in a restricted folder. I have tried to do this using realpath(3) but it is only returning the first segment of the path. Below is my code...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  #define SUEXEC_STR_LEN 2048
  #define RUBY_APP "/usr/bin/ruby"
  #define DIRECTORY_SEPARATOR "/"
  
  static void safepath(const char *path_in, char * path_out, int outlen) {
      realpath(path_in, path_out);
  }
  
  int main ( int argc, char *argv[] )
  {
      char cmd[SUEXEC_STR_LEN];
      char path_out[SUEXEC_STR_LEN];
      char path_in[SUEXEC_STR_LEN];
  
      char *cp = &amp;cmd[0];
  
      strncpy(cp, RUBY_APP, SUEXEC_STR_LEN - 1);
  
      strncpy(path_in, DIRECTORY_SEPARATOR, SUEXEC_STR_LEN - 1);
      strncat(path_in,argv[1],SUEXEC_STR_LEN - 1);
  
      safepath(path_in,path_out,SUEXEC_STR_LEN - 1);
  
      printf("path_in=%s path_out=%s\n",path_in,path_out);
  
      setuid( 0 );
      // system( cmd );
  
      return 0;
  }
  </code></pre>
  
  <p>This is an example of the result I'm getting</p>
  
  <pre><code>root@server01:/root/src# ./a.out foo/bar/../test
  path_in=/foo/bar/../test path_out=/foo
  </code></pre>
  
  <p>This is the result I want</p>
  
  <pre><code>root@server01:/root/src# ./a.out foo/bar/../test
  path_in=/foo/bar/../test path_out=/foo/test
  </code></pre>