Skip to content

librity/ft_pipex

Repository files navigation

42 São Paulo - Pipex

42 São Paulo License Code size in bytes Lines of code Top language Last commit

Build Norminette v3 denisgodoy_pipex-tester gsilva-v_pipextester vfurmane_pipex-tester yoo0lh_pipex_tester_42

A simple implementation of UNIX |, <, <<, > and >> in pure C.


📜 Table of Contents

🧐 About

In this projects we implement UNIX pipes and redirections, namely |, <, <<, > and >>. These mechanisms allow us to link commands and files together, where the output of one serves as the input of the next.

This toolbox philosophy is one of the reasons why UNIX is still popular after 40 years: it encourages developers to create programs that do one thing well, and that work well with other programs. It's also extremely modular and versatile.

I learned about child processes, filesystem interfaces and the internals of bash, the target of our emulation. Since I did both bonuses I'm pretty confident about the next big challenge in the 42 curriculum: minishell. It's basically this with a parser and dynamic environment variables.

✅ Checklist

Mandatory

  • DONT TURN IN LIBS AS SUBMODULES
  • MAKEFILE EXPLICIT SOURCE FILES (echo M_SOURCES)
  • Make must compile without relinking
    • SHOOULDNT RECOMPILE/REARCHIVE OBJECTS WITH MAKE ALL
      • Add .keep to object dirs
      • Remove initialize from all /build
      • Create non-phony rule for each lib archive
  • .linux file (42 Workspaces)
  • Test in workspaces
  • Follows norminette 3.3.51
  • Turn in Makefile, *.h, *.c , .linux , .gitignore
  • Makefile rules: $(NAME) all clean fclean re
  • Program name pipex
  • Compiles with -Wall -Wextra -Werror
  • Should not quit unexpectedly (segmentation fault, bus error, double free, etc.)
  • All allocated heap memory properly freed, no memory leaks.
    • Check memory leaks with valgrind
  • Allowed functions:
    • open, close, read, write, malloc, free, perror, strerror, access, dup, dup2, execve, exit, fork, pipe, unlink, wait, waitpid
    • libft allowed
    • Your ft_printf (may be modified)
      • No printf from stdio.h
  • Handle arguments file1 cmd1 cmd2 file2
    • Wrong number of arguments exits with help message
  • Behaves exactly like < file1 cmd1 | cmd2 > file2
    1. < file1 cmd1: < file1 read file1 and pipes it to cmd1’s STDIN
    2. cmd1 | cmd2: cmd1’s STDOUT is piped to cmd2’s STDIN
    3. cmd2 > file2: cmd2’s STDOUT is piped to file2, overwriting the file
  • Handle errors exactly like < file1 cmd1 | cmd2 > file2
  • Handle input errors (in order):
    • argc != 5 , return 0
    • No infile, return 0
    • No cmd1 , return 0
    • No cmd2 , return 127
    • cmd1 bad arguments, return 0
    • cmd2 bad arguments, return 1
  • Pass all testers

Bonus

  • Handle multiple pipes:
    • < file1 cmd1 | cmd2 | cmd3 ... | cmdn > file2
    • ./pipex file1 cmd1 cmd2 cmd3 ... cmdn file2
  • Support « and » when the first parameter is "here_doc":
    • ./pipex here_doc LIMITER cmd cmd1 file
    • cmd << LIMITER | cmd1 >> file

🏁 Getting Started

🖥️ Installing

Clone the repo and build with make:

$ git clone --recurse-submodules https://github.com/librity/ft_pipex.git
$ cd ft_pipex
$ make

It works exactly like < infile cmd1 | cmd2 > outfile:

./pipex infile "ls" "wc" outfile

./pipex infile "ls -l" "wc -l" outfile
./pipex infile "grep a1" "wc -w" outfile

./pipex .gitignore "tr a b" "tr b c" outfile
./pipex EOF "tr a b" "tr b c" outfile
./pipex .gitignore "ping 8.8.8.8" "grep ms" outfile

Bonus

You can also compile the bonus implementation:

$ make bonus

It handles multiple pipes < infile cmd1 | cmd2 | cmd3 | ... | cmdn > outfile:

./pipex infile "grep a" "grep d" "grep s" outfile
./pipex infile "ls -l" "grep a" "wc -l" outfile
./pipex infile "cat" "tr a b" "tr b a" "tr a b" "tr b a" outfile

It also takes a heredoc as input cmd1 << LIMITER | cmd2 >> file:

./pipex here_doc l "grep a" "grep d" "grep s" outfile
./pipex here_doc l "grep a" "wc -l" outfile
./pipex here_doc x "cat" "tr a b" "tr c d"  outfile

📝 Notes

< infile cmd

Redirect infile ’s contents to cmd ’s STDIN

python hello.py < foo.txt      # feed foo.txt to stdin for python
diff <(ls -r) <(ls)            # Compare two stdout without files

cmd1 | cmd2

Redirects previous cmd1's STDOUT to cmd2 ’s STDIN

ls -la | wc -l

cmd > outfile

Truncates outfile with cmd ’s STDOUT

python hello.py > output.txt   # stdout to (file)
python hello.py >> output.txt  # stdout to (file), append
python hello.py 2> error.log   # stderr to (file)
python hello.py 2>&1           # stderr to stdout
python hello.py 2>/dev/null    # stderr to (null)
python hello.py &>/dev/null    # stdout and stderr to (null)

perror

errno

# GET ALL ERRNO CODES
$ sudo apt install moreutils
$ errno -l

strerror

open

close

access

Checks if the calling process can access a specific file

dup & dup2

Adjust file descriptors so they reference the same file.

execve

Replaces the current process with a system call.

fork

Forks the parent process, creating a child process. The child process is almost identical to its parent, with a copy of its memory space at the time of fork.

pipe

Creates a pipe: a data channel through which processes can communicate. Emitting processes can write topipefd[1], and receiving ones can read from pipefd[0].

unlink

wait & waitpid

Wait for a process to change state.

char **envp

A string array with all the environment variables of the parent shell.

Processes vs Threads

  • Threads are lighter: easier to create and destroy
  • Processes don’t share memory

lalloc - Listed Memory Allocation

A linked list in the control structure with all the allocated memory pointers. The interface function ft_lalloc allocates memory and adds the pointer to the list. The interface function ft_free_lalloc frees all pointers and the list.

🛸 42 São Paulo

Part of the larger 42 Network, 42 São Paulo is a software engineering school that offers a healthy alternative to traditional education:

  • It doesn't have any teachers and classes.
  • Students learn by cooperating and correcting each other's work (peer-to-peer learning).
  • Its focus is as much on social skills as it is on technical skills.
  • It's completely free to anyone that passes its selection process - The Piscine

It's an amazing school, and I'm grateful for the opportunity.

📚 Resources

Bash Pipes

Pipe Order

nm -un

CS 61

File Descriptors

FORBIDDEN! execlp

A more convenient version of execve.

Valgrind Child Leaks

Testers

Parsing Tokens

C Quirks

Should I free before exiting?

Git submodule

Bash Quirks

Videos