2

I'm trying to write a wrapper of execl function, but the command doesn't run (with execl directly it works)

Then I added a vfprintf for debugging purpose. But vfprintf only prints /bin/ls, so I think it's truncated somewhere in the middle.

What's wrong with my code?

int my_execl(const char *file, const char *format, ...)
{
    int ret = 0;

    va_list args;
    va_start (args, format);
    vfprintf (stdout, format, args);
    ret = execl (file, format, args);
    va_end (args);

    return ret;
}

int main (int argc , char **argv)
{
    my_execl ("/bin/ls", "/bin/ls", "-r", "-t", NULL);
    return 0;
}
daisy
  • 22,498
  • 29
  • 129
  • 265
  • 3
    There are no function in the [`exec`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html) family which takes a format specification and a `va_list` argument. – Some programmer dude Dec 18 '13 at 09:50
  • That the `vprintf` only prints `"/bin/ls"` is because that's the format string, there are no actual formatting or extra argument in the string. – Some programmer dude Dec 18 '13 at 09:51
  • 1
    And if you really need to write a wrapper (why would you want that?) you have to construct the argument list manually and call e.g. [`execv`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html) with it. – Some programmer dude Dec 18 '13 at 09:53

1 Answers1

0

you have to construct the argument list manually and call e.g. execv with it. – Joachim Pileborg

#include <stdarg.h>
#include <unistd.h>

int my_execl(const char *file, ...)
{
    int ret;
    va_list args;
    va_start(args, file);
    ret = execv(file, (char **)args);
    va_end(args);
    return ret;
}
Armali
  • 18,255
  • 14
  • 57
  • 171