diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45c80e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +.idea/* diff --git a/TP2/A_propos_des_exos_2_3.txt b/TP2/A_propos_des_exos_2_3.txt new file mode 100644 index 0000000..e2b9749 --- /dev/null +++ b/TP2/A_propos_des_exos_2_3.txt @@ -0,0 +1 @@ +En fait j'avais pas vu que l'exo 3 c'etait le 2 + 1, donc en fait quand je pensais faire le 2 je faisais deja le 3. diff --git a/TP2/bite b/TP2/bite new file mode 100644 index 0000000..f4e1651 --- /dev/null +++ b/TP2/bite @@ -0,0 +1 @@ +servgndrtsvbsedrtdsrtgrd diff --git a/TP2/ex1 b/TP2/ex1 new file mode 100755 index 0000000..79aa04b Binary files /dev/null and b/TP2/ex1 differ diff --git a/TP2/ex1.c b/TP2/ex1.c new file mode 100644 index 0000000..7b5969a --- /dev/null +++ b/TP2/ex1.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +void my_putnbr(int nb) +{ + char c; + + if (nb < 0) + { + if (nb == -2147483648) + { + write(1, "-", 1); + write(1, "2", 1); + my_putnbr(147483648); + return ; + } + nb = nb * -1; + write(1, "-", 1); + } + if (nb >= 0 && nb < 10) + { + c = nb + '0'; + write(1, &c, 1); + } + if (nb >= 10) + { + my_putnbr(nb / 10); + my_putnbr(nb % 10); + } +} + +void my_putnbrUnsignedLong(long nb) +{ + char c; + + if (nb >= 0 && nb < 10) + { + c = nb + '0'; + write(1, &c, 1); + } + if (nb >= 10) + { + my_putnbr(nb / 10); + my_putnbr(nb % 10); + } +} + +int my_strlen(char *str) +{ + int len; + + len = 0; + while (str[len] != '\0') + len++; + return (len); +} + +void showFileInfos(char *fileName) +{ + struct stat fileStat; + + if(lstat(fileName, &fileStat) == -1) + { + write(1, "Foirage du statage\n", 20); + exit(1); + } + write(1, fileName, my_strlen(fileName)); + write(1, " ", 1); + my_putnbr(fileStat.st_size); + write(1, " ", 1); + my_putnbrUnsignedLong(fileStat.st_mtim.tv_sec); + write(1, "\n", 2); +} + +int main(int argc, char **argv) +{ + if(argc != 2) + { + write(1, "T'es bourré, on veut le nom d'un fichier qu'on t'dis\n", 54); + exit(1); + } + showFileInfos(argv[1]); + return 0; +} diff --git a/TP2/ex2 b/TP2/ex2 new file mode 100755 index 0000000..5a7cb17 Binary files /dev/null and b/TP2/ex2 differ diff --git a/TP2/ex2.c b/TP2/ex2.c new file mode 100644 index 0000000..8860d26 --- /dev/null +++ b/TP2/ex2.c @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void my_putnbr(int nb) +{ + char c; + + if (nb < 0) + { + if (nb == -2147483648) + { + write(1, "-", 1); + write(1, "2", 1); + my_putnbr(147483648); + return ; + } + nb = nb * -1; + write(1, "-", 1); + } + if (nb >= 0 && nb < 10) + { + c = nb + '0'; + write(1, &c, 1); + } + if (nb >= 10) + { + my_putnbr(nb / 10); + my_putnbr(nb % 10); + } +} + +void my_putnbrUnsignedLong(long nb) +{ + char c; + + if (nb >= 0 && nb < 10) + { + c = nb + '0'; + write(1, &c, 1); + } + if (nb >= 10) + { + my_putnbr(nb / 10); + my_putnbr(nb % 10); + } +} + +int my_strlen(char *str) +{ + int len; + + len = 0; + while (str[len] != '\0') + len++; + return (len); +} + +char *my_strcat(char *dest, char *src, int len) +{ + int i; + int j; + char *final; + + i = 0; + j = 0; + final = (char*)malloc(len); + while (dest[i] != '\0') + { + final[i] = dest[i]; + i++; + } + final[i] = '/'; + i++; + while (src[j] != '\0') + { + final[i] = src[j]; + i++; + j++; + } + final[i] = '\0'; + return (final); +} + +void showFileInfos(char *fileName) +{ + struct stat fileStat; + + if(lstat(fileName, &fileStat) == -1) + { + write(1, "Foirage du statage\n", 20); + exit(1); + } + write(1, fileName, my_strlen(fileName)); + write(1, " ", 1); + my_putnbr(fileStat.st_size); + write(1, " ", 1); + my_putnbrUnsignedLong(fileStat.st_mtim.tv_sec); + write(1, "\n", 2); +} + +void listDirContent(char *leDossier) +{ + DIR *dir; + struct dirent *lesEntrees; + char *fileAbsolPath; + size_t path_length; + + dir = opendir(leDossier); + if(!dir) + { + if(errno == ENOTDIR) //C'est quand meme pas mal errno, je decouvre + { + write(1, "t'es bourré, on veut le nom d'un dossier et pas autre chose\n", 61); + exit(1); + } + write(1, "Probleme ouverture dossier\n", 28); + } + path_length = my_strlen(leDossier) + NAME_MAX + 2; + fileAbsolPath = (char*)malloc(path_length); + while((lesEntrees = readdir(dir))) + { + fileAbsolPath = my_strcat((char *)leDossier, lesEntrees->d_name, path_length); + showFileInfos(fileAbsolPath); + } + free(fileAbsolPath); + closedir(dir); +} + +int main(int argc, char **argv) +{ + if(argc != 2) + { + write(1, "t'es bourré, on veut le nom d'un dossier qu'on t'dis\n", 54); + exit(1); + } + listDirContent(argv[1]); + return 0; +} diff --git a/TP2/ex4 b/TP2/ex4 new file mode 100755 index 0000000..ffef5a9 Binary files /dev/null and b/TP2/ex4 differ diff --git a/TP2/ex4.c b/TP2/ex4.c new file mode 100644 index 0000000..47a3b0e --- /dev/null +++ b/TP2/ex4.c @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void my_putnbr(int nb) +{ + char c; + + if (nb < 0) + { + if (nb == -2147483648) + { + write(1, "-", 1); + write(1, "2", 1); + my_putnbr(147483648); + return ; + } + nb = nb * -1; + write(1, "-", 1); + } + if (nb >= 0 && nb < 10) + { + c = nb + '0'; + write(1, &c, 1); + } + if (nb >= 10) + { + my_putnbr(nb / 10); + my_putnbr(nb % 10); + } +} + +void my_putnbrUnsignedLong(long nb) +{ + char c; + + if (nb >= 0 && nb < 10) + { + c = nb + '0'; + write(1, &c, 1); + } + if (nb >= 10) + { + my_putnbr(nb / 10); + my_putnbr(nb % 10); + } +} + +int my_strlen(char *str) +{ + int len; + + len = 0; + while (str[len] != '\0') + len++; + return (len); +} + +char *my_strcat(char *dest, char *src, int len) +{ + int i; + int j; + char *final; + + i = 0; + j = 0; + final = (char*)malloc(len); + while (dest[i] != '\0') + { + final[i] = dest[i]; + i++; + } + final[i] = '/'; + i++; + while (src[j] != '\0') + { + final[i] = src[j]; + i++; + j++; + } + final[i] = '\0'; + return (final); +} + +void showFileInfos(char *fileName) +{ + struct stat fileStat; + + if(lstat(fileName, &fileStat) == -1) + { + write(1, "Foirage du statage\n", 20); + exit(1); + } + write(1, fileName, my_strlen(fileName)); + write(1, " ", 1); + my_putnbr(fileStat.st_size); + write(1, " ", 1); + my_putnbrUnsignedLong(fileStat.st_mtim.tv_sec); + write(1, "\n", 2); +} + +int my_strcmp(char *s1, char *s2) +{ + int i; + + i = 0; + while (s1[i]) + { + if (s1[i] != s2[i]) + return (s1[i] - s2[i]); + i++; + } + return (s1[i] - s2[i]); +} + +void listDirContent(char *leDossier) +{ + DIR *dir; + struct dirent *lesEntrees; + char *fileAbsolPath; + size_t path_length; + + dir = opendir(leDossier); + if(!dir) + { + if(errno == ENOTDIR) //C'est quand meme pas mal errno, je decouvre + { + write(1, "t'es bourré, on veut le nom d'un dossier et pas autre chose\n", 61); + exit(1); + } + write(1, "Probleme ouverture dossier, droit d'acces ?\n", 45); + return; + } + path_length = my_strlen(leDossier) + NAME_MAX + 2; + fileAbsolPath = (char*)malloc(path_length); + while((lesEntrees = readdir(dir))) + { + if(lesEntrees->d_type == DT_DIR && my_strcmp(lesEntrees->d_name,".") && my_strcmp(lesEntrees->d_name,"..")) + { + fileAbsolPath = my_strcat((char *)leDossier, lesEntrees->d_name, path_length); + showFileInfos(fileAbsolPath); + listDirContent(fileAbsolPath); + } + else + { + + fileAbsolPath = my_strcat((char *)leDossier, lesEntrees->d_name, path_length); + showFileInfos(fileAbsolPath); + } + } + free(fileAbsolPath); + closedir(dir); +} + +int main(int argc, char **argv) +{ + if(argc != 2) + { + write(1, "t'es bourré, on veut le nom d'un dossier qu'on t'dis\n", 54); + exit(1); + } + listDirContent(argv[1]); + return 0; +} diff --git a/TP2/pouet/essai1 b/TP2/pouet/essai1 new file mode 100644 index 0000000..e69de29 diff --git a/TP2/pouet/essai2 b/TP2/pouet/essai2 new file mode 100644 index 0000000..e69de29 diff --git a/TP3/ex1 b/TP3/ex1 new file mode 100755 index 0000000..103f24f Binary files /dev/null and b/TP3/ex1 differ diff --git a/TP3/ex1.c b/TP3/ex1.c new file mode 100644 index 0000000..42eb9a7 --- /dev/null +++ b/TP3/ex1.c @@ -0,0 +1,74 @@ +#include +#include +#include + +#define MAX_MSG_LEN 256 +#define MAX_PROC 10 + +int my_strlen(char *str) +{ + int len; + + len = 0; + while (str[len] != '\0') + len++; + return (len); +} + +int main(void) +{ + const int N = MAX_PROC; + int fd[MAX_PROC][2]; + pid_t pid[MAX_PROC]; + char buf[MAX_MSG_LEN]; + int dest; + char msg[MAX_MSG_LEN]; + int i; + + i = 0; + while(i < N) + { + if(pipe(fd[i]) == -1) + { + perror("foirage du pipage"); + exit(EXIT_FAILURE); + } + pid[i] = fork(); + if (pid[i] == -1) + { + perror("foirage du forkage"); + exit(EXIT_FAILURE); + } + if(pid[i] == 0) + { + close(fd[i][1]); + while(read(fd[i][0], buf, MAX_MSG_LEN) > 0) + { + printf("Processus %d a reçu: %s\n", i, buf); + } + close(fd[i][0]); + exit(EXIT_SUCCESS); + } + i++; + } + i = 0; + while(i < N) + { + close(fd[i][0]); + i++; + } + while(scanf("%d %[^\n]", &dest, msg) != EOF) + { + if(dest >= 0 && dest < N) + { + write(fd[dest][1], msg, my_strlen(msg) + 1); + } + } + i = 0; + while(i < N) + { + close(fd[i][1]); + i++; + } + return 0; +}