Minishell

Publicado por Marcelo R. Castelo Branco 17/04/2005

[ Hits: 12.366 ]

Download shell.c




Um minishell desenvolvido em C.
As bibliotecas abaixo devem estar instaladas:
types.h
stat.h
fcntl.h
unistd.h
stdio.h
string.h

  



Esconder código-fonte

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

char cmd[512];
char dir[4096];
char *argv[3];
int pid;
int main(void)
{
   while (1) {
      printf("[Crasher]$ ");
                            //escreve o prompt na  tela.
      fgets(cmd, 511, stdin);
      cmd[strlen(cmd) - 1] = 0;  
                // troca o enter = \n por um novo {FONTE} = 0
      if (strcmp(cmd, "exit") == 0)  
                // se o resultado da comparação for 0 = V finaliza o shell
         exit (0);
      else {
         argv[0] = strtok(cmd, " ");
         argv[1] = strtok(NULL, " ");
         argv[2] = NULL;
         
         if (strcmp(argv[0], "pwd") == 0) {
            getcwd(dir, 4096);
            printf("%s\n", dir);
            }
         else if (strcmp(argv[0], "cd") == 0) {
            if (chdir(argv[1]) != 0) 
               printf("Caminho inválido!\n");
            }
         else {
            pid = fork();
            if (pid == 0) {
               if (execvp(argv[0], argv) == -1) {
                  printf("Comando inválido\n");
                  exit (0);
                  }
               }
            else {
               wait();
               }
         }
      }
   }
   return 0;
}



Scripts recomendados

Árvore B com Arquivos (inserção e pesquisa)

Entendendo a função fork()

AVL

Script MakePach para correção de platarforma 32 bits para 64

Arquivos utilizados no artigo: "Desenvolvendo um plugin para o XMMS"


  

Comentários
[1] Comentário enviado por HeltonBarbosa em 10/07/2006 - 10:55h

Beleza. Gostei do seu script. Mas vc sabe como pode fazer para implementar uma pilha para fazer o seguinte: a cada vez que vc teclar o botão p/ cima, aparecer o último comando digitado pelo usuário. Assim como acontece no csh?

[2] Comentário enviado por gabrield em 21/01/2008 - 16:15h

Cara, deiuma pequena modificada em teu minishell para que ele mostrasse o nome de usuario e o hostname, para que ficasse mais parecido com os shells mais populares, ai vai o código:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
char cmd[512];
char dir[4096];
char *argv[3];
int pid;
char shorthostname[255];
char *s = NULL;


void exit_shell(){
exit(0);
}


int main(void)
{
while (1) {
gethostname(shorthostname, 255);
s = strchr(shorthostname, '.');
if (!s)
s = shorthostname;
*s = 0;
printf("%s@%s$ ",getenv("USER"),shorthostname);

fgets(cmd, 511, stdin);
cmd[strlen(cmd) - 1] = 0;

if (strcmp(cmd, "exit") == 0)
exit_shell();

else {
argv[0] = strtok(cmd, " ");
argv[1] = strtok(NULL, " ");
argv[2] = NULL;

if (strcmp(argv[0], "pwd") == 0) {
getcwd(dir, 4096);
printf("%s\n", dir);
}

else if (strcmp(argv[0], "cd") == 0) {
if (chdir(argv[1]) != 0)
printf("No such directory!\n");
}
else {
pid = fork();
if (pid == 0) {
if (execvp(argv[0], argv) != 0) {
printf("Command not found\n");
exit_shell();
}
}
else {
wait();
}
}
}
}
return 0;
}


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts