Specializations > System programming & Algorithm > Linux Programming
Alexandre Gautier
12-15-2020 to 01-12-2021
Revisiting (235) 0x16. C - Simple Shell to build a second clone of dash
/Ubuntu sh
, now with logical operators, piping, and input/output redirections.
- Allowed Functions and System Calls:
access |
chdir |
close |
execve |
fork |
stat |
lstat |
fstat |
kill |
open |
read |
signal |
wait |
waitpid |
wait3 |
wait4 |
write |
_exit |
dup |
dup2 |
pipe |
unlink |
time |
gethostname |
geteuid |
sigaction |
sigemptyset |
select |
getpid |
__errno_location (errno macros) |
closedir |
exit |
free |
getcwd |
getline |
malloc |
opendir |
perror |
readdir |
strtok |
localtime |
getpwuid |
isatty |
printf |
fflush |
fprintf |
vfprintf |
sprintf |
- Error printing should mimic
sh
:
$ echo "qwerty" | /bin/sh
/bin/sh: 1: qwerty: not found
$ echo "qwerty" | /bin/../bin/sh
/bin/../bin/sh: 1: qwerty: not found
$
This project can be found in a separate repository: shell_v2
. This README is to provide the project profile separate from the README originally included with the project itself.
- Write a
README
- Write a
man
for your shell. - You should have an
AUTHORS.md
file at the root of your repository, listing all individuals having contributed content to the repository. Format, see Docker
File(s): README.md
man_1_hsh
AUTHORS.md
Write a beautiful code that passes the betty
linter checks
Be aware that every single C source file (*.c
and *.h
) inside your repository will be checked
Compiled: make all
Write a UNIX command line interpreter.
- Usage:
hsh
Your shell must:
- Display a prompt and wait for the user to type a command. A command line always ends with a new line.
- The prompt is displayed again each time a command has been executed.
- If an executable cannot be found, print an error message and display the prompt again.
- Handle errors.
- You have to handle the “end of file” condition (Ctrl+D)
- Handle command lines with arguments
- Handle and parse the
PATH
environment variable
Builtins
-
Implement the built-in command
exit
:- Your
exit
built-in must behave exactly likesh
‘s - You have to handle arguments
- Your
-
Implement the built-in command
cd
:- Changes the current directory of the process.
- Command syntax:
cd [path]
- If no argument is given to
cd
the command must be interpreted likecd $HOME
- You have to handle the command
cd -
- You have to update the environment variable
PWD
when you change directory
-
Implement the built-in command env:
- Prints the current environment
You don’t have to:
- handle special characters :
"
,'
,\``,
`,*
,&
,#
- be able to move the cursor
- handle pipes and redirections
Compiled: make all
hsh 1.0 +
- Handle the right stream redirection
>
- Usage:
command > output_file
Compiled: make all
hsh 1.1 +
- Handle the double right stream redirection
>>
- Usage:
command >> output_file
- You don’t have to handle the syntax:
>> output_file command
Compiled: make all
hsh 1.2 +
- Handle the left stream redirection
<
- Usage:
command < input_file
Compiled: make all
hsh 1.3 +
- Handle the double left stream redirection
<<
(also known asheredoc
) - Usage:
command << delimiter
Compiled: make all
hsh 1.4 +
- Handle the pipe stream redirection
|
- Usage:
command1 | command2
Compiled: make all
hsh 1.5 +
- Handle the commands separator
;
Compiled: make all
hsh 1.6 +
- Handle the
&&
logical operator
Compiled: make all
hsh 1.7 +
- Handle the
||
logical operator
Compiled: make all
hsh 1.8 +
Implement the setenv
and unsetenv
builtin commands
setenv
- Initialize a new environment variable, or modify an existing one
- Command syntax:
setenv VARIABLE VALUE
- Should print something on stderr on failure
unsetenv
- Remove a environment variable
- Command syntax:
unsetenv VARIABLE
- Should print something on stderr on failure
Compiled: make all
hsh 1.1 +
- Handle the right stream redirection
>
- Usage:
command n> output_file
- Where
n
is the file descriptor of the file to redirect the output from n
is a number between 0 and 9
- Where
Compiled: make all
hsh 1.0 +
- Handle Ctrl+C: your shell should not quit when the user inputs
^C
- Hint:
man 2 signal
Compiled: make all
hsh 1.0 +
- Implement the
alias
builtin command - Usage:
alias [name[='value'] ...]
alias
: Prints a list of all aliases, one per line, in the formname='value'
alias name [name2 ...]
: Prints the aliasesname
,name2
, etc 1 per line, in the formname='value'
alias name='value' [...]
: Defines an alias for eachname
whosevalue
is given. Ifname
is already an alias, replaces its value withvalue
Compiled: make all
hsh 1.0 +
- Handle variables replacement
- Handle the
$?
variable - Handle the
$$
variable
Compiled: make all
hsh 1.0 +
- Handle comments (
#
)
Compiled: make all
hsh 1.0 +
- Implement the
help
builtin command - Usage:
help [BUILTIN]
Compiled: make all
hsh 1.0 +
- Implement the
history
builtin command, without any argument - The
history
builtin displays the history list, one command by line, preceded with line numbers (starting at 0) - On
exit
, write the entire history, without line numbers, to a file named.hsh_history
in the directory$HOME
- When the shell starts, read the file
.hsh_history
in the directory$HOME
if it exists, and set the first line number of the current session to the total number of lines in the file modulo 4096
Compiled: make all
hsh 1.0 +
- Usage:
hsh [filename]
- Your shell can take a file as a command line argument
- The file contains all the commands that your shell should run before exiting
- The file should contain one command per line
- In this mode, the shell should not print a prompt and should not read from
stdin
Compiled: make all
hsh 1.0 +
- At startup, your shell must open the file
.hshrc
located in the$HOME
directory, and execute it - Since this task requires only hsh 1.0, there won’t be any advanced content in
.hshrc
file during the correction (no aliases, no redirections, …)
Compiled: make all
hsh 1.0 +
- When executing interactively, your shell must displays the primary prompt
PS1
when it is ready to read a command - The
PS1
variable can be customized by inserting a number of backslash-escaped special characters - You must handle the following backslash-escaped characters:
\d
: The date in “Weekday Month Date” format (e.g., “Tue Feb 05”, “Sat Jul 26”)\H
: The hostname\s
: The name of the shell\u
: The username of the current user\w
: The current working directory, with $HOME abbreviated with a tilde (~
)
Compiled: make all
- Samuel Pomeroy - allelomorph