Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions sys/shell.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Simple shell
* September 18, 2010 */
* September 18, 2010
* November 21, 2024 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -8,14 +9,27 @@

int main(int argc, char *argv[]) {
char x[256], y[256], z[256];
char host[60];
char *user = getlogin();

if (!user) {
fprintf(stderr, "Could not get current user\n");
return 1;
}

gethostname(host, sizeof(host));

while (1) {
getcwd(y, sizeof(y));
printf("%s$ ", y);
printf("[%s@%s]$ ", user, host);
fgets(x, sizeof(x), stdin);
if (x[0] == 'c' && x[1] == 'd' && x[2] == ' ') {
sscanf(x, "cd %s", z);
chdir(z);
}
else if (x[0] == 'p' && x[1] == 'w' && x[2] == 'd' && x[3] == '\n') {
getcwd(y, sizeof(y));
printf("%s\n", y);
}
else if (strcmp(x, "exit\n") == 0) break;
else system(x);
}
Expand Down
Loading