diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-02-27 23:18:00 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-02-27 23:18:00 +0100 |
| commit | 331f1375858b6cebf2257c45844f32ab00187540 (patch) | |
| tree | 2d59a14c5fd111aa6b3ded89d3ee23ba1442e2df | |
| parent | 5ce212ed0b7abbbb862df0cf436688046a012da2 (diff) | |
fixed the files and folders a bit
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | project.md | 123 |
2 files changed, 0 insertions, 128 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 6ebafe3..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -set(CMAKE_C23_STANDARD) - -project(unixshell) -add_executable(program main.c)
\ No newline at end of file diff --git a/project.md b/project.md deleted file mode 100644 index d8561cf..0000000 --- a/project.md +++ /dev/null @@ -1,123 +0,0 @@ -### **Building a Unix Shell from Scratch** -When building a Unix shell from scratch, you typically create a **command-line interface (CLI)** that can: -1. Read user input (commands). -2. Parse the input (tokenizing, handling arguments, redirections, etc.). -3. Execute the command, either internally (built-in commands) or by forking a new process. -4. Handle job control (foreground/background processes, signals). -5. (Optionally) Support scripting, aliases, and advanced features like pipes (`|`) and I/O redirection (`>`, `<`). - -A minimal shell implementation in C follows these steps: - -#### **1. Read User Input** -Use `getline()` or `read()` to accept input from the terminal. - -#### **2. Tokenize Input* -convert into **tokens** (command and arguments) using `strtok()`. - -#### **3. Execute the Command** -- If it is a built-in command (e.g., `cd`, `exit`), handle it directly. -- Otherwise, use `fork()` to create a child process and `execvp()` to execute the command. - -#### **4. Handle Process Management** -- Use `waitpid()` to wait for processes. -- Implement background jobs (`&` support). -- Signal handling (`SIGCHLD`, `SIGINT` for `Ctrl+C`). - -#### **5. Optional Features** -- Environment variables handling (`export`, `$PATH` resolution). -- File redirections (`>` for output, `<` for input). -- Pipe (`|`) execution with `pipe()`, `dup2()`, and multiple processes. - -Here’s a **basic Unix shell** in C: - -```c -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <sys/types.h> -#include <sys/wait.h> - -#define MAX_CMD_LEN 1024 -#define MAX_ARGS 64 - -void execute_command(char **args) { - pid_t pid = fork(); - if (pid == 0) { // Child process - execvp(args[0], args); - perror("exec failed"); - exit(1); - } else if (pid > 0) { // Parent process - wait(NULL); - } else { - perror("fork failed"); - } -} - -int main() { - char input[MAX_CMD_LEN]; - char *args[MAX_ARGS]; - - while (1) { - printf("mysh> "); - if (!fgets(input, MAX_CMD_LEN, stdin)) break; // Read input - - input[strcspn(input, "\n")] = 0; // Remove newline - - // Tokenize input - int i = 0; - args[i] = strtok(input, " "); - while (args[i] && i < MAX_ARGS - 1) { - args[++i] = strtok(NULL, " "); - } - args[i] = NULL; // NULL-terminate args - - if (args[0]) { - if (strcmp(args[0], "exit") == 0) break; // Built-in exit - execute_command(args); - } - } - return 0; -} -``` - -This basic shell: -- Reads input -- Tokenizes it -- Forks a process and executes commands using `execvp()` -- Waits for processes to complete -- Supports built-in `exit` command - -A full-featured shell would include: -- **Job control** (`fg`, `bg`, `jobs`) -- **Signal handling** (`SIGTSTP`, `SIGINT`) -- **Redirections** (`>`, `<`) -- **Pipelines** (`|`) -- **Shell scripting** (`if`, `for`, `while` loops) - ---- - -### **Unix Shell vs. POSIX Shell** -A **Unix shell** is a general term for any command interpreter that runs on Unix-like systems (e.g., **bash, zsh, ksh, tcsh, dash**). - -A **POSIX shell** is a shell that strictly conforms to the **POSIX standard (IEEE 1003.1-2008)**, ensuring portability across Unix systems. The **POSIX shell specification** is based on **Bourne Shell (sh)** and defines: -- **Syntax** (`if`, `for`, `case`, `while`, etc.) -- **Built-in commands** (`cd`, `echo`, `export`, `set`, etc.) -- **I/O redirection** (`>`, `<`, `2>`, `&>`, etc.) -- **Substitution** (`$()`, `` ` ``, `${VAR}`) - -**Key Differences:** -| Feature | Unix Shell (Bash, Zsh, etc.) | POSIX Shell (Dash, sh) | -|---------|-----------------|------------------| -| Scripting Extensions | Yes (arrays, `[[ ]]`, associative arrays, `printf -v`, etc.) | No, follows strict POSIX | -| Performance | Slower due to extra features | Faster (used for system scripts) | -| Compatibility | Linux, BSD, Mac, Solaris | Strictly portable | -| Interactive Features | Yes (command history, autocomplete, themes) | Minimal | - -For example: -- **Bash (`bash`)** is a Unix shell but has extra non-POSIX features (`[[` test, associative arrays). -- **Dash (`/bin/dash`)** is a strict POSIX shell, often used for system scripts due to speed. - -If you want to write **portable shell scripts**, stick to **POSIX sh** (e.g., `#!/bin/sh` instead of `#!/bin/bash`). - -Would you like help adding features like redirections or pipes to the basic shell? 🚀 |
