add first day
This commit is contained in:
commit
146746ddcf
3 changed files with 79 additions and 0 deletions
9
01-cat/Makefile
Normal file
9
01-cat/Makefile
Normal file
|
@ -0,0 +1,9 @@
|
|||
CC = gcc
|
||||
CFLAGS = -ansi -pedantic -pedantic-errors -Wall -Werror -Wextra
|
||||
SRC = cat.c
|
||||
|
||||
all:
|
||||
$(CC) $(CFLAGS) $(SRC) -o cat
|
||||
|
||||
clean:
|
||||
rm cat
|
56
01-cat/cat.c
Normal file
56
01-cat/cat.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
const int BUFF_SIZE = 4096;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *buffer, *ptr;
|
||||
int i = 1;
|
||||
|
||||
for (; i < argc; i++)
|
||||
{
|
||||
int fd = open(argv[i], O_RDONLY);
|
||||
int ret, nb_bytes = 1, n;
|
||||
|
||||
if (fd == -1) perror(argv[i]);
|
||||
else
|
||||
{
|
||||
while (nb_bytes > 0)
|
||||
{
|
||||
buffer = (char *) calloc(BUFF_SIZE, sizeof(char));
|
||||
nb_bytes = read(fd, buffer, BUFF_SIZE);
|
||||
if (nb_bytes == -1)
|
||||
{
|
||||
perror(argv[i]);
|
||||
break;
|
||||
}
|
||||
|
||||
ptr = buffer;
|
||||
n = nb_bytes;
|
||||
|
||||
while (n > 0)
|
||||
{
|
||||
ret = write(STDOUT_FILENO, ptr, n);
|
||||
if (ret == -1)
|
||||
{
|
||||
perror(argv[i]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ptr += ret;
|
||||
n -= ret;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
14
README.md
Normal file
14
README.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Calendrier de l'avent des syscall
|
||||
|
||||
Du 1er au 24 décembre, on découvre des syscalls. Vous pouvez trouver tous les
|
||||
défis sur [le site de l'Operating System Group](https://osg.tuhh.de/Advent/).
|
||||
|
||||
Il suffit d'aller dans un dossier et faire `make` pour avoir un exécutable.
|
||||
|
||||
## Plus d'informations sur les jours
|
||||
|
||||
### 01 - cat
|
||||
|
||||
Syscalls utilisés: open, read, write, close.
|
||||
|
||||
Il s'agit d'un cat. Il prend en paramètres N fichiers et les affiche.
|
Loading…
Reference in a new issue