A better implementation of the `execvp` call: the code implements `execvp` I'm trying to implement execvp () using execv (). I don't know if the code is the best implementation.
my code:
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;

extern char **environ; 

int my_execvp(char *file, char *argv[])
{
  char *p, *colon, *pathseq = getenv(&quot;PATH&quot;);
  size_t len;

  if(strchr (file, '/'))
    return execv(file, argv);

  if (pathseq == NULL) {
        len = confstr(_CS_PATH, (char *) NULL, 0);
        pathseq = (char *) alloca(1 + len);
        pathseq[0] = ':';
        (void) confstr (_CS_PATH, pathseq + 1, len);
  }
  len = strlen(file) + strlen(pathseq) + 2;

  for(p = pathseq; p &amp;&amp; *p; p = colon)
  {
     char b[len];
     colon = strchr(p, ':');
     if(colon)
     {
        memcpy(b, p, (size_t) (colon - p));
        b[colon++-p] = 0;
     }
     else
        strcpy(b, p);
     strcat (b, &quot;/&quot;);
     strcat (b, file);
     if(!access(b, F_OK)) {
            execv(b, argv);
            fprintf(stderr, &quot;an error occurred in execv\n&quot;);
            abort();
     }
   }
   errno = ENOENT;
     return -1;
}

int main()
{
    /* &quot;ls / -l&quot; */
    char *arg[] = {&quot;ls&quot;, &quot;/&quot;, &quot;-l&quot;, NULL};
    my_execvp (arg[0], arg);

    return 0;
} 

And the question I have, is it a good implementation? How could it be improved or write a better implementation?
The implementation is non-conformant.

No handling of undefined PATH. Quoting man execp:


search path is the path specified in the environment by
``PATH'' variable.  If this variable is not specified, the default path
is set according to the _PATH_DEFPATH definition in &lt;paths.h&gt;

Once confstr returns 0, check errno, and act accordingly.

Calling abort on execv failure is plain wrong. At least return what execv returns.

Calling access is also incorrect. If the file exists, but doesn't have correct permissions, execvp shall continue searching.


As a side note, access is very rarely useful. Consider calling execvp blindly, and testing errno upon return.

Final errno = ENOENT; is also non-compliant. execvp sets ENOENT if the file does not exist in any path. If it was found at least once, and fails to execute, the errno shall be EACCES.

execvp is not supposed to fprintf anything.