Thread: Problem execl()

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    24

    Problem execl()

    Hello everyone,

    I am facing a problem with this code. Actually my program should create a new Shell that takes the hole path to a bin command (/bin/ls , /bin/date ...) so it can execute it by using the function execl()

    I succeeded in creating the new Shell that works great but I still have a problem with using execl()

    Here's my code :

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main() {
      int pid, longueur;
      char tab[TABSIZE], *s;
      int r;
    
      for (;;) {
        fputs("small_shell...> ", stdout);  
        s = fgets(tab, TABSIZE, stdin);
    
        if (s == NULL) {
          fprintf(stderr, "End Shell\n");
          exit(EXIT_SUCCESS);
        }
        longueur = strlen(s);
        tab[longueur - 1] = '\0';
    
        pid = fork();
    
        switch (pid) {
          case -1:
            break;
          case 0:
            r = execl(tab, (char*) NULL);
            if (r==-1) {          
              fprintf(stderr, "Error in exec\n");
            }
          default: 
            wait(NULL);
        }
      }
    }

    when I write /bin/date in the "small shell" for example, it tells me that "A NULL argv[0] was passed through an exec system call."

    Does anyone has an idea how to solve this ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It should be
    r = execl(tab, tab, (char*) NULL);

    The first param is the path to the command you want to execute.
    The second param is what will become argv[0], which by convention is the path to the program being run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    Quote Originally Posted by Salem View Post
    It should be
    r = execl(tab, tab, (char*) NULL);

    The first param is the path to the command you want to execute.
    The second param is what will become argv[0], which by convention is the path to the program being run.
    thank u Salem, it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in using execl and pthread
    By gda in forum C Programming
    Replies: 3
    Last Post: 07-13-2012, 09:34 AM
  2. Using execl()
    By dudeomanodude in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2010, 02:13 AM
  3. Problem with execl() and redirection.
    By arunj in forum C Programming
    Replies: 6
    Last Post: 03-20-2008, 08:59 PM
  4. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  5. execl
    By laasunde in forum C Programming
    Replies: 2
    Last Post: 11-19-2002, 05:07 PM

Tags for this Thread