简体   繁体   中英

char array input with space

my program works fine if i give hard code value to char *w="ls -l" but i am trying to take input form user not working help my code:: using input error occur i don't understand the concept of fgets using fgets its gives the garbig value to execv

#include<stdio.h>
#include<sys/wait.h>
#include<stdbool.h>

void func(char **arr, char *w)
{
    int i = 0, j = 0, k = 0;

    char temp[100];

    for (i = 0; i < 100; i++)
    {
        if (w[i] == '')
        {
            arr[k] = temp;
            arr[k+1] = NULL;
            break;
        }
        if (w[i] == ' ')
        {
            arr[k] = temp;
            k++;
            j = 0;
        }
        else
        {
            temp[j] = w[i];
            j++;
        }

    }
}
int main()
{
    char *n = "/bin/ls";
    char *arr[10] = {''};
    char p[100] = {''};
    char *w = "ls -l";
    int i = 0;
    //printf("bilal-hassan-qadri $ >>");
    //fgets(p, 100, stdin);
    arr[2] = NULL;
    bool found = false;
    for (i = 0; i < sizeof(w); i++)
    {
        if (w[i] == ' ')
        {
            found=true;
            func(arr,w);
            break;
        }
    }
    if (!found)
      arr[0] = w;
    int status;
    int id = fork();
    if (id == 0)
    {
        if (execv(n,arr) < 0)
        {
            printf("invalid commandn");
        }
        else
        {
            printf("ninvalid command");
        }
    }
    else
    {
        wait(&status);
    }
}
  • In the function func , You have to copy the string to elements of arr instead of just passing the address of temp , which will vanish on leaving the function. You can use strdup instead of copy_string if your system supports it.
  • You have to terminate the string in temp before copying it.
  • Empty string constant '' seems invalid. You shouldn't use it.
  • fgets stores new-line character \\n if it exists. Check for it and remove if it isn't wanted.

Fixed code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/wait.h>
#include<stdbool.h>

char *copy_string(const char *str) {
    char *s = malloc(strlen(str) + 1);
    if (s) strcpy(s, str); else {perror("malloc"); exit(1);}
    return s;
}

void func(char **arr, char *w)
{
    int i = 0, j = 0, k = 0;

    char temp[100];

    for (i = 0; i < 100; i++)
    {
        if (w[i] == '\0' || w[i] == '\n')
        {
            temp[j] = '\0';
            arr[k] = copy_string(temp);
            arr[k+1] = NULL;
            break;
        }
        if (w[i] == ' ')
        {
            temp[j] = '\0';
            arr[k] = copy_string(temp);
            k++;
            j = 0;
        }
        else
        {
            temp[j] = w[i];
            j++;
        }

    }
}
int main(void)
{
    char *n = "/bin/ls";
    char *arr[10] = {NULL};
    char p[100] = {0};
    char *w = "ls -l";
    int i = 0;
    //printf("bilal-hassan-qadri $ >>");
    fgets(p, 100, stdin);
    w = p;
    arr[2] = NULL;
    bool found = false;
    for (i = 0; w[i] != '\0'; i++)
    {
        if (w[i] == ' ')
        {
            found=true;
            func(arr,w);
            break;
        }
    }
    if (!found)
      arr[0] = w;
    int status;
    int id = fork();
    if (id == 0)
    {
        if (execv(n,arr) < 0)
        {
            printf("invalid commandn");
        }
        else
        {
            printf("ninvalid command");
        }
    }
    else
    {
        wait(&status);
        for (i = 0; arr[i] != NULL; i++) free(arr[i]);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM