samedi 27 juin 2015

the C program does not execute the function outside the main

If I execute the exec() function in another C program as a main function it works perfectly, while if I put it as a function called in the main menu it gives me some warning and the function does not run.

My code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */

int exec (void) {

        char array[100];
        char character;
        int i = 0;
        char* point;
        int j = 0;

        printf ("Digita una stringa");
        printf ("\n");
        do {
            character = getchar();
            array[i] = character;
            i++;
        }
        while (character != '\n');
        array[i-1] = '\0';
        i = 0;

        char* string[100];

        char *word = strtok(array, " .");
        j = 0;
        while (word != NULL) {
            printf("%s\n", word);
            string[j++] = word; // Increment j
            word = strtok(NULL, " .");
        }
        string[j] = NULL; // Make sure the array is NULL term

        printf ("\n");  

    pid_t  pid;
    pid = fork();
    int status;

    if (pid == -1) {
        perror("");
    }else if (pid == 0) {
        execvp(string[0], string);     /* execute the command  */
                fprintf(stderr, "Failed to exec");  
                exit(1);
            }
    else {

        //.. wait until the child ends
        waitpid(-1, &status, 0);
      }

return;
}

int read_input (void) {
    int choice;
    printf("Seleziona una voce dal menu");
    do {    
        printf("\n");
        scanf ("%i", &choice);
        if (choice > 8 || choice < 1)
            printf ("Attenzione, inserisci un valore contemplato dal menu");
    }
    while ( choice > 8 || choice < 1);

return choice;
}

void main (int argc, char *argv[]) {

    printf ("------------------------\n");
    printf ("          MENU         \n");
    printf ("------------------------\n");
    printf ("  \n");
    printf ("1) Esecuzione in foreground di un processo\n");
    printf ("2) Ctrl -C\n");
    printf ("3) Exit\n");
    printf ("4) Background\n");
    printf ("5) Pipe\n");
    printf ("6) Jobs\n");
    printf ("7) fg\n");
    printf ("8) kill\n");
    int menu = read_input();
    switch (menu) {

        case '1' :  
                exec ();
                break; 
        case '2' :
                //ctrl();
                break;
        case '3' :
                //exit_();
                break; 
        case '4' : 
                //background();
                break;
        case '5' :
                //pipe();
                break;
        case '6' : 
                //jobs();
                break;
        case '7' : 
                //fg();
                break;
        case '8' : 
                //kill();
                break;
    }
}

this is the warning:

elaborato.c:31:16: warning: initialization makes pointer from integer without a cast [enabled by default] char *word = strtok(array, " ."); 

Aucun commentaire:

Enregistrer un commentaire