Skip to main content

Overview

Minishell is organized into logical modules, each handling specific aspects of shell functionality. The project contains 24 source files, 1 header file, and follows the 42 school coding standards.

File Tree

minishell/
├── Makefile
├── minishell.h              # Main header file
├── main.c                   # Entry point and main loop
├── init_shell.c             # Shell initialization and builtins
├── parse_imput.c            # Input parsing and validation
├── more_parsing.c           # Additional parsing utilities
├── split.c                  # String splitting with quote handling
├── split_utils.c            # Splitting helper functions
├── ft_execute_commands.c    # Command execution orchestration
├── ft_execute_one_command.c # Single command execution
├── ft_prepare_nodes.c       # Command node preparation
├── ft_count_pipes.c         # Pipe counting and command splitting
├── builtins.c              # Built-in commands implementation
├── builtins_utils.c        # Built-in helper functions
├── exit.c                  # Exit command implementation
├── env_parsed.c            # Environment variable parsing
├── env_parsed2.c           # Additional environment utilities
├── set_bin_path.c          # Binary path resolution
├── set_full_cmd_path.c     # Full command path construction
├── set_infile_outfile.c    # File descriptor setup
├── signals.c               # Signal handling (SIGINT, SIGQUIT)
├── ft_print_prompt.c       # Prompt display
├── utils.c                 # General utility functions
├── utils2.c                # Additional utilities
└── utils3.c                # Memory management utilities

Module Breakdown

main.c

Purpose: Entry point and main REPL (Read-Eval-Print Loop)Key Functions:
  • main() - Initializes shell and runs the main loop (line 97)
  • enterdata() - Processes each command line (line 17)
  • asignenvp() - Copies environment variables to linked list (line 59)
  • init_data() - Initializes the main data structure (line 84)
Key Data Structures:
typedef struct s_mini
{
    char     **commands;      // Parsed command array
    char     *full_path;      // Full path to executable
    int      splits;          // Number of arguments
    int      ft_count_pipes;  // Number of pipes
    char     **execute_envp;  // Environment variables
    t_node   **nodes;         // Command execution nodes
    int      nbr_nodes;       // Number of command nodes
    char     **bin_path;      // PATH directories
    t_prompt *env;            // Environment linked list
} t_mini;
Global Variable:
  • g_status - Last command exit status (line 15)

init_shell.c

Purpose: Shell initialization and built-in command routingKey Functions:
  • execute_builtin() - Routes commands to appropriate built-in (line 118)
  • fork_actions() - Handles env and export commands (line 64)
  • other_actions() - Handles echo, pwd, cd, unset (line 93)
  • export() - Implements export built-in (line 35)

parse_imput.c

Purpose: Input validation and quote handlingKey Functions:
  • remove_quotes() - Strips quotes from parsed arguments (line 13)
  • detectopenquotes() - Validates quote pairing (line 14)
  • check_wrong_pipes() - Validates pipe syntax (line 15)
  • check_wrong_redir() - Validates redirection syntax (line 16)

more_parsing.c

Purpose: Advanced parsing and input preparationKey Functions:
  • prepare_line() - Preprocesses input for parsing (line 19)
  • ft_more_checkers() - Additional syntax validation (line 20)
  • ft_check_input() - Main input validation entry point (line 21)

split.c

Purpose: String splitting with shell-aware tokenizationKey Functions:
  • ft_split() - Standard string splitting (line 84)
  • ft_splitquotes() - Quote-aware string splitting (line 85)
  • ft_num_word() - Counts words respecting quotes (line 86)
Used for breaking command lines into tokens while preserving quoted strings.

split_utils.c

Purpose: Splitting helper utilitiesKey Functions:
  • free_split() - Frees split arrays (line 89)
  • ft_count_splits() - Counts split elements (line 90)

ft_execute_commands.c

Purpose: Main command execution orchestrationKey Functions:
  • ft_execute_commands() - Main execution entry point (line 97)
  • excecute_pipe_sequence() - Handles piped commands (line 68)
  • child_process() - Sets up child process I/O (line 41)
  • child_execution() - Executes command in child (line 15)
Logic Flow:
  1. Single command without pipes → Execute directly or as built-in
  2. Multiple commands with pipes → Create pipe chain
  3. Fork child processes for each command
  4. Set up file descriptors and redirections
  5. Execute via execve() or built-in function

ft_execute_one_command.c

Purpose: Single command executionKey Functions:
  • execute_simple_command() - Executes non-piped commands (line 156)
  • prepare_builtin() - Prepares built-in execution environment (line 155)

ft_prepare_nodes.c

Purpose: Command node preparation and setupKey Functions:
  • ft_prepare_nodes() - Creates execution nodes from parsed input (line 133)
Each node represents a single command in a pipeline:
typedef struct s_node
{
    char  **full_cmd;   // Command and arguments
    char  *full_path;   // Full path to executable
    int   infile;       // Input file descriptor
    int   outfile;      // Output file descriptor
    int   is_set;       // Node validity flag
    pid_t n_pid;        // Process ID
} t_node;

builtins.c

Purpose: Core built-in command implementationsImplemented Commands:
  • echo() - Prints arguments with optional newline (line 87)
  • pwd() - Prints current working directory (line 55)
  • cd() - Changes directory (line 70)
  • unset() - Removes environment variables (line 42)
  • remove_env() - Helper for unset (line 15)
Example from code:
void pwd(int argc)
{
    char *pwd;
    if (argc > 1)
    {
        printf("pwd: too many arguments\n");
        return ;
    }
    pwd = getcwd(NULL, 0);
    printf("%s\n", pwd);
    free(pwd);
    g_status = 0;
}

builtins_utils.c

Purpose: Built-in command helper functionsKey Functions:
  • ft_lscopy() - Copies environment variable to list (line 99)
  • ft_lstsort() - Sorts environment list (export display) (line 100)
  • dup_env() - Duplicates environment list (line 101)
  • check_env_name() - Validates environment variable names (line 102)
  • asign_env_value() - Assigns or updates environment values (line 103)

exit.c

Purpose: Exit command implementationKey Functions:
  • ft_exit() - Handles shell exit with status code (line 110)

env_parsed.c

Purpose: Environment variable expansionKey Functions:
  • vars() - Main variable expansion function (line 159)
  • env_split() - Splits environment strings (line 160)
  • ft_itoa() - Converts integers to strings (for $?) (line 161)
Handles:
  • $VAR - Environment variable expansion
  • $? - Last exit status
  • Variable expansion in double quotes

env_parsed2.c

Purpose: Additional environment parsing utilitiesKey Functions:
  • ft_asign_rare_value() - Handles special variable cases (line 164)
  • ft_check_dolar_length() - Measures variable name length (line 165)
  • ft_check_dolars() - Detects dollar signs in input (line 166)
  • ft_check_for_quotes() - Tracks quote context during parsing (line 167)
  • ft_checkvar_value() - Retrieves variable value (line 168)
Environment variables are stored in a linked list:
typedef struct s_prompt
{
    char           *envp;  // "KEY=value" format
    struct s_prompt *next;
} t_prompt;

set_bin_path.c

Purpose: Extracts and parses PATH environment variableKey Functions:
  • set_bin_path() - Parses PATH into array of directories (line 149)
Splits PATH into searchable directories for executable resolution.

set_full_cmd_path.c

Purpose: Resolves command to full executable pathKey Functions:
  • set_full_path() - Finds executable in PATH (line 139)
  • set_full_cmd() - Constructs full command array (line 140)
Resolution order:
  1. Absolute path (/bin/ls)
  2. Relative path (./program)
  3. PATH search (ls/bin/ls)

set_infile_outfile.c

Purpose: File descriptor setup for redirectionsKey Functions:
  • set_infile_outfile() - Opens files for < and > (line 143)
  • get_here_doc() - Implements here-document (<<) (line 145)
  • ft_create_temp() - Creates temporary file for here-docs (line 152)
Handles:
  • < - Input redirection
  • > - Output redirection (truncate)
  • >> - Output redirection (append)
  • << - Here-document (reads until delimiter)

signals.c

Purpose: Manages Unix signalsKey Functions:
  • setup_signals() - Initializes signal handlers (line 25)
  • handle_sigint() - Handles Ctrl+C (SIGINT) (line 15)
Behavior:
void handle_sigint(int sig)
{
    (void)sig;
    printf("\n");
    rl_on_new_line();      // Move to new line
    rl_replace_line("", 0); // Clear input buffer
    rl_redisplay();         // Redraw prompt
    g_status = 130;         // Set exit status
}
  • SIGINT (Ctrl+C): Clears line, displays new prompt
  • SIGQUIT (Ctrl+): Ignored in interactive mode

utils.c

Purpose: String manipulation and linked list operationsKey Functions:
  • ft_strncmp() - String comparison (line 64)
  • ft_strlen() - String length (line 65)
  • ft_strlcpy() - Safe string copy (line 66)
  • ft_strjoin() - String concatenation (line 67)
  • ft_memcpy() - Memory copy (line 68)
  • ft_lstsort(), ft_lstadd_back(), ft_lstlast(), ft_lstnew() - List operations

utils2.c

Purpose: Additional string and utility functionsLocated at: utils2.c

utils3.c

Purpose: Memory management and conversionKey Functions:
  • ft_calloc() - Allocates and zeros memory (line 77)
  • ft_free() - Frees string arrays (line 78)
  • ft_free_stack() - Frees linked lists (line 79)
  • ft_isdigit() - Digit validation (line 80)
  • ft_atoi() - String to integer conversion (line 81)

ft_count_pipes.c

Purpose: Pipe analysis and command segmentationKey Functions:
  • ft_count_pipes() - Counts pipes in command (line 128)
  • ft_strdup2() - Duplicates command segments (line 129)
  • ft_len_to_pipe() - Measures distance to next pipe (line 130)

ft_print_prompt.c

Purpose: Prompt generation and displayKey Functions:
  • print_logo() - Displays startup logo (line 175)
  • ft_print_user() - Generates user prompt string (line 176)
Uses ANSI color codes defined in header:
  • COLOR_AZUL: Blue
  • GREEN: Green
  • MAGENTA: Magenta
  • RST: Reset

Header File Structure

minishell.h

The main header contains: Includes:
  • Standard libraries: unistd.h, stdio.h, stdlib.h, fcntl.h
  • System: sys/stat.h, sys/types.h, sys/wait.h
  • Readline: readline/readline.h, readline/history.h
  • Signal handling: signal.h
  • Directory operations: dirent.h
Definitions:
  • Color macros for terminal output (lines 27-30)
  • TEMP_FILE constant for here-documents (line 32)
Structures:
  • t_prompt - Environment variable linked list node
  • t_node - Command execution node
  • t_mini - Main shell data structure
Function Prototypes: All function declarations organized by source file (lines 63-176) Global Variable:
  • extern int g_status - Shared exit status (line 178)

Naming Conventions

42 School Style

The project follows 42 school conventions:
  • Functions: snake_case with ft_ prefix for custom implementations
    • Example: ft_strlen(), ft_split(), ft_execute_commands()
  • Structures: typedef struct s_name with t_name alias
    • Example: typedef struct s_minit_mini
  • Variables: snake_case
    • Example: full_cmd, nbr_nodes, bin_path
  • Constants: UPPER_SNAKE_CASE
    • Example: TEMP_FILE, COLOR_AZUL

File Headers

All files include 42 school header comment blocks:
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   filename.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: author <[email protected]>              +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: date by author                         #+#    #+#             */
/*   Updated: date by author                        ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

Code Organization Principles

  1. Modular Design: Each file focuses on a specific functionality
  2. Clear Separation: Parsing, execution, and I/O are separate modules
  3. Utility Functions: Common operations centralized in utils files
  4. Header Organization: All public interfaces in single header
  5. 42 Norm Compliance: Follows 42 school coding standards

Next Steps

Build docs developers (and LLMs) love