[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;
}