Welcome to my holbertonschool-simple_shell repository! This directory marks the beginning of my intensive journey into learning the C programming language, with a strong focus on low-level programming. It compiles the exercises completed as part of my training, centered around building a simple shell in collaboration with two fellow students: Mathis and Heytem.
Through these projects, my goal is to develop a deep understanding of how programs work "under the hood"—exploring memory management, compilation processes, and direct interactions with the operating system.
- Project Context
- Learned Ressources
- General Requirements
- Usage
- Compilation
- Flowchart et Snippets
- Project Structure and Tasks
- Authors
This project involves the development of a simple UNIX command-line interpreter, often referred to as a shell. The primary goal is to replicate the basic functionality of the original Thompson shell. Through this process, we'll gain a deep understanding of core system programming concepts, including process management, system calls like fork()
and execve()
, and the manipulation of environment variables. The shell will function in both interactive and non-interactive modes, handling commands with arguments, searching the PATH
for executables, and implementing built-in commands such as exit
and env.
At the end of this project, you are expected to be able to explain to anyone, without the help of Google:
- Who designed and implemented the original Unix operating system
- Who wrote the first version of the UNIX shell
- Who invented the B programming language (the direct predecessor to the C programming language)
- Who is Ken Thompson
- How does a shell work
- What is a pid and a ppid
- How to manipulate the environment of the current process
- What is the difference between a function and a system call
- How to create processes
- What are the three prototypes of main
- How does the shell use the PATH to find the programs
- How to execute another program with the execve system call
- How to suspend the execution of a process until one of its children terminates
- What is EOF / “end-of-file”?
General Allowed editors: vi, vim, emacs All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89 All your files should end with a new line A README.md file, at the root of the folder of the project is mandatory Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl Your shell should not have any memory leaks No more than 5 functions per file All your header files should be include guarded Use system calls only when you need to (why?) GitHub There should be one project repository per group. If you clone/fork/whatever a project repository with the same name before the second deadline, you risk a 0% score.
More Info Output Unless specified otherwise, your program must have the exact same output as sh (/bin/sh) as well as the exact same error output. The only difference is when you print an error, the name of the program must be equivalent to your argv[0] (See below) Example of error with sh:
julien@ubuntu:/# echo "qwerty" | /bin/sh
/bin/sh: 1: qwerty: not found
julien@ubuntu:/# echo "qwerty" | /bin/../bin/sh
/bin/../bin/sh: 1: qwerty: not found
Same error with your program hsh:
julien@ubuntu:/# echo "qwerty" | ./hsh
./hsh: 1: qwerty: not found
julien@ubuntu:/# echo "qwerty" | ./././hsh
./././hsh: 1: qwerty: not found
List of allowed functions and system calls+ all functions from string.h
- access (man 2 access)
- chdir (man 2 chdir)
- close (man 2 close)
- closedir (man 3 closedir)
- execve (man 2 execve)
- exit (man 3 exit)
- _exit (man 2 _exit)
- fflush (man 3 fflush)
- fork (man 2 fork)
- free (man 3 free)
- getcwd (man 3 getcwd)
- getline (man 3 getline)
- getpid (man 2 getpid)
- isatty (man 3 isatty)
- kill (man 2 kill)
- malloc (man 3 malloc)
- open (man 2 open)
- opendir (man 3 opendir)
- perror (man 3 perror)
- printf (man 3 printf)
- fprintf (man 3 fprintf)
- vfprintf (man 3 vfprintf)
- sprintf (man 3 sprintf)
- putchar (man 3 putchar)
- read (man 2 read)
- readdir (man 3 readdir)
- signal (man 2 signal)
- stat (__xstat) (man 2 stat)
- lstat (__lxstat) (man 2 lstat)
- fstat (__fxstat) (man 2 fstat)
- strtok (man 3 strtok)
- wait (man 2 wait)
- waitpid (man 2 waitpid)
- wait3 (man 2 wait3)
- wait4 (man 2 wait4)
- write (man 2 write)
Compilation Your shell will be compiled this way:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh
Testing Your shell should work like this in interactive mode:
julien@ubuntu:/# ./hsh
($) /bin/ls
hsh main.c shell.c
($)
($) exit
julien@ubuntu:/#
But also in non-interactive mode:
julien@ubuntu:/# echo "/bin/ls" | ./hsh
hsh main.c shell.c test_ls_2
julien@ubuntu:/# cat test_ls_2
/bin/ls
/bin/ls
julien@ubuntu:/# cat test_ls_2 | ./hsh
hsh main.c shell.c test_ls_2
hsh main.c shell.c test_ls_2
julien@ubuntu:/#
git clone https://github.com/username/repositoryname.git
you can add it to your personnal repository and username.
To access the manpage:
~/Holberton/holbertonschool-simple_shell$man ./man_1_simple_shell
The files in this project are compiled using gcc with the following options:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh
To check for memory leaks, use:
valgrind --leak-check=full ./hsh
Output :
user@user:~/Holberton/holbertonschool-simple_shell$ valgrind --leak-check=full ./hsh
==1331== Memcheck, a memory error detector
==1331== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==1331== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==1331== Command: ./hsh
==1331==
$ exit
==1331==
==1331== HEAP SUMMARY:
==1331== in use at exit: 0 bytes in 0 blocks
==1331== total heap usage: 3 allocs, 3 frees, 2,168 bytes allocated
==1331==
==1331== All heap blocks were freed -- no leaks are possible
==1331==
==1331== For lists of detected and suppressed errors, rerun with: -s
==1331== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Snippet.hsh.v1.mp4
The project is organized around the progressive implementation of the simple shell and its functionalities.
This project was developed incrementally, with new features added at each stage:
-
Initialization The README.md, man_1_simple_shell, and AUTHORS files were created to document the project and list all contributors.
-
Betty Would Be Proud The entire source code adheres to the Betty coding style, ensuring clean and readable code.
-
Simple Shell 0.1 A basic UNIX command-line interpreter was implemented.
The shell displays a prompt and executes simple, single-word commands without arguments.
It handles the end-of-file condition (Ctrl+D) and execution errors.
-
Simple Shell 0.2 The shell was improved to handle command lines with arguments.
-
Simple Shell 0.3 The shell can now find executables located in the directories specified by the PATH environment variable.
It checks if a command exists before calling fork.
-
Simple Shell 0.4 The exit built-in was implemented, allowing for a clean exit from the shell.
-
Simple Shell 1.0 The env built-in was added, which prints the current environment.
✍️ Authors
Mathis ROY, Heytem KEDDOUS, Mathieu GODALIER — Students in programming at Holberton School