Skip to content

Commit 226129d

Browse files
authored
Merge pull request #82 from codecrafters-io/andy/add-c
[kafka/c] CC-2017: 2025-11 Langauges support
2 parents 82fb9ca + 56cc314 commit 226129d

33 files changed

+722
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
12+
cmake --build ./build
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec $(dirname "$0")/build/kafka "$@"

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/.gitignore

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.obj
8+
*.elf
9+
10+
# Linker output
11+
*.ilk
12+
*.map
13+
*.exp
14+
15+
# Precompiled Headers
16+
*.gch
17+
*.pch
18+
19+
# Libraries
20+
*.lib
21+
*.a
22+
*.la
23+
*.lo
24+
25+
# Shared objects (inc. Windows DLLs)
26+
*.dll
27+
*.so
28+
*.so.*
29+
*.dylib
30+
31+
# Executables
32+
*.exe
33+
*.out
34+
*.app
35+
*.i*86
36+
*.x86_64
37+
*.hex
38+
39+
# Debug files
40+
*.dSYM/
41+
*.su
42+
*.idb
43+
*.pdb
44+
45+
# Kernel Module Compile Results
46+
*.mod*
47+
*.cmd
48+
.tmp_versions/
49+
modules.order
50+
Module.symvers
51+
Mkfile.old
52+
dkms.conf
53+
54+
build
55+
vcpkg_installed

compiled_starters/c/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(codecrafters-kafka)
4+
5+
file(GLOB_RECURSE SOURCE_FILES src/*.c src/*.h)
6+
7+
set(CMAKE_C_STANDARD 23) # Enable the C23 standard
8+
9+
add_executable(kafka ${SOURCE_FILES})

compiled_starters/c/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/kafka.png)
2+
3+
This is a starting point for C solutions to the
4+
["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka).
5+
6+
In this challenge, you'll build a toy Kafka clone that's capable of accepting
7+
and responding to ApiVersions & Fetch API requests. You'll also learn about
8+
encoding and decoding messages using the Kafka wire protocol. You'll also learn
9+
about handling the network protocol, event loops, TCP sockets 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 Kafka implementation is in `src/main.c`. Study and
17+
uncomment the relevant code, and push your changes to pass the first stage:
18+
19+
```sh
20+
git commit -am "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 `cmake` installed locally
31+
1. Run `./your_program.sh` to run your Kafka broker, which is implemented in
32+
`src/main.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.
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-23
11+
buildpack: c-23

compiled_starters/c/src/main.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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(int argc, char* argv[]) {
11+
// Disable output buffering
12+
setbuf(stdout, NULL);
13+
setbuf(stderr, NULL);
14+
15+
int server_fd, client_addr_len;
16+
struct sockaddr_in client_addr;
17+
18+
server_fd = socket(AF_INET, SOCK_STREAM, 0);
19+
if (server_fd < 0) {
20+
printf("Socket creation failed: %s...\n", strerror(errno));
21+
return 1;
22+
}
23+
24+
// Since the tester restarts your program quite often, setting SO_REUSEADDR
25+
// ensures that we don't run into 'Address already in use' errors
26+
int reuse = 1;
27+
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
28+
printf("SO_REUSEADDR failed: %s \n", strerror(errno));
29+
return 1;
30+
}
31+
32+
struct sockaddr_in serv_addr = {
33+
.sin_family = AF_INET,
34+
.sin_port = htons(9092),
35+
.sin_addr = { htonl(INADDR_ANY) },
36+
};
37+
38+
if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
39+
printf("Bind failed: %s \n", strerror(errno));
40+
return 1;
41+
}
42+
43+
int connection_backlog = 5;
44+
if (listen(server_fd, connection_backlog) != 0) {
45+
printf("Listen failed: %s \n", strerror(errno));
46+
return 1;
47+
}
48+
49+
printf("Waiting for a client to connect...\n");
50+
client_addr_len = sizeof(client_addr);
51+
52+
// You can use print statements as follows for debugging, they'll be visible when running tests.
53+
printf("Logs from your program will appear here!\n");
54+
55+
// TODO: Uncomment the code below to pass the first stage
56+
//
57+
// int client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
58+
// printf("Client connected\n");
59+
// close(client_fd);
60+
61+
close(server_fd);
62+
return 0;
63+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"default-registry": {
3+
"kind": "git",
4+
"baseline": "c4af3593e1f1aa9e14a560a09e45ea2cb0dfd74d",
5+
"repository": "https://github.com/microsoft/vcpkg"
6+
},
7+
"registries": [
8+
{
9+
"kind": "artifact",
10+
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
11+
"name": "microsoft"
12+
}
13+
]
14+
}

compiled_starters/c/vcpkg.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dependencies": []
3+
}

0 commit comments

Comments
 (0)