C program, execvp() not working, executing only "sh -x"

Asked by Bf332

I was attempting to turn verbose mode on/off in my C program. I was reading some documents online and entered the command "sh -x" and not execvp() will only execute "sh -x" when being called. I am giving the correct arguments for execvp because it was working before attempting to turn on verbose mode. Could anyone point me in the right direction to fix this?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Bf332
Solved:
Last query:
Last reply:
Revision history for this message
Bf332 (beau1087) said :
#1

I'm using VirtualBox.

Using ubuntu 64-bit iso file.

C program is an interface. Takes user input, parses it, creates a child process and the child calls execvp() with the user input. However, execvp() is executing "sh -x" instead of the user's input (say "ls -al").

This was working perfectly fine until attempting verbose mode. I was reading about "sh -x" and gcc options. I can't find a way to turn verbose mode off.

Example output:

      osh > (user input goes here)
      $ (user input) <----------------------------- This should not occur, it should display the output of the user's previous command
      $ exit
      osh > exit
      $ exit
      ...

Revision history for this message
Bf332 (beau1087) said :
#2

I have attempted to create a new virtual machine and run the program on there, but the same issue occurs.

Also, when compiling with "gcc osh.c -o -lm osh" I am now receiving compilation errors which did not occur before.

If this is the wrong place for this question, please let me know. Thank you.

Revision history for this message
Puru (tuladharpuru) said :
#3

Hi,

Hope, I can be of help to you :)

Regarding the 1st question. Can you paste the code? (assuming its open-source)

Regarding the compilation error, can you try: gcc osh.c -lm -o osh

Thanks

Revision history for this message
Bf332 (beau1087) said :
#4

Hello,

The code is quite long. I know the issue is happening when calling execvp(). However, I know I'm giving the correct arguments. This code was working correctly before attempting to turn on verbose.

I believe compiling using gcc osh.c -lm -o osh worked but I still had the issue with execvp().

Here's a snippet of how I know execvp() is the issue:

    char* temp[2];
    strcpy(temp[0],"ls");
    if(execvp(temp[0],temp) == -1)

Revision history for this message
Bf332 (beau1087) said :
#5

Sorry, that wasn't the whole bit of code. Here it is:

    char* temp[2];
    strcpy(temp[0],"ls");
    if(execvp(temp[0],temp) == -1)
    {
        printf("EXEC ERROR");
        exit(1);
    }
    exit(0);

Revision history for this message
Bf332 (beau1087) said :
#6

I know execvp() isn't working because that should display the contents of the current directory. I have a history file that shows all of the commands that were executed by execvp(). The history file is diplaying this:

3 sh -x
2 sh -x
1 sh -x

Revision history for this message
Bf332 (beau1087) said :
#7

Incredibly sorry about this. My code was the issue, I edited my code without properly commenting so i forgot I had it. I was changing my arguments before passing them to execvp(). Thank you for trying to help and I apologize for wasting your time.

Revision history for this message
Puru (tuladharpuru) said :
#8

Aah! There you go!

Also, try to get help from #ubuntu IRC channel, it'll be must quicker.

Enjoy :)