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
Beer.h
Tipos de Dados Abstrato - TDA - Números Complexos
[C] Listas Duplamente Encadeadas
Busca, inserção e remoção de elementos numa lista
Pilhas C/C++ - Analisador de expressões simples
Comentários
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?
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;
}
Mensagem
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
Enviar