Coder Social home page Coder Social logo

lsh's Introduction

LSH

LSH is a simple implementation of a shell in C, and it is the subject of a tutorial on my website. It demonstrates the basics of how a shell works. That is: read, parse, fork, exec, and wait. Since its purpose is demonstration (not feature completeness or even fitness for casual use), it has many limitations, including:

  • Commands must be on a single line.
  • Arguments must be separated by whitespace.
  • No quoting arguments or escaping whitespace.
  • No piping or redirection.
  • Only builtins are: cd, help, exit.

Running

Use gcc -o lsh src/main.c to compile, and then ./lsh to run. If you would like to use the standard-library based implementation of lsh_read_line(), then you can do: gcc -DLSH_USE_STD_GETLINE -o lsh src/main.c.

Contributing

Since this is the subject of a tutorial, I'm not looking to extend it with additional features at this time. So I won't be accepting any pull requests that aren't related to bug fixes (and I'm sure there are still bugs in the code!).

However, that doesn't mean that you shouldn't play with the code, make changes, and explore new features! That's the whole point of this project! It's just that other people are doing the same thing, and this project is merely a starting point for your own exploration.

On that note, I would be just tickled if you dropped me a line (see my website for contact info) to show me the cool new features you've added!

License

This code is in the public domain (see UNLICENSE for more details). This means you can use, modify, and distribute it without any restriction. I appreciate, but don't require, acknowledgement in derivative works.

lsh's People

Contributors

brenns10 avatar guo-sj avatar harishankarv avatar kkmzero avatar timgates42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lsh's Issues

EOF not handled in getline()

I did not know where else to raise an issue regarding the blog post, as getline is not there in the code on github.

The code on the blog post does not handle the case where the user inputs an EOF, wanting to exit the shell. I believe it should be:

char *lsh_read_line(void)
{
  char *line = NULL;
  ssize_t bufsize = 0;  // have getline allocate a buffer for us
  if (getline(&line, &bufsize, stdin) == -1)
    exit(EXIT_SUCCESS);
  return line;
}

Or, to be even more pedantic:

char *lsh_read_line(void)
{
  char *line = NULL;
  ssize_t bufsize = 0;  // have getline allocate a buffer for us

  if (getline(&line, &bufsize, stdin) == -1){
    if (feof(stdin)) {  
      exit(EXIT_SUCCESS);  // We recieved an EOF
    } else  {
      perror("readline");
      exit(EXIT_FAILURE);
    }
  }
  return line;
}

exit in Child process

Why in child process, it exits with EXIT_FAILURE ?
if (execvp(args[0], args) == -1) { perror("lsh"); } exit(EXIT_FAILURE);

I think it should be:
_exit(EXIT_SUCCESS);

Some libc libraries only flush stdout when they see a newline

I compiled your shell against musl libc and then the ">" prompt is missing.

The fix is easy enough:

diff --git a/src/main.c b/src/main.c
index 39b8528..0a705eb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -255,6 +255,7 @@ void lsh_loop(void)

do {
printf("> ");

  • fflush(stdout);
    line = lsh_read_line();
    args = lsh_split_line(line);
    status = lsh_execute(args);

Its not working with sftp

I modified user to use it bash shell
usermod -s "binary" username
it does not working, when i change back usermod -s /bin/bash username

it works

Shell gets killed by the kernel

Hi,

If you run the command bash -ic <some_command> the shell gets stopped. Not sure why though, I'm running in the same issue with mine ;(

Contribution to the repository

Hi! Is this project open to contribution?
He never contributes no projects in github, but he caught my attention, it would be a good challenge for me. If you want help, I'm at your disposal.

EOF is not handled properly

Why is the last byte replaced with \0 on EOF?
This makes it impossible to exit the shell using ^D.
A simple fix would be:

if (c == EOF)
    exit(0);

Memory Allocation Error

I believe there is a slight mistake in implementation of realloc function in the main.c at line 182.
Instead of:
buffer = realloc(buffer, bufsize);
The code should have been:
buffer = realloc(buffer, bufsize * sizeof(char));

To express gratitude and ask a small question

I really enjoyed reading this tutorial, I read it all in one go, tried to write the code, and it finally ran successfully.
But I have a small problem is that I did not find any code related to the two commands ls and pwd, but I can use them when launch the program, can you explain why?
Finally, thank you very much for writing this tutorial, it brought me a happy afternoon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.