开发者

C Programming - execlp() help?

开发者 https://www.devze.com 2023-03-27 11:46 出处:网络
I\'m creating a shell in C for learning purposes, and so far I\'ve gotten to the point where you can input a string via fgets(), the string is broken down into \"chunks\", and then these chunks are pa

I'm creating a shell in C for learning purposes, and so far I've gotten to the point where you can input a string via fgets(), the string is broken down into "chunks", and then these chunks are passed to execlp(). The first chunk being the name of the command, and subsequent chunks being the commands arguments.

Everything works just fine, except the execlp() call. But I don't see what I'm doing wrong, this all looks legit to me, according to the man pages!

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

#define MAX_CHUNKS 10

/*==========================================================================
 *  Given a string, Break it down into chunks. Separated by ' ', skipping \n
 * ========================================================================*/
int break_down_string(char *input_string, char *pointer_array[MAX_CHUNKS])
{
        char *p = input_string, buffer[100]={0};//Initialize buffer to zero's.
        short int index = 0, space_count = 0, i;


    strncat(p, " ", 1);

    while (*p != '\0')
    {   
        if (index == MAX_CHUNKS) break; //End if MAX_CHUNKS chunks taken from string.
        if (*p == '\n'){ //Skip newline characters.
            p++;
            continue;
            }

        if (*p == ' ') //Space Detected 
        {
            if (space_count == 0)
            {
                pointer_array[index] = (char *)malloc(sizeof(char) * strlen(buffer) +1);
                strncpy(pointer_array[index], buffer, strlen(buffer));
                strncat(pointer_array[index], "\0", 1);
                bzero(buffer, sizeof(buffer));
                index++;
            }
            space_count = 1;
        }
        else //Non-Space Detected
        {
            if (space_count > 0) space_count = 0;
            strncat(buffer, p, 1);
        }
        p++;
    }

pointer_array[index] = NULL; //Set end pointer to NULL for execlp().

return 0;
}



/*--------------------------------MAIN()-----------------------------------*/
int main(void)
{
    char buffer[100];
    char *pointer_array[MA开发者_如何学GoX_CHUNKS]; //Array which will hold string chunks

    fgets(buffer, sizeof(buffer), stdin); 

    break_down_string(buffer, pointer_array);

    if (fork() == 0)
    {
        printf("Child process!\n");
        execlp(pointer_array[0], (pointer_array+1), NULL);
    }
    else
    {
        printf("Parent process!\n");
    }

return 0;
}

Help would be greatly appreciated, I'm really stuck here!


This isn't right:

char *pointer_array[MAX_CHUNKS];
execlp(pointer_array[0], (pointer_array+1), NULL);

execlp is declared as int execlp(const char *file, const char *arg, ...);. A warning should make it quite clear you can't pass a char ** where a char * is expected.


Personally I prefer execvp quite strongly. It also allows you to pass many arguments to the new process.

/* Make sure the last element of pointer_array is NULL. */
execvp(pointer_array[0], pointer_array);

You could also try:

execlp(pointer_array[0], pointer_array[1], NULL);
0

精彩评论

暂无评论...
验证码 换一张
取 消