Title: "Unknown Command" error when passing "uname" to execv()
Tags: c;parsing;ubuntu;execv
Question: So I have to build a program in C that practically takes a command from keyboard , split it into tokens that are stored in an array and use those tokens as input to "execv" (a command in ubuntu) , I chose the command "uname" with the parameter "-a", but for some reason it keeps saying "Comanda necunoscuta!" (Unknown Command!) Heres my code:

```#include <stdio.h&gt;
#include<stdlib.h&gt;
#include <string.h&gt;   /*strtok strcpy*/
#include<malloc.h&gt;   /*malloc*/
#include <sys/types.h&gt; /* pid_t */
#include <sys/wait.h&gt;  /* waitpid */
#include <unistd.h&gt;    /* _exit, fork */

int main()
{
    int i=0;
    char *cuvinte[256]; //words
    char comanda[256];  //command

    printf("Introduceti comanda: "); // command input
    fgets(comanda,sizeof(comanda),stdin); // read command
    char *c = strtok(comanda," "); // break command into tokens

    while(c!=0)
    {
        cuvinte[i] = malloc( strlen( c ) + 1 ); //alocate memory
        strcpy(cuvinte[i++],c); // copy them
        printf("%s\n",c);       // print them
        c=strtok(NULL, " ,.!?");
        }
    printf("Sunt %d elemente stocate in array! \n\n",i); // no of elements stored
    printf("Primul cuvant este: %s \n\n",cuvinte[0]); // shows the first token
    if((cuvinte[0]=='uname')&amp;&amp;(cuvinte[1]=='-a')){    // here lays the problem i guess
      /*face un proces copil*/
      pid_t pid=fork();
        if (pid==0) { /* procesul copil*/
        static char *argv[]={"/bin/uname","-a",NULL};
        execv(argv[0],argv);
        exit(127); /*in caz ca execv da fail*/
        }
        else { /* pid!=0; proces parinte */
        waitpid(pid,0,0); /* asteapta dupa copil */
        }
    }
    else printf("Comanda necunoscuta !\n"); // problem
    //getch();
    return 0;
}
```

Comment: There is no reason to run `/bin/uname` ([uname(1)](http://man7.org/linux/man-pages/man1/uname.1.html)) as a separate process in C when you could simply use the [uname(2)](http://man7.org/linux/man-pages/man2/uname.2.html) & [gethostname(2)](http://man7.org/linux/man-pages/man2/gethostname.2.html) syscalls (using them is faster and more reliable, since you don't need any external file like `/bin/uname` to be present).

Comment: Its my program,but back then I had another problem with it,which I solved , now I hit another bump,someone told me to repost question not edit the old one, so i did !

Comment: possible duplicate of [C Progam parser for ubuntu?](http://stackoverflow.com/questions/26755741/c-progam-parser-for-ubuntu)

Comment: Please compile with warnings enabled. You have a multi-character constant `'uname'`, which isn't what you think it is.

Here is the accepted answer: Firstly add

``` cuvinte[1][strlen(cuvinte[1])-1]='\0';
```

after the while loop and just before "printf("Sunt %d elemente stocate in array! \n\n",i);"

second thing 

use 

``` if((strcmp(cuvinte[0],"uname")==0) &amp;&amp; (strcmp(cuvinte[1],"-a")==0))
```

instead of "=="

program will work !!

Comment for this answer: yes it has to be added so that "\n" in the end can be avoided. Please accept the answer then :)
Comment for this answer: Ive tryid this method before but I didn't add  cuvinte[1][strlen(cuvinte[1])-1]='\0'; and because of that it didn't work.Thanks it works now !

Here is another answer: First of all, I don't have enough reputation to post a comment sorry for that.

I'm not sure you can compare two strings with the '==' operator in C. Try using the strcmp function.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:11/05/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><parsing><ubuntu><execv>', 'Title': '"Unknown Command" error when passing "uname" to execv()', 'ViewCount': 456, 'id': 26757777, 'user_AboutMe': '', 'user_AccountId': 5283624.0, 'user_DisplayName': 'Alexandru Ianas', 'user_Reputation': 95.0, 'user_Views': 33.0, 'user_id': 4218247.0}}