Skip to content

Commit 7d14c03

Browse files
authored
Merge pull request #1 from ImpulseVersionControl/master
Make the simple shell better
2 parents 60c4499 + e3a7f46 commit 7d14c03

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

sys/shell.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* Simple shell
2-
* September 18, 2010 */
2+
* September 18, 2010
3+
* November 21, 2024 */
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
@@ -8,14 +9,27 @@
89

910
int main(int argc, char *argv[]) {
1011
char x[256], y[256], z[256];
12+
char host[60];
13+
char *user = getlogin();
14+
15+
if (!user) {
16+
fprintf(stderr, "Could not get current user\n");
17+
return 1;
18+
}
19+
20+
gethostname(host, sizeof(host));
21+
1122
while (1) {
12-
getcwd(y, sizeof(y));
13-
printf("%s$ ", y);
23+
printf("[%s@%s]$ ", user, host);
1424
fgets(x, sizeof(x), stdin);
1525
if (x[0] == 'c' && x[1] == 'd' && x[2] == ' ') {
1626
sscanf(x, "cd %s", z);
1727
chdir(z);
1828
}
29+
else if (x[0] == 'p' && x[1] == 'w' && x[2] == 'd' && x[3] == '\n') {
30+
getcwd(y, sizeof(y));
31+
printf("%s\n", y);
32+
}
1933
else if (strcmp(x, "exit\n") == 0) break;
2034
else system(x);
2135
}

0 commit comments

Comments
 (0)