Skip to content

Instantly share code, notes, and snippets.

@paulido
Last active August 12, 2023 14:07
Show Gist options
  • Save paulido/5d9f919e6ebcb14a7e831a647106c305 to your computer and use it in GitHub Desktop.
Save paulido/5d9f919e6ebcb14a7e831a647106c305 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *args[]){
if(argc != 2){
printf("syntaxe incorrecte");
return EXIT_FAILURE;
}
int nb_read, t_write, size = 1024;
int *buffer = malloc(sizeof(int)*size);
int nb_write;
int descriptor = open(args[1], O_RDONLY);
if(descriptor < 0){
printf("impossible lire le fichier");
return EXIT_FAILURE;
}
do{
nb_read = read(descriptor, buffer, size);
t_write = 0;
do{
nb_write = write(STDOUT_FILENO, (const void *)buffer, nb_read);
t_write += nb_write;
}while(nb_write < nb_read);
}while( nb_read > 0);
close(descriptor);
return EXIT_SUCCESS;
}
@paulido
Copy link
Author

paulido commented Aug 12, 2023

This is an implementation of the cat function on UNIX like systems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment