123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /*
- * File: command.c
- * Author: Arthur Brandao
- *
- * Created on 9 novembre 2018
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "error.h"
- #include "str.h"
- #include "parser.h"
- #include "command.h"
- /* --- Extern --- */
- extern Error error;
- char* cmdlist[2] = {
- "cd",
- NULL
- };
- /* --- Fonctions publiques --- */
- boolean is_internal_cmd(const char* cmd){
- //Parcours tableau pour trouver correspondance
- for(int i = 0; cmdlist[i] != NULL; i++){
- printf("%s - %s\n", cmd, cmdlist[i]);
- if(strncmp(cmd, cmdlist[i], strlen(cmdlist[i]) + 1) == 0){
- return true;
- }
- }
- return false;
- }
- int launch_internal_command(Command* cmd){
- int result, length = strlen(cmd->name) + 1;
- //cd
- if(strncmp(cmd->name, cmdlist[0], length) == 0){
- result = cd(cmd->argc, cmd->argv);
- return result;
- }
- //Aucune commande
- else {
- return SHELL_FAIL;
- }
- }
- /* --- Commandes --- */
- int cd(int argc, char** argv) {
- //Si trop d'arguments
- if (argc > 2) {
- error.print("too many arguments : 1 required, %d given\n", argc - 1);
- return SHELL_FAIL;
- } else {
- //Si aucun argument on va à la racine
- if (argc == 1) {
- if (chdir("/") == ERR) {
- addperror("Erreur chdir()");
- return SHELL_ERR;
- }
- }
- //Sinon on va dans le dossier indiqué par l'utilisateur
- else {
- if (chdir(argv[1]) == ERR) {
- error.print("path does not exist\n");
- return SHELL_ERR;
- }
- }
- }
- return SHELL_OK;
- //show_current_dir("current working directory is: ", "\n");
- }
|