Skip to content

Commit

Permalink
Merge pull request #1 from ImpulseVersionControl/master
Browse files Browse the repository at this point in the history
Make the simple shell better
  • Loading branch information
Babkock authored Nov 21, 2024
2 parents 60c4499 + e3a7f46 commit 7d14c03
Showing 1 changed file with 17 additions and 3 deletions.
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

0 comments on commit 7d14c03

Please sign in to comment.