NKerb 0 Newbie Poster

Hi all,

I'm having some problems with passing execv arguments. The following code appears to work fine, until theres between 4 and 8 arguments in the vector.... I can't see what's wrong.... have I missed something really obvious?

P.S. also any suggestions as to the general level of coding would be appreciated, i'm pretty new to all this!

Many thanks,

Nick

#include <stdio.h>
#include <fcntl.h>
#include <wait.h>
#include <string.h>
#define MAXLEN  256
#define MAXARGS 9

char* getst(void);

main()
{
    int pid, return_pid, return_status, i;
    int argc = 0;
    char path[MAXLEN], prog[MAXLEN];
    char* argv[MAXARGS];
    char *run;
    char *temp = malloc(MAXLEN * sizeof(char));
    char end[] = "end";
    
    // get program details
    printf("path to the program (include trailing slash):");
    scanf("%s", &path);
    printf("program:");
    scanf("%s", &prog);
    run = strcat(path, prog);
    argv[0] = prog;
    // create argument vector
    printf("arguments (enter 'end' to finish):\n");
    do {
        temp = getst();
        if (strcmp(temp, end) != 0) {
            argc++;
            argv[argc] = malloc(MAXLEN * sizeof(char));
            argv[argc] = temp;}
        else
            ; // skip
    } while (strcmp(temp, end) != 0);
    // if child process
    if ((pid = fork())== 0) {
        printf("Child process %d running...\n", getpid());
        execv(run, argv); // execute program
        fprintf(stderr, "execv() failed\n"); // error handling
        exit(-1); // exit with error code
    } 
    else if (pid > 0) { // if parent process
        printf("Parent process %d waiting...\n", getpid());
        // wait for child, get return status and child pid
        return_pid = wait(&return_status);
        printf("Parent finished waiting\n");
        if (WEXITSTATUS(return_status) == 0)
            printf("Child process %d completed successfully\n", return_pid);
        else
            printf("Child process %d failed\n", return_pid);        
    }
    else {
        fprintf(stderr, "fork failed\n"); // error handling
        exit(-1); }
}

// gets string from keyboard, returns as char pointer
char* getst(void)
{
    char* tempst = malloc(MAXLEN * sizeof(char));

    scanf("%s", tempst);    
    return tempst;
}
Sample output:

nick@Garfield:~/source/sandbox$ ./execv
path to the program (include trailing slash):/bin/
program:echo
arguments (enter 'end' to finish):
1
2
3
end
Child process 6309 running...
Parent process 6304 waiting...
1 2 3
Parent finished waiting
Child process 6309 completed successfully
nick@Garfield:~/source/sandbox$ ./execv
path to the program (include trailing slash):/bin/
program:echo
arguments (enter 'end' to finish):
1
2
3
4
end
Child process 6317 running...
execv() failed
Parent process 6312 waiting...
Parent finished waiting
Child process 6317 failed
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.