mysh.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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("\n");
  42. }
  43. //Supprime
  44. clean_command(&ct); */
  45. cd(argc, argv);
  46. return (EXIT_SUCCESS);
  47. }
  48. /* --- Commandes internes --- */
  49. void cd(int argc, char** argv){
  50. if(argc > 2) {
  51. printf("too many arguments : 1 required, %d given\n", argc-1);
  52. }
  53. else {
  54. if(argc == 1) {
  55. if(chdir("/") == ERR){
  56. perror("Erreur chdir() : ");
  57. }
  58. }
  59. else {
  60. if(chdir(argv[1]) == ERR){
  61. printf("path does not exist\n");
  62. }
  63. }
  64. }
  65. //show_current_dir("current working directory is: ", "\n");
  66. }
  67. /* --- Fonctions utilitaires --- */
  68. void show_current_dir(const char* before, const char* after){
  69. char buffer[512];
  70. if (getcwd(buffer, sizeof(buffer)) == NULL){
  71. perror("Erreur getcwd() : ");
  72. }
  73. else {
  74. printf("%s%s%s", before, buffer, after);
  75. }
  76. }