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 8240185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:08+00:00 2026-06-07T20:24:08+00:00

I am writing a simple shell program that must take input from a file

  • 0

I am writing a simple shell program that must take input from a file one character at a time using the read() function in C. The read() function is the only way I want to take input from the user. I can get this part to operate properly in a very simple like this one:

int main( int argc, char *argv[] )
{
int error = 0;
int input_result = 1; /*Integer to track the result of inputs.*/
int input_exit = 0;
char input_buffer[100];
char *buffer_pointer = &input_buffer[0];
char *input;

write( 1, "# ", 2 ); /*Display # to signal the user to input data.*/

int input_counter = 0; /*Int that tracks the number of input characters.*/

/*While loop to continually take input from the user.*/
/*Step one of the programming assignment.*/
while( input_exit == 0 && input_result == 1 && input_counter < 15 && error == 0)
{
    input_result = read( 0, input, (size_t) 1 );

    input_buffer[input_counter] = *input;

    printf( "%c - %d\n", input_buffer[input_counter], input_result );

    input_counter++;
} /*End while for user input*/

write( 1, input_buffer, (size_t) input_counter );
}

Using my input file, which is also very simple I get the proper output with no errors what so ever. Once I start to make the code more complicated and add some nested while loops I start to receive the “Bad address” error. The code that is throwing the error can be seen below. As a side note, I do realize my C programming is not the greatest overall and there are things I could change, however I would like to just focus on the issues with read. Thank you in advance for your assistance it is greatly appreciated.

int main( int argc, char *argv[] )
{
/*Infinite loop to keep the shell running until it is implicitly ended by the user.*/
while( 1 )
{
    int error = 0; /*Int to keep track if an error occurred.*/

    char input_buffer[101]; /*Array of chars to hold the input.*/
    char *input_bufferp = &input_buffer[0]; /*Pointer to the first element of the char array.*/
    char *input; /*Char pointer to hold the read input*/
    char *newline = "\n";
    int buffer_counter = 0; /*Int that tracks the number of input characters.*/
    int input_result = 1; /*Int to hold the result of the read.*/

    char input_string[17][64]; /*Array to the parsed input data.*/
    char **input_stringp; /*Pointer to the first element of the string array.*/
    char *input_strings; /*Holds the parsed information before it is organized.*/
    int string_counter = 0; /*Int to track the number of strings.*/


    write( 1, "# ", 2 ); /*Display # to signal the user to input data.*/

    /*While loop to parse the information into separate strings.*/
    /*This while loop contains steps 1 and 2.*/
    while( string_counter == 0 && error == 0)
    {
        /*While to take in information via read.*/
        while( buffer_counter < 100 && input_result == 1 )
        {
            input_result = read( 0, input, (size_t) 1 ); /*Read one char from the user.*/

            /*If statement to signal an input error.*/
            if( input_result == -1 )
            {
                error = 1; /*Signal the error*/
                printf( "\nInput errno: %s\n", (char *)strerror(errno) ); /*Inform the user of the error.*/

                exit(0);
            }/*End if to signal an error.*/

            /*If to handle the end of the file.*/
            else if( input_result == 0 )
            {
                input_buffer[buffer_counter] = '\0'; /*Place the escape char.*/
            }/*End if for end of file.*/

            /*If statement handles a proper read from the user.*/
            else if( input_result == 1 )
            {
                /*If statement to check for a new line.*/
                if( strcmp( input_bufferp[buffer_counter], newline ) == 0 )
                {
                    input_result = 0; /*Set variable to exit the while loop.*/
                    input_buffer[buffer_counter] = '\0'; /*Place the escape char.*/
                }/*End new line if.*/

                /*Else statement to put input into the buffer.*/
                else
                {
                    input_buffer[buffer_counter++] = *input; /*Place the input into the buffer.*/
                }/*End buffer else.*/
            } /*End good input read.*/
        } /*End input gather while loop.*/

        write( 1, input_bufferp, (size_t) buffer_counter ); /*Echo the input to the user*/

        input_strings = strtok( input_bufferp, " \t" ); /*Split the input into tokens.*/

        /*While loop to tokenize the rest of the data.*/
        while( input_strings != NULL )
        {
            input_stringp[string_counter++] = input_strings; /*Store the tokenized string.*/
            input_strings = strtok( NULL, " \t" ); /*Spilt the remain data into tokens.*/
        } /*End tokenizer while loop.*/

        input_stringp[string_counter--][0] = '\0'; /*Please the escape char.*/
    } /*End parsing while loop.*/

    /*Check if the user wants to exit.*/
    if( strncmp( *input_stringp, "exit", 4 ) == 0 )
    {
        exit( 0 ); /*Exit the shell.*/
    } /*End if to exit program.*/


} /*End of infinite while loop.*/
}
  • 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-07T20:24:09+00:00Added an answer on June 7, 2026 at 8:24 pm

    You cannot use the value of a variable until you set it to some value. You never initialize input to point to anything and then you pass its value to read, reading data to no place in particular.

    The second parameter to read tells read where to store the data. It must point to some space that you have allocated to store one or more characters, which your code never does.

    Change input to be a char, not a char *. That will allocate space to store ono character. Then pass the address of input to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple shell script that fetches output from one command and
I'm writing a shell script that outputs all untranslated strings from gettext .po file,
I am writing simple parallel program in C++ using OpenMP. I am working on
In one of my courses we're writing our own shell (basically from scratch). We've
I am currently writing my own shell program. This simple shell can just execute
When writing a simple program for a POSIX-compliant OS which accepts input and produces
I am making simple ANSI C program, that simulates Unix shell. So I am
I'm writing a very simple shell scripts that would looked at the log of
So i'm writing a C program for a simple shell. Not too difficult but
I am writing a program that's similar to a shell. Once started up, there's

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.