#include #include #include #define RANDOM(min,max) (min + ((max - min) * (rand() / (RAND_MAX + 1.0)))) #define BUFSZ 1024 int main() { char prompt[] = "[root:~]#"; int ch, nch, chc = 0, nsp = 0; char buf[BUFSZ]; srand(getpid() % getppid() ^ 42); system("stty raw"); system("stty -echo"); printf("%s ", prompt); fflush(stdout); for (;;) { ch = getc(stdin); switch(ch) { case 4: /* ^D */ printf("use \"logout\" to leave the shell."); case 3: /* ^C */ chc = 0; printf("\r\n%s ", prompt); break; case '\r': case '\n': buf[chc] = 0; if (chc && nsp) printf("\r\nsh: %s: command not found", buf); printf("\r\n%s ", prompt); nsp = chc = 0; break; case ' ': if (chc < (BUFSZ-1) && nsp) buf[chc++] = ' '; printf(" "); break; case 127: /* Backspace */ if (chc) { printf("\b \b"); buf[--chc] = 0; } break; case 50: /* ? F12.. */ system("reset"); exit(0); break; default: nsp++; nch = ch+RANDOM(1,5); putchar(nch); if (chc < (BUFSZ-1)) buf[chc++] = nch; break; } fflush(stdout); } }