1
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#define MAXLINIE 100


main(int argc, char* argv[]) {
    if (fork()==0){

        execl("/bin/> temporar.txt", "/bin/> temporar.txt", ">temporar.txt", NULL);

    }
}

Basically, what I am trying to do is creating a file using a process in unix, here is my code, but for some reason it does not work, I do not really understand the execl command and why the first two parameters have to be the same: execl("/bin/ls", "/bin/ls", "-l", NULL); this is working well, could someone help me ?

Thanks a lot!

Shimon Rachlenko
  • 5,469
  • 40
  • 51
JackRobinson
  • 225
  • 2
  • 11
  • 24
  • What is the idea behind `"/bin/> temporar.txt"`? – alk Apr 14 '13 at 11:04
  • The last parameter shall be `(char *) NULL`, not `NULL`. – alk Apr 14 '13 at 11:05
  • All that parameters will be passed as variardic arguments, that have `void*` type. Anyway, C accepts some kind of implicit conversion, so `NULL` is OK even for `char*`. – loentar Apr 14 '13 at 11:06
  • @Ioentar: Verbatim from `man execl`: `The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.` – alk Apr 14 '13 at 11:08
  • You're right. That is written in `man execl`, that it should be idealogicaly. But this is not a problem for C. Even more no one casts `(char*)NULL` even execl implementation. If you compile `execl(..., NULL)` with maximum level of warning enabled you will not get any warning regarding to implicit cast to `char*`. – loentar Apr 14 '13 at 11:17
  • 1st parameter to `execl` is the name of the program and then you start arguments to the program as subsequent arguments... The argv[0] to any C program is the name of the program itself. Hence the first two arguments are the same... – Sam Apr 14 '13 at 11:32

2 Answers2

2

Consider use system() instead:

system("/bin/ls -l > temporar.txt");

Or using execl call /bin/sh to redirect stream:

execl("/bin/sh", "/bin/sh", "-c" , "/bin/ls -l >temporar.txt", NULL);

First parameter of execl is a command to execute, second is a first parameter to be passed to command (argv[0]), third and next - other arguments argv[1] ...

loentar
  • 5,111
  • 1
  • 24
  • 25
  • 1
    I am not allowed to use system, i have to create myself the process, i don't want all the "ls -l", only to create the file – JackRobinson Apr 14 '13 at 11:00
  • I get "No such file or directory" error when i run it like this "execl("/bin/sh", "/bin/sh", "/bin/>temporar.txt", NULL);" because i only want to create a file in the folder from where I am launching the c source, the ls-l was an example that worked and I've tried to adapt it to my problem – JackRobinson Apr 14 '13 at 11:06
  • Sorry, forgot to pass -c flag – loentar Apr 14 '13 at 11:10
2

first search whereis is touch:

~$ whereis touch
touch: /bin/touch /usr/bin/touch /usr/bin/X11/touch 

use: int execl(const char *path, const char *arg, ...);

execl("/bin/touch", "touch", "filename", NULL);
          ^            ^         ^         ^
       command       command   argument    
        path           name
                     arg 0     arg 1    
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208