What happens when you type “ls -l *.c” ?

H_e_d
Shell command dive
2 min readApr 11, 2021

--

First thing you need to know is that the shell is command interpreter that takes the entry from the user and tells the operating system to perform the task.

You will first need to type a command then press Enter for the shell to read the input. Once the input is read the shell determines if the input is a command that can be executed.

Let’s see what happens for the ls -l *.c as input.

“Ls -l *.c” :

  • Read the line:

The first step will be to read the line using the prompt. To accomplish this task the getline function is used. So as long as the user write something into the prompt. The function won’t stop reading it. After the line is read and entered the prompt is displayed again.(cf: man getline)

  • Analize of the input:

If the input is a correct command, the program will split it into tokens, then check each tokens and tries to execute. Before that the shell will check for alias and replace it with their actual value if there is.

  • Search into the environment:

Once the command is read and analized, the shell will redirect to the $PATH variable looking for the binary executable file to finally execute the command. In the PATH variable there is absolute path way to get the correct executable. The path must be split to link it to the correct binary followed by the command.

  • Application:

Upon passing all these steps the command will be execute and grant the user an access to valuable intels.

To achieve that, the function fork must be called to create a new process and the function waitpid to suspend the parent process untill the child process terminates. If the fork does not fail the execve function will execute the command.

Ls -l is to list current files and directory using long format with ownership, permissions, file size and timestamp.

Adding the *.c only the file with the .c suffix will be listed.

--

--