1

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>
#include<stdlib.h>
#include <string.h>   /*strtok strcpy*/
#include<malloc.h>   /*malloc*/
#include <sys/types.h> /* pid_t */
#include <sys/wait.h>  /* waitpid */
#include <unistd.h>    /* _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')&&(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;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Alexandru Ianas
  • 95
  • 1
  • 11
  • possible duplicate of [C Progam parser for ubuntu?](http://stackoverflow.com/questions/26755741/c-progam-parser-for-ubuntu) – Philip Kendall Nov 05 '14 at 13:01
  • 1
    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). – Basile Starynkevitch Nov 05 '14 at 13:03
  • Please compile with warnings enabled. You have a multi-character constant `'uname'`, which isn't what you think it is. – M Oehm Nov 05 '14 at 13:15
  • 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 ! – Alexandru Ianas Nov 05 '14 at 13:18

2 Answers2

2

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.

Tristan Djahel
  • 1,082
  • 2
  • 12
  • 22
2

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) && (strcmp(cuvinte[1],"-a")==0))

instead of "=="

program will work !!

MrTambourineMan
  • 1,025
  • 1
  • 11
  • 19