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

  • SEARCH
  • Home
  • 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 8705107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:16:27+00:00 2026-06-13T03:16:27+00:00

I have an assignment in which I need to create a unix shell using

  • 0

I have an assignment in which I need to create a unix shell using fork(). I’ve got this working correctly. Now I need to check user input to see if it is a valid unix command. If it is not valid (ie its “1035813”) I need to tell the user to enter a valid command.

Is there a way I can get a list of every possible unix command so I can compare the user input with every string in this list? Or is there an easier way to do this?

  • 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-13T03:16:29+00:00Added an answer on June 13, 2026 at 3:16 am

    The appropriate way to do this is:

    1. check if it’s a built-in command in your shell. For example, cd should probably be a built in command.
    2. fork and try to exec it. (execvp is probably what you really want, actually). If that fails, check errno to determine why.

    Example:

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int main(int argc, char* argv[])
    {
      if (argc != 2) {
        printf("usage: %s <program-to-run>\n", argv[0]);
        return -1;
      }
    
      char* program      = argv[1];
      /* in this case we aren't passing any arguments to the program */
      char* const args[] = { program, NULL };
    
      printf("trying to run %s...\n", program);
    
      pid_t pid = fork();
    
      if (pid == -1) {
        perror("failed to fork");
        return -1;
      }
    
      if (pid == 0) {
        /* child */
        if (execvp(program, args) == -1) {
          /* here errno is set.  You can retrieve a message with either
           * perror() or strerror()
           */
          perror(program);
          return -1;
        }
      } else {
        /* parent */
        int status;
        waitpid(pid, &status, 0);
        printf("%s exited with status %d\n", program, WEXITSTATUS(status));
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment in which I need to create a function that tells
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have a database assignment which I have to create some relational algebra for
I have a school assignment where I need to create a data-driven style of
I have got an assignment in which I have to implement a AS3 wrapper
I've this question from an assignment to create a Store which rent out books,
I'm working on an assignment for uni where I have to create a Breakout
I'm pretty new to C# and got this assignment. I have a class called
I have an assignment in which I have to allow a user to plot
I have a Scheme assignment in which a user is to input a list

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.