{"id":15920330,"url":"https://github.com/michaelsproul/simple-shell","last_synced_at":"2026-05-15T22:37:49.448Z","repository":{"id":66211011,"uuid":"49345366","full_name":"michaelsproul/simple-shell","owner":"michaelsproul","description":"A simple shell written in C","archived":false,"fork":false,"pushed_at":"2016-01-09T23:11:52.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-04-24T08:42:29.179Z","etag":null,"topics":["shell"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/michaelsproul.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-09T23:11:36.000Z","updated_at":"2019-01-17T12:56:20.000Z","dependencies_parsed_at":"2023-02-23T23:15:21.162Z","dependency_job_id":null,"html_url":"https://github.com/michaelsproul/simple-shell","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/michaelsproul/simple-shell","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelsproul%2Fsimple-shell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelsproul%2Fsimple-shell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelsproul%2Fsimple-shell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelsproul%2Fsimple-shell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michaelsproul","download_url":"https://codeload.github.com/michaelsproul/simple-shell/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelsproul%2Fsimple-shell/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33082135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T20:25:35.270Z","status":"ssl_error","status_checked_at":"2026-05-15T20:25:34.732Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["shell"],"created_at":"2024-10-06T19:22:13.151Z","updated_at":"2026-05-15T22:37:49.432Z","avatar_url":"https://github.com/michaelsproul.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Shell\n\nA simple shell written in C.\n\n## Features\n\n* Unlimited pipes.\n* Unlimited arguments.\n* Sensible file redirection.\n* Changing directories.\n\n## Usage\n\n```\n$ make\n$ ./shell\n```\n\n![Shell Screenshot](http://i.imgur.com/fN6Medw.png)\n\n## High-Level Description\n\nThere is a central loop in `shell.c` that repeatedly fetches commands from standard input and executes them. A flex tokeniser provides a token stream to the `parse` function, which ensures the input is well formed and stores all relevant information in the `program_args` variable. If parsing is successful, the main function passes the details of the command to be executed to the execute function, which does all piping and forking.\n\n## Desirable Properties\n\n* Parsing is separated from execution. The execution stage can assume well-formed input, and treat program data structures as read-only.\n* No memory leaks. Failure is bubbled up and carefully handled.\n* Important forking and piping is contained in a very small section of code in `exec.c`.\n* Failure to execute a command just results in the main execution loop continuing to the next bit of input. The shell is not programmed to exit unless instructed to by the user.\n\n## Parsing Details\n\nI decided to allow input of the following form:\n\n1. One or more programs, each with zero or more arguments.\n2. File redirection only where sensible. It doesn't make sense to read in a file on anything but the first command, nor does it make sense to redirect output to a file on anything but the last command.\n3. No program arguments following a file redirection operation.\n\nTo achieve this I had to implement dynamic arrays with `void *` hackery.\n\nThe main parsing function takes a `char **** program_args`, which can be thought of as a pointer to a dynamic array of dynamic string arrays. There's an extra level of redirection so that it can be re-assigned if adding elements to the array causes a resize.\n\n## Execution Details\n\nTo support unlimited piping my shell iterates through each program to be run and creates a pipe for its output. At each iteration a file descriptor pointing to the read-end of the previous program's pipe is held by the main process. This file descriptor is used to replace the standard input of newly spawned programs (using the standard `fork` and `exec` method). The only special cases are the first and last program, which need not be distinct, which interact with `stdin` (or an input file) and `stdout` (or an output file). This nice simplicity arises from the limitation on file redirection enforced by the parser. The tradeoff seems fair given that file redirection in the middle of a pipeline is fairly uncommon and can be done with `tee` anyway.\n\nOnce all of the children have been spawned, the main process waits for each of them in order. Initially I was just using `wait`, but switched to `waitpid` so that I could report the names of failed processes.\n\n## Builtins\n\nThere are two shell builtins, `cd` and `exit`. Both must be run on their own without piping or file redirection. This decision was made to keep thing as simple as possible. The calling format for `cd` is:\n\n```\ncd \u003cdirectory\u003e\n```\n\nThe directory argument *must* be provided, calling `cd` without an argument will not go to `~` as the shell has no concept of home directories.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelsproul%2Fsimple-shell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichaelsproul%2Fsimple-shell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelsproul%2Fsimple-shell/lists"}