### Information

 Author(s) Cyril Pletinckx Deadline No deadline Submission limit No limitation Category tags Files, S6, Mmap, Level 4

## [S6] Sort my points

There is a problem with the points you obtained at your exams in January : the averages have been lost in the file where all the points were stored. They are stored in structures defined as follow :

typedef struct points {
int NOMA;
float LEPL1103;
float LEPL1203;
float LEPL1108;
float LEPL1302;
float LEPL1402;
float average;
} points_t;


As averages are really important to sort the file and then to assign the merits, we ask you to get the structures from the file, compute the average of each student based on his/her five exams, sort the students comparing averages and then write the new sorted structures back in the file. For this, you have to write two functions : a compare function and the main function that will do the rest.

##### Question 1: Compare function

We ask you to implement a function that will allow us to compare the structures based on the averages. This function must be generic so it takes two const void pointers as arguments (don't forget to cast them !). Write the body of the function.

/*
* Function used to compare two structures based on their averages.
*
* @s1 : a pointer to the first structure
* @s2 : a pointer to the second structure
*
* @return an integer less than, equal to, or greater than zero if the first argument is considered to be
*        respectively less than, equal to, or greater than the second
*/
int compar(const void* s1, const void* s2){

##### Question 2: Sorting function

Now, implement a function that will allow us to read the file, sort the array and then write the sorted structures back to the file. For this, you can only use these functions : open(2), mmap(2), munmap(2), msync(2), fstat(2) and close(2). The file will always exist and will always contain at least one structure. You can use your compare function previously done.

Hint : if you are not familiar with theses functions, please do or redo the exercices about files.

Hint : There is certainly a built-in function that can be used to sort the array mapped with mmap.

/*
* Function used to update the file with the structures stored in it
*
* @filename : the path indicating where to find the file (!= NULL)
*
* @return 0 if no error
*        -1 if open failed
*        -2 if mmap failed
*        -3 if munmap failed
*        -4 if msync failed
*        -5 if fstat failed
*        -6 if close failed
*/
int sort(char* filename){