Skip to content

Commit 9a77e45

Browse files
authored
Merge pull request #3 from codecrafters-io/CC-1274-c
Add support for C
2 parents 1c259c3 + 9ec7bd2 commit 9a77e45

File tree

18 files changed

+228
-2
lines changed

18 files changed

+228
-2
lines changed

compiled_starters/c/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/c/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/shell.png)
2+
3+
This is a starting point for C solutions to the
4+
["Build Your Own Shell" Challenge](https://app.codecrafters.io/courses/shell/overview).
5+
6+
In this challenge, you'll build your own POSIX compliant shell that's capable of
7+
interpreting shell commands, running external programs and builtin commands like
8+
cd, pwd, echo and more. Along the way, you'll learn about shell command parsing,
9+
REPLs, builtin commands, and more.
10+
11+
**Note**: If you're viewing this repo on GitHub, head over to
12+
[codecrafters.io](https://codecrafters.io) to try the challenge.
13+
14+
# Passing the first stage
15+
16+
The entry point for your `shell` implementation is in `app/main.c`. Study and
17+
uncomment the relevant code, and push your changes to pass the first stage:
18+
19+
```sh
20+
git add .
21+
git commit -m "pass 1st stage" # any msg
22+
git push origin master
23+
```
24+
25+
Time to move on to the next stage!
26+
27+
# Stage 2 & beyond
28+
29+
Note: This section is for stages 2 and beyond.
30+
31+
1. Ensure you have `c (9.2)` installed locally
32+
1. Run `./your_shell.sh` to run your program, which is implemented in
33+
`app/main.c`.
34+
1. Commit your changes and run `git push origin master` to submit your solution
35+
to CodeCrafters. Test output will be streamed to your terminal.

compiled_starters/c/app/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
// You can use print statements as follows for debugging, they'll be visible when running tests
5+
printf("Logs from your program will appear here!\n");
6+
7+
// Uncomment this block to pass the first stage
8+
// printf("$ ");
9+
// fflush(stdout);
10+
11+
// Wait for user input
12+
char input[100];
13+
fgets(input, 100, stdin);
14+
return 0;
15+
}

compiled_starters/c/codecrafters.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the C version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: c-9.2
11+
language_pack: c-9.2

compiled_starters/c/your_shell.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
set -e
9+
tmpFile=$(mktemp)
10+
gcc app/*.c -o $tmpFile
11+
exec $tmpFile "$@"

course-definition.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ languages:
2020
- slug: "python"
2121
- slug: "rust"
2222
- slug: "javascript"
23+
- slug: "c"
2324

2425
marketing:
2526
difficulty: medium

dockerfiles/c-9.2.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM n0madic/alpine-gcc:9.2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

solutions/c/01-oo8/code/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/shell.png)
2+
3+
This is a starting point for C solutions to the
4+
["Build Your Own Shell" Challenge](https://app.codecrafters.io/courses/shell/overview).
5+
6+
In this challenge, you'll build your own POSIX compliant shell that's capable of
7+
interpreting shell commands, running external programs and builtin commands like
8+
cd, pwd, echo and more. Along the way, you'll learn about shell command parsing,
9+
REPLs, builtin commands, and more.
10+
11+
**Note**: If you're viewing this repo on GitHub, head over to
12+
[codecrafters.io](https://codecrafters.io) to try the challenge.
13+
14+
# Passing the first stage
15+
16+
The entry point for your `shell` implementation is in `app/main.c`. Study and
17+
uncomment the relevant code, and push your changes to pass the first stage:
18+
19+
```sh
20+
git add .
21+
git commit -m "pass 1st stage" # any msg
22+
git push origin master
23+
```
24+
25+
Time to move on to the next stage!
26+
27+
# Stage 2 & beyond
28+
29+
Note: This section is for stages 2 and beyond.
30+
31+
1. Ensure you have `c (9.2)` installed locally
32+
1. Run `./your_shell.sh` to run your program, which is implemented in
33+
`app/main.c`.
34+
1. Commit your changes and run `git push origin master` to submit your solution
35+
to CodeCrafters. Test output will be streamed to your terminal.

solutions/c/01-oo8/code/app/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("$ ");
5+
fflush(stdout);
6+
7+
// Wait for user input
8+
char input[100];
9+
fgets(input, 100, stdin);
10+
return 0;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the C version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: c-9.2
11+
language_pack: c-9.2

solutions/c/01-oo8/code/your_shell.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
set -e
9+
tmpFile=$(mktemp)
10+
gcc app/*.c -o $tmpFile
11+
exec $tmpFile "$@"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@@ -1,15 +1,11 @@
2+
#include <stdio.h>
3+
4+
int main() {
5+
- // You can use print statements as follows for debugging, they'll be visible when running tests
6+
- printf("Logs from your program will appear here!\n");
7+
-
8+
- // Uncomment this block to pass the first stage
9+
- // printf("$ ");
10+
- // fflush(stdout);
11+
+ printf("$ ");
12+
+ fflush(stdout);
13+
14+
// Wait for user input
15+
char input[100];
16+
fgets(input, 100, stdin);
17+
return 0;
18+
}

solutions/c/01-oo8/explanation.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
The entry point for your Shell implementation is in `app/main.c`.
2+
3+
Study and uncomment the relevant code:
4+
5+
```c
6+
// Uncomment this block to pass the first stage
7+
printf("$ ");
8+
fflush(stdout);
9+
```
10+
11+
Push your changes to pass the first stage:
12+
13+
```
14+
git add .
15+
git commit -m "pass 1st stage" # any msg
16+
git push origin master
17+
```

starter-repository-definitions.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@
6161
template_attributes:
6262
required_executable: "go (1.19)"
6363
user_editable_file: "cmd/myshell/main.go"
64+
65+
- language: c
66+
file_mappings:
67+
- source: starter_templates/README.md
68+
target: README.md
69+
- source: starter_templates/codecrafters.yml
70+
target: codecrafters.yml
71+
- source: starter_templates/c/your_shell.sh
72+
target: your_shell.sh
73+
- source: starter_templates/c/app/main.c
74+
target: app/main.c
75+
- source: starter_templates/.gitattributes
76+
target: .gitattributes
77+
template_attributes:
78+
required_executable: "c (9.2)"
79+
user_editable_file: "app/main.c"
80+
6481
- language: javascript
6582
file_mappings:
6683
- source: starter_templates/README.md
@@ -81,4 +98,4 @@
8198
target: .gitignore
8299
template_attributes:
83100
required_executable: "node (21)"
84-
user_editable_file: "app/main.js"
101+
user_editable_file: "app/main.js"

starter_templates/c/app/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
// You can use print statements as follows for debugging, they'll be visible when running tests
5+
printf("Logs from your program will appear here!\n");
6+
7+
// Uncomment this block to pass the first stage
8+
// printf("$ ");
9+
// fflush(stdout);
10+
11+
// Wait for user input
12+
char input[100];
13+
fgets(input, 100, stdin);
14+
return 0;
15+
}

starter_templates/c/your_shell.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
set -e
9+
tmpFile=$(mktemp)
10+
gcc app/*.c -o $tmpFile
11+
exec $tmpFile "$@"

starter_templates/codecrafters.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ language_pack: go-1.22
1919
# Available versions: rust-1.77
2020
language_pack: rust-1.77
2121
{{/ language_is_rust }}
22+
{{# language_is_c }}
23+
# Available versions: c-9.2
24+
language_pack: c-9.2
25+
{{/ language_is_c }}
2226
{{# language_is_javascript }}
2327
# Available versions: nodejs-21
2428
language_pack: nodejs-21
25-
{{/ language_is_javascript }}
29+
{{/ language_is_javascript }}

0 commit comments

Comments
 (0)