Coder Social home page Coder Social logo

lsh's Issues

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 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;
}

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.

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);

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);

Handling of EOF in lsh_read_line()

Hi :) I just wanted to contribute to your excellent tutorial by (I think) improving the error handling in lsh_read_line(). Unless I'm mistaken, I don't think it currently handles the case when EOF actually indicates a read error and not a "successful" EOF/an EOF proper. My own code for the while loop looks more or less like this at the moment (you can see I'm (a) checking for the meaning of the returned EOF and (b) ensuring the buffer is freed before exiting):

// rest of code

while (1) {
        c = getc(stdin);
        
        if (c == '\n') {
            rl_buf[pos] = '\0';
            return rl_buf;
        } else if (c == EOF && ferror(stdin)) {
            fprintf(stderr, "ERROR: read error (%d)\n", errno);
            free(rl_buf);
            exit(EXIT_FAILURE);
        } else if (c == EOF && feof(stdin)){
            free(rl_buf);
            exit(EXIT_SUCCESS);
        }

        rl_buf[pos] = c;
        pos++;
        
        // rest of code
}

What do you think? Have I made a mistake in my reasoning?

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

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));

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.