Skip to content

Commit 12657b5

Browse files
authored
Merge pull request #3 from aim97/add-features
add a whole bunch of features
2 parents 9a3d00a + f3b0d37 commit 12657b5

File tree

17 files changed

+327
-37
lines changed

17 files changed

+327
-37
lines changed

.vscode/c_cpp_properties.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [],
9+
"compilerPath": "/usr/bin/gcc",
10+
"cStandard": "gnu17",
11+
"cppStandard": "gnu++17",
12+
"intelliSenseMode": "linux-gcc-x64"
13+
}
14+
],
15+
"version": 4
16+
}

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"readline.h": "c"
4+
}
5+
}

Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CC = gcc
2+
3+
# shell headers, code, and targets
4+
INC=src/include
5+
CPPFLAGS=src/lib
6+
OBJDIR=build/dist
7+
8+
# commands code and libs
9+
CMDIR=src/code
10+
BINDIR=build/bin
11+
12+
objects = $(addprefix $(OBJDIR)/, basic.o internal.o env.o shell.o)
13+
cmds = $(addprefix $(BINDIR)/, echo hello ls)
14+
15+
16+
CFLAGS = -I$(CPPFLAGS)
17+
LDFLAGS = -L$(OBJDIR) -lreadline # -Lbuild/dist
18+
19+
shell: $(objects)
20+
$(CC) $(objects) $(LDFLAGS) -o ./build/shell
21+
22+
$(OBJDIR)/%.o: $(CPPFLAGS)/%.c
23+
$(CC) $(CFLAGS) -I$(INC) -c -o $@ $<
24+
25+
$(BINDIR)/%: $(CMDIR)/%.c
26+
$(CC) -o $@ $<
27+
28+
commands: $(cmds)
29+
30+
.PRECIOUS: %.o

Readme.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
# Simple shell
22

3+
## dependencies
4+
35
to install readline, feel free to edit it to use `fgets` or `scanf` refer to this [link](https://stackoverflow.com/questions/17294809/reading-a-line-using-scanf-not-good) for reference on both.
46

57
```bash
68
sudo apt-get install libreadline6 libreadline6-dev
79
```
810

11+
## Features
12+
13+
1. external commands
14+
1. ls
15+
2. echo
16+
3. hello (deprecated, before being released 🤣🤣)
17+
2. internal commands
18+
1. hello (yes again, this is the one that will actually run)
19+
2. cd
20+
3. env
21+
4. set
22+
23+
## Make from source
24+
925
to compile the code
26+
1027
```bash
1128
mkdir build
1229
mkdir build/bin
13-
gcc shell.c -lreadline -o ./build/shell
14-
gcc ./lib/echo.c -o ./build/bin/echo
30+
make shell
31+
make commands
1532
```
1633

17-
to run the command
34+
## Run
1835

1936
```bash
20-
cd build
21-
./shell
37+
./build/shell
2238
```
2339

24-
You need to change into the build directory for the path to take actual effect
40+
## Notes
41+
42+
This code is developed to simulate how I expect an actual shell works. any resmeblance between this and reality is mere coincidence.

lib/cd.c

Whitespace-only changes.

lib/ls.c

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

src/code/hello.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("Hello world");
5+
return 0;
6+
}

src/code/ls.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <sys/types.h>
2+
#include <sys/stat.h>
3+
#include <unistd.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <dirent.h>
7+
8+
int main(int argc, char* argv[])
9+
{
10+
DIR *mydir;
11+
struct dirent *myfile;
12+
struct stat mystat;
13+
char buf[512];
14+
15+
char* path;
16+
if (argc >= 2) path = argv[1];
17+
else path = ".";
18+
mydir = opendir(path);
19+
20+
while((myfile = readdir(mydir)) != NULL)
21+
{
22+
sprintf(buf, "%s/%s", path, myfile->d_name);
23+
stat(buf, &mystat);
24+
printf("%zu\t%s\n",mystat.st_size, myfile->d_name);
25+
}
26+
closedir(mydir);
27+
}

0 commit comments

Comments
 (0)