error.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * File: error.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 8 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include "error.h"
  11. /* --- Extern --- */
  12. int serrno = 0;
  13. char* serrlib[] = {
  14. "Reussite",
  15. "Ligne de commande mal terminée",
  16. "& inattendue",
  17. "Impossible d'ouvrir/créer le fichier pour la redirection",
  18. "Type de redirection incorrect",
  19. "Redirection Incorrect",
  20. "Command incorrect",
  21. "Analyse wildcard impossible",
  22. "Echec ajout wildcard dans argv"
  23. };
  24. /* --- Fonctions publiques --- */
  25. int redirect_fd(int fd1, int fd2){
  26. int tmpfd;
  27. //On duplique le 1er fd
  28. tmpfd = dup(fd1);
  29. if(tmpfd == ERR){
  30. addperror("Erreur pendant la duplication");
  31. return ERR;
  32. }
  33. //On fait en sorte que fd1 soit lié au fichier de fd2
  34. if(dup2(fd2, fd1) == ERR){
  35. addperror("Erreur pendant le changement de fd");
  36. return ERR;
  37. }
  38. //On ferme l'ancien fd2
  39. if(close(fd2) == ERR){
  40. addperror("Erreur pendant la fermeture de fd2");
  41. return ERR;
  42. }
  43. //On retourne le nouveau fd de fd1
  44. return tmpfd;
  45. }