Execute the TOP command in exec in C - Linux?

0

Updated code, it does not execute commands

#include <stdlib.h> 
#include <unistd.h> 
#include <stdio.h>  



void ExeclLS(int argc, char **argv)  {
    char *args[] = {"ls", "-aF", "/", 0};
    char *env[] = { 0 };

    printf("Rodando o bin/ls\n");
    execve("bin/ls", args, env);
}

void ExeclTop(int argc, char **argv) {
    char *args[] = {"top", "-aF", "/", 0};
    char *env[] = { 0 };

    printf("Rodando o bin/top\n");
    execve("/usr/bin/top", args, env);
}

int main() {

    int valorMenu;

    printf("Digite o comando : 1 - ls , 2-top\n");
    scanf("%i",&valorMenu);

    if (valorMenu == 1){
    printf("Rodando o bin/ls\n");
    ExeclLS(0, NULL);

    }else{
    printf("Rodando o bin/Top\n");
    ExeclTop(0,NULL);

    }

        perror("execve");   
        exit(1);
    return 0;
 }

Can anyone help me?

    
asked by anonymous 07.03.2016 / 15:03

2 answers

3

No snippet

    if (valorMenu == 1) {
        printf("Rodando o bin/ls\n");
        void ExeclLS(int argc, char **argv);    // <== ATENÇÃO
    }

the line with the attention call "does nothing". It's just a statement.

To execute the writes function, for example

        ExeclLS(0, NULL);
    
07.03.2016 / 17:26
2

The message of perror()

  

/ bin / top: no files found

indicates that top is not in directory /bin . At the command line, it does whereis top to try to find out where the program in question is.

$ whereis top
top: /usr/bin/top

In this case, the top is in the /usr/bin directory.

    
07.03.2016 / 17:21