Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:59:17+00:00 2026-06-11T17:59:17+00:00

I’m writing a program to take user input from the command line (linux/unix commands),

  • 0

I’m writing a program to take user input from the command line (linux/unix commands), and execute them within my program.

My steps so far:

  1. Ask user for number of commands input
  2. Fork() to create child process
  3. Output Child PID and Parent PID
  4. Allow user to input each command, read each input into an index of argv
  5. Use execv to run each command inside of argv

The main issue is that when it executes, it merely does the “bin/ls/” in the execv command.

Here is a sample output from running my program:

Enter number of commands: 2
Child’s PID is 3487. Parent’s PID is 3485
Enter a UNIX command: ls
Enter a UNIX command: -al

LIST OF FILES AS IF TYPING “LS” ON THE CMD LINE

Process Complete.

And here is my source code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>

void main(int argc, char *argv[20])
{
        int pid;
        int num = 0;

        printf("Enter number of commands: ");
        scanf("%d", &argc);

        pid = fork();

        if(pid == 0)
        {
                printf("Child's PID is %d. Parent's PID is %d\n", (int)getpid(), (int)getppid());

                for(num=0; num < argc; num++)
                {
                        printf("Enter a UNIX command: ");
                        scanf("%s", argv[num]);
                }

                argv[num+1] = 0;

                execv("bin/ls/", argv);
        }
        else
        {
                wait(pid);
                printf("Process Complete.\n");
                exit(0);
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:59:18+00:00Added an answer on June 11, 2026 at 5:59 pm

    Firstly you are defining char* argv[20] in main which is not a good idea. If you ever pass in more than 20 arguments you will exceed the bounds of the array.

    Secondly, you are attempting to read a string with scanf("%s", argv[num]) into an address space that is not initialized as far as I can tell.

    The argv[] array of “strings” is initialized by the OS when your program is invoked and if you don’t pass any arguments to your program you will not have any “strings”, meaning that you will be writing to random memory which you might not own.

    If you really want to load your commands the way you are doing it now please try the following:

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    
    void main(int argc, char *argv[])
    {
        int pid;
        int num = 0;
        int argc2 = 0;
        char* argv2[20]; // argv2 will point inside of buffer for convenience.
        char* buffer[2000]; // note each array has a limit of 100 characters.
    
        printf("Enter number of commands: ");
        scanf("%d", &argc2);
    
        pid = fork();
    
        if(pid == 0)
        {
                printf("Child's PID is %d. Parent's PID is %d\n", (int)getpid(), (int)getppid());
    
                for(num=0; num < argc2 && num < 20; num++) // your array is 20 long
                {
                        argv2[num] = &buffer[num * 100];
                        printf("Enter a UNIX command: ");
                        scanf("%s", argv2[num]);
                }
    
                argv[num] = 0; // no need to add + 1 because the for loop did already anyway.
    
                execv("Assignments/ls", argv2);
        }
        else
        {
                wait(pid);
                printf("Process Complete.\n");
                exit(0);
        }
    }
    

    Alternatively you could just pass arguments to your main program which simply passes them onto the called program like so:

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    
    void main(int argc, char *argv[])
    {
        int pid;
        int num = 0;
    
        printf("You entered %d commands: \n", argc);
    
        for (num = 0; num < argc; ++num)
        {
            printf("\t%s\n", argv[num]);
        }
    
        pid = fork();
    
        if(pid == 0)
        {
                printf("Child's PID is %d. Parent's PID is %d\n", (int)getpid(), (int)getppid());
    
                execv("Assignments/ls", &argv[1]);
        }
        else
        {
                wait(pid);
                printf("Process Complete.\n");
                exit(0);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.