This commit is contained in:
2025-10-02 14:46:01 +02:00
parent ced39240a2
commit 24e505f788
3 changed files with 0 additions and 0 deletions

36
tp1/ex1/copy_stdio.c Normal file
View File

@@ -0,0 +1,36 @@
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#define BLOCK_SIZE 1
int main(int argc, char *argv[])
{
FILE* fin,
* fout;
char buf[BLOCK_SIZE];
assert( argc == 3 );
fin = fopen(argv[1], "r");
assert( fin != NULL );
fout = fopen(argv[2],"w");
assert( fout != NULL );
while(1){
ssize_t nb_read;
nb_read = fread(buf,BLOCK_SIZE,1,fin);
if (nb_read <= 0)
break;
fwrite(buf,BLOCK_SIZE,nb_read,fout);
}
fclose(fin);
fclose(fout);
return 0;
}

35
tp1/ex1/copy_sys.c Normal file
View File

@@ -0,0 +1,35 @@
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#define BLOCK_SIZE 1
int main(int argc, char *argv[])
{
int fin,
fout;
char buf[BLOCK_SIZE];
assert( argc == 3 );
fin = open(argv[1],O_RDONLY);
assert( fin >= 0 );
fout = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0600);
assert( fout >= 0 );
while(1){
ssize_t nb_read;
nb_read = read(fin,buf,BLOCK_SIZE);
if (nb_read <= 0)
break;
write(fout,buf,nb_read);
}
close(fin);
close(fout);
return 0;
}

44
tp1/ex2/copy_mmap.c Normal file
View File

@@ -0,0 +1,44 @@
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
assert(argc == 3);
int fd_in = open(argv[1], O_RDONLY);
assert(fd_in >= 0);
struct stat st;
fstat(fd_in, &st);
// on mappe le fichier source en mémoire
void *src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd_in, 0);
assert(src != MAP_FAILED);
int fd_out = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644);
assert(fd_out >= 0);
// on fixe la taille du fichier de sortie
ftruncate(fd_out, st.st_size);
// on mappe le fichier destination en mémoire
void *dst = mmap(NULL, st.st_size, PROT_WRITE, MAP_SHARED, fd_out, 0);
assert(dst != MAP_FAILED);
// copie mémoire → mémoire
memcpy(dst, src, st.st_size);
// nettoyage
munmap(src, st.st_size);
munmap(dst, st.st_size);
close(fd_in);
close(fd_out);
return 0;
}