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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:58:33+00:00 2026-06-09T04:58:33+00:00

I have developed a mini-shell that accepts text from stdin. It works so far,

  • 0

I have developed a mini-shell that accepts text from stdin. It works so far, but I need to make it return (in the little thing before the >)

The machine name they are logged into AND
The current working directory.

I have

#include <unistd.h>

in the code, and the current working directory was gotten using getcwd(2), which worked fine, but gethostname(2) does not seem to be working. It won’t compile using gcc on MINGW32, giving error: undefined referencew to ‘gethostname’ collect2: ld reurned 1 exit status.

Here is my code, so far.

#include <stdio.h>
#include <string.h>
#include <unistd.h>

static int LINE_MAX = 10000;
static char *cmdLine = "Samcmd --> ";


int main( int argc, char *argv[] )
{
    char cwd[LINE_MAX];
    char hostName[LINE_MAX];
    int looper = 1;
    char *token = NULL;
    char line[LINE_MAX];
    char *placehold = NULL;

    getcwd(cwd, LINE_MAX - 1);
    gethostname(hostName, LINE_MAX - 1);
    while ( looper == 1 )
    {
        printf( "%s | %s", hostName, cmdLine );
        if( fgets( line, LINE_MAX, stdin ) != NULL )
        {
            token = strtok( line, ";" );
            do{
                if( strcmp(token, "exit\n") == 0 ) /*if an exit command is issued*/
                {
                    looper = 0;
                }
                system(token);
                token = strtok( NULL, ";" );
            } while ( token != NULL );
        }
    }
    return 0;
}

any critiques and comments are welcome 🙂

  • 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-09T04:58:36+00:00Added an answer on June 9, 2026 at 4:58 am

    That works fine on my system, the hostname is printed, followed by the prompt. It doesn’t print the current working directory but that’s just because it’s not in the printf, which would be more suitable as:

    printf( "%s | %s | %s", hostName, cwd, cmdLine );
    

    On my system, that gives me:

    paxbox01 | /home/pax/Pax | Samcmd -->
    

    If you’re not seeing the hostname, you need to be aware that the call can fail. You should check the return value from gethostname (along with errno) to see if this is the case.

    You may also want to clarify what you mean by “does not seem to be working”. Does it crash? Does it give you an empty string? Does it give you a non-empty string that you think is incorrect? Are you simply confused because only one string was output before the prompt (see above for a fix to this)?

    This will greatly assist anyone here in helping you out.


    Based on your added text:

    It won’t compile using gcc on MINGW32, giving error: undefined reference to ‘gethostname’ collect2: ld reurned 1 exit status.

    I think you need to link against an extra library for that in MinGW. MinGW is not meant to provide all the functionality of UNIX (as, for example, CygWin tries to do). It’s more to provide the gcc tool chain to build Windows applications.

    You should add the -lws2_32 option to your compiler line (at the end) so that it brings in the ws2_32 library, which is where the code for gethostname is located.

    Alternatively, the Win32 API includes GetComputerNameEx which I believe has no extra linking requirements, though your code will of course be non-portable at that point.

    In addition, under MinGW, you may find that you need to use WinSock (include winsock2.h and call WSAStartup() before calling any network functions). It’s been a while since I attempted this under MinGw but I seem to recall that was a requirement (it may have changed).

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

Sidebar

Related Questions

I have developed a Java server using Eclipse that accepts TCP socket connection from
We have developed a custom control in monotouch (inherited from UITextField, but that shouldn't
I have developed an app that in testing has worked fine but when it
I have developed a sample WCF REST service that accepts that creates an Order
We have a couple mini-applications (single Web form look-up stuff) that need to run
I have developed a web site that requires user registration and authentication for some
I have developed a behavior that changes the Clip property of the AssociatedObject. When
I would like to add value to my website that I have developed at
Have developed a small Windows application using SQL Server as its database. I need
I have developed a web application that uses the jQuery DateTimePicker by Trent Richardson

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.