12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- /*
- * File: mysh.c
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018, 12:43
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "parser.h"
- #include "mysh.h"
- /* --- Fonctions privées --- */
- void test_write(){
- char* a = "azerty\n";
- int tmp = write(1, a, strlen(a));
- printf("%d\n", tmp);
- }
- /* --- Main --- */
- int main(int argc, char* argv[]) {
- /*CommandTab ct;
- char str[500];
- int a;
- //Recup ligne
- //printf("%s\n", fgets(str, 500, stdin));&
- memset(str, 0, 500);
- a = read(STDIN, str, 500);
- printf("%s\n", str);
- //Separe les commandes
- a = parse_line(&ct, str);
- printf("Result : %d\n\n", a);
- //Parse les commandes
- a = parse_all_command(&ct);
- printf("Result : %d\n\n", a);
- //Affiche resultat
- for(int i = 0; i < ct.length; i++){
- Command* c = ct.cmd[i];
- printf("Commande %d (%s) : \n", i, c->name);
- for(int j = 0; j < c->argc; j++){
- printf(" Argument %d : %s\n", j, c->argv[j]);
- }
- printf("\n");
- }
- //Supprime
- clean_command(&ct); */
- cd(argc, argv);
- return (EXIT_SUCCESS);
- }
- /* --- Commandes internes --- */
- void cd(int argc, char** argv){
- if(argc > 2) {
- printf("too many arguments : 1 required, %d given\n", argc-1);
- }
- else {
- if(argc == 1) {
- if(chdir("/") == ERR){
- perror("Erreur chdir() : ");
- }
- }
- else {
- if(chdir(argv[1]) == ERR){
- printf("path does not exist\n");
- }
- }
- }
- //show_current_dir("current working directory is: ", "\n");
- }
- /* --- Fonctions utilitaires --- */
- void show_current_dir(const char* before, const char* after){
- char buffer[512];
- if (getcwd(buffer, sizeof(buffer)) == NULL){
- perror("Erreur getcwd() : ");
- }
- else {
- printf("%s%s%s", before, buffer, after);
- }
- }
|