What happens when you type `ls -l *.c` in the shell

ls -la *.c simulation in shell
Flowchart of “simple_shell” program
  • check if it is interactive or not to print the prompt
  • read the line (getline)
  • Handle format (‘#’, ‘;’, ‘||’, ‘&&’, ‘$’)
  • Convert in an array **commands
  • Check if the command exists
  • Create a child process and run it depending on the logical also(‘||’, ‘&&’)
  • Handle type of errors

Now, let’s go to the example: “ls -l *.c”

  1. We write “ls -l *.c” in the prompt
  2. Main.c — Entry Point
main: Entry point of the program
  • Import the headers from shell.h: if shell.h not exist(#ifndef), (#define) & (#enddefine)
header of the program simple_shell
“isatty” is a function that returns 1 if fd is an open file descriptor referring to
a terminal; otherwise 0 is returned
  • Print prompt For this example, it is worked with a prompt “$ “ but if we automatized this work, we can use the PS1 to print the prompt and the start of the line of error.
  • Print buffer
  • Execute and print the result
  • And repeat!
  • PS1 which contains the value of the default prompt. That ‘s why, we can use it to modify the prompt automatically. Also it is helpful when we want to write the start of an error message
  • Example for the PROMPT:
  • Example for the error, with PS1, we can write the start of error message (path):

Secondly, we will read “ls -l *.c” from the buffer

function of getline, also the program have an own _getline that simulates getline

Third, Handle format:

  • Handle_comment: Check if there is an “#” command and delete it from the buffer
handle comment
  • After remove the comments, we handle the “\n” through the _strok function
handle “\n”
  • After it, we handle the format (“;”, “&&” ,“||”, “ “) through different levels using the function “parse_user_input”
  • Parse_user_input: The shell takes the user’s input and tokenizes that input, then that tokenized input is what we can call commands.
  • Now, WE can handle the “;”, “&&” and “||” through different levels using the function “parse_user_input” because the logical operator OR (||) prints only until finding a positive execution (correct command)
  • Finally, we can handle “ “ and “exit” in the function “execute_commands”
  1. Create a child process by calling fork syscall
  2. Make parent wait until it’s child has terminated by calling “wait” syscall
  3. In the child process, call execve and pass the commands and env vars
  4. Continue parent process execution

Handling *.c:

source: https://pubs.opengroup.org/onlinepubs/009604599/utilities/xcu_chap02.html#tag_02_02_03
  1. reverse command
  2. strncmp with the 2 first char (before, 2 last: “.c”)
  3. if strncmp(“command_reverse”, “c.”, 2) == 0, the line of *.c will list , otherwise, it will erase it .




Software engineer in progress

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Am I Doing This Again?

7-step beginner’s guide to open-source contribution on GitHub

Introducing Training V2: A new and improved way to train your Bixby Capsules

Should I write my thesis with Word or LaTeX?

Charles Hoskinson On ETC Cooperative Advisory Board

Google Analytics Roadmap for Tracking Web and Mobile App

Asynchronous coroutines with C# 8.0 and IAsyncEnumerable

Coroutines flow

When Your Problem Isn’t Your Problem

Two unsolved Rubik’s cubes stack on top of each other.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Katherine Soto

Katherine Soto

Software engineer in progress

More from Medium

Did I really just commit and push a file with a password in it?-Remove

How to set up an SFTP server on Rocky Linux 8

C++ — Command Line Arguments

How to solve the problem that getting a dark image with Raspberry Pi + Motion