mysh.c 2.1 KB

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