Skip to content

CyberSpace0/Internal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐚 Simple Shell

A UNIX command line interpreter written in C.

πŸ“Œ Project Overview

This project is a minimalist implementation of a UNIX shell. It replicates the core behavior of /bin/sh using low-level system calls and without relying on external libraries beyond the standard C library.

The shell supports:

Interactive and non-interactive mode Execution of commands with arguments PATH resolution Built-in commands (exit, env) Proper error handling Correct exit status propagation Memory management (Valgrind clean) πŸ“š Learning Objectives

Through this project we practiced:

Process creation (fork) Program execution (execve) Process synchronization (wait) Environment handling (environ) PATH parsing Dynamic memory management System call error handling UNIX process lifecycle πŸ›  Compilation gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh πŸš€ Usage Interactive Mode $ ./hsh $ ls $ ls -l /tmp $ env $ exit Non-Interactive Mode echo "ls -l" | ./hsh βš™οΈ Features Implemented (Holberton Requirements) βœ… 1. Basic Shell Displays a prompt in interactive mode Reads user input using getline Executes commands using fork and execve Handles EOF (Ctrl + D) βœ… 2. Handle Arguments

Supports commands with arguments:

ls -l /var

Tokenization is implemented using strtok.

βœ… 3. Handle PATH

If the command does not contain / or ., the shell searches for it in the PATH environment variable.

Example:

ls

Internally searches:

/bin/ls /usr/bin/ls ...

If not found β†’ prints error without calling fork.

βœ… 4. fork Must Not Be Called If Command Doesn't Exist

The shell validates command existence before forking:

if (cmd_path == NULL) { fprintf(stderr, "./hsh: %d: %s: not found\n", line_num, argv[0]); return (127); }

This satisfies Holberton’s strict requirement.

βœ… 5. Built-in: exit

Usage:

exit Exits the shell Returns the last command exit status No arguments required (per project specification) βœ… 6. Built-in: env

Usage:

env

Prints all environment variables using:

extern char **environ; 🧠 Execution Flow START ↓ Display prompt (if interactive) ↓ Read input (getline) ↓ Remove newline ↓ Tokenize input ↓ Check built-ins (exit / env) ↓ Search in PATH (if needed) ↓ If command not found β†’ print error (NO fork) ↓ fork() ↓ Child β†’ execve() Parent β†’ wait() ↓ Return exit status ↓ Repeat πŸ“‚ Main Components πŸ”Ή main() Shell loop Input parsing Built-in handling Status tracking πŸ”Ή execute_command() Validates executable path Forks process Calls execve Waits for child Returns exit status πŸ”Ή find_in_path() Parses PATH Iterates directories Constructs full path Validates using stat Returns full path or NULL πŸ”Ή print_env() Iterates through environ Prints environment variables ❌ Error Handling

Handles:

Command not found Fork failure Execve failure Empty input EOF Memory allocation errors

Error format:

./hsh: line_number: command: not found

Exit status 127 is returned when command is not found.

🧡 Memory Management All dynamically allocated memory is freed cmd_path freed after use line freed before exit No memory leaks (Valgrind clean) πŸ§ͺ Example Valid Command $ ls main.c hsh README.md Invalid Command $ fakecmd ./hsh: 3: fakecmd: not found πŸ” System Calls Used fork execve wait write access stat getline malloc free 🧾 Return Status Behavior Returns the exit status of the last executed command. Returns 127 if command not found. Built-in exit returns last status. 🧩 Project Constraints (Respected)

βœ” No use of system() βœ” No advanced shell features (pipes, redirections, etc.) βœ” No unnecessary forks βœ” Proper PATH handling βœ” No memory leaks βœ” Only allowed functions used

πŸ‘¨β€πŸ’» Authors AZZAM AL DUYULI && ABDULMALIK BIN AQEEL

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors