Specializations > System programming & Algorithm > Linux Programming
Julien Barbier
12-07-2020 to 12-15-2020
Introduction to static variables in C and building a clone of the glibc function getline.
- Allowed Functions and System Calls
readwritemallocreallocfreestrcpystrncpystrcatstrdupmemsetmemcpy
Write a function that keeps track of the number of laps made by several cars in a race.
- Prototype:
void race_state(int *id, size_t size) idis an array ofintrepresenting the “identifier” of each car.sizeis the size of this array- Each car identifier is unique
- If an identifier is unknown:
- Create a new car with the number of laps = 0
- Print
Car X joined the racefollowed by a new line (whereXis the identifier)
- Each time the function is called:
- The number of laps of each cars listed in
idmust be incremented by 1 - Print the state of the race:
- Header:
Race state:followed by a new line - For each car sorted by the identifier:
Car X [Y laps](whereXis the identifier andYthe number of laps already done)
- Header:
- The number of laps of each cars listed in
- If your function is called with
size = 0, you must free all allocated memory.
File(s): laps.c laps.h
Compiled: gcc -Wall -Wextra -Werror -pedantic 0-main.c laps.c -o laps
Write a function that reads an entire line from a file descriptor.
- Prototype:
char *_getline(const int fd) - Where
fdis the file descriptor to read from - If there are no more lines to return, or if there is an error, the function should return NULL
- The function returns a null-terminated string that does not include the newline character
- Your header file
_getline.hshould define a macro calledREAD_SIZE.- This macro defines the number of bytes you will read each time you will call
read:read(fd, buffer, READ_SIZE) - You are not allowed to read more or less than
READ_SIZEbytes at once fromfd - You are free to pick the value that you want for
READ_SIZE
- This macro defines the number of bytes you will read each time you will call
- You can assume that
fdwill always be the same
File(s): _getline.c _getline.h
Compiled: gcc -Wall -Wextra -Werror -pedantic 1-main.c _getline.c -o getline
Handle multiple file descriptors.
- When called with
-1you should free everything and reset all your static variables
File(s): _getline.c _getline.h
Compiled: gcc -Wall -Wextra -pedantic -Werror 2-main.c _getline.c -o getline
Handle characters \0 in lines.
- Here’s the file used for the example below: zero
$ cat zero
line 1 Holberton.....
line 2 still line 2
line 3 ......School()
$ gcc -g -Wall -Wextra -pedantic 100-main.c _getline.c && ./a.out
6c 69 6e 65 20 31 20 48 6f 6c 62 65 72 74 6f 6e 2e 2e 2e 2e 2e
6c 69 6e 65 20 32 20 00 73 74 69 6c 6c 20 6c 69 6e 65 20 32 00
6c 69 6e 65 20 33 20 2e 2e 2e 2e 2e 2e 53 63 68 6f 6f 6c 28 29
$File(s): _getline.c _getline.h
Compiled: gcc -Wall -Wextra -pedantic -Werror 100-main.c _getline.c -o getline
- Samuel Pomeroy - allelomorph