#Execv not working

8 messages · Page 1 of 1 (latest)

sick rover
#

I wrote a programm to simulate a shell, if i type "echo test" it doesnt work

#
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>

#include "plist.h"

#define buffsize 1337

void fehler(char * meldung){
    perror(meldung);
    exit(EXIT_FAILURE);
}

int main(){
    char directory[buffsize];
    char cmdline[buffsize];
    if (getcwd(directory, buffsize) == NULL){
        fehler("getcwd");
    }
    printf("%s: ", directory);
    
    if (fgets(cmdline, buffsize, stdin) == NULL){
        fehler("fgets");
    }
    
    
    char **arguments = malloc(sizeof(char*));
    
    char delimiter[] = " \t";
    
    char *token = strtok(cmdline, delimiter);
    
    
    arguments[0] = malloc(sizeof(directory));
    strcpy(arguments[0], directory);
    int i = 1;
    
    while (token != NULL){

        arguments = realloc(arguments, sizeof(char*) * (i+2));
        
        arguments[i] = malloc(sizeof(token));
        
        strcpy(arguments[i], token);
        token = strtok(NULL, delimiter);
        
        i++;
    } 
    
    arguments[i] = NULL;
    
    for (int j = 0; j < i; j++){
        printf("%s\n", arguments[j]);
    }
    
    int p = fork();
    
    if (p == 0){
        if (execv(directory, arguments) == -1){
            fehler("execv");
        }
    }
    

}
#

directory is the directory (obviously) extracted with getcwd

#

if i type "echo test", it gets split by strtok and stored into the arguments array

#

i pass it into execv

signal horizon
#

I can help in the morning.

sick rover
#

no need i got a solution

#

thank you