-2

I wrote the following code but I always get the output: "ERROR!" (the execv function not scheduled to return)

What am I doing wrong???

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
#include "LineParser.h"

#define LOCATION_LEN 200
char* getL(void);

int main(int argc,char *argv[])
{
    char *loc = getL();
    char *args[] = {loc,"ls",NULL};
    int i;
    execv(args[0],args);
    printf("ERROR!");
    free(loc);
}

char* getL(void)
{
    char *buff = (char**)malloc(sizeof(char)*LOCATION_LEN);
    getcwd(buff,LOCATION_LEN);
    return buff;
}
Duck
  • 26,924
  • 5
  • 64
  • 92
Roni
  • 73
  • 2
  • 8

2 Answers2

3

Read documentation of execv(3) and of execve(2) and of perror(3). At the very least, you should code

int main(int argc, char *argv[]) {
  char *loc = getL();
  char *args[] = { loc, "ls", NULL };
  int i;
  execv(args[0], args);
  perror("execv");
  free(loc);
}

You should compile with gcc -Wall -g then use the gdb debugger.

Your usage of execv is obviously wrong (you need a full path, e.g. "/bin/ls", and the order of arguments is wrong). You probably want exevcp(3) and you should in fact code at least:

  char *args = { "ls", loc, NULL };
  execvp("ls", args);
  perror("execvp")

If you insist on using specifically execv(3) you could try

  char *args = { "ls", loc, NULL };
  execv("/bin/ls", args);
  perror("execv")

I don't understand what your code is supposed to do. You might be interested by glob(7) & glob(3).

You probably should read Advanced Linux Programming. It seems that there are several concepts that you don't understand well enough. I guess that strace(1) could be useful to you (at least by running strace ls *.c to understand what is happening).

Maybe your getL is exactly what the GNU function get_current_dir_name(3) is doing, but then the (char**) cast inside it is grossly wrong. And you should better clear the buffer buff using memset(3) before calling getcwd(2) (and you should test against failure of ̀ mallocand ofgetcwd`)

Perhaps you want opendir(3), readdir(3), asprintf(3), stat(2); with all these, you could even avoid running ls

If you are coding some shell, you should strace some existing shell, and after having read all the references I am giving here, study the source code of free software shells like sash and GNU bash

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • thanks But must use in execv so can you please explain to me how it works? I did not understand the function parameters. – Roni Oct 05 '14 at 16:12
  • Read all the links I gave you (that should take several hours of reading). I won't try to explain what the references I gave you are explaining better than I could explain in a few minutes. And please *edit your question* to *improve it*. – Basile Starynkevitch Oct 05 '14 at 16:17
  • I tried what you said But it still does not work http://www.siz.co.il/my.php?i=yme2vgnyyt1y.png – Roni Oct 05 '14 at 16:41
  • Your image show `/bin/is` (with a I lower-case) but it should be `/bin/ls` (with an L lower-case), which is given by `which ls` – Basile Starynkevitch Oct 05 '14 at 16:43
0

You are not passing the correct arguments to execv. The first argument must be a path to the executable you wish to run but you are passing the path to the current working directory.

Update getL to return the full path to ls.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187