What happens where you type ls -l in a Linux Shell? (Building a simple shell)

Img 1: Typing ls -l in
  1. The first thing that you see when you are running a shell is the prompt. A prompt should be displayed to let the user type the commands, and it would look like this:
$
$ ls -l
PATH=/home/vagrant/.vscode-server/bin/db40434f562994116e5b21c24015a2e40b2504e6/bin:/home/vagrant/.vscode-server/bin/db40434f562994116e5b21c24015a2e40b2504e6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Img 2: Graphical System Calls ( taken from geeksforgeeks)
Img 3: Typing ls -l and exit the interactive mode.

Functions and system calls used

GETLINE:

man 3 getline
  • The first argument is the address of the first character position where the input string will be stored.
  • The second argument is the address of the variable that holds the size of the input buffer, another pointer.
  • The thrid arguments is the input file handle, stdin in this case. So you could use getline() to read a line of text from a file, but when stdin is specified, standard input is read.
  • Return: The number of characters read on success, and -1 on failure reading the line.

STRTOK:

man 3 strtok
  • The first argument is the string to be split.
  • The second argument is the delimiter
  • Return: A pointer to the next token, and NULL if there is no more tokens.

GETENV:

man 3 getenv
  • The first argument is environment variable name.
  • Return: a pointer to the value in the environment, or NULL if there is no match

FORK:

man 2 fork
  • Return: On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created.

EXECVE:

man 2 execve
  • Return: Nothing on succes, and -1 on failure

WAIT:

man 2 wait
  • Return: on success, returns the process ID of the terminated child; on error, -1 is returned.

--

--

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
Valentina Gómez A.

Valentina Gómez A.

Hi I am a financial engineer, and a software development student in Holberton School!