Search code examples
clinuxshellexecstrtok

Problems with execvp() and/or strings , why this code does not work?


I wrote this code that opens a file and executes every line of files exept comment "#":

myfile:

ls -l
ls -a
cat /tmp/filex

Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void run(const char *cmd, const char *arg){
    char *argv[3];
    asprintf(&argv[0],"%s", cmd);
    asprintf(&argv[1], "%s", arg);
    argv[2] = NULL;
    pid_t pid = fork();
    if(pid == 0){
        execvp(argv[0], (char * const *) argv);
        perror("error: ");
        exit(EXIT_FAILURE);
    }
    waitpid(pid, NULL, 0);
}

void scriptexec(FILE *fp){
    char *line = NULL, *cmd = NULL, *arg=NULL, *delim=" \n\t";
    size_t len = 0;
    while(getline(&line, &len, fp) != -1){
        cmd = strtok_r(line, delim, &arg);
        if(cmd[0] != '#')
            run(cmd, arg);
    }
}

int main(int argc, char *argv[]){
    if(argc < 2) exit(EXIT_FAILURE);
    FILE *fp;
    if((fp = fopen(argv[1], "r" )) == NULL) exit(EXIT_FAILURE);
    scriptexec(fp);
    fclose(fp);
    return 0;
}

$ ./a.out file

I get this output:

ls: invalid option -- '
'
Try 'ls --help' for more information.
ls: invalid option -- '
'
Try 'ls --help' for more information.
cat: /tmp/filex
: No such file or directory

I also tried to write another code using the run() function, it works, no problems!

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void run(const char *cmd, const char *arg){
    char *argv[3];
    asprintf(&argv[0],"%s", cmd);
    asprintf(&argv[1], "%s", arg);
    argv[2] = NULL;
    pid_t pid = fork();
    if(pid == 0){
        execvp(argv[0], (char * const *) argv);
        perror("error: ");
        exit(EXIT_FAILURE);
    }
    waitpid(pid, NULL, 0);
}

int main(){
    run("ls", "-l");
}

I can't understand...


Solution

  • ls: invalid option -- '
    '
    

    See the two single-quotes. There is a new-line being passed to ls, which is indeed an "invalid option".

    getline() returns the new-line, the input had been triggered with.

    Just chop it off, if around, before passing on what had been read.

    To do so you can use this:

    line[strcspn(line, "\r\n")] = '\0';