brenns10 / lsh Goto Github PK
View Code? Open in Web Editor NEWSimple shell implementation. Tutorial here ->
Home Page: http://brennan.io/2015/01/16/write-a-shell-in-c/
License: The Unlicense
Simple shell implementation. Tutorial here ->
Home Page: http://brennan.io/2015/01/16/write-a-shell-in-c/
License: The Unlicense
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 ;(
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.
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;
}
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.
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("> ");
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);
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?
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
I really enjoyed testing your lsh and reading the blog post.
FYI & OFFTOPIC: I also created a simple Rust version for fun: https://gist.github.com/rubenrua/55b2ab6757b6c716f05595548784593a
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);
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));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.