Home Ask Login Register

Developers Planet

Your answer is one click away!

user5588405 February 2016

execv waiting for input input instead of executing program

i am have a program named "test" that executes another program called "hello". After receiving the name of the desired program, my program seems to wait for more input in order to display the "hello" program code. A snippet of example code

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h> /* for fork */
#include <sys/wait.h> /* for wait */
int main()  {

    char *temp[] = {NULL,NULL,NULL,NULL};
    char  buf[BUFSIZ];
    char s[256];

    while(1) {

        printf("type r at next input\n");
        fgets(buf, sizeof(buf), stdin); 
        strtok(buf, "\n");

        if((strcmp(buf,"r")) == 0) { //if r typed
            printf("run what file : ");
            scanf("%s ",s);

            pid_t i = fork();
            if (i == 0) //we are in child process
            {
                execv(s,temp);
                _exit(1);
            }
            if (i != 0) { //parent
                wait(NULL);
            }
        }
        else
            exit(0);
    }
}

the "hello" program is...

#include<stdio.h>

main(int argc, char* argv[])
{
    printf("\nHello World\n");

}

and an example run from the shell on linux is...* means my input, // are comments

issac@issac-ThinkPad-T440s ~$ ./a.out
type r at next input
*r*
run what file : *hello*
*r*    //i cant proceed unless i type a character so i input *r*?

Hello World    //after ?additional? input it finally displays the "hello" program code
type r at next input    //loops back but program skips my input?
run what file ex ? hello : 

i realize this may be a simple mistake but i cant figure out whats wrong with this. i realize the last part with the skipped input is probably due to the input buffer having the newline in there but more confusing is why after executing "hello" my program waits for more input to display result.

Answers


Arkadiusz Drabczyk February 2016

To answer your first question: drop a trailing whitespace in scanf() so it looks like this: scanf("%s",s);.

To answer your second question: your problem is that you mix scanf() and fgets(). A newline is not consumed be scanf() and is passed as a new input to the next (non-first) fgets. The easiest solution is to use fgets in both places:

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

int main()  {

    char *temp[] = {NULL,NULL,NULL,NULL};
    char  buf[BUFSIZ];
    char s[256];

    while(1) {
        printf("type r at next input\n");
        fgets(buf, sizeof(buf), stdin);
        strtok(buf, "\n");

        if((strcmp(buf,"r")) == 0) { //if r typed
            printf("run what file : ");
            fgets(s, sizeof(s), stdin);
            strtok(s, "\n");
            pid_t i = fork();
            if (i == 0) //we are in child process
                {
                    execv(s,temp);
                    _exit(1);
                }
            if (i != 0) { //parent
                wait(NULL);
            }
        }
        else
            exit(0);
    }
}

Post Status

Asked in February 2016
Viewed 2,594 times
Voted 5
Answered 1 times

Search




Leave an answer


Quote of the day: live life