Skip to content
/ pipex Public

A tutorial / tips and tricks how to do Pipex 42 (125/100)

Notifications You must be signed in to change notification settings

jkczech/pipex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pipex

A project from 42 Vienna focused on processes, pipe and file management.

This project is about pipes.

Following information and the code in this repository is made for inspiration and help for students working on this project.

If you copy any part of the code, you only cheat yourself because you will learn nothing. Get just inspired instead.

drawing

Requirements

as of Jan '24

Program name pipex, pipex_bonus
Turn in files Makefile, *.h, *.c
Makefile NAME, all, clean, fclean, re
Arguments file1 cmd1 cmd2 file2
External allowed
functions
open, close, read, write, malloc, free, perror,
strerror, access, dup, dup2, execve, exit, fork,
pipe, unlink, wait, waitpid, ft_printf (prev. project)
Libft authorized Yes (also prev. project - set of useful functions)

The project must comply with the following rules:

  • You must turn in a Makefile that will compile your source files. It must not relink.
  • You have to handle errors thoroughly. In any way, your program shouldn't quit unexpectedly (segmentation fault, bus error, double free, and so forth).
  • Your program can't have memory leaks.
  • If you have any doubt, handle the errors like the shell command: $> < file1 cmd1 | cmd2 > file2

Mandatory part

The program execution is as follows:

$> ./pipex file1 cmd1 cmd2 file2

It takes four arguments:

  • file1 and file2 are file names
  • cmd1 and cmd2 are shell commands with their parameters

It behaves the same as the shell command below:

  • $> < file1 cmd1 | cmd2 > file2

(In my implementation you can create the program by executing make)

Bonus part

Handle multiple pipes

The program execution is as follows:

$> ./pipex_bonus file1 cmd1 cmd2 cmd3 ... cmdn file2

Behaves like this:

$> < file1 cmd1 | cmd2 | cmd3 ... | cmdn > file2

It takes at least four arguments:

  • file1 and file2 input and output file names
  • cmd1 to cmdn are shell commands with their parameters

(In my implementation you can create the program by executing make bonus)

Support « and » when the first parameter is "here_doc"

The program execution is as follows:

$> ./pipex_bonus here_doc LIMITER cmd cmd1 file

Behaves like this:

$> cmd << LIMITER | cmd1 >> file

It takes at least four arguments:

  • here_doc this specific string will open stdin for the user-given input
  • LIMITER string that will close the input
  • cmd and cmd1 are shell commands with their parameters
  • file output file

Working on the project

Before starting with the actual coding, I would recommend watching this youtube playlist. It explains well the processes, pipes, forks and all important stuff needed for this project. This should give you a nice headstart:)

Tips

Some of the key aspects of my code that you can consider doing. But of course, remember, there are many ways to implement the code to reach the same result.

  • having all data in one structure (in my case t_pipex), it helps saving arguments in the function prototypes, and since there are many things to 'remember' it's a very useful thing
  • for each command create one child process... I would not recomend having parent process that executes one command and child process that executes the other one since afterwards its nearly impossible to implement the bonus this way or use your pipex code in minishell project (but it is certainly still possible to do it that way)
  • for each function, try to have one functionality. What also helps is documentation of code, which means that above each function I put one or two lines of comment saying what the function does... it helps both to keep your code readable and have one functionality for function. Also when you later hover over the function name in VS Code, you can see your comments and know right away what the function does.

Testing, submitting, evaluating

image

I submitted the project many times until I finally finished (: these are the things that should be implemented for the evaluation:

Mandatory

  • empty path - unset PATH - code can't segfault after unsetting path
  • NULL parameters - ./pipex in cat "" out - should be handled correctly
  • error messages - correct error messages - no file, no permissions, cmd not found
  • nonsense input - ./pipex a a a a or ./pipex a b c d - especially for handling the error messages correctly
  • Makefile - all makefile targets should work on their own without failing

Bonus

  • heredoc limiter - if you end your heredoc file with LIMITERLIMITER it shouldn't end
  • CTRL D - EOF ending the heredoc instead of the LIMITER
  • middle command, no permissions - ./pipex infile cat "rm -f file" cat outfile the rm should be executed even if infile and outfile don't have the correct permissions
  • exitcode - outfile has no permissions (1), last command doesn't exist (127), correct last process and outfile (0), the way to acces exitcode is echo $? after execution of the program (I'm not sure if exitcode handling is necessary here but some evaluators check that and it will be required in minishell)

Things you should also test

  • protection - functions like fork, pipe, ft_split, ft_strdup, execve, and dup2 should be protected, and after some of them, you need to exit the program properly
  • random file - ./pipex /dev/random cat "head -1" outfile
  • norminette
  • valgrind - make sure to use flags too especially --track-fds=all --trace-children=yes, at the end of the execution, only three std fds are allowed to be open (be aware that in VS Code, there are by default some extra fds open therefore it's better to test this in a shell outside of VS Code)
  • scan-build-12 make re
  • possibly funcheck
  • pipex-tester

If you found these tips and tricks helpful, please put a star ⭐ on my repository, thanks :)

© Copyright

All of the code belongs to me (@jkczech).

The project idea and the requirements belong to 42 Vienna / 42 Network.

If sharing this repository is against the rules of the 42 Network, please let me know, and I will take it down.

About

A tutorial / tips and tricks how to do Pipex 42 (125/100)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published