mysh.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. void test_write(){
  12. char* a = "azerty\n";
  13. int tmp = write(1, a, strlen(a));
  14. printf("%d\n", tmp);
  15. }
  16. void current_rep(){
  17. char buffer[256];
  18. char* tryErr;
  19. if ((tryErr = getcwd(buffer, sizeof(buffer))) == NULL)
  20. perror("getcwd() error");
  21. else
  22. printf("current working directory is: %s\n", buffer);
  23. }
  24. void cd(int argc, char** argv){
  25. int tryErr;
  26. current_rep();
  27. if(argc > 2){
  28. tryErr = printf("too many arguments : 1 required, %d give\n", argc-1);
  29. }
  30. else{
  31. if(argc == 1)
  32. tryErr = chdir("/");
  33. else{
  34. if((tryErr = chdir(argv[1])) == -1)
  35. tryErr = printf("path does not exist\n");
  36. }
  37. }
  38. current_rep();
  39. }
  40. int main(int argc, char* argv[]) {
  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("\n");
  63. }
  64. //Supprime
  65. clean_command(&ct); */
  66. cd(argc, argv);
  67. return (EXIT_SUCCESS);
  68. }