Because reading a line from a fd is way too tedious, this project is about creating your own function that returns a line read from one file descriptor or more, if you consider the bonus project requirement. The function get_next_line() returns NULL, if there is nothing else to read or if an error occurred. It should work as expected both when reading a file and when reading from the standard output.
The main objetive of this project is to make students learn about static variables.
There are many ways to solve the problem stated. Either using an array or a singly linked list as a data structure. I decided to implement the last one because it was an opportunity to see how linked list are used.
The project contains six files. The ones that ends with _bonus
are the bonus project that reads a line from two or more file descriptors at the same time. To sum up,
get_next_line.h
: contains the prototypes of all necessary functions to the project.get_next_line.c
: it has five functions. They are:get_next_line()
: it's the project main function. It reads a line, saves the content and joins the line when it matches anEOF
(end of file) or'\n'
(new line break);read_line()
: reads a line at a max length of BUFFER_SIZE, which is defined at compilation time. If BUFFER_SIZE is not defined, the program sets the standard buffer value to 1024;is_new_line()
: checks whether the chunk read is a new line (i. e., it has\n
) or not. It returns one if true and zero if false;create_line()
: creates a line by joining read buffer sized chunks of a line untilEOF
or\n
is encountered;refactor_line()
: if a buffer sized chunk has\n
, the function will save the content after\n
to create the next line of the file descriptor by the time theget_next_line()
function is called again. That's the reason why we need a static variable, the information isn't lost by the time the function ends.
get_next_line_utils.c
: implements helper functions to manipulate heap(or dynamic) memory and singly linked lists.
To use the get_next_line()
in your project it's necessary to include the three files mentioned above to be compiled, either the mandatory or the bonus files, and be sure to include the following flags:
$> gcc -Wall -Wextra -Werror -D BUFFER_SIZE=<length> <files>.c
Where <length> can be substituted by the number of bytes read at a time into the buffer for the read function. If BUFFER_SIZE is not defined, it will be defined to 1024 as standard value for the buffer size.
To reference the get_next_line()
function inside your project, don't forget to add the header:
#include "get_next_line.h"
If you want to use the mandatory project, that is. If you want to use the bonus project to read multiple file descriptors, add the header:
#include "get_next_line_bonus.h"
- Acelera GNL by rodsmade.
- To test the project and check for memory leaks, I used the gnlTester, made by jgambard.