mysh.c 2.5 KB

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