Skip to content

Commit 01d2bbc

Browse files
committed
feat: Recompiled using course-sdk
1 parent 1cb99d4 commit 01d2bbc

File tree

166 files changed

+4019
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

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

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)
2+
3+
This is a starting point for C solutions to the
4+
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).
5+
6+
In this challenge, you'll build a toy Redis clone that's capable of handling
7+
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
8+
event loops, the Redis protocol and more.
9+
10+
**Note**: If you're viewing this repo on GitHub, head over to
11+
[codecrafters.io](https://codecrafters.io) to try the challenge.
12+
13+
# Passing the first stage
14+
15+
The entry point for your Redis implementation is in `app/server.c`. Study and
16+
uncomment the relevant code, and push your changes to pass the first stage:
17+
18+
```sh
19+
git add .
20+
git commit -m "pass 1st stage" # any msg
21+
git push origin master
22+
```
23+
24+
That's all!
25+
26+
# Stage 2 & beyond
27+
28+
Note: This section is for stages 2 and beyond.
29+
30+
1. Ensure you have `gcc` installed locally
31+
1. Run `./spawn_redis_server.sh` to run your Redis server, which is implemented
32+
in `app/server.c`.
33+
1. Commit your changes and run `git push origin master` to submit your solution
34+
to CodeCrafters. Test output will be streamed to your terminal.

solutions/c/01-jm1/code/app/server.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/socket.h>
4+
#include <netinet/in.h>
5+
#include <netinet/ip.h>
6+
#include <string.h>
7+
#include <errno.h>
8+
#include <unistd.h>
9+
10+
int main() {
11+
// Disable output buffering
12+
setbuf(stdout, NULL);
13+
14+
int server_fd, client_addr_len;
15+
struct sockaddr_in client_addr;
16+
17+
server_fd = socket(AF_INET, SOCK_STREAM, 0);
18+
if (server_fd == -1) {
19+
printf("Socket creation failed: %s...\n", strerror(errno));
20+
return 1;
21+
}
22+
23+
// Since the tester restarts your program quite often, setting SO_REUSEADDR
24+
// ensures that we don't run into 'Address already in use' errors
25+
int reuse = 1;
26+
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
27+
printf("SO_REUSEADDR failed: %s \n", strerror(errno));
28+
return 1;
29+
}
30+
31+
struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
32+
.sin_port = htons(6379),
33+
.sin_addr = { htonl(INADDR_ANY) },
34+
};
35+
36+
if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
37+
printf("Bind failed: %s \n", strerror(errno));
38+
return 1;
39+
}
40+
41+
int connection_backlog = 5;
42+
if (listen(server_fd, connection_backlog) != 0) {
43+
printf("Listen failed: %s \n", strerror(errno));
44+
return 1;
45+
}
46+
47+
printf("Waiting for a client to connect...\n");
48+
client_addr_len = sizeof(client_addr);
49+
50+
accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
51+
printf("Client connected\n");
52+
53+
close(server_fd);
54+
55+
return 0;
56+
}
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
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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
@@ -1,61 +1,56 @@
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <sys/socket.h>
5+
#include <netinet/in.h>
6+
#include <netinet/ip.h>
7+
#include <string.h>
8+
#include <errno.h>
9+
#include <unistd.h>
10+
11+
int main() {
12+
// Disable output buffering
13+
setbuf(stdout, NULL);
14+
15+
- // You can use print statements as follows for debugging, they'll be visible when running tests.
16+
- printf("Logs from your program will appear here!\n");
17+
+ int server_fd, client_addr_len;
18+
+ struct sockaddr_in client_addr;
19+
20+
- // Uncomment this block to pass the first stage
21+
- //
22+
- // int server_fd, client_addr_len;
23+
- // struct sockaddr_in client_addr;
24+
- //
25+
- // server_fd = socket(AF_INET, SOCK_STREAM, 0);
26+
- // if (server_fd == -1) {
27+
- // printf("Socket creation failed: %s...\n", strerror(errno));
28+
- // return 1;
29+
- // }
30+
- //
31+
- // // Since the tester restarts your program quite often, setting SO_REUSEADDR
32+
- // // ensures that we don't run into 'Address already in use' errors
33+
- // int reuse = 1;
34+
- // if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
35+
- // printf("SO_REUSEADDR failed: %s \n", strerror(errno));
36+
- // return 1;
37+
- // }
38+
- //
39+
- // struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
40+
- // .sin_port = htons(6379),
41+
- // .sin_addr = { htonl(INADDR_ANY) },
42+
- // };
43+
- //
44+
- // if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
45+
- // printf("Bind failed: %s \n", strerror(errno));
46+
- // return 1;
47+
- // }
48+
- //
49+
- // int connection_backlog = 5;
50+
- // if (listen(server_fd, connection_backlog) != 0) {
51+
- // printf("Listen failed: %s \n", strerror(errno));
52+
- // return 1;
53+
- // }
54+
- //
55+
- // printf("Waiting for a client to connect...\n");
56+
- // client_addr_len = sizeof(client_addr);
57+
- //
58+
- // accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
59+
- // printf("Client connected\n");
60+
- //
61+
- // close(server_fd);
62+
+ server_fd = socket(AF_INET, SOCK_STREAM, 0);
63+
+ if (server_fd == -1) {
64+
+ printf("Socket creation failed: %s...\n", strerror(errno));
65+
+ return 1;
66+
+ }
67+
+
68+
+ // Since the tester restarts your program quite often, setting SO_REUSEADDR
69+
+ // ensures that we don't run into 'Address already in use' errors
70+
+ int reuse = 1;
71+
+ if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
72+
+ printf("SO_REUSEADDR failed: %s \n", strerror(errno));
73+
+ return 1;
74+
+ }
75+
+
76+
+ struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
77+
+ .sin_port = htons(6379),
78+
+ .sin_addr = { htonl(INADDR_ANY) },
79+
+ };
80+
+
81+
+ if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
82+
+ printf("Bind failed: %s \n", strerror(errno));
83+
+ return 1;
84+
+ }
85+
+
86+
+ int connection_backlog = 5;
87+
+ if (listen(server_fd, connection_backlog) != 0) {
88+
+ printf("Listen failed: %s \n", strerror(errno));
89+
+ return 1;
90+
+ }
91+
+
92+
+ printf("Waiting for a client to connect...\n");
93+
+ client_addr_len = sizeof(client_addr);
94+
+
95+
+ accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
96+
+ printf("Client connected\n");
97+
+
98+
+ close(server_fd);
99+
100+
return 0;
101+
}

solutions/c/01-jm1/explanation.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
The entry point for your Redis implementation is in `app/server.c`.
2+
3+
Study and uncomment the relevant code:
4+
5+
```c
6+
// Uncomment this block to pass the first stage
7+
8+
int server_fd, client_addr_len;
9+
struct sockaddr_in client_addr;
10+
11+
server_fd = socket(AF_INET, SOCK_STREAM, 0);
12+
if (server_fd == -1) {
13+
printf("Socket creation failed: %s...\n", strerror(errno));
14+
return 1;
15+
}
16+
17+
// Since the tester restarts your program quite often, setting SO_REUSEADDR
18+
// ensures that we don't run into 'Address already in use' errors
19+
int reuse = 1;
20+
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
21+
printf("SO_REUSEADDR failed: %s \n", strerror(errno));
22+
return 1;
23+
}
24+
25+
struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
26+
.sin_port = htons(6379),
27+
.sin_addr = { htonl(INADDR_ANY) },
28+
};
29+
30+
if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
31+
printf("Bind failed: %s \n", strerror(errno));
32+
return 1;
33+
}
34+
35+
int connection_backlog = 5;
36+
if (listen(server_fd, connection_backlog) != 0) {
37+
printf("Listen failed: %s \n", strerror(errno));
38+
return 1;
39+
}
40+
41+
printf("Waiting for a client to connect...\n");
42+
client_addr_len = sizeof(client_addr);
43+
44+
accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
45+
printf("Client connected\n");
46+
47+
close(server_fd);
48+
```
49+
50+
Push your changes to pass the first stage:
51+
52+
```
53+
git add .
54+
git commit -m "pass 1st stage" # any msg
55+
git push origin master
56+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pom.xml
2+
pom.xml.asc
3+
*.jar
4+
*.class
5+
/lib/
6+
/classes/
7+
/target/
8+
/checkouts/
9+
.lein-deps-sum
10+
.lein-repl-history
11+
.lein-plugins/
12+
.lein-failures
13+
.nrepl-port
14+
.cpcache/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)
2+
3+
This is a starting point for Clojure solutions to the
4+
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).
5+
6+
In this challenge, you'll build a toy Redis clone that's capable of handling
7+
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
8+
event loops, the Redis protocol and more.
9+
10+
**Note**: If you're viewing this repo on GitHub, head over to
11+
[codecrafters.io](https://codecrafters.io) to try the challenge.
12+
13+
# Passing the first stage
14+
15+
The entry point for your Redis implementation is in `src/redis/core.clj`. Study
16+
and uncomment the relevant code, and push your changes to pass the first stage:
17+
18+
```sh
19+
git add .
20+
git commit -m "pass 1st stage" # any msg
21+
git push origin master
22+
```
23+
24+
That's all!
25+
26+
# Stage 2 & beyond
27+
28+
Note: This section is for stages 2 and beyond.
29+
30+
1. Ensure you have `lein` installed locally
31+
1. Run `./spawn_redis_server.sh` to run your Redis server, which is implemented
32+
in `src/redis/core.clj`.
33+
1. Commit your changes and run `git push origin master` to submit your solution
34+
to CodeCrafters. Test output will be streamed to your terminal.
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 Clojure version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: clojure-1.10.3
11+
language_pack: clojure-1.10.3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(defproject redis "0.1.0-SNAPSHOT"
2+
:description "A barebones implementation of a Redis server"
3+
:url "http://github.com/codecrafters-io/redis-starter-clojure"
4+
:license {:name "MIT"
5+
:url "https://opensource.org/licenses/MIT"}
6+
:dependencies [[org.clojure/clojure "1.10.3"]]
7+
:main ^:skip-aot redis.core
8+
:target-path "target/%s"
9+
:profiles {:uberjar {:aot :all
10+
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
exec lein run "$@"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns redis.core
2+
(:gen-class))
3+
4+
(require '[clojure.java.io :as io])
5+
(import '[java.net ServerSocket])
6+
7+
(defn receive-message
8+
"Read a line of textual data from the given socket"
9+
[socket]
10+
(.readLine (io/reader socket)))
11+
12+
(defn send-message
13+
"Send the given string message out over the given socket"
14+
[socket msg]
15+
(let [writer (io/writer socket)]
16+
(.write writer msg)
17+
(.flush writer)))
18+
19+
(defn serve [port handler]
20+
(with-open [server-sock (ServerSocket. port)
21+
sock (.accept server-sock)]
22+
;; Since the tester restarts your program quite often, setting SO_REUSEADDR
23+
;; ensures that we don't run into 'Address already in use' errors
24+
(. server-sock (setReuseAddress true))
25+
(let [msg-in (receive-message sock)
26+
msg-out (handler msg-in)]
27+
(send-message sock msg-out))))
28+
29+
(defn handler
30+
[& args]
31+
"TODO: Respond with output")
32+
33+
(defn -main
34+
"I don't do a whole lot ... yet."
35+
[& args]
36+
(serve 6379 handler)
37+
)

0 commit comments

Comments
 (0)