From 903a4268aad9e0c76f250de71e372e1da4f46f92 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Tue, 15 Apr 2025 11:29:44 +0700 Subject: [PATCH] fix-build-warnings --- CMakeLists.txt | 2 ++ Makefile | 9 +++++---- meson.build | 2 +- src/smol-http-pthreads.c | 7 ++----- src/smol-http.c | 8 ++------ src/smol-pthreads.c | 8 +++----- src/smol.c | 6 ++---- 7 files changed, 17 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1533ef9..3b3e81b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.6) project(smol) +set(CMAKE_C_FLAGS "-Wall") + add_executable( smol src/smol.c diff --git a/Makefile b/Makefile index dddc4ce..82d28c6 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ STRIP=strip BIN=build SRC=src PREFIX=/usr +CFLAGS=-Wall default: all @@ -11,16 +12,16 @@ all: build build: smol smol-pthreads smol-http smol-http-pthreads smol: - $(CC) -o $(BIN)/smol $(SRC)/smol.c -lcrypto -lssl + $(CC) -o $(BIN)/smol $(SRC)/smol.c -lcrypto -lssl $(CFLAGS) smol-pthreads: - $(CC) -o $(BIN)/smol-pthreads $(SRC)/smol-pthreads.c -lcrypto -lssl + $(CC) -o $(BIN)/smol-pthreads $(SRC)/smol-pthreads.c -lcrypto -lssl $(CFLAGS) smol-http: - $(CC) -o $(BIN)/smol-http $(SRC)/smol-http.c + $(CC) -o $(BIN)/smol-http $(SRC)/smol-http.c $(CFLAGS) smol-http-pthreads: - $(CC) -o $(BIN)/smol-http-pthreads $(SRC)/smol-http-pthreads.c + $(CC) -o $(BIN)/smol-http-pthreads $(SRC)/smol-http-pthreads.c $(CFLAGS) clean: if [ -d $(BIN) ]; then rm -rv $(BIN);mkdir -pv $(BIN);fi diff --git a/meson.build b/meson.build index 5eac70f..e757653 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('smol', 'c') +project('smol', 'c', default_options: 'c_args=-Wall') deps = dependency('openssl') executable( 'smol', diff --git a/src/smol-http-pthreads.c b/src/smol-http-pthreads.c index c38093f..5ef926a 100644 --- a/src/smol-http-pthreads.c +++ b/src/smol-http-pthreads.c @@ -10,7 +10,7 @@ #define PORT 8081 #define BUFFER_SIZE 512 -#define MAX_PATH 128 +#define MAX_PATH 512 #define ROOT_DIR "." typedef struct handle_request_data { @@ -66,10 +66,8 @@ static void list_files(int ssl, char *directory) { while ((entry = readdir(dir)) && pos < sizeof(buf) - 100) { if (!strcmp(entry->d_name, ".")) continue; if (!strcmp(entry->d_name, "..") && !strcmp(directory, ROOT_DIR)) continue; - struct stat st; char path[MAX_PATH]; snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name); - // if (stat(path, &st) < 0 || S_ISDIR(st.st_mode)) continue; pos += snprintf(buf + pos, sizeof(buf) - pos, "
  • %s
  • ", directory, entry->d_name, entry->d_name); } @@ -83,7 +81,7 @@ void *handle_request(void *param) { handle_request_data *param_data = (handle_request_data*)param; int ssl = param_data->socket; char *req = param_data->req; - char method[8], path[MAX_PATH], proto[8]; + char method[8], path[MAX_PATH-2], proto[8]; if (sscanf(req, "%7s %127s %7s", method, path, proto) != 3) { send_response(ssl, "400 Bad Request", "text/plain", "Bad Request", 11); close(ssl); @@ -203,7 +201,6 @@ int main() { pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&tid, &attr, handle_request, &data); - // pthread_join(tid, NULL); } } diff --git a/src/smol-http.c b/src/smol-http.c index 6da4a81..22ee04c 100644 --- a/src/smol-http.c +++ b/src/smol-http.c @@ -9,7 +9,7 @@ #define PORT 8081 #define BUFFER_SIZE 512 -#define MAX_PATH 128 +#define MAX_PATH 512 #define ROOT_DIR "." static const struct { const char *ext; const char *type; } mime_types[] = { @@ -60,10 +60,8 @@ static void list_files(int ssl, char *directory) { while ((entry = readdir(dir)) && pos < sizeof(buf) - 100) { if (!strcmp(entry->d_name, ".")) continue; if (!strcmp(entry->d_name, "..") && !strcmp(directory, ROOT_DIR)) continue; - struct stat st; char path[MAX_PATH]; snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name); - // if (stat(path, &st) < 0 || S_ISDIR(st.st_mode)) continue; pos += snprintf(buf + pos, sizeof(buf) - pos, "
  • %s
  • ", directory, entry->d_name, entry->d_name); } @@ -74,7 +72,7 @@ static void list_files(int ssl, char *directory) { } static void handle_request(int ssl, char *req) { - char method[8], path[MAX_PATH], proto[8]; + char method[8], path[MAX_PATH-2], proto[8]; if (sscanf(req, "%7s %127s %7s", method, path, proto) != 3) { send_response(ssl, "400 Bad Request", "text/plain", "Bad Request", 11); return; @@ -180,8 +178,6 @@ int main() { buf[n] = '\0'; handle_request(client_sock, buf); } - - // close(client_sock); } close(sock); diff --git a/src/smol-pthreads.c b/src/smol-pthreads.c index 64f4311..ab1ff9c 100644 --- a/src/smol-pthreads.c +++ b/src/smol-pthreads.c @@ -12,7 +12,7 @@ #define PORT 8081 #define BUFFER_SIZE 512 -#define MAX_PATH 128 +#define MAX_PATH 512 #define ROOT_DIR "." typedef struct handle_request_data { @@ -69,7 +69,6 @@ static void list_files(SSL *ssl, char *directory) { while ((entry = readdir(dir)) && pos < sizeof(buf) - 100) { if (!strcmp(entry->d_name, ".")) continue; if (!strcmp(entry->d_name, "..") && !strcmp(directory, ROOT_DIR)) continue; - struct stat st; char path[MAX_PATH]; snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name); pos += snprintf(buf + pos, sizeof(buf) - pos, @@ -86,7 +85,7 @@ void *handle_request(void *param) { int client_sock = param_data->socket; SSL *ssl = param_data->ssl; char *req = param_data->req; - char method[8], path[MAX_PATH], proto[8]; + char method[8], path[MAX_PATH-2], proto[8]; if (sscanf(req, "%7s %127s %7s", method, path, proto) != 3) { send_response(ssl, "400 Bad Request", "text/plain", "Bad Request", 11); SSL_shutdown(ssl); @@ -129,12 +128,11 @@ void *handle_request(void *param) { char file_path[MAX_PATH]; snprintf(file_path, sizeof(file_path), "%s/%s", ROOT_DIR, path[0] == '/' ? path + 1 : path); - if (file_path[0] == "." && file_path[1] == "/") { + if (file_path[0] == '.' && file_path[1] == '/') { for (int i = 2; i < MAX_PATH -1; i++) { file_path[i-2] = file_path[i]; } } - https://localhost:8081/home/tola/Pictures/home/tola/Pictures/wallpaper/3d-tech.jpg struct stat st; if (stat(file_path, &st) < 0 || S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) { diff --git a/src/smol.c b/src/smol.c index ac17970..172a9cb 100644 --- a/src/smol.c +++ b/src/smol.c @@ -11,7 +11,7 @@ #define PORT 8081 #define BUFFER_SIZE 512 -#define MAX_PATH 128 +#define MAX_PATH 512 #define ROOT_DIR "." static const struct { const char *ext; const char *type; } mime_types[] = { @@ -62,10 +62,8 @@ static void list_files(SSL *ssl, char *directory) { while ((entry = readdir(dir)) && pos < sizeof(buf) - 100) { if (!strcmp(entry->d_name, ".")) continue; if (!strcmp(entry->d_name, "..") && !strcmp(directory, ROOT_DIR)) continue; - struct stat st; char path[MAX_PATH]; snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name); - // if (stat(path, &st) < 0 || S_ISDIR(st.st_mode)) continue; pos += snprintf(buf + pos, sizeof(buf) - pos, "
  • %s
  • ", directory, entry->d_name, entry->d_name); } @@ -76,7 +74,7 @@ static void list_files(SSL *ssl, char *directory) { } static void handle_request(SSL *ssl, char *req) { - char method[8], path[MAX_PATH], proto[8]; + char method[8], path[MAX_PATH-2], proto[8]; if (sscanf(req, "%7s %127s %7s", method, path, proto) != 3) { send_response(ssl, "400 Bad Request", "text/plain", "Bad Request", 11); return;