LSH is a simple implementation of a shell in C

Related tags

CLI c shell tutorial
Overview

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.

Comments
  • EOF not handled in getline()

    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;
    }
    
    opened by harishankarv 5
  • To express gratitude and ask a small question

    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.

    opened by wintermorn1ng 2
  • Memory Allocation Error

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

    opened by shah-anurag 2
  • Contribution to the repository

    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.

    opened by fernandoeqc 1
  • docs: fix simple typo, recieved -> received

    docs: fix simple typo, recieved -> received

    There is a small typo in src/main.c.

    Should read received rather than recieved.

    Semi-automated pull request generated by https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md

    opened by timgates42 1
  • Network support

    Network support

    Added simple arguments parsing code Support for ipv4 tcp client support

    define FEATURE_CONNECT_TO_SERVER to add the support (-DFEATURE_CONNECT_TO_SERVER in gcc)

    server example: nc -l -p 8090

    opened by baselsayeh 1
  • Update main.c

    Update main.c

    In case of Error:

    main.c: In function 'lsh_launch':
    main.c:103:3: error: unknown type name 'pid_t'
       pid_t pid;
       ^
    

    "In older Posix standards, pid_t was only defined in <sys/types.h>, but since Posix.1-2001 (Issue 7) it is also in <unistd.h>. However, in order to get definitions in Posix.1-2001, you must define an appropriate feature test macro before including any standard header file."

    opened by kkmzero 1
  • exit in Child process

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

    opened by danghai 1
  • Its not working with sftp

    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

    wontfix 
    opened by surjit 1
  • Add a couple of missing malloc checks

    Add a couple of missing malloc checks

    Nice little project you have here. I noticed these while reading your blog post that hit HN.

    There is a bug in your getline alternative on the blog too -- you need to initialise line to NULL since POSIX says "The application shall ensure that *lineptr is a valid argument that could be passed to the free() function."

    opened by ghost 1
  • Congratulations and Rust version

    Congratulations and Rust version

    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

    image

    opened by rubenrua 0
  • Some libc libraries only flush stdout when they see a newline

    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);
    opened by sjpschutte 0
  • fixed possible memory leak while reallocating for bigger buffer size

    fixed possible memory leak while reallocating for bigger buffer size

    In case realloc() fails, you would lose the pointer to the memory, leaving it unable to be freed. That's why I added a temporary buffer just so it is possible to free the original memory.

    opened by stolo93 0
Owner
Stephen Brennan
professional beginner
Stephen Brennan
SimPle SHell - minimalist Unix interactive shell written in a single C file

SimPle SHell - minimalist Unix interactive shell written in a single C file. The shell does not support scripting yet and is in an early stage of development. If you notice any bug, please open an issue on github.

sewe2000 2 Oct 24, 2021
crypted admin shell: SSH-like strong crypto remote admin shell for Linux, BSD, Android, Solaris and OSX

crypted admin shell: SSH-like strong crypto remote admin shell for Linux, BSD, Android, Solaris and OSX

Sebastian 131 Aug 25, 2022
Pine's ok shell, a shell in C++

POSH Pine's ok shell, a shell in C++ Answers to questions nobody asked. "Is your name Pine?" No, although that would be neat. Pine is supposed to be a

Boops-Boops 1 Nov 6, 2021
An implementation of shell commands in C++

ShellSynergy An implementation of shell commands in C++ using std::filesystem Build To run the project execute command: [email protected]:~$ ./RUN.sh Short shel

Vova Makotkin 6 Dec 7, 2021
Linux Shell Implementation In C - Language

Linux-Shell ASSIGNMENT 2 Name : Naman Anand Roll no : 200101070 SUBJECT : CS242 ASSIGNMENT : 2 COMPILING AND FOR RUNNING COMMANDS :-> 1)gcc -o 2001010

Naman  Anand 18 Jun 24, 2022
Linux Shell Implementation In C - Language

Linux-Mini-Shell This is a C code for Linux Shell (a mini version). The code is designed to work properly in LINUX terminal. To compile the code and r

Akshat Mittal 1 Jun 25, 2022
a shell written in C

shell a shell written in C. CREDITS = https://github.com/brenns10/lsh/blob/master/src/main.c -for most of the template code. This project has a 10% ch

Arin 11 Jul 1, 2021
A Windows Shell Extension for the Pixar USD file format.

Activision USD Shell Extension A Windows Shell Extension for the Pixar USD file format. Windows Explorer Features Hydra Realtime Preview Thumbnails Co

null 303 Sep 20, 2022
IDAShell is a shell extension for launching IDA from the context menu of executables.

IDAShell About IDAShell is a shell extension for launching IDA from the context menu of executables. Usage Just install and it works. If you moved IDA

null 164 Sep 15, 2022
LwSHELL is lightweight, platform independent, command line shell for embedded systems.

LwSHELL is lightweight, platform independent, command line shell for embedded systems. It targets communication with embedded systems from remote terminal to quickly send commands and the retrieve data from the device.

Tilen Majerle 69 Sep 10, 2022
Yori is a CMD replacement shell that supports backquotes, job control, and improves tab completion, file matching, aliases, command history, and more.

Yori is a CMD replacement shell that supports backquotes, job control, and improves tab completion, file matching, aliases, command history, and more.

Malcolm Smith 1.1k Sep 17, 2022
A C-based Mini Shell: mumsh

A C-based Mini Shell: mumsh This project is a course project in VE482 Operating System @UM-SJTU Joint Institute. In this project, a mini shell mumsh i

Kexuan Huang 6 Sep 22, 2022
Flexible and fast Z-shell plugin manager that will allow installing everything from GitHub and other sites.

ZINIT News Zinit Wiki Quick Start Install Automatic Installation (Recommended) Manual Installation Usage Introduction Plugins and snippets Upgrade Zin

z-shell 25 Jul 9, 2022
Mini Shell in C implementing the basic command line functionalities

Mini-Shell Mini Shell in C implementing the basic command line functionalities Instructions to execute the shell: Download the readline library using

Mansi 1 Nov 19, 2021
Mosh: the mobile shell

Mosh: the mobile shell Mosh is a remote terminal application that supports intermittent connectivity, allows roaming, and provides speculative local e

Mosh (mobile shell) 11.1k Sep 21, 2022
A tiny UNIX shell.

Tiny SHell - TSH Description This project is a tiny UNIX shell supports only job control. It's the 5th lab of 15-213: Introduction to Computer Systems

Ali Ghorab 3 Jul 7, 2022
Shpp - Call c++ functions from a shell with any arguments of any types parsed automatically

shpp Call c++ functions from a shell with any arguments of any types parsed automatically Declare a variable or define a function and register it in s

Pedro Moreira 96 Jun 8, 2022
Implementation of a lock-free ring buffer class.

Lock-free ring buffer This is an implementation of a lock-free ring buffer, using only C++11 STL level features. Originally designed for the NymphCast

Maya Posch 12 Apr 9, 2022
A simple header-only C++ argument parser library. Supposed to be flexible and powerful, and attempts to be compatible with the functionality of the Python standard argparse library (though not necessarily the API).

args Note that this library is essentially in maintenance mode. I haven't had the time to work on it or give it the love that it deserves. I'm not add

Taylor C. Richberger 1k Sep 18, 2022