mysh.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * File: mysh.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 31 octobre 2018, 12:43
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include "parser.h"
  11. #include "mysh.h"
  12. /* --- Fonctions privées --- */
  13. void test_write(){
  14. char* a = "azerty\n";
  15. int tmp = write(1, a, strlen(a));
  16. printf("%d\n", tmp);
  17. }
  18. /* --- Main --- */
  19. int main(int argc, char* argv[]) {
  20. CommandTab ct;
  21. char str[500];
  22. int a;
  23. //Recup ligne
  24. //printf("%s\n", fgets(str, 500, stdin));&
  25. memset(str, 0, 500);
  26. a = read(STDIN, str, 500);
  27. printf("%s\n", str);
  28. //Separe les commandes
  29. a = parse_line(&ct, str);
  30. printf("Result : %d\n\n", a);
  31. //Parse les commandes
  32. a = parse_all_command(&ct);
  33. printf("Result : %d\n\n", a);
  34. //Affiche resultat
  35. for(int i = 0; i < ct.length; i++){
  36. Command* c = ct.cmd[i];
  37. printf("Commande %d (%s) : \n", i, c->name);
  38. for(int j = 0; j < c->argc; j++){
  39. printf(" Argument %d : %s\n", j, c->argv[j]);
  40. }
  41. printf("Commande en fond : %d\n\n", ct.bck);
  42. }
  43. //Supprime
  44. clean_command(&ct);
  45. return (EXIT_SUCCESS);
  46. }
  47. /* --- Commandes internes --- */
  48. void cd(int argc, char** argv){
  49. //Si trop d'arguments
  50. if(argc > 2) {
  51. printf("too many arguments : 1 required, %d given\n", argc-1);
  52. }
  53. else {
  54. //Si aucun argument on vas à la racine
  55. if(argc == 1) {
  56. if(chdir("/") == ERR){
  57. perror("Erreur chdir() : ");
  58. }
  59. }
  60. //Sinon on va dans le dossier indiqué par l'utilisateur
  61. else {
  62. if(chdir(argv[1]) == ERR){
  63. printf("path does not exist\n");
  64. }
  65. }
  66. }
  67. //show_current_dir("current working directory is: ", "\n");
  68. }
  69. /* --- Fonctions utilitaires --- */
  70. void show_current_dir(const char* before, const char* after){
  71. char buffer[512];
  72. if (getcwd(buffer, sizeof(buffer)) == NULL){
  73. perror("Erreur getcwd() : ");
  74. }
  75. else {
  76. printf("%s%s%s", before, buffer, after);
  77. }
  78. }