Skip to content

sayalikulk/custom-unix-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CS 537 Project 3 — Unix Shell (wsh)

Name: Sayali Kulkarni
CS Login: sskulkarni34
Email: sskulkarni34@wisc.edu

Status

  • Interactive mode: Implemented (wsh> prompt shown only when stdin is a terminal).
  • Batch mode: Implemented (reads commands from file, no prompt, exits on EOF).
  • External commands: Implemented using fork(), execv(), waitpid().
  • PATH resolution: Implemented manually using getenv("PATH"), tokenizing on :, and checking executability with access(..., X_OK).
    • If PATH is unset/empty, defaults to /bin.
    • If the command contains /, it is treated as a path and executed directly.
  • Built-ins: Implemented
    • exit (ignores args)
    • cd [dir] (defaults to $HOME, prints cd: HOME not set if missing, uses perror("cd") on failure)
    • env [VAR=val] (no args prints all env vars; env VAR=val sets; env VAR sets empty string)
  • Environment variable substitution: Implemented via get_variable() hook used by the provided parser.
    • If variable is not set, returns empty string.
  • Pipelines: Implemented for arbitrary-length pipelines using pipe(), dup2(), and fork() per command.
    • Built-ins inside a pipeline run in the child (so they do not affect the parent shell state).
    • Parent waits for all pipeline children; shell status is set from the last command in the pipeline.

Design Notes / Key Decisions

  • Uses the provided parser (parse_input()) to split input into:
    • pipelines separated by ;
    • pipeline commands separated by |
    • argv arrays for each command
  • Prompt behavior:
    • wsh> is printed only if isatty(STDIN_FILENO) is true.
  • Memory management:
    • Input line is read using getline() to support arbitrary-length lines.
    • Parsed command lines are freed with free_command_line() after execution.
    • Any heap allocations used for PATH resolution are freed after use.

Error Handling (per spec)

  • Invalid usage: prints Usage: ./wsh [file] and exits(1).
  • Batch file open failure: uses perror("fopen") and exits(1).
  • System call failures: checked and reported with perror() (e.g., fork, pipe, dup2, setenv, malloc/strdup where applicable).
  • Command not found:
    • If a command cannot be resolved in PATH (or exec fails), prints Command not found to stderr.
  • Empty lines/comments:
    • Empty/invalid parse results are ignored and the shell continues.

How to Build

From the project root (inside the CS537 Docker environment):

make

How to Run

Interactive mode

./wsh

Batch mode

./wsh path/to/batchfile.txt

Testing

Provided tests

cd tests
./run-tests.sh

Valgrind leak check

Interactive:

valgrind --leak-check=full --track-origins=yes ./wsh

Batch:

valgrind --leak-check=full --track-origins=yes ./wsh tests/inputs/some_batch_file.txt

Known Limitations / Notes

  • The implementation relies on the provided parser’s assumptions and limits (e.g., MAX_ARGS).
  • cd only affects the parent shell when executed as a standalone command (not in a pipeline), as required.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors